commit 012f525c1d0259efdeff745cf1138b08b04131fc Author: xnz233 Date: Sun Jul 14 00:10:48 2024 +0800 First diff --git a/__pycache__/game_functions.cpython-39.pyc b/__pycache__/game_functions.cpython-39.pyc new file mode 100644 index 0000000..539dd70 Binary files /dev/null and b/__pycache__/game_functions.cpython-39.pyc differ diff --git a/__pycache__/settings.cpython-39.pyc b/__pycache__/settings.cpython-39.pyc new file mode 100644 index 0000000..1451157 Binary files /dev/null and b/__pycache__/settings.cpython-39.pyc differ diff --git a/game_functions.py b/game_functions.py new file mode 100644 index 0000000..2826152 --- /dev/null +++ b/game_functions.py @@ -0,0 +1,60 @@ +import pygame +import sys + + +def check_events(settings): + for event in pygame.event.get(): + # 当按关闭或者按ESC键退出游戏 + if event.type == pygame.QUIT: + pygame.quit() + sys.exit() + elif event.type == pygame.KEYDOWN: + if event.key == pygame.K_ESCAPE: + pygame.quit() + sys.exit() + # 按1键选择手动模式 + elif event.key == pygame.K_1: + settings.game_mode = -1 + # 按2键选择自动模式 + elif event.key == pygame.K_2: + settings.game_mode = -1 + + +def show_start(settings, screen): + """模式选择界面""" + title_Font = pygame.font.Font('simkai.ttf', 80) # 设置标题字体 + title_image = title_Font.render("贪吃蛇", True, (255, 255, 255), (0, 0, 0)) + title_rect = title_image.get_rect() + title_rect.center = screen.get_rect().center + + presskey_font = pygame.font.Font('simkai.ttf', 15) # 设置说明文字的字体 + presskey_image = presskey_font.render( + '按1为手动模式,按2为自动模式,按ESC可退出游戏', True, (255, 255, 255), (0, 0, 0)) + presskey_rect = presskey_image.get_rect() + presskey_rect.centerx = title_rect.centerx + presskey_rect.top = title_rect.bottom + 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 + pygame.display.flip() + + +def show_end(settings, screen): + """结算界面""" + title_font = pygame.font.Font('simkai.ttf', 80) + game_image = title_font.render('Game', True, (233, 150, 122)) + over_image = title_font.render('Over', True, (233, 150, 122)) + game_rect = game_image.get_rect() + over_rect = over_image.get_rect() + screen_rect = screen.get_rect() + game_rect.midtop = (settings.width / 2, screen_rect.top + 70) + over_rect.midtop = (settings.width / 2, game_rect.bottom + 50) + screen.blit(game_image, game_rect) + screen.blit(over_image, over_rect) + pygame.display.flip() + pygame.time.wait(1500) + settings.game_mode = 0 diff --git a/idea.km b/idea.km new file mode 100644 index 0000000..2f30464 --- /dev/null +++ b/idea.km @@ -0,0 +1,115 @@ +{ + "root": { + "data": { + "id": "d2oi0jx7h6o0", + "created": 1720882729134, + "text": "Astar贪吃蛇" + }, + "children": [ + { + "data": { + "id": "d2oi1r6uoww0", + "created": 1720882823319, + "text": "游戏本体(Pygame)", + "layout_mind_offset": { + "x": -29, + "y": 1 + } + }, + "children": [ + { + "data": { + "id": "d2oi2k2ygw00", + "created": 1720882886210, + "text": "蛇", + "expandState": "expand", + "layout_right_offset": { + "x": 8, + "y": -54 + } + }, + "children": [ + { + "data": { + "id": "d2oi21ezqoo0", + "created": 1720882845579, + "text": "如何表示蛇" + }, + "children": [] + }, + { + "data": { + "id": "d2oi2vcyync0", + "created": 1720882910760, + "text": "蛇如何移动" + }, + "children": [] + }, + { + "data": { + "id": "d2oi30i8aj40", + "created": 1720882921962, + "text": "如何判定游戏结束?" + }, + "children": [] + } + ] + }, + { + "data": { + "id": "d2oi3jwwljc0", + "created": 1720882964209, + "text": "食物" + }, + "children": [ + { + "data": { + "id": "d2oi3vw1ujc0", + "created": 1720882990278, + "text": "如何生成食物" + }, + "children": [] + } + ] + }, + { + "data": { + "id": "d2oi7hsyoao0", + "created": 1720883273073, + "text": "界面" + }, + "children": [] + } + ] + }, + { + "data": { + "id": "d2oijamyvjc0", + "created": 1720884197843, + "text": "游戏玩法" + }, + "children": [ + { + "data": { + "id": "d2oi4mwhxi80", + "created": 1720883049078, + "text": "自动(A*寻路算法)" + }, + "children": [] + }, + { + "data": { + "id": "d2oijfl72io0", + "created": 1720884208620, + "text": "手动" + }, + "children": [] + } + ] + } + ] + }, + "template": "default", + "theme": "snow", + "version": "1.4.43" +} \ No newline at end of file diff --git a/idea.png b/idea.png new file mode 100644 index 0000000..1c73717 Binary files /dev/null and b/idea.png differ diff --git a/main.py b/main.py new file mode 100644 index 0000000..bff7e7d --- /dev/null +++ b/main.py @@ -0,0 +1,21 @@ +from settings import Settings +import game_functions as gf +import pygame + +settings = Settings() + +screen = pygame.display.set_mode((settings.width, settings.height)) # 设置屏幕 +pygame.display.set_caption('贪吃蛇') # 添加游戏标题 + + +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) + + +if __name__ == "__main__": + pygame.init() + run_game() diff --git a/settings.py b/settings.py new file mode 100644 index 0000000..e801171 --- /dev/null +++ b/settings.py @@ -0,0 +1,19 @@ +import pygame + + +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 = 5 # 刷新频率 + self.my_clock = pygame.time.Clock() diff --git a/simkai.ttf b/simkai.ttf new file mode 100644 index 0000000..ece2ac2 Binary files /dev/null and b/simkai.ttf differ