我已经搜索了一些问题,但它并没有真正回答我的问题。
我是Python& Pygame,现在几周了:),做一个关于游戏开发的教程。 我这里有一个代码&它应该在屏幕上显示我的错误图像,但它没有,而是我得到一个黑色的窗口...... 我哪里出错?
import pygame, sys
pygame.init()
WIDTH, HEIGHT = (800, 600)
screen=pygame.display.set_mode((WIDTH, HEIGHT),0, 32)
img_bug = pygame.image.load("bug.png").convert_alpha()
clock = pygame.time.Clock()
FPS = 24
fivesecondinterval = FPS * 5
totalframes = 0
while True: #Never ending loop
for event in pygame.event.get():
if event.type==pygame.QUIT:
pygame.quit()
sys.exit()
totalframes += 1
if totalframes % fivesecondinterval ==0 :
screen.blit(img_bug, (200, 200))
pygame.display.flip()
clock.tick(FPS)
答案 0 :(得分:0)
您必须正确缩进代码。这应该有效:
import pygame, sys
pygame.init()
WIDTH, HEIGHT = (800, 600)
screen=pygame.display.set_mode((WIDTH, HEIGHT),0, 32)
img_bug = pygame.image.load("bug.png").convert_alpha()
clock = pygame.time.Clock()
FPS = 24
fivesecondinterval = FPS * 5
totalframes = 0
while True: #Never ending loop
for event in pygame.event.get():
if event.type==pygame.QUIT:
pygame.quit()
sys.exit()
totalframes += 1
if totalframes % fivesecondinterval ==0 :
screen.blit(img_bug, (200, 200))
pygame.display.flip()
clock.tick(FPS)
import和part是(在修复导致异常的每个缩进错误之后):
if totalframes % fivesecondinterval==0:
screen.blit(img_bug, (200, 200))
pygame.display.flip()
clock.tick(FPS)
部分代码在循环内部。