[英]Why isn't the ellipse drawn to my surface showing up?
I'm trying to do a relatively simple animation that requires rotating an ellipse on a rotating background. 我正在尝试做一个相对简单的动画,需要在旋转的背景上旋转一个椭圆。 I've had to resort to a bit of trickery to get
pygame.transform.rotate
to play nice with the surfaces I'm trying to rotate. 我不得不采取一些
pygame.transform.rotate
来使pygame.transform.rotate
与我要旋转的表面配合使用。 Namely, I've made this function that recenters the new rect obtained from pygame's rotation function: 就是说,我做了这个函数,可以对从pygame的旋转函数获得的新矩形进行更新:
def recenter(orig_rect, rotated_surf):
oldcenter = orig_rect.center
rotRect = rotated_surf.get_rect()
rotRect.center = oldcenter
screen.blit(rotated_surf, rotRect)
The functionning is fairly self-explanatory. 功能相当不言自明。 I realize that the original (unrotated) surface remains blitted onto the display, but this turns out not to really be a problem.
我意识到原始的(未旋转的)表面仍会渗入显示屏,但这并不是真正的问题。 Don't worry about that.
不用担心
The above function is used by the rotating functions (background & ellipse) as such: 旋转功能(背景和椭圆)使用上述功能,例如:
# 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))
These rotation functions are called one for each frame. 这些旋转功能每帧称为一个。
Here's a simplified version of my main game loop. 这是我的主游戏循环的简化版本。 Note that this is not the actual production code, but serves as an illustration of how the above elements (which are taken directly from the code base) tie together:
注意,这是不实际的生产代码,但作为上述元件( 其直接取自代码库)如何结合在一起的图示:
ellipse = create_ellipse() # returns a surf
background = create_background() # returns a surf
while True:
rotate_background()
rotate_ellipse()
pygame.display.flip()
A word about the background
and ellipse
variables above. 关于上面的
background
和ellipse
变量的一句话。 Both variables contain a pygame.Surface
instance on which things have been drawn. 这两个变量都包含一个
pygame.Surface
实例,在该实例上绘制了事物。 I won't go into details about background
since that is working fine. 我不会介绍有关
background
详细信息,因为这很好。 In create_ellipse
, an ellipse was drawn onto the ellipse
surface by means of pygame.draw.ellipse
, in yellow. 在
create_ellipse
,通过pygame.draw.ellipse
将ellipse
绘制为黄色。 Prior to that, ellipse.fill(GREEN)
was called. 在此之前,调用了
ellipse.fill(GREEN)
。 The colorkey for ellipse
was also set to GREEN
to make sure that the entire surface is transparent except where the yellow ellipse was drawn. ellipse
也设置为GREEN
,以确保除了绘制黄色椭圆的位置之外 ,整个表面都是透明的。
The Problem: I'm not seeing the ellipse 问题:我没看到椭圆形
I commented out ellipse.set_colorkey
to make sure that the ellipse was properly blitted. 我注释掉了
ellipse.set_colorkey
,以确保正确地遮住了椭圆。 It is. 它是。 I see a green rectagle appear that changes in dimension as the ellipse is rotated.
我看到绿色的椭圆形出现,随着椭圆的旋转尺寸发生变化。 This led me to infer that there is, indeed, an ellipse being drawn since it can be rotated.
这使我推断出确实存在一个椭圆,因为它可以旋转。
What gives? 是什么赋予了? I can provide the full code if it can be useful.
如果有用,我可以提供完整的代码。 The whole thing is approximately 200 lines, but I hope my explanation above is enough for you guys.
整个过程大约需要200行,但是我希望上面的解释对你们来说足够了。 I figured we should start locally and work outwards =)
我认为我们应该从本地开始并向外工作=)
Thanks very much in advance! 首先十分感谢!
while True:
rotate_background()
rotate_ellipse
pygame.display.flip()
rotate_elipse()
Is not being called, and has no arguments. rotate_elipse()
没有被调用,并且没有参数。 If it's a typo, use a print to check if elipse is blitting to where you expect. 如果是错字,请使用印刷品检查椭圆形是否渗色到您期望的位置。 Maybe the math isn't right?
也许数学不对吗?
You can simplify things, like width/2
is equivalent to rect.centerx
您可以简化事情,例如
width/2
等于rect.centerx
Okay so the problem stems from my own dire stupidity. 好吧,问题出在我自己的愚蠢。
I mistook the rect
argument for pygame.draw.ellipse
as being center_x_coordinate, center_y_coordinate, width, height
. 我把
pygame.draw.ellipse
的rect
参数误认为是center_x_coordinate, center_y_coordinate, width, height
。
I changed my calls to pygame.draw.ellipse
and everything is now working beautifully. 我更改了对
pygame.draw.ellipse
调用,现在一切正常。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.