以下是我现在如何实现我的简单 pygames:
import pygame, sys
from pygame.locals import *
def run_game():
pygame.init()
SIZE = (640, 400)
BG_COLOUR = (0, 0, 0)
LINE_COLOUR = (255, 255, 255)
screen = pygame.display.set_mode(SIZE)
clock = pygame.time.Clock()
while True:
time_passed = clock.tick(30)
for event in pygame.event.get():
if event.type == QUIT:
exit_game()
screen.fill(BG_COLOUR)
pygame.draw.aaline(screen, LINE_COLOUR, (1, 1), (639, 399))
pygame.display.flip()
def exit_game():
sys.exit()
if __name__ == "__main__"
run_game()
我还看到一个keeprunning
标志被用来退出主事件循环,以及使用pygame.event.poll()
而不是循环遍历pygame.event.get()
. 有什么建议,比如变量的大小写/命名,有什么可以让它更有效或更易读的吗?