snake/game_functions.py
2024-07-15 00:53:49 +08:00

141 lines
5.1 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import pygame
import sys
def check_events(settings):
for event in pygame.event.get():
# 当按关闭或者按ESC键退出游戏
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
pygame.quit()
sys.exit()
# 按1键选择手动模式
elif event.key == pygame.K_1:
settings.game_mode = 1
# 按2键选择自动模式
elif event.key == pygame.K_2:
settings.game_mode = 2
def show_start(settings, screen):
"""模式选择界面"""
title_Font = pygame.font.Font(settings.font_name, 80) # 设置标题字体
title_image = title_Font.render("贪吃蛇", True, (255, 255, 255), (0, 0, 0))
title_rect = title_image.get_rect()
title_rect.center = screen.get_rect().center
presskey_font = pygame.font.Font(settings.font_name, 15) # 设置说明文字的字体
presskey_image = presskey_font.render(
'按1为手动模式,按2为自动模式,按ESC可退出游戏', True, (255, 255, 255), (0, 0, 0))
presskey_rect = presskey_image.get_rect()
presskey_rect.centerx = title_rect.centerx
presskey_rect.top = title_rect.bottom
while True:
screen.fill(settings.bg_color) # 绘制屏幕
screen.blit(title_image, title_rect) # 绘制标题
screen.blit(presskey_image, presskey_rect) # 绘制说明文字
check_events(settings) # 检测键盘
if settings.game_mode != 0: # 说明按了1或2退出循环
break
pygame.display.flip()
def show_end(settings, screen):
"""失败结算界面"""
title_font = pygame.font.Font(settings.font_name, 80)
game_image = title_font.render('Game', True, (233, 150, 122))
over_image = title_font.render('Over', True, (233, 150, 122))
game_rect = game_image.get_rect()
over_rect = over_image.get_rect()
screen_rect = screen.get_rect()
game_rect.midtop = (settings.width / 2, screen_rect.top + 70)
over_rect.midtop = (settings.width / 2, game_rect.bottom + 50)
screen.blit(game_image, game_rect)
screen.blit(over_image, over_rect)
pygame.display.flip()
pygame.time.wait(1500)
settings.game_mode = 0
def draw_grid(settings, screen):
#绘制横向线
for x in range(0, settings.width, settings.cell_size):
pygame.draw.line(screen,(40,40,40),(x,0),(x,settings.height))
#绘制竖向线
for y in range(0, settings.height, settings.cell_size):
pygame.draw.line(screen,(40,40,40),(0, y),(settings.width,y))
def update_screen(settings, screen):
screen.fill(settings.bg_color)
draw_grid(settings, screen)
#绘制蛇
def draw_snake(ai_settings, screen, snake):
#头部用白色
x = snake.coords[0]['x'] * 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)
pygame.draw.rect(screen,(255,255,255), snake_head_rect)
#蛇身内部用浅绿,外框用深绿
for coord in snake.coords[1: -1]:
x = coord['x'] * 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)
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)
pygame.draw.rect(screen,(0,255,0), snake_part_inner_rect)
#蛇尾用浅绿
coord = snake.coords[-1]
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):
screen.fill(settings.bg_color)
draw_grid(settings, screen)
#移动蛇
del snake.coords[-1]#加蛇头
snake.update()#去蛇尾
if not is_game_over(settings, snake):
settings.game_mode = -1
else:
draw_snake(settings, screen, snake)
pygame.display.flip()
#暂停一下
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 check_play_events(snake):
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
pygame.quit()
sys.exit()
elif event.key == pygame.K_LEFT and not snake.direction == 'right':
snake.direction = 'left'
elif event.key == pygame.K_RIGHT and not snake.direction == 'left':
snake.direction = 'right'
elif event.key == pygame.K_UP and not snake.direction == 'down':
snake.direction = 'up'
elif event.key == pygame.K_DOWN and not snake.direction == 'up':
snake.direction = 'down'