除非keydown,否则Screen.blit不起作用

时间:2015-01-07 04:09:04

标签: python pygame python-2.6

我开始使用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()

1 个答案:

答案 0 :(得分:0)

那是因为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)