import math, sys, os, pygame, random, time
pygame.init()
screen = pygame.display.set_mode((500,500))
pygame.display.set_caption('Tester.')
pygame.mouse.set_visible(0)
def smileMove():
smiley = pygame.image.load('smiley.png')
random.seed()
xMove = random.randrange(1,501)
yMove = random.randrange(1,501)
screen.blit(smiley,(xMove,yMove))
c = 0
while c <5:
smileMove()
time.sleep(3)
c = c + 1
pygame.quit()
我对编程非常陌生,我只是尝试使用 PyGame 进行一些基本操作。 屏幕保持黑色,并且没有出现笑脸。我试图让面孔出现在黑色背景上,并且每3秒改变到另一个随机位置,5次然后退出。
最佳答案
您缺少对 pygame.display.flip()
的调用来实际更新窗口内容 - 将其放在 time.sleep
调用之前。
在尝试 Python 和 pygame API 的早期阶段,我的建议是在交互式控制台上进行尝试。
关于python - 为什么这段代码不起作用? (Python 和 PyGame),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15751257/