【问题标题】:Screen.blit doesnt work unless keydown除非 keydown,否则 Screen.blit 不起作用
【发布时间】:2015-01-07 04:09:04
【问题描述】:

我从 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()

【问题讨论】:

    标签: python pygame python-2.6


    【解决方案1】:

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

    【讨论】:

    • 哇,我现在感觉很傻,尽管在我的编辑器中它们似乎在同一个制表位上。有趣的。我只是将 for 循环移到了 while 循环的底部。
    • 很高兴我能帮上忙。但可能会发生这种情况,因为您将制表符和空格混合用于缩进。获得一个从制表符转换为 4 个空格的编辑器,您将不会再遇到此问题。 ;)
    • 不,我使用制表符作为空格,但我可以使用更好的编辑器,你能推荐一个吗? :)
    • 我使用 PyCharm,但这有点矫枉过正。还有记事本++,也很不错。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-01-19
    • 2019-05-12
    • 2018-02-03
    • 1970-01-01
    • 1970-01-01
    • 2013-03-17
    • 1970-01-01
    相关资源
    最近更新 更多