我执行以下代码,只得到一个空白(黑色)窗口。
窗口标题显示,但我还没有加载图像(我也尝试使用除所使用的图像之外的其他图像)。 .py 文件和图像位于同一目录中。
background_image_filename='checkmark.jpg'
mouse_image_filename='digestive_bw.png'
import pygame, sys
from pygame.locals import*
from sys import exit
pygame.init()
screen=pygame.display.set_mode((800,800),0,32)
#pygame.display.set_caption("Hello, Howdy, Mate, and Hi there world!")
background=pygame.image.load(background_image_filename).convert()
mouse_cursor=pygame.image.load(mouse_image_filename).convert_alpha()
while True:
for event in pygame.event.get():
if event.type==QUIT:
pygame.quit()
sys.exit()
screen.blit(background,(0,0))
x,y=pygame.mouse.get_pos()
x-=mouse_cursor.get_width() /2
y=-mouse_cursor.get_height() /2
screen.blit(mouse_cursor,(x,y))
pygame.display.update()
我已经安装了 python 3.2 和 pygame 1.9.2。如果我无法让它工作,我会考虑卸载它们并安装 3.1 + 1.9.1。
您应该将代码放在循环内,并使用时钟以避免使用所有 cpu:
clock = pygame.time.Clock()
while True:
for event in pygame.event.get():
if event.type==QUIT:
pygame.quit()
sys.exit()
screen.blit(background,(0,0))
x,y=pygame.mouse.get_pos()
x-=mouse_cursor.get_width() /2
y=-mouse_cursor.get_height() /2
screen.blit(mouse_cursor,(x,y))
pygame.display.update()
clock.tick(30) # keep 30 fps
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)