add apple

This commit is contained in:
Billts_noo 2024-07-18 16:48:01 +08:00
parent 67e05a0964
commit 9a01b8eb29
5 changed files with 91 additions and 60 deletions

11
apple.py Normal file
View File

@ -0,0 +1,11 @@
import random
class Apple():
def __init__(self,settings):
self.x = random.randint(1, settings.cell_w - 1)
self.y = random.randint(1, settings.cell_h - 1)
def spawn(self,settings):
self.x = random.randint(1, settings.cell_w - 1)
self.y = random.randint(1, settings.cell_h - 1)

View File

@ -20,6 +20,30 @@ def check_events(settings):
settings.game_mode = 2 settings.game_mode = 2
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'
break
elif event.key == pygame.K_RIGHT and not snake.direction == 'left':
snake.direction = 'right'
break
elif event.key == pygame.K_UP and not snake.direction == 'down':
snake.direction = 'up'
break
elif event.key == pygame.K_DOWN and not snake.direction == 'up':
snake.direction = 'down'
break
def show_start(settings, screen): def show_start(settings, screen):
"""模式选择界面""" """模式选择界面"""
title_Font = pygame.font.Font(settings.font_name, 80) # 设置标题字体 title_Font = pygame.font.Font(settings.font_name, 80) # 设置标题字体
@ -33,10 +57,11 @@ def show_start(settings, screen):
presskey_rect = presskey_image.get_rect() presskey_rect = presskey_image.get_rect()
presskey_rect.centerx = title_rect.centerx presskey_rect.centerx = title_rect.centerx
presskey_rect.top = title_rect.bottom presskey_rect.top = title_rect.bottom
while True:
screen.fill(settings.bg_color) # 绘制屏幕 screen.fill(settings.bg_color) # 绘制屏幕
screen.blit(title_image, title_rect) # 绘制标题 screen.blit(title_image, title_rect) # 绘制标题
screen.blit(presskey_image, presskey_rect) # 绘制说明文字 screen.blit(presskey_image, presskey_rect) # 绘制说明文字
while True:
check_events(settings) # 检测键盘 check_events(settings) # 检测键盘
if settings.game_mode != 0: # 说明按了1或2退出循环 if settings.game_mode != 0: # 说明按了1或2退出循环
break break
@ -69,84 +94,75 @@ def draw_grid(settings, screen):
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 draw_snake(settings, screen, snake):
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 head_index = snake.coords[0]
y = snake.coords[0]['y'] * ai_settings.cell_size x = head_index['x'] * settings.cell_size
y = head_index['y'] * settings.cell_size
snake_head_rect = pygame.Rect( snake_head_rect = pygame.Rect(
x, y, ai_settings.cell_size, ai_settings.cell_size) x, y, settings.cell_size, 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 bodies_index in snake.coords[1: -1]:
x = coord['x'] * ai_settings.cell_size x = bodies_index['x'] * settings.cell_size
y = coord['y'] * ai_settings.cell_size y = bodies_index['y'] * settings.cell_size
snake_part_rect = pygame.Rect( snake_part_rect = pygame.Rect(
x, y, ai_settings.cell_size, ai_settings.cell_size) x, y, settings.cell_size, 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( snake_part_inner_rect = pygame.Rect(
x + 4, y + 4, ai_settings.cell_size - 8, ai_settings.cell_size - 8) x + 4, y + 4, settings.cell_size - 8, 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] tail_index = snake.coords[-1]
x = coord['x'] * ai_settings.cell_size x = tail_index['x'] * settings.cell_size
y = coord['y'] * ai_settings.cell_size y = tail_index['y'] * settings.cell_size
snake_tail_rect = pygame.Rect( snake_tail_rect = pygame.Rect(
x, y, ai_settings.cell_size, ai_settings.cell_size) x, y, settings.cell_size, settings.cell_size)
pygame.draw.rect(screen, (0, 255, 0), snake_tail_rect) pygame.draw.rect(screen, (0, 255, 0), snake_tail_rect)
def draw_apple(apple,settings,screen):
x = apple.x * settings.cell_size
y = apple.y * settings.cell_size
apple_rect = pygame.Rect(
x, y, settings.cell_size, settings.cell_size)
pygame.draw.rect(screen, (255, 0, 0), apple_rect)
# 绘制游戏界面 # 绘制游戏界面
def update_screen(settings, screen, snake,apple):
def update_screen(settings, screen, snake):
screen.fill(settings.bg_color) screen.fill(settings.bg_color)
draw_grid(settings, screen) draw_grid(settings, screen)
draw_apple(apple,settings,screen)
# 移动蛇 # 移动蛇
del snake.coords[-1] # 加蛇头 snake.move_head() # 加蛇头
snake.update() # 去蛇尾 if game_over(snake,settings):
if not is_game_over(settings, snake):
settings.game_mode = -1 settings.game_mode = -1
elif eat_apple(snake,apple):
apple.spawn(settings)
draw_apple(apple,settings,screen)
else: else:
del snake.coords[-1] # 去蛇尾
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): def game_over(snake,settings):
# 碰到左右墙壁 # 碰到左右墙壁
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):
return False return True
# 碰到上下墙壁 # 碰到上下墙壁
if (snake.coords[snake.head_index]['y'] == -1 or snake.coords[snake.head_index]['y'] == settings.cell_h): if (snake.coords[snake.head_index]['y'] == -1 or snake.coords[snake.head_index]['y'] == settings.cell_h):
return False return True
# 碰到自己 # 碰到自己
if (snake.coords[snake.head_index] in snake.coords[1:]): if (snake.coords[snake.head_index] in snake.coords[1:]):
return False
return True return True
return False
# 检测游戏过程中的按键 def eat_apple(snake,apple):
def check_play_events(snake): if (snake.coords[snake.head_index]['x'] == apple.x and snake.coords[snake.head_index]['y'] == apple.y):
for event in pygame.event.get(): return True
if event.type == pygame.QUIT: return False
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'

View File

@ -2,6 +2,7 @@ import pygame
from settings import Settings from settings import Settings
import game_functions as gf import game_functions as gf
from snake import Snake from snake import Snake
from apple import Apple
settings = Settings() settings = Settings()
# 设置屏幕 # 设置屏幕
@ -10,11 +11,13 @@ screen = pygame.display.set_mode((settings.width, settings.height))
pygame.display.set_caption('贪吃蛇') pygame.display.set_caption('贪吃蛇')
# 创建蛇 # 创建蛇
snake = Snake(settings) snake = Snake(settings)
# 创建苹果
apple = Apple(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,apple)
def run_game(): def run_game():

View File

@ -16,8 +16,9 @@ class Settings:
self.game_mode = 0 # 游戏状态 self.game_mode = 0 # 游戏状态
self.score = 0 # 游戏得分 self.score = 0 # 游戏得分
self.clock_frq = 5 # 刷新频率 self.clock_frq = 3 # 刷新频率
self.my_clock = pygame.time.Clock() self.my_clock = pygame.time.Clock()
if sys.platform.startswith("linux"): if sys.platform.startswith("linux"):
self.font_name = pygame.font.match_font('wenquanyizenhei') self.font_name = pygame.font.match_font('wenquanyizenhei')
else: else:

View File

@ -21,7 +21,7 @@ class Snake():
# 按常识,每次移动后,不仅仅是蛇头向前移动一个,更重要的是蛇尾也要向前移,但是在该函数中先不处理蛇尾 # 按常识,每次移动后,不仅仅是蛇头向前移动一个,更重要的是蛇尾也要向前移,但是在该函数中先不处理蛇尾
# 因为如果蛇吃了食物,此时蛇尾其实并没有移动 # 因为如果蛇吃了食物,此时蛇尾其实并没有移动
def update(self): def move_head(self):
newHead = {} newHead = {}
# 根据移动方向确定蛇头 # 根据移动方向确定蛇头
if self.direction == 'up': if self.direction == 'up':