我一直在试验Pygame,并遇到了一个我无法找到答案的问题。 在这个粘贴中 ,展示了我的基本游戏框架。 如何正确完成我的ballSnapLeft()定义?
编辑:我不是在寻找要完成的代码,但我正在寻找有人解释'Rect'类(?)如何工作以及如何应用它的人。
Edit2:我试图使用x和y坐标来做到这一点,但我认为有一种更简单的方法可以实际工作,而不是使用粗暴坐标。
I have been experimenting with Pygame, and have come across a problem that I could not find the answer to. In this paste, my basic game framework is exhibited. How can i complete my ballSnapLeft() definition correctly?
Edit: I am not looking for my code to be completed, but I am looking for someone to explain how the 'Rect' class(?) works, and how it could be applied.
Edit2: I have tried to use the x and y coordinates to do so, but I think there is a simpler way that can actually work, instead of using brute coordinates.
最满意答案
从使用Python和Pygame制作游戏 :
myRect.left矩形左侧X坐标的int值。
myRect.right矩形右侧X坐标的int值。
myRect.top矩形顶部Y坐标的int值。
myRect.bottom底部Y坐标的int值。
因为所有这些属性都返回整数,这可能是您的代码无法正常工作的原因。
此外,如果你使用ballSnapLeft()目标是将球移动到远离玩家的位置, ballRect.right = playerRect.left - distance只会改变矩形的X坐标。 为了使球也在Y坐标上移动你可以做类似的事情
def ballSnapTop(): ballRect.top = playerRect.bottom - distanceFrom Making Games With Python and Pygame:
myRect.left The int value of the X-coordinate of the left side of the rectangle.
myRect.right The int value of the X-coordinate of the right side of the rectangle.
myRect.top The int value of the Y-coordinate of the top side of the rectangle.
myRect.bottom The int value of the Y-coordinate of the bottom side.
Because all of these attributes return integers, that's probably why your code isn't working.
Also, if your goal with ballSnapLeft() is to move the ball to a position away from the player, ballRect.right = playerRect.left - distance would only change the X coordinate of the rect. To make the ball also move in the Y coordinate you could do something like
def ballSnapTop(): ballRect.top = playerRect.bottom - distance更多推荐
发布评论