import pygame, sys, random
from pygame.locals import *
snake_color = pygame.Color("#8B7D1C")
food_color = pygame.Color("#8B0000")
background_color = pygame.Color("#BACB03")
text_color = pygame.Color("#EFEFEF")
speed = 10
"""
坐标设置
游戏中的坐标计算尤其关键。
因为贪吃蛇和食物都是由小方块组成的,所以我们设定小方块为占据 15 个像素单位的正方形,
游戏中均是以一个小方块的边长为长度单位来计算坐标的。
下文我们简称小方块的边长为一个长度单位。
在此基础上,我们设定游戏窗口的宽和高分别为 44 个长度单位和 36 个长度单位,
游戏作战区域距窗口上边框、下边框、左边框和右边框的距离分别是四个长度单位、两个长度单位、两个长度单位、两个长度单位。
因此,游戏窗口宽高以及游戏作战区域四个顶点坐标设置如下:
"""
pixel = 15
line = 44
row = 36
window_width = pixel * line
window_high = pixel * row
point_left_up = [pixel * 2, pixel * 4]
point_left_down = [pixel * 2, pixel * (row - 2)]
point_right_up = [pixel * (line - 2), pixel * 4]
point_right_down = [pixel * (line - 2), pixel * (row - 2)]
snake_head = [pixel * 8, pixel * 8]
snake_body = [[snake_head[0] - x * pixel, snake_head[1]] for x in range(5)]
"""
方向设置
在这个游戏中,我们分别以 0、90、180、270 代表右、上、左、下。
很多程序喜欢用字符串来表示方向,我们这里之所以用数字是因为数字更容易处理蛇的转向问题,
只需计算二者的夹角是否为 90 度即可。即二者的差值绝对值为 90 或者 270 即代表转向。
"""
direction_right = 0
direction_up = 90
direction_left = 180
direction_down = 270
move = {direction_right: [pixel, 0], direction_left: [-pixel, 0],
direction_up: [0, -pixel], direction_down: [0, pixel]}
"""
分数设置
最后,因为要记录玩家的历史最高分,简单起见这里直接将最高分写入文件中,游戏初始化时加载一下即可。
"""
score = 5
filename = 'db.txt'
def write_score(content):
with open(filename, 'w+') as f:
f.write(str(content))
def read_score():
with open(filename, 'w+') as f:
result = f.readline()
return 0 if result.strip() == '' else int(result)
def init():
pygame.init()
my_screen = pygame.display.set_mode((window_width, window_high), 0, 32)
pygame.display.set_caption("Eat Snake")
return my_screen
def game_over(max_score, current_score):
if max_score < current_score:
write_score(current_score)
pygame.quit()
sys.exit()
screen = init()
time_clock = pygame.time.Clock()
"""
画蛇
我们先把分数和边框画起来。
"""
def draw_box():
for point in [[point_left_up, point_right_up], [point_right_up, point_right_down],
[point_right_down, point_left_down], [point_left_down, point_left_up]]:
pygame.draw.line(screen, snake_color, point[0], point[1], 1)
def is_alive():
if (snake_head[0] < point_left_up[0] or snake_head[0] > (point_right_down[0] - pixel) or
snake_head[1] < point_left_up[1] or snake_head[1] > (point_right_down[1]) - pixel):
return False
if snake_head in snake_body:
return False
return True
def create_food():
while True:
x = random.randint(point_left_up[0] / pixel, point_right_down[0] / pixel - 1) * pixel
y = random.randint(point_left_up[1] / pixel, point_right_down[1] / pixel - 1) * pixel
if [x, y] not in snake_body:
break
return [x, y]
def draw_snake(food_position):
for point in snake_body:
pygame.draw.rect(screen, snake_color, Rect(point[0], point[1], pixel, pixel))
pygame.draw.rect(screen, food_color, Rect(food_position[0], food_position[1], pixel, pixel))
def display_message(text, color, size, postion):
font = pygame.font.Font(None, size)
text = font.render(text, True, color)
screen.blit(text, postion)
pygame.display.update()
def run():
food_position = create_food()
max_score = read_score()
current_score = 0
is_dead = False
origin_direction = direction_right
target_direction = origin_direction
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_over(max_score, current_score)
if event.type == KEYDOWN:
if event.key == K_RIGHT or event.key == K_d:
target_direction = direction_right
if event.key == K_LEFT or event.key == K_a:
target_direction = direction_left
if event.key == K_UP or event.key == K_w:
target_direction = direction_up
if event.key == K_DOWN or event.key == K_s:
target_direction = direction_down
if event.key == K_ESCAPE:
game_over(max_score, current_score)
angle = abs(origin_direction - target_direction)
if angle == 90 or angle == 270:
origin_direction = target_direction
if not is_dead:
snake_head[0] += move[origin_direction][0]
snake_head[1] += move[origin_direction][1]
if not is_dead and is_alive():
snake_body.insert(0, list(snake_head))
if snake_head[0] == food_position[0] and snake_head[1] == food_position[1]:
food_position = create_food()
current_score += score
else:
snake_body.pop()
else:
is_dead = True
screen.fill(background_color)
draw_box()
draw_snake(food_position)
pygame.display.update()
display_message(f"{current_score}/{max_score}", text_color, 30, (pixel * 2, pixel * 2))
if is_dead:
display_message(f"Game Over", text_color, 50, (pixel * 16, pixel * 15))
time_clock.tick(speed)
if __name__ == '__main__':
run()