Screen.blit 不工作,除非 keydown

Screen.blit doesnt work unless keydown

我从 python 和 pygame 开始。所以有人介绍我 screen.blit 在屏幕上显示图片。但是,当我使用 screen.blit 时,除非我移动鼠标或单击按钮,否则我的程序中不会发生任何事情。我不知道为什么会这样,我简化了一个只有一只猫在屏幕上移动的例子。除非我按下按钮或移动鼠标,否则什么也不会发生。我正在使用 python 2.6.6

import pygame

pygame.init()
 
# Set the width and height of the screen [width, height]
size = (800, 600)
screen = pygame.display.set_mode(size)

pygame.display.set_caption("My Game")

# Loop until the user clicks the close button.
done = False

# Used to manage how fast the screen updates
clock = pygame.time.Clock()

floor = pygame.image.load("floor.png").convert()
cat = pygame.image.load("cat.png").convert()

a=0
b=0

# -------- Main Program Loop -----------
while not done:
    # --- Main event loop
    for event in pygame.event.get(): # User did something
        if event.type == pygame.QUIT: # If user clicked close
            done = True # Flag that we are done so we exit this loop
  
    # --- Game logic should go here
 a+=1
 b+=1
    # --- Drawing code should go here
 
 # First, clear the screen to white. Don't put other drawing commands
 # above this, or they will be erased with this command.
 screen.blit(floor, [0,0])
 screen.blit(cat, [a,b])
 # --- Go ahead and update the screen with what we've drawn.
 pygame.display.flip()
  pygame.display.update()
    # --- Limit to 60 frames per second
 clock.tick(60)
 
# Close the window and quit.
# If you forget this line, the program will 'hang'
# on exit if running from IDLE.
pygame.quit()

那是因为 screen.blip 在 for event in pygame.event.get():

里面

更正缩进,它将被修复。

试试这个:

while not done:
    # --- Main event loop
    for event in pygame.event.get(): # User did something
        if event.type == pygame.QUIT: # If user clicked close
            done = True # Flag that we are done so we exit this loop

    # --- Game logic should go here
    a+=1
    b+=1
    # --- Drawing code should go here

    # First, clear the screen to white. Don't put other drawing commands
    # above this, or they will be erased with this command.
    screen.blit(floor, [0,0])
    screen.blit(cat, [a,b])
    # --- Go ahead and update the screen with what we've drawn.
    pygame.display.flip()
    pygame.display.update()
    # --- Limit to 60 frames per second
    clock.tick(60)