我的程序有两个问题:
keys = pygame.key.get_pressed() pygame.error: video system not initialized守则的重要部分是:
import pygame
from pygame.locals import*
pygame.init()
screen = pygame.display.set_mode((1200, 700))
ticket1 = True
# ...
c = 550
d = 100
# ...
color2 = (250, 20, 20)
while ticket1 == True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
ticket1 = False
pygame.quit()
pygame.display.quit()
keys = pygame.key.get_pressed()
if keys[pygame.K_d]:
c += 1
# ...
screen.fill((255, 250, 245))
pygame.draw.rect(screen, color2, pygame.Rect(c, d, 50, 75))
pygame.display.flip()如果我只在write循环中编写keys = pygame.key.get_pressed(),那么它没有错误,但它看起来更慢。
我还有另一个错误:pygame.error: display Surface quit,但我总是和我所有的游戏程序都有它,它不那么重要,但其他的东西是重要的。
发布于 2014-03-01 21:14:31
1.?
在pygame.quit()之后,你不需要pygame.display.quit(),而需要sys.exit()。pygame.quit()不退出程序,所以程序仍然试图调用screen.fill()和pygame.quit()下面的其他函数
或者你必须把pygame.quit()放在while ticket == True:之外(然后你就不需要sys.exit()了)
你可以用while ticket1:来代替while ticket == True: --它更像丙酮。
while ticket1: # it is more pythonic
for event in pygame.event.get():
if event.type == pygame.QUIT:
ticket1 = False
keys = pygame.key.get_pressed()
if keys[pygame.K_d]:
c += 1
# ...
screen.fill((255, 250, 245))
pygame.draw.rect(screen, color2, pygame.Rect(c, d, 50, 75))
pygame.display.flip()
pygame.quit()2.?
if keys[pygame.K_d]: c += 1在for event循环中,所以它只在事件发生时调用鼠标移动、按键按下或“解压”时调用。将它移到for event循环之外。
while ticket1: # it is more pythonic
for event in pygame.event.get():
if event.type == pygame.QUIT:
ticket1 = False
keys = pygame.key.get_pressed()
# outside of `for event` loop
if keys[pygame.K_d]:
c += 1
# ...
screen.fill((255, 250, 245))
pygame.draw.rect(screen, color2, pygame.Rect(c, d, 50, 75))
pygame.display.flip()
pygame.quit()有些人在没有get_pressed()的情况下做这件事
# clock = pygame.time.Clock()
move_x = 0
while ticket1 == True:
# events
for event in pygame.event.get():
if event.type == pygame.QUIT:
ticket1 = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
ticket1 = False
elif event.key == pygame.K_d:
move_x = 1
elif event.type == pygame.KEYUP:
if event.key == pygame.K_d:
move_x = 0
# variable modification
c += move_x
# ...
# draws
screen.fill((255, 250, 245))
pygame.draw.rect(screen, color2, pygame.Rect(c, d, 50, 75))
pygame.display.flip()
# 60 FPS (Frame Per Second) to make CPU cooler
# clock.tick(60)
pygame.quit()顺便说一句:使用pygame.time.Clock()在快速和慢的计算机上获得相同的FPS。如果没有FPS程序,刷新屏幕的想法是每秒发送次数,因此CPU是繁忙和热的。
如果您使用FPS,您必须添加到c更大的价值,以获得相同的速度,然后再之前。
https://stackoverflow.com/questions/22119244
复制相似问题