新手请教,游戏入门程序 c
import sys
import pygame
def run_game():
pygame.init()
screen = pygame.display.set_mode((1200, 800))
pygame.display.set_caption("Alien Invasion")
while True:
for event in pygame.event.get():
if event.type == pygame.quit():
sys.exit()
screen.fill((200, 200, 200))
pygame.display.flip()
这段代码运行结果是这样,请问什么问题啊?照着书本的范例写的。
Traceback (most recent call last):
File "E:/My doc/Python/excersize/newgame/alien_invasion.py", line 36, in
run_game()
File "E:/My doc/Python/excersize/newgame/alien_invasion.py", line 15, in run_game
screen.fill((200, 200, 200))
pygame.error: display Surface quit
Process finished with exit code 1
本作品采用《CC 协议》,转载必须注明作者和本文链接
关于 LearnKu
有没有人帮忙看下 :confused: :confused: :confused: :confused:
如果你的
pygame的版本是1.9.4的话,建议pygame.quit()·改为pygame.QUIT即可,原因是if条件永远成立,根本不会触发事件监听,进行画面绘制时系统退出了,找不到surface,。前者返回的是常量, 后者若所有pygame 模块未初始化则返回 None值,很明显 最开始执行条件时,event.type 是None值,条件永远成立,紧接退出 ,后续
fill之流不成立,报错是应该的if event.type == pygame.quit():
.....
screen.fill((200, 200, 200))
pygame.quit() 是一个函数/方法, 呼叫这个会结束pygame, 你再呼叫screen.fill((200,200,200)), 就会出错, 因为screen.fill()会调用到pygame.
这里有两个错:
所以你只要把quit()改成QUIT就可以了 !