From a62bf23e7c37d40a35efe4f17cf6964b020bc802 Mon Sep 17 00:00:00 2001 From: Billts_noo <179370147@qq.com> Date: Wed, 14 Aug 2024 12:31:10 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=8A=E4=BC=A0=E6=96=87=E4=BB=B6=E8=87=B3?= =?UTF-8?q?=20/?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 续 --- main.py | 81 +++++++++++++++++++++++++++---------------------- settings.py | 50 +++++++++++++++--------------- snake.py | 87 +++++++++++++++++++++++++++++------------------------ 3 files changed, 118 insertions(+), 100 deletions(-) diff --git a/main.py b/main.py index 1cc8299..8b64d00 100644 --- a/main.py +++ b/main.py @@ -1,36 +1,45 @@ -import pygame -from settings import Settings -import game_functions as gf -from snake import Snake -from apple import Apple - -settings = Settings() -# 设置屏幕 -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,apple) - - -def run_game(): - while True: - if settings.game_mode == 0: - gf.show_start(settings, screen) - elif settings.game_mode == -1: - gf.show_end(settings, screen) - snake.reset(settings) - elif settings.game_mode == 1: - run_game1() - - -if __name__ == "__main__": - pygame.init() - run_game() +import pygame +from settings import Settings +import game_functions as gf +from snake import Snake +from apple import Apple +from ai import Ai + +settings = Settings() +# 设置屏幕 +screen = pygame.display.set_mode((settings.width, settings.height)) +# 添加游戏标题 +pygame.display.set_caption('贪吃蛇') +# 创建蛇 +snake = Snake(settings) +# 创建苹果 +apple = Apple(settings) +#创建AI +ai = Ai() +ai.start(snake,apple,settings) + +#手动模式 +def run_game1(): + gf.check_play_events(snake) + gf.update_screen(settings, screen, snake,apple) + + +def run_game2(): + gf.autocheck_events(ai,snake,apple,settings) + gf.autoupdate_screen(settings, screen, snake,apple,ai) + +def run_game(): + while True: + if settings.game_mode == 0: + gf.show_start(settings, screen) + elif settings.game_mode == -1: + 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() diff --git a/settings.py b/settings.py index 2ca95b9..6a03e37 100644 --- a/settings.py +++ b/settings.py @@ -1,25 +1,25 @@ -import pygame -import sys - - -class Settings: - def __init__(self): - self.bg_color = (0, 0, 0) # 背景颜色 - self.width = 400 - self.height = 400 - - self.cell_size = 20 # 格子大小 - self.cell_w = int(self.width / self.cell_size) # 一行的格子数 - self.cell_h = int(self.height / self.cell_size) # 一列的格子数 - self.num = self.cell_w * self.cell_h - - self.game_mode = 0 # 游戏状态 - self.score = 0 # 游戏得分 - - self.clock_frq = 3 # 刷新频率 - self.my_clock = pygame.time.Clock() - - if sys.platform.startswith("linux"): - self.font_name = pygame.font.match_font('wenquanyizenhei') - else: - self.font_name = pygame.font.match_font('simhei') +import pygame +import sys + + +class Settings: + def __init__(self): + self.bg_color = (0, 0, 0) # 背景颜色 + self.width = 400 + self.height = 400 + + self.cell_size = 25 # 格子大小 + self.cell_w = int(self.width / self.cell_size) # 一行的格子数 + self.cell_h = int(self.height / self.cell_size) # 一列的格子数 + self.num = self.cell_w * self.cell_h + + self.game_mode = 0 # 游戏状态 + self.score = 0 # 游戏得分 + + self.clock_frq = 3 # 刷新频率 + self.my_clock = pygame.time.Clock() + + if sys.platform.startswith("linux"): + self.font_name = pygame.font.match_font('wenquanyizenhei') + else: + self.font_name = pygame.font.match_font('simhei') diff --git a/snake.py b/snake.py index da37a31..210cd95 100644 --- a/snake.py +++ b/snake.py @@ -1,39 +1,48 @@ -import random - - -class Snake(): - def __init__(self, settings): - self.reset(settings) - - # 蛇的初始化 - def reset(self, settings): - # 蛇头的坐标 - self.start_x = random.randint(5, settings.cell_w - 6) - self.start_y = random.randint(5, settings.cell_h - 6) - self.head_index = 0 - # 蛇的初始运动方向 - self.direction = 'right' - # 蛇的初始坐标字典,初始蛇为蛇头位置及其左边的两个格子 - self.coords = [{'x': self.start_x, 'y': self.start_y}, - {'x': self.start_x - 1, 'y': self.start_y}, - {'x': self.start_x - 2, 'y': self.start_y}] - # 更新蛇,每次蛇移动一步,因此相当于每次移动在蛇的坐标字典中再插入一个新蛇头 - # 按常识,每次移动后,不仅仅是蛇头向前移动一个,更重要的是蛇尾也要向前移,但是在该函数中先不处理蛇尾 - # 因为如果蛇吃了食物,此时蛇尾其实并没有移动 - - def move_head(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) +import random + + +class Snake(): + def __init__(self, settings): + self.reset(settings) + + # 蛇的初始化 + def reset(self, settings): + # 蛇头的坐标 + self.start_x = 5#random.randint(5, settings.cell_w - 6) + self.start_y = 5#random.randint(5, settings.cell_h - 6) + self.head_index = 0 + # 蛇的初始运动方向 + self.direction = 'right' + # 蛇的初始坐标字典,初始蛇为蛇头位置及其左边的两个格子 + self.coords = [{'x': self.start_x, 'y': self.start_y}, + {'x': self.start_x - 1, 'y': self.start_y}, + {'x': self.start_x - 2, 'y': self.start_y}] + # 更新蛇,每次蛇移动一步,因此相当于每次移动在蛇的坐标字典中再插入一个新蛇头 + # 按常识,每次移动后,不仅仅是蛇头向前移动一个,更重要的是蛇尾也要向前移,但是在该函数中先不处理蛇尾 + # 因为如果蛇吃了食物,此时蛇尾其实并没有移动 + + def move_head(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 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() \ No newline at end of file