问题描述
我一直遵循“通过制作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 (将#修改为@)