Python 如何在Pygame中为Pong设置碰撞检测

Python 如何在Pygame中为Pong设置碰撞检测,python,pygame,collision-detection,pong,Python,Pygame,Collision Detection,Pong,我正在Pygame中创建一个乒乓球克隆。我不知道如何将碰撞检测添加到球和桨上,以便球从桨上反弹。我该怎么做?当球碰到球拍时,我怎么能让它朝相反的方向运动呢 bif="bg.jpg" import pygame, sys from pygame.locals import * pygame.init() pygame.display.set_caption("Griffin West's Python Pong") pygame.mixer.init() sounda= pygame.mixe

我正在Pygame中创建一个乒乓球克隆。我不知道如何将碰撞检测添加到球和桨上,以便球从桨上反弹。我该怎么做?当球碰到球拍时,我怎么能让它朝相反的方向运动呢

bif="bg.jpg"

import pygame, sys
from pygame.locals import *

pygame.init()
pygame.display.set_caption("Griffin West's Python Pong")
pygame.mixer.init()
sounda= pygame.mixer.Sound("Music.wav")

sounda.play()

screen=pygame.display.set_mode((1280,720),0,32)
background=pygame.image.load(bif).convert()

color1=(255,255,255)
color2=(255,255,0)
color3=(0,0,255)
color4=(0,255,0)
pos1=(640,0)
pos2=(640,720)
pos3=(640,360)
pos4=(0,360)
pos5=(1280,360)
radius=(100)
x1,y1=75,0
x2,y2=1175,0
x3,y3=1,1
clock=pygame.time.Clock()
speedx=750
speedy=750
movex1, movey1=0,0
movex2, movey2=0,0
s1=0
s2=0


while True:


    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
        if event.type==KEYDOWN:
            if event.key==K_w:
                movey1=-2
            elif event.key==K_s:
                movey1=+2
            if event.key==K_UP:
                movey2=-2
            elif event.key==K_DOWN:
                movey2=+2
        if event.type==KEYUP:
            if event.key==K_w:
                movey1=0
            elif event.key==K_s:
                movey1=0
            if event.key==K_UP:
                movey2=0
            elif event.key==K_DOWN:
                movey2=0

    x1+=movex1
    y1+=movey1
    x2+=movex2
    y2+=movey2


    milli=clock.tick()
    seconds=milli/1000.0
    dx=seconds*speedx
    dy=seconds*speedy
    x3+=dx
    y3+=dy

    if x3>1280: 
        x3+=-dx  # get ball out of the wall
        speedx = -speedx  # change direction
        s1=s1+1
    if y3>720:
        y3+=-dy
        speedy = -speedy
    if x3<0:
        x3+dx
        speedx = -speedx
        x3=x3+1
        s2=s2+1
    if y3<0:
        y3=y3+1
        y3+=dy
        speedy = -speedy
    if x3<=y3y3+30:
        speedx = -speedy


    screen.blit(background, (0,0))

    screen.lock()
    pygame.draw.line(screen, color1, pos1, pos2, 1)
    pygame.draw.circle(screen, color1, pos3, radius, 1)
    pygame.draw.circle(screen, color1, pos4, radius, 1)
    pygame.draw.circle(screen, color1, pos5, radius, 1)
    pygame.draw.rect(screen, color3, Rect((x1,y1),(30,100)))
    pygame.draw.rect(screen, color2, Rect((x2,y2),(30,100)))
    pygame.draw.circle(screen, color4, (int(x3),int(y3)), 15)

    screen.unlock()
    myfont = pygame.font.SysFont("Press Start 2P", 50)
    myfont2 = pygame.font.SysFont("Press Start 2P", 25)
    label = myfont.render("Python", 1, (255,0,0))
    screen.blit(label, (494, 115))
    label = myfont.render("Pong", 1, (255,0,0))
    screen.blit(label, (544, 175))
    label = myfont2.render("Player 1: "+str(s1), 1, (255,255,255))
    screen.blit(label, (25, 675))
    label = myfont2.render("Player 2: "+str(s2), 1, (255,255,255))
    screen.blit(label, (950, 675))
    pygame.display.update()
bif=“bg.jpg”
导入pygame,sys
从pygame.locals导入*
pygame.init()
pygame.display.set_标题(“格里芬·韦斯特的Python Pong”)
pygame.mixer.init()
sounda=pygame.mixer.Sound(“Music.wav”)
sounda.play()
screen=pygame.display.set_模式((1280720),0,32)
background=pygame.image.load(bif.convert())
颜色1=(255255)
颜色2=(255255,0)
颜色3=(0,0255)
颜色4=(0255,0)
pos1=(640,0)
位置2=(640720)
位置3=(640360)
位置4=(0360)
位置5=(1280360)
半径=(100)
x1,y1=75,0
x2,y2=1175,0
x3,y3=1,1
clock=pygame.time.clock()
速度X=750
速度=750
movex1,movey1=0,0
movex2,movey2=0,0
s1=0
s2=0
尽管如此:
对于pygame.event.get()中的事件:
如果event.type==退出:
pygame.quit()
sys.exit()
如果event.type==KEYDOWN:
如果event.key==K_w:
移动1=-2
elif event.key==K_s:
移动1=+2
如果event.key==K\u UP:
movey2=-2
elif event.key==K_DOWN:
movey2=+2
如果event.type==KEYUP:
如果event.key==K_w:
移动1=0
elif event.key==K_s:
移动1=0
如果event.key==K\u UP:
movey2=0
elif event.key==K_DOWN:
movey2=0
x1+=x1
y1+=移动y1
x2+=movex2
y2+=移动y2
milli=时钟滴答声()
秒=毫/1000.0
dx=秒*速度x
dy=秒*快
x3+=dx
y3+=dy
如果x3>1280:
x3+=-dx#把球从墙上拿出来
speedx=-speedx#改变方向
s1=s1+1
如果y3>720:
y3+=-dy
斯皮蒂=-斯皮蒂

if x3做一个if语句,检查球的x坐标是否超出桨的x坐标,y坐标是否在桨的顶部和底部y坐标范围内。如果是这样,将球的x速度乘以-1

有一个包含冲突检测的示例pong实现。具体来说,您可能对下面列出的代码中的
bounce
方法感兴趣

在乒乓球课上:

def move_all(self):
    for obj in self.p1, self.p2, self.ball:
        obj.move()
        if obj is not self.ball:
            obj.bounce(self.ball)
def bounce(self, ball):
    minimum = self.size.x + ball.radius
    if self.position.x != ball.position.x and self.overlap(ball, minimum):
        if not self.just_bounced:
            self.just_bounced = True
            self.score += abs(ball.velocity.y)
        sign = +1 if self.position.x < ball.position.x else -1
        if self.collision_area == self.PART.center:
            ball.position.x = self.position.x + minimum * sign
        else:
            ball.position.adjust(self.middle_point, minimum)
        ball.velocity.x = copy_sign(ball.velocity.x, sign)
        ball.change_speed()
    else:
        self.just_bounced = False
def bounce(self):
    if self.position.y - self.radius < 0:
        self.position.y = self.radius
        self.velocity.y = copy_sign(self.velocity.y, +1)
        self.change_speed()
    elif self.position.y + self.radius > self.board.y:
        self.position.y = self.board.y - self.radius
        self.velocity.y = copy_sign(self.velocity.y, -1)
        self.change_speed()
划桨类:

def move_all(self):
    for obj in self.p1, self.p2, self.ball:
        obj.move()
        if obj is not self.ball:
            obj.bounce(self.ball)
def bounce(self, ball):
    minimum = self.size.x + ball.radius
    if self.position.x != ball.position.x and self.overlap(ball, minimum):
        if not self.just_bounced:
            self.just_bounced = True
            self.score += abs(ball.velocity.y)
        sign = +1 if self.position.x < ball.position.x else -1
        if self.collision_area == self.PART.center:
            ball.position.x = self.position.x + minimum * sign
        else:
            ball.position.adjust(self.middle_point, minimum)
        ball.velocity.x = copy_sign(ball.velocity.x, sign)
        ball.change_speed()
    else:
        self.just_bounced = False
def bounce(self):
    if self.position.y - self.radius < 0:
        self.position.y = self.radius
        self.velocity.y = copy_sign(self.velocity.y, +1)
        self.change_speed()
    elif self.position.y + self.radius > self.board.y:
        self.position.y = self.board.y - self.radius
        self.velocity.y = copy_sign(self.velocity.y, -1)
        self.change_speed()
def弹跳(自身、球):
最小值=自身尺寸x+球半径
如果self.position.x!=球位置x和自身重叠(球,最小):
如果不是self.just_反弹:
self.just_bunced=真
自我得分+=绝对值(球速度y)
如果self.position.x
在球类课程中:

def move_all(self):
    for obj in self.p1, self.p2, self.ball:
        obj.move()
        if obj is not self.ball:
            obj.bounce(self.ball)
def bounce(self, ball):
    minimum = self.size.x + ball.radius
    if self.position.x != ball.position.x and self.overlap(ball, minimum):
        if not self.just_bounced:
            self.just_bounced = True
            self.score += abs(ball.velocity.y)
        sign = +1 if self.position.x < ball.position.x else -1
        if self.collision_area == self.PART.center:
            ball.position.x = self.position.x + minimum * sign
        else:
            ball.position.adjust(self.middle_point, minimum)
        ball.velocity.x = copy_sign(ball.velocity.x, sign)
        ball.change_speed()
    else:
        self.just_bounced = False
def bounce(self):
    if self.position.y - self.radius < 0:
        self.position.y = self.radius
        self.velocity.y = copy_sign(self.velocity.y, +1)
        self.change_speed()
    elif self.position.y + self.radius > self.board.y:
        self.position.y = self.board.y - self.radius
        self.velocity.y = copy_sign(self.velocity.y, -1)
        self.change_speed()
def反弹(自):
如果自位置y-自半径<0:
self.position.y=self.radius
self.velocity.y=复制符号(self.velocity.y,+1)
self.change_speed()
elif self.position.y+self.radius>self.board.y:
self.position.y=self.board.y-self.radius
self.velocity.y=复制符号(self.velocity.y,-1)
self.change_speed()