格式化

This commit is contained in:
xnz233 2024-07-15 01:07:37 +08:00
parent db85f0a0ce
commit 67e05a0964
Signed by: xnz233
SSH Key Fingerprint: SHA256:6ufAAU+qjfNoOJpvu0bY6QK7NH4mYIQj/lVR/jWsI3g
4 changed files with 144 additions and 128 deletions

View File

@ -59,6 +59,7 @@ def show_end(settings, screen):
pygame.time.wait(1500) pygame.time.wait(1500)
settings.game_mode = 0 settings.game_mode = 0
def draw_grid(settings, screen): def draw_grid(settings, screen):
# 绘制横向线 # 绘制横向线
for x in range(0, settings.width, settings.cell_size): for x in range(0, settings.width, settings.cell_size):
@ -67,33 +68,42 @@ def draw_grid(settings, screen):
for y in range(0, settings.height, settings.cell_size): for y in range(0, settings.height, settings.cell_size):
pygame.draw.line(screen, (40, 40, 40), (0, y), (settings.width, y)) pygame.draw.line(screen, (40, 40, 40), (0, y), (settings.width, y))
def update_screen(settings, screen): def update_screen(settings, screen):
screen.fill(settings.bg_color) screen.fill(settings.bg_color)
draw_grid(settings, screen) draw_grid(settings, screen)
# 绘制蛇 # 绘制蛇
def draw_snake(ai_settings, screen, snake): def draw_snake(ai_settings, screen, snake):
# 头部用白色 # 头部用白色
x = snake.coords[0]['x'] * ai_settings.cell_size x = snake.coords[0]['x'] * ai_settings.cell_size
y = snake.coords[0]['y'] * ai_settings.cell_size y = snake.coords[0]['y'] * ai_settings.cell_size
snake_head_rect = pygame.Rect(x, y, ai_settings.cell_size, ai_settings.cell_size) snake_head_rect = pygame.Rect(
x, y, ai_settings.cell_size, ai_settings.cell_size)
pygame.draw.rect(screen, (255, 255, 255), snake_head_rect) pygame.draw.rect(screen, (255, 255, 255), snake_head_rect)
# 蛇身内部用浅绿,外框用深绿 # 蛇身内部用浅绿,外框用深绿
for coord in snake.coords[1: -1]: for coord in snake.coords[1: -1]:
x = coord['x'] * ai_settings.cell_size x = coord['x'] * ai_settings.cell_size
y = coord['y'] * ai_settings.cell_size y = coord['y'] * ai_settings.cell_size
snake_part_rect = pygame.Rect(x, y, ai_settings.cell_size, ai_settings.cell_size) snake_part_rect = pygame.Rect(
x, y, ai_settings.cell_size, ai_settings.cell_size)
pygame.draw.rect(screen, (0, 155, 0), snake_part_rect) pygame.draw.rect(screen, (0, 155, 0), snake_part_rect)
snake_part_inner_rect = pygame.Rect(x + 4, y + 4, ai_settings.cell_size - 8, ai_settings.cell_size - 8) snake_part_inner_rect = pygame.Rect(
x + 4, y + 4, ai_settings.cell_size - 8, ai_settings.cell_size - 8)
pygame.draw.rect(screen, (0, 255, 0), snake_part_inner_rect) pygame.draw.rect(screen, (0, 255, 0), snake_part_inner_rect)
# 蛇尾用浅绿 # 蛇尾用浅绿
coord = snake.coords[-1] coord = snake.coords[-1]
x = coord['x'] * ai_settings.cell_size x = coord['x'] * ai_settings.cell_size
y = coord['y'] * ai_settings.cell_size y = coord['y'] * ai_settings.cell_size
snake_tail_rect = pygame.Rect(x, y, ai_settings.cell_size, ai_settings.cell_size) snake_tail_rect = pygame.Rect(
x, y, ai_settings.cell_size, ai_settings.cell_size)
pygame.draw.rect(screen, (0, 255, 0), snake_tail_rect) pygame.draw.rect(screen, (0, 255, 0), snake_tail_rect)
# 绘制游戏界面 # 绘制游戏界面
def update_screen(settings, screen, snake): def update_screen(settings, screen, snake):
screen.fill(settings.bg_color) screen.fill(settings.bg_color)
draw_grid(settings, screen) draw_grid(settings, screen)
@ -108,6 +118,7 @@ def update_screen(settings, screen, snake):
# 暂停一下 # 暂停一下
settings.my_clock.tick(settings.clock_frq) settings.my_clock.tick(settings.clock_frq)
def is_game_over(settings, snake): def is_game_over(settings, snake):
# 碰到左右墙壁 # 碰到左右墙壁
if (snake.coords[snake.head_index]['x'] == -1 or snake.coords[snake.head_index]['x'] == settings.cell_w): if (snake.coords[snake.head_index]['x'] == -1 or snake.coords[snake.head_index]['x'] == settings.cell_w):

View File

@ -11,10 +11,12 @@ pygame.display.set_caption('贪吃蛇')
# 创建蛇 # 创建蛇
snake = Snake(settings) snake = Snake(settings)
def run_game1(): def run_game1():
gf.check_play_events(snake) gf.check_play_events(snake)
gf.update_screen(settings, screen, snake) gf.update_screen(settings, screen, snake)
def run_game(): def run_game():
while True: while True:
if settings.game_mode == 0: if settings.game_mode == 0:
@ -25,6 +27,7 @@ def run_game():
elif settings.game_mode == 1: elif settings.game_mode == 1:
run_game1() run_game1()
if __name__ == "__main__": if __name__ == "__main__":
pygame.init() pygame.init()
run_game() run_game()

View File

@ -1,6 +1,7 @@
import pygame import pygame
import sys import sys
class Settings: class Settings:
def __init__(self): def __init__(self):
self.bg_color = (0, 0, 0) # 背景颜色 self.bg_color = (0, 0, 0) # 背景颜色

View File

@ -1,5 +1,6 @@
import random import random
class Snake(): class Snake():
def __init__(self, settings): def __init__(self, settings):
self.reset(settings) self.reset(settings)
@ -19,6 +20,7 @@ class Snake():
# 更新蛇,每次蛇移动一步,因此相当于每次移动在蛇的坐标字典中再插入一个新蛇头 # 更新蛇,每次蛇移动一步,因此相当于每次移动在蛇的坐标字典中再插入一个新蛇头
# 按常识,每次移动后,不仅仅是蛇头向前移动一个,更重要的是蛇尾也要向前移,但是在该函数中先不处理蛇尾 # 按常识,每次移动后,不仅仅是蛇头向前移动一个,更重要的是蛇尾也要向前移,但是在该函数中先不处理蛇尾
# 因为如果蛇吃了食物,此时蛇尾其实并没有移动 # 因为如果蛇吃了食物,此时蛇尾其实并没有移动
def update(self): def update(self):
newHead = {} newHead = {}
# 根据移动方向确定蛇头 # 根据移动方向确定蛇头
@ -35,4 +37,3 @@ class Snake():
newHead = {'x': self.coords[self.head_index]['x'] + 1, newHead = {'x': self.coords[self.head_index]['x'] + 1,
'y': self.coords[self.head_index]['y']} 'y': self.coords[self.head_index]['y']}
self.coords.insert(0, newHead) self.coords.insert(0, newHead)