pygame如何让球碰撞 [英] Pygame how to let balls collide

查看:132
本文介绍了pygame如何让球碰撞的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在pygame中编写一个脚本,其中两个球彼此相对飞行,当它们碰撞时,它们应该彼此弹开,但我不知道该怎么做,所以您能帮我吗?

解决方案

要检测是否有2个圆(分别是球)相撞,您必须测试


< pre class = lang-py prettyprint-override> import pygame

pygame.init()

width,height = 400,400
窗口= pygame.display.set_mode((宽度,高度))
时钟= pygame.time.Clock()

x1,y1,r1,mx1,my1 = 200,200 ,50,2,0.5
x2,y2,r2,mx2,my2 = 300,200,50,-1,-1.5

def move(c,v,r,m) :
c + = v
如果c< r:c,v = r,如果c> v,则-v
。 mr:c,v = mr,-v
return c,v

hit_count = 0
run =真实
运行时:
clock.tick (60)pygame.event.get()中事件的
:如果event.type == pygame.QUIT:
=
run = False

x1,mx1 = move(x1,mx1,r1,宽度)
y1,my1 = move(y1,my1,r1,高度)
x2,mx2 = move(x2,mx2,r2,宽度)
y2,my2 = move(y2,my2,r2,高度)

v1 = pygame.math.Vector2(x1,y1)
v2 = pygame.math.Vector2(x2,y2 )
,如果v1.distance_to(v2)< r1 + r2-2:
hit_count + = 1
print( hit:,hit_count)

nv = v2-v1
m1 = pygame。 math.Vector2(mx1,my1).reflect(nv)
m2 = pygame.math.Vector2(mx2,my2).reflect(nv)
mx1,my1 = m1.x,m1.y
mx2,my2 = m2.x,m2.y

window.fill((127,127,127))
pygame.draw.circle(窗口,(255,0 ,0),(round(x1),round(y1)),r1,4)
pygame.draw.circle(window,(0,0,255),(round(x2),round(y2) ),r2、4)
pygame.display.flip()


I want to make a script in pygame where two balls fly towards each other and when they collide they should bounce off from each other but I don't know how to do this so can you help me?

解决方案

To detect if 2 circles (respectively balls) are colliding, you've to test, if the Euclidean distance between the circles center points is less than the sum of the radii. I recommend to use pygame.math.Vector2 / distance_to() for the computation.
In the following the 1 circle is defined by the center point (x1, y1) and the radius r1. The 2nd circle is defined by (x2, y2) and r2:

v1 = pygame.math.Vector2(x1, y1)
v2 = pygame.math.Vector2(x2, y2)
if v1.distance_to(v2) < r1 + r2:
    print("hit")

If you want to make the circles bounce, you have to reflect the motion vector of the circle at the normal vector of the intersection like a billiard ball. Use pygame.math.Vector2 / reflect_ip() or reflect() to compute the new direction of the circle.
The movements of the circles are given by (mx1, my1) and (mx2, my2):

nv = v2 - v1
m1 = pygame.math.Vector2(mx1, my1).reflect(nv)
m2 = pygame.math.Vector2(mx2, my2).reflect(nv)
mx1, my1 = m1.x, m1.y
mx2, my2 = m2.x, m2.y


See the example:

import pygame

pygame.init()

width, height = 400, 400
window = pygame.display.set_mode((width, height))
clock = pygame.time.Clock()

x1, y1, r1, mx1, my1 = 200, 200, 50, 2, 0.5
x2, y2, r2, mx2, my2 = 300, 200, 50, -1, -1.5

def move(c, v, r, m):
    c += v
    if c < r: c, v = r, -v
    if c > m-r: c, v = m-r, -v   
    return c, v

hit_count = 0
run = True
while run:
    clock.tick(60)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

    x1, mx1 = move(x1, mx1, r1, width)
    y1, my1 = move(y1, my1, r1, height)
    x2, mx2 = move(x2, mx2, r2, width)
    y2, my2 = move(y2, my2, r2, height)

    v1 = pygame.math.Vector2(x1, y1)
    v2 = pygame.math.Vector2(x2, y2)
    if v1.distance_to(v2) < r1 + r2 - 2:
        hit_count += 1
        print("hit:", hit_count)

        nv = v2 - v1
        m1 = pygame.math.Vector2(mx1, my1).reflect(nv)
        m2 = pygame.math.Vector2(mx2, my2).reflect(nv)
        mx1, my1 = m1.x, m1.y
        mx2, my2 = m2.x, m2.y

    window.fill((127, 127, 127))
    pygame.draw.circle(window, (255, 0, 0), (round(x1), round(y1)), r1, 4)
    pygame.draw.circle(window, (0, 0, 255), (round(x2), round(y2)), r2, 4)
    pygame.display.flip()

这篇关于pygame如何让球碰撞的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆