如何解决Pygame:如果球在中间以下/上方而不是在桨尖,如何移动对手ai
我一直遵循“通过制作Pong来学习Pygame”这4部分系列(https://youtu.be/Qf3-aDXG8q4)。在第一集中,对手桨的AI基本上是这样的。如果球在屏幕上的任何位置都低于对手球拍的下方,则当球高于球拍顶端时,对手的速度增加(向上移动),如果球低于球拍末端,则对手的速度减小(向下移动)。如果球不在球拍下方或上方,则球拍不会移动。以下是相关代码:
def opponent_ai():
#Increasing Speed if ball is above
if opponent.top < ball.y:
opponent.top += opponent_speed
#Decreasing speed if ball is below
if opponent.bottom > ball.y:
opponent.bottom -= opponent_speed
#Staying the same
if opponent.top <= 0:
opponent.top = 0
#Preventing from going off screen
if opponent.bottom >=screen_height:
opponent.bottom = screen_height
while True:
opponent_ai()
所以我要做的是对代码进行编码,以使如果球位于中间(而不是中间)上方/下方,则球拍也会移动。我已经尝试过opponent.middle
,但是该方法调用不存在。
预先感谢:)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。