snake/main.py

37 lines
831 B
Python
Raw Permalink Normal View History

2024-07-15 00:53:49 +08:00
import pygame
2024-07-14 00:10:48 +08:00
from settings import Settings
import game_functions as gf
2024-07-15 00:53:49 +08:00
from snake import Snake
2024-07-18 16:48:01 +08:00
from apple import Apple
2024-07-15 01:07:37 +08:00
2024-07-14 00:10:48 +08:00
settings = Settings()
2024-07-15 01:07:37 +08:00
# 设置屏幕
2024-07-15 00:53:49 +08:00
screen = pygame.display.set_mode((settings.width, settings.height))
2024-07-15 01:07:37 +08:00
# 添加游戏标题
2024-07-15 00:53:49 +08:00
pygame.display.set_caption('贪吃蛇')
2024-07-15 01:07:37 +08:00
# 创建蛇
2024-07-15 00:53:49 +08:00
snake = Snake(settings)
2024-07-18 16:48:01 +08:00
# 创建苹果
apple = Apple(settings)
2024-07-15 01:07:37 +08:00
2024-07-18 16:48:01 +08:00
#手动模式
2024-07-15 00:53:49 +08:00
def run_game1():
2024-07-15 01:07:37 +08:00
gf.check_play_events(snake)
2024-07-18 16:48:01 +08:00
gf.update_screen(settings, screen, snake,apple)
2024-07-15 01:07:37 +08:00
2024-07-14 00:10:48 +08:00
def run_game():
2024-07-15 01:07:37 +08:00
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()
2024-07-14 00:10:48 +08:00
if __name__ == "__main__":
2024-07-15 01:07:37 +08:00
pygame.init()
run_game()