snake/main.py

46 lines
1.1 KiB
Python
Raw Normal View History

2024-08-14 12:31:10 +08:00
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()