格式化

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,83 +59,94 @@ 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):
pygame.draw.line(screen,(40,40,40),(x,0),(x,settings.height)) pygame.draw.line(screen, (40, 40, 40), (x, 0), (x, settings.height))
#绘制竖向线 # 绘制竖向线
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(
pygame.draw.rect(screen,(255,255,255), snake_head_rect) x, y, ai_settings.cell_size, ai_settings.cell_size)
#蛇身内部用浅绿,外框用深绿 pygame.draw.rect(screen, (255, 255, 255), snake_head_rect)
for coord in snake.coords[1: -1]: # 蛇身内部用浅绿,外框用深绿
x = coord['x'] * ai_settings.cell_size for coord in snake.coords[1: -1]:
y = coord['y'] * ai_settings.cell_size x = coord['x'] * ai_settings.cell_size
snake_part_rect = pygame.Rect(x, y, ai_settings.cell_size, ai_settings.cell_size) y = coord['y'] * ai_settings.cell_size
pygame.draw.rect(screen,(0,155,0), snake_part_rect) snake_part_rect = pygame.Rect(
snake_part_inner_rect = pygame.Rect(x + 4, y + 4, ai_settings.cell_size - 8, ai_settings.cell_size - 8) x, y, ai_settings.cell_size, ai_settings.cell_size)
pygame.draw.rect(screen,(0,255,0), snake_part_inner_rect) pygame.draw.rect(screen, (0, 155, 0), snake_part_rect)
#蛇尾用浅绿 snake_part_inner_rect = pygame.Rect(
coord = snake.coords[-1] x + 4, y + 4, ai_settings.cell_size - 8, ai_settings.cell_size - 8)
x = coord['x'] * ai_settings.cell_size pygame.draw.rect(screen, (0, 255, 0), snake_part_inner_rect)
y = coord['y'] * ai_settings.cell_size # 蛇尾用浅绿
snake_tail_rect = pygame.Rect(x, y, ai_settings.cell_size, ai_settings.cell_size) coord = snake.coords[-1]
pygame.draw.rect(screen,(0,255,0), snake_tail_rect) x = coord['x'] * 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)
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)
#移动蛇 # 移动蛇
del snake.coords[-1]#加蛇头 del snake.coords[-1] # 加蛇头
snake.update()#去蛇尾 snake.update() # 去蛇尾
if not is_game_over(settings, snake): if not is_game_over(settings, snake):
settings.game_mode = -1 settings.game_mode = -1
else: else:
draw_snake(settings, screen, snake) draw_snake(settings, screen, snake)
pygame.display.flip() pygame.display.flip()
#暂停一下 # 暂停一下
settings.my_clock.tick(settings.clock_frq) settings.my_clock.tick(settings.clock_frq)
def is_game_over(settings,snake):
#碰到左右墙壁
if (snake.coords[snake.head_index]['x'] == -1 or snake.coords[snake.head_index]['x'] == settings.cell_w):
return False
#碰到上下墙壁
if(snake.coords[snake.head_index]['y'] == -1 or snake.coords[snake.head_index]['y'] == settings.cell_h):
return False
#碰到自己
if(snake.coords[snake.head_index] in snake.coords[1:]):
return False
return True
#检测游戏过程中的按键 def is_game_over(settings, snake):
# 碰到左右墙壁
if (snake.coords[snake.head_index]['x'] == -1 or snake.coords[snake.head_index]['x'] == settings.cell_w):
return False
# 碰到上下墙壁
if (snake.coords[snake.head_index]['y'] == -1 or snake.coords[snake.head_index]['y'] == settings.cell_h):
return False
# 碰到自己
if (snake.coords[snake.head_index] in snake.coords[1:]):
return False
return True
# 检测游戏过程中的按键
def check_play_events(snake): def check_play_events(snake):
for event in pygame.event.get(): for event in pygame.event.get():
if event.type == pygame.QUIT: if event.type == pygame.QUIT:
pygame.quit() pygame.quit()
sys.exit() sys.exit()
elif event.type == pygame.KEYDOWN: elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE: if event.key == pygame.K_ESCAPE:
pygame.quit() pygame.quit()
sys.exit() sys.exit()
elif event.key == pygame.K_LEFT and not snake.direction == 'right': elif event.key == pygame.K_LEFT and not snake.direction == 'right':
snake.direction = 'left' snake.direction = 'left'
elif event.key == pygame.K_RIGHT and not snake.direction == 'left': elif event.key == pygame.K_RIGHT and not snake.direction == 'left':
snake.direction = 'right' snake.direction = 'right'
elif event.key == pygame.K_UP and not snake.direction == 'down': elif event.key == pygame.K_UP and not snake.direction == 'down':
snake.direction = 'up' snake.direction = 'up'
elif event.key == pygame.K_DOWN and not snake.direction == 'up': elif event.key == pygame.K_DOWN and not snake.direction == 'up':
snake.direction = 'down' snake.direction = 'down'

33
main.py
View File

@ -4,27 +4,30 @@ import game_functions as gf
from snake import Snake from snake import Snake
settings = Settings() settings = Settings()
#设置屏幕 # 设置屏幕
screen = pygame.display.set_mode((settings.width, settings.height)) screen = pygame.display.set_mode((settings.width, settings.height))
#添加游戏标题 # 添加游戏标题
pygame.display.set_caption('贪吃蛇') 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:
gf.show_start(settings,screen) gf.show_start(settings, screen)
elif settings.game_mode == -1: elif settings.game_mode == -1:
gf.show_end(settings,screen) gf.show_end(settings, screen)
snake.reset(settings) snake.reset(settings)
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,38 +1,39 @@
import random import random
class Snake(): class Snake():
def __init__(self,settings): def __init__(self, settings):
self.reset(settings) self.reset(settings)
#蛇的初始化 # 蛇的初始化
def reset(self,settings): def reset(self, settings):
#蛇头的坐标 # 蛇头的坐标
self.start_x = random.randint(5, settings.cell_w - 6) self.start_x = random.randint(5, settings.cell_w - 6)
self.start_y = random.randint(5, settings.cell_h - 6) self.start_y = random.randint(5, settings.cell_h - 6)
self.head_index = 0 self.head_index = 0
#蛇的初始运动方向 # 蛇的初始运动方向
self.direction = 'right' self.direction = 'right'
#蛇的初始坐标字典,初始蛇为蛇头位置及其左边的两个格子 # 蛇的初始坐标字典,初始蛇为蛇头位置及其左边的两个格子
self.coords = [{'x' : self.start_x, 'y': self.start_y}, self.coords = [{'x': self.start_x, 'y': self.start_y},
{'x' : self.start_x - 1, 'y': self.start_y}, {'x': self.start_x - 1, 'y': self.start_y},
{'x' : self.start_x - 2, 'y': self.start_y}] {'x': self.start_x - 2, 'y': self.start_y}]
#更新蛇,每次蛇移动一步,因此相当于每次移动在蛇的坐标字典中再插入一个新蛇头 # 更新蛇,每次蛇移动一步,因此相当于每次移动在蛇的坐标字典中再插入一个新蛇头
#按常识,每次移动后,不仅仅是蛇头向前移动一个,更重要的是蛇尾也要向前移,但是在该函数中先不处理蛇尾 # 按常识,每次移动后,不仅仅是蛇头向前移动一个,更重要的是蛇尾也要向前移,但是在该函数中先不处理蛇尾
#因为如果蛇吃了食物,此时蛇尾其实并没有移动 # 因为如果蛇吃了食物,此时蛇尾其实并没有移动
def update(self):
newHead = {}
#根据移动方向确定蛇头
if self.direction == 'up':
newHead = {'x' : self.coords[self.head_index]['x'],
'y' : self.coords[self.head_index]['y'] - 1}
elif self.direction == 'down':
newHead = {'x' : self.coords[self.head_index]['x'],
'y' : self.coords[self.head_index]['y'] + 1}
elif self.direction == 'left':
newHead = {'x' : self.coords[self.head_index]['x'] - 1,
'y' : self.coords[self.head_index]['y']}
elif self.direction == 'right':
newHead = {'x' : self.coords[self.head_index]['x'] + 1,
'y' : self.coords[self.head_index]['y']}
self.coords.insert(0,newHead)
def update(self):
newHead = {}
# 根据移动方向确定蛇头
if self.direction == 'up':
newHead = {'x': self.coords[self.head_index]['x'],
'y': self.coords[self.head_index]['y'] - 1}
elif self.direction == 'down':
newHead = {'x': self.coords[self.head_index]['x'],
'y': self.coords[self.head_index]['y'] + 1}
elif self.direction == 'left':
newHead = {'x': self.coords[self.head_index]['x'] - 1,
'y': self.coords[self.head_index]['y']}
elif self.direction == 'right':
newHead = {'x': self.coords[self.head_index]['x'] + 1,
'y': self.coords[self.head_index]['y']}
self.coords.insert(0, newHead)