上传文件至 /

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
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()

View File

@ -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')

View File

@ -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()