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

@ -19,6 +19,30 @@ def check_events(settings):
elif event.key == pygame.K_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):
"""模式选择界面"""
@ -33,10 +57,11 @@ def show_start(settings, screen):
presskey_rect = presskey_image.get_rect()
presskey_rect.centerx = title_rect.centerx
presskey_rect.top = title_rect.bottom
screen.fill(settings.bg_color) # 绘制屏幕
screen.blit(title_image, title_rect) # 绘制标题
screen.blit(presskey_image, presskey_rect) # 绘制说明文字
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
@ -69,84 +94,75 @@ def draw_grid(settings, screen):
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):
def draw_snake(settings, screen, snake):
# 头部用白色
x = snake.coords[0]['x'] * ai_settings.cell_size
y = snake.coords[0]['y'] * ai_settings.cell_size
head_index = snake.coords[0]
x = head_index['x'] * settings.cell_size
y = head_index['y'] * settings.cell_size
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)
# 蛇身内部用浅绿,外框用深绿
for coord in snake.coords[1: -1]:
x = coord['x'] * ai_settings.cell_size
y = coord['y'] * ai_settings.cell_size
for bodies_index in snake.coords[1: -1]:
x = bodies_index['x'] * settings.cell_size
y = bodies_index['y'] * settings.cell_size
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)
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)
# 蛇尾用浅绿
coord = snake.coords[-1]
x = coord['x'] * ai_settings.cell_size
y = coord['y'] * ai_settings.cell_size
tail_index = snake.coords[-1]
x = tail_index['x'] * settings.cell_size
y = tail_index['y'] * settings.cell_size
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)
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):
def update_screen(settings, screen, snake,apple):
screen.fill(settings.bg_color)
draw_grid(settings, screen)
draw_apple(apple,settings,screen)
# 移动蛇
del snake.coords[-1] # 加蛇头
snake.update() # 去蛇尾
if not is_game_over(settings, snake):
snake.move_head() # 加蛇头
if game_over(snake,settings):
settings.game_mode = -1
elif eat_apple(snake,apple):
apple.spawn(settings)
draw_apple(apple,settings,screen)
else:
draw_snake(settings, screen, snake)
pygame.display.flip()
# 暂停一下
settings.my_clock.tick(settings.clock_frq)
del snake.coords[-1] # 去蛇尾
draw_snake(settings, screen, snake)
pygame.display.flip()
# 暂停一下
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):
return False
return True
# 碰到上下墙壁
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:]):
return False
return True
return True
return False
# 检测游戏过程中的按键
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'
def eat_apple(snake,apple):
if (snake.coords[snake.head_index]['x'] == apple.x and snake.coords[snake.head_index]['y'] == apple.y):
return True
return False

View File

@ -2,6 +2,7 @@ import pygame
from settings import Settings
import game_functions as gf
from snake import Snake
from apple import Apple
settings = Settings()
# 设置屏幕
@ -10,11 +11,13 @@ screen = pygame.display.set_mode((settings.width, settings.height))
pygame.display.set_caption('贪吃蛇')
# 创建蛇
snake = Snake(settings)
# 创建苹果
apple = Apple(settings)
#手动模式
def run_game1():
gf.check_play_events(snake)
gf.update_screen(settings, screen, snake)
gf.update_screen(settings, screen, snake,apple)
def run_game():

View File

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

View File

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