如何使用Pygame Rect(How to Use the Pygame Rect)

编程入门 行业动态 更新时间:2024-10-23 19:31:16
如何使用Pygame Rect(How to Use the Pygame Rect)

我一直在试验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 - distance

From 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

更多推荐

本文发布于:2023-08-03 16:31:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1393557.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   Pygame   Rect

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!