import cfg
import sys
import pygame
from modules import *
”’主函数”’
def main(cfg):
pygame.init()
screen = pygame.display.set_mode(cfg.SCREENSIZE)
pygame.display.set_caption(‘Greedy Snake —— 九歌’)
clock = pygame.time.Clock()
pygame.mixer.music.load(cfg.BGMPATH)
pygame.mixer.music.play(-1)
snake = Snake(cfg)
apple = Apple(cfg, snake.coords)
score = 0
while True:
screen.fill(cfg.BLACK)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key in [pygame.K_UP, pygame.K_DOWN, pygame.K_LEFT, pygame.K_RIGHT]:
snake.setDirection({pygame.K_UP: ‘up’, pygame.K_DOWN: ‘down’, pygame.K_LEFT: ‘left’, pygame.K_RIGHT: ‘right’}[event.key])
if snake.update(apple):
apple = Apple(cfg, snake.coords)
score += 1
if snake.isgameover: break
drawGameGrid(cfg, screen)
snake.draw(screen)
apple.draw(screen)
showScore(cfg, score, screen)
pygame.display.update()
clock.tick(cfg.FPS)
return endInterface(screen, cfg)
”’run”’
if __name__ == ‘__main__’:
while True:
if not main(cfg):
break
- 最新
- 最热
只看作者