请用python写一个贪吃蛇
时间: 2023-10-11 16:08:44 浏览: 61
教你一步步利用python实现贪吃蛇游戏
好的,我可以为您提供一个简单的贪吃蛇游戏代码,如下所示:
```python
import pygame
import random
# Initialize pygame
pygame.init()
# Set display dimensions
display_width = 600
display_height = 400
# Set colors
black = (0, 0, 0)
white = (255, 255, 255)
red = (255, 0, 0)
# Set game clock
clock = pygame.time.Clock()
# Set font
font = pygame.font.SysFont(None, 25)
# Create window
game_display = pygame.display.set_mode((display_width, display_height))
pygame.display.set_caption('Snake Game')
# Define snake block size
block_size = 10
# Define function to display message to screen
def message_to_screen(msg, color):
screen_text = font.render(msg, True, color)
game_display.blit(screen_text, [display_width/4, display_height/4])
# Define function to create snake
def snake(block_size, snake_list):
for x in snake_list:
pygame.draw.rect(game_display, white, [x[0], x[1], block_size, block_size])
# Define main game loop
def game_loop():
game_exit = False
game_over = False
# Set starting position of snake
lead_x = display_width/2
lead_y = display_height/2
lead_x_change = 0
lead_y_change = 0
# Create initial snake body
snake_list = []
snake_length = 1
# Set location of food
food_x = round(random.randrange(0, display_width - block_size)/10)*10
food_y = round(random.randrange(0, display_height - block_size)/10)*10
# Main game loop
while not game_exit:
# Handle events
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_exit = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
lead_x_change = -block_size
lead_y_change = 0
elif event.key == pygame.K_RIGHT:
lead_x_change = block_size
lead_y_change = 0
elif event.key == pygame.K_UP:
lead_y_change = -block_size
lead_x_change = 0
elif event.key == pygame.K_DOWN:
lead_y_change = block_size
lead_x_change = 0
# Update position of snake head
lead_x += lead_x_change
lead_y += lead_y_change
# Check for collision with walls
if lead_x >= display_width or lead_x < 0 or lead_y >= display_height or lead_y < 0:
game_over = True
# Check for collision with food
if lead_x == food_x and lead_y == food_y:
food_x = round(random.randrange(0, display_width - block_size)/10)*10
food_y = round(random.randrange(0, display_height - block_size)/10)*10
snake_length += 1
# Create snake
snake_head = []
snake_head.append(lead_x)
snake_head.append(lead_y)
snake_list.append(snake_head)
if len(snake_list) > snake_length:
del snake_list[0]
for each_segment in snake_list[:-1]:
if each_segment == snake_head:
game_over = True
# Update display
game_display.fill(black)
pygame.draw.rect(game_display, red, [food_x, food_y, block_size, block_size])
snake(block_size, snake_list)
pygame.display.update()
# Check for game over
if game_over:
message_to_screen('Game over. Press R to restart or Q to quit.', red)
pygame.display.update()
while game_over:
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_q:
game_exit = True
game_over = False
if event.key == pygame.K_r:
game_loop()
# Set clock tick rate
clock.tick(20)
# Quit pygame and quit program
pygame.quit()
quit()
# Start game loop
game_loop()
```
希望这个代码对您有所帮助。
阅读全文