上传文件至 /

This commit is contained in:
Billts_noo 2024-08-14 12:31:10 +08:00
parent 4738b5c4ff
commit a62bf23e7c
3 changed files with 118 additions and 100 deletions

81
main.py
View File

@ -1,36 +1,45 @@
import pygame 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 from apple import Apple
from ai import Ai
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)
apple = Apple(settings) # 创建苹果
apple = Apple(settings)
#手动模式 #创建AI
def run_game1(): ai = Ai()
gf.check_play_events(snake) ai.start(snake,apple,settings)
gf.update_screen(settings, screen, snake,apple)
#手动模式
def run_game1():
def run_game(): gf.check_play_events(snake)
while True: gf.update_screen(settings, screen, snake,apple)
if settings.game_mode == 0:
gf.show_start(settings, screen)
elif settings.game_mode == -1: def run_game2():
gf.show_end(settings, screen) gf.autocheck_events(ai,snake,apple,settings)
snake.reset(settings) gf.autoupdate_screen(settings, screen, snake,apple,ai)
elif settings.game_mode == 1:
run_game1() def run_game():
while True:
if settings.game_mode == 0:
if __name__ == "__main__": gf.show_start(settings, screen)
pygame.init() elif settings.game_mode == -1:
run_game() gf.show_end(settings, screen)
snake.reset(settings)
elif settings.game_mode == 1:
run_game1()
elif settings.game_mode == 2:
run_game2()
if __name__ == "__main__":
pygame.init()
run_game()

View File

@ -1,25 +1,25 @@
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) # 背景颜色
self.width = 400 self.width = 400
self.height = 400 self.height = 400
self.cell_size = 20 # 格子大小 self.cell_size = 25 # 格子大小
self.cell_w = int(self.width / self.cell_size) # 一行的格子数 self.cell_w = int(self.width / self.cell_size) # 一行的格子数
self.cell_h = int(self.height / self.cell_size) # 一列的格子数 self.cell_h = int(self.height / self.cell_size) # 一列的格子数
self.num = self.cell_w * self.cell_h self.num = self.cell_w * self.cell_h
self.game_mode = 0 # 游戏状态 self.game_mode = 0 # 游戏状态
self.score = 0 # 游戏得分 self.score = 0 # 游戏得分
self.clock_frq = 3 # 刷新频率 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:
self.font_name = pygame.font.match_font('simhei') self.font_name = pygame.font.match_font('simhei')

View File

@ -1,39 +1,48 @@
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 = 5#random.randint(5, settings.cell_w - 6)
self.start_y = random.randint(5, settings.cell_h - 6) self.start_y = 5#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 move_head(self): def move_head(self):
newHead = {} newHead = {}
# 根据移动方向确定蛇头 # 根据移动方向确定蛇头
if self.direction == 'up': if self.direction == 'up':
newHead = {'x': self.coords[self.head_index]['x'], newHead = {'x': self.coords[self.head_index]['x'],
'y': self.coords[self.head_index]['y'] - 1} 'y': self.coords[self.head_index]['y'] - 1}
elif self.direction == 'down': elif self.direction == 'down':
newHead = {'x': self.coords[self.head_index]['x'], newHead = {'x': self.coords[self.head_index]['x'],
'y': self.coords[self.head_index]['y'] + 1} 'y': self.coords[self.head_index]['y'] + 1}
elif self.direction == 'left': elif self.direction == 'left':
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']}
elif self.direction == 'right': elif self.direction == 'right':
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)
def create_iterator(self,path):
def automove_head():
for newhead in path:
self.coords.insert(0, newhead)
return automove_head
def automove_head(self,ai):
ai.iterator()