import pygame
import random
import sys
from collections import deque
# 初始化Pygame
pygame.init()
# 设置屏幕大小和标题
SCREEN_WIDTH = 600
SCREEN_HEIGHT = 480
SIZE = 20
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption('贪吃蛇')
# 定义颜色
LIGHT = (100, 100, 100) # 蛇的颜色
DARK = (200, 200, 200) # 食物颜色
BLACK = (0, 0, 0) # 网格线颜色
BGCOLOR = (40, 40, 60) # 背景色
# 初始化字体
font1 = pygame.font.SysFont('SimHei', 24)
font2 = pygame.font.Font(None, 72)
RED = (200, 30, 30) # GAME OVER 的字体颜色
# 初始化方向和范围
pos_x = 1
pos_y = 0
scope_x = (0, SCREEN_WIDTH // SIZE - 1)
scope_y = (2, SCREEN_HEIGHT // SIZE - 1)
# 初始化蛇和食物的位置
snake = deque()
food_x = 0
food_y = 0
def _init_snake():
global snake
snake.clear()
snake.append((2, scope_y[0]))
snake.append((1, scope_y[0]))
snake.append((0, scope_y[0]))
def _create_food():
global food_x, food_y
food_x = random.randint(scope_x[0], scope_x[1])
food_y = random.randint(scope_y[0], scope_y[1])
while (food_x, food_y) in snake:
food_x = random.randint(scope_x[0], scope_x[1])
food_y = random.randint(scope_y[0], scope_y[1])
def main():
global pos_x, pos_y, food_x, food_y
_init_snake()
_create_food()
game_over = True
start = False
score = 0
orispeed = 0.5
speed = orispeed
last_move_time = None
pause = False
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_RETURN:
if game_over:
start = True
game_over = False
_init_snake()
_create_food()
pos_x = 1
pos_y = 0
score = 0
last_move_time = time.time()
elif event.key == pygame.K_SPACE:
if not game_over:
pause = not pause
elif event.key in (pygame.K_w, pygame.K_UP):
if not pos_y:
pos_x = 0
pos_y = -1
elif event.key in (pygame.K_s, pygame.K_DOWN):
if not pos_y:
pos_x = 0
pos_y = 1
elif event.key in (pygame.K_a, pygame.K_LEFT):
if not pos_x:
pos_x = -1
pos_y = 0
elif event.key in (pygame.K_d, pygame.K_RIGHT):
if not pos_x:
pos_x = 1
pos_y = 0
screen.fill(BGCOLOR)
for x in range(SIZE, SCREEN_WIDTH, SIZE):
pygame.draw.line(screen, BLACK, (x, scope_y[0] * SIZE), (x, SCREEN_HEIGHT), 1)
for y in range(scope_y[0] * SIZE, SCREEN_HEIGHT, SIZE):
pygame.draw.line(screen, BLACK, (0, y), (SCREEN_WIDTH, y), 1)
if game_over:
if start:
imgText = font2.render('GAME OVER', True, RED)
screen.blit(imgText, ((SCREEN_WIDTH - imgText.get_width()) // 2, (SCREEN_HEIGHT - imgText.get_height()) // 2))
else:
curTime = time.time()
if curTime - last_move_time > speed:
last_move_time = curTime
next_s = (snake[0][0] + pos_x, snake[0][1] + pos_y)
if next_s == (food_x, food_y):
_create_food()
snake.appendleft(next_s)
score += 10
speed = orispeed - 0.03 * (score // 100)
else:
if scope_x[0] <= next_s[0] <= scope_x[1] and scope_y[0] <= next_s[1] <= scope_y[1] and next_s not in snake:
snake.appendleft(next_s)
snake.pop()
else:
game_over = True
if not game_over:
pygame.draw.rect(screen, LIGHT, (food_x * SIZE, food_y * SIZE, SIZE, SIZE), 0)
for s in snake:
pygame.draw.rect(screen, DARK, (s[0] * SIZE + 1, s[1] * SIZE + 1, SIZE - 2, SIZE - 2), 0)
imgText = font1.render(f'得分: {score}', True, (255, 255, 255))
screen.blit(imgText, (450, 7))
pygame.display.update()
if __name__ == '__main__':
main()