上传文件至 /

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

11
main.py
View File

@ -3,6 +3,7 @@ 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()
# 设置屏幕 # 设置屏幕
@ -13,6 +14,9 @@ pygame.display.set_caption('贪吃蛇')
snake = Snake(settings) snake = Snake(settings)
# 创建苹果 # 创建苹果
apple = Apple(settings) apple = Apple(settings)
#创建AI
ai = Ai()
ai.start(snake,apple,settings)
#手动模式 #手动模式
def run_game1(): def run_game1():
@ -20,6 +24,10 @@ def run_game1():
gf.update_screen(settings, screen, snake,apple) 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(): def run_game():
while True: while True:
if settings.game_mode == 0: if settings.game_mode == 0:
@ -29,7 +37,8 @@ def run_game():
snake.reset(settings) snake.reset(settings)
elif settings.game_mode == 1: elif settings.game_mode == 1:
run_game1() run_game1()
elif settings.game_mode == 2:
run_game2()
if __name__ == "__main__": if __name__ == "__main__":
pygame.init() pygame.init()

View File

@ -8,7 +8,7 @@ class Settings:
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

View File

@ -8,8 +8,8 @@ class Snake():
# 蛇的初始化 # 蛇的初始化
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'
@ -26,14 +26,23 @@ class Snake():
# 根据移动方向确定蛇头 # 根据移动方向确定蛇头
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()