Commit 3937fc10 by BellCodeEditor

save project

parent d363cf32
Showing with 45 additions and 7 deletions
import pygame
import pygame
import random
WINDOW_WIDTH = 800
WINDOW_HEIGHT = 600
SNAKE_SIZE = 20
pygame.init()
screen = pygane.display.set_mode((660480))
while True:
for phgane.event.egt()
locals(event.type)
\ No newline at end of file
window = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
pygame.display.set_caption("贪吃蛇游戏")
snake_x = WINDOW_WIDTH // 2
snake_y = WINDOW_HEIGHT // 2
snake_length = 1
snake_body = [[snake_x, snake_y]]
food_x = random.randint(0, (WINDOW_WIDTH - SNAKE_SIZE) // SNAKE_SIZE) * SNAKE_SIZE
food_y = random.randint(0, (WINDOW_HEIGHT - SNAKE_SIZE) // SNAKE_SIZE) * SNAKE_SIZE
clock = pygame.time.Clock()
game_over = False
while not game_over:
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_over = True
keys = pygame.key.get_pressed()
if keys[pygame.K_UP]:
snake_y -= SNAKE_SIZE
elif keys[pygame.K_DOWN]:
snake_y += SNAKE_SIZE
elif keys[pygame.K_LEFT]:
snake_x -= SNAKE_SIZE
elif keys[pygame.K_RIGHT]:
snake_x += SNAKE_SIZE
if snake_x == food_x and snake_y == food_y:
snake_length += 1
food_x = random.randint(0, (WINDOW_WIDTH - SNAKE_SIZE) // SNAKE_SIZE) * SNAKE_SIZE
food_y = random.randint(0, (WINDOW_HEIGHT - SNAKE_SIZE) // SNAKE_SIZE) * SNAKE_SIZE
snake_body.append([snake_x, snake_y])
if len(snake_body) > snake_length:
del snake_body[0]
if snake_x < 0 or snake_x >= WINDOW_WIDTH or snake_y < 0 or snake_y >= WINDOW_HEIGHT or [snake_x, snake_y] in snake_body[:-1]:
game_over = True
window.fill((0, 0, 0))
for body_part in snake_body:
pygame.draw.rect(window, (0, 255, 0), (body_part[0], body_part[1], SNAKE_SIZE, SNAKE_SIZE))
pygame.draw.rect(window, (255, 0, 0), (food_x, food_y, SNAKE_SIZE, SNAKE_SIZE))
pygame.display.update()
clock.tick(10)
pygame.quit()
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment