[英]Why isn't the ellipse drawn to my surface showing up?
我正在尝试做一个相对简单的动画,需要在旋转的背景上旋转一个椭圆。 我不得不采取一些pygame.transform.rotate
来使pygame.transform.rotate
与我要旋转的表面配合使用。 就是说,我做了这个函数,可以对从pygame的旋转函数获得的新矩形进行更新:
def recenter(orig_rect, rotated_surf):
oldcenter = orig_rect.center
rotRect = rotated_surf.get_rect()
rotRect.center = oldcenter
screen.blit(rotated_surf, rotRect)
功能相当不言自明。 我意识到原始的(未旋转的)表面仍会渗入显示屏,但这并不是真正的问题。 不用担心
旋转功能(背景和椭圆)使用上述功能,例如:
# width, height, screen are globals
def rotate_background(surf, speed, t0):
new_angle = ((pygame.time.get_ticks() - t0) * (speed / 1000)) % 360
w, h = surf.get_size()
blittedRect = screen.blit(surf, (width / 2 - w / 2, height / 2 - h / 2))
recenter(blittedRect, pygame.transform.rotate(surf, new_angle))
return surf
def rotate_ellipse(surf, coords, speed, t0):
new_angle = ((pygame.time.get_ticks() - t0) * (speed / 1000)) % 360
if new_angle > 90:
new_angle = 90 # limit rotation
w, h = surf.get_size()
x, y = coords
blittedRect = screen.blit(surf, (width / 2 - w / 2 + x, height / 2 - h / 2 + y))
recenter(blittedRect, pygame.transform.rotate(surf, new_angle))
这些旋转功能每帧称为一个。
这是我的主游戏循环的简化版本。 注意,这是不实际的生产代码,但作为上述元件( 其直接取自代码库)如何结合在一起的图示:
ellipse = create_ellipse() # returns a surf
background = create_background() # returns a surf
while True:
rotate_background()
rotate_ellipse()
pygame.display.flip()
关于上面的background
和ellipse
变量的一句话。 这两个变量都包含一个pygame.Surface
实例,在该实例上绘制了事物。 我不会介绍有关background
详细信息,因为这很好。 在create_ellipse
,通过pygame.draw.ellipse
将ellipse
绘制为黄色。 在此之前,调用了ellipse.fill(GREEN)
。 ellipse
也设置为GREEN
,以确保除了绘制黄色椭圆的位置之外 ,整个表面都是透明的。
问题:我没看到椭圆形
我注释掉了ellipse.set_colorkey
,以确保正确地遮住了椭圆。 它是。 我看到绿色的椭圆形出现,随着椭圆的旋转尺寸发生变化。 这使我推断出确实存在一个椭圆,因为它可以旋转。
是什么赋予了? 如果有用,我可以提供完整的代码。 整个过程大约需要200行,但是我希望上面的解释对你们来说足够了。 我认为我们应该从本地开始并向外工作=)
首先十分感谢!
while True:
rotate_background()
rotate_ellipse
pygame.display.flip()
rotate_elipse()
没有被调用,并且没有参数。 如果是错字,请使用印刷品检查椭圆形是否渗色到您期望的位置。 也许数学不对吗?
您可以简化事情,例如width/2
等于rect.centerx
好吧,问题出在我自己的愚蠢。
我把pygame.draw.ellipse
的rect
参数误认为是center_x_coordinate, center_y_coordinate, width, height
。
我更改了对pygame.draw.ellipse
调用,现在一切正常。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.