用Python做个小游戏(3)

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

原文地址:https://fendou.gqr5.cn/4101.html
------本页内容已结束,喜欢请分享------

感谢您的来访,获取更多精彩文章请收藏本站。

THE END
喜欢就打个赏呗
点赞1 分享
评论 共1条

请登录后发表评论