简体   繁体   English

乒乓球游戏-球碰球拍?

[英]Pong Game - Ball Collision with Paddles?

Here is my collision code for the ball with the walls - Created in python(Codeskulptor) 这是我与墙壁的球的碰撞代码-在python(Codeskulptor)中创建

The ball bounces off the bottom and top walls, and disappears from the left and right side if it is not hit by the paddle and reappears in the center to repeat 球从底壁和顶壁反弹,如果没有被桨击中,则从左侧和右侧消失,并再次出现在中心以重复

However, I cannot get the ball to hit the paddle and rebound - the paddle is a Circle object placed on the left of the screen (Height/2) 但是,我无法使球击中桨板并反弹-桨板是放置在屏幕左侧的一个Circle对象(高度/ 2)

Any help would be appreciated 任何帮助,将不胜感激

#Bottom and top walls
if Ballpos[1] >= (Height - Ballradius):
    Ballvel[1] = - Ballvel[1]
if Ballpos[1] <= (Ballradius):
    Ballvel[1] = - Ballvel[1]

if(Ballpos[0] <= 0):
    Score2 += 1
    Ball_Spawn(True)
elif(Ballpos[0] >= Width):
    Score1 += 1
    Ball_Spawn(False) 


#Update Position of Ball
Ballpos[0] += Ballvel[0]
Ballpos[1] += Ballvel[1]

Let's pretend your paddle has coordinates Padpos and radius Padradius . 让我们假设您的桨具有坐标Padpos和半径Padradius From your code, it looks like the coordinates represent the center of the circle, but you can make necessary adjustments if I'm wrong on that. 从您的代码来看,坐标看起来代表圆心,但是如果我错了,可以进行必要的调整。

if (Ballpos[0] - Padpos[0])**2 + (Ballpos[1] - Padpos[1])**2 <= (Ballradius + Padradius)**2:
    # code for rebounding

Basically, just apply Pythagorean Theorem to see if the distance between their centers is closer than the sum of their radii. 基本上,仅应用勾股定理即可确定其中心之间的距离是否比其半径之和更近。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM