[英]pygame won't blit inside function?
I have a block of code that worked outside of a function, but not inside. 我有一段代码在函数外部工作,但不在内部工作。
I also made sure the necessary variables were global. 我还确保必要的变量是全局的。
I can get the width of the surface using chair.get_width()
inside the function and it works fine, but I cannot execute screen.blit(chair, (10,10))
for some reason. 我可以使用该函数内的
chair.get_width()
获得表面的宽度,并且可以正常工作,但是由于某种原因,我无法执行screen.blit(chair, (10,10))
。 I don't get an error, it just doesn't do anything... 我没有错误,它什么也没做...
Here is my script (it creates a window, then calls a function every 100 milliseconds that gets the mouse position, rotates an image x degrees and then blits (or should blit) the image to the window): 这是我的脚本(它创建一个窗口,然后每100毫秒调用一次获取鼠标位置的函数,将图像旋转x度,然后将图像平移(或应平移)到窗口):
cif = "images/chair3.png"
import pygame, sys
from pygame.locals import *
pygame.init()
screen = pygame.display.set_mode((640,480),0,32)
chair = pygame.image.load(cif).convert_alpha()
pygame.time.set_timer(USEREVENT + 1, 100)
def cursor(speed):
global i
global chair
global screen
x,y = pygame.mouse.get_pos()
x -= chair.get_width()/2
y -= chair.get_height()/2
if i < 360:
i = i + 360/(1000/speed)
else:
i = 0
orig_chair_rect = chair.get_rect()
chair1 = pygame.transform.rotate(chair, i);
rot_chair_rect = orig_chair_rect.copy()
rot_chair_rect.center = chair1.get_rect().center
chair1 = chair1.subsurface(rot_chair_rect).copy()
print chair.get_width()
screen.blit(chair1,(x,y))
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
if event.type == USEREVENT + 1:
cursor(50)
You do need to use pygame.display.flip()
or pygame.display.update()
, the screen will never update if you don't, and therefore your chair will not be blitted onto the screen. 您确实需要使用
pygame.display.flip()
或pygame.display.update()
,否则,屏幕将永远不会更新,因此您的椅子不会在屏幕上变灰。
Tried it, it works. 试了一下,行得通。 If it doesn't work for you, reinstall Pygame, and try again.
如果它不适合您,请重新安装Pygame,然后重试。
您需要在函数末尾调用pygame.display.flip()
,否则,图形就不会从后缓冲区复制到实时显示中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.