Как добавить детектор столкновений в мою игру в понг в pygame

У меня возникли проблемы с тем, чтобы детектор столкновений работал для моей игры в понг без необходимости изменять все классы (спрайт, рендеринг).

Я видел несколько полезных тем здесь, на StackOverflow, но не могу заставить их работать.

#Create a class named sprite to use for the paddles and the ball.
class Sprite():
    def __init__(self,x,y,width,height,color):

        self.x = x

        self.y = y

        self.width = width

        self.height = height

        self.color= (255,255,255)
#attirbute for drawing the sprite(s) to the screen
    def render(self):
        pygame.draw.rect(screen,self.color,(self.x,self.y,self.width,self.height))
 #Create the sprites       
Paddle1 = Sprite(50,175,25,150,color[0])
Paddle2 = Sprite(650,175,25,150,color[1])
Ball = Sprite(300,250,25,25, color[2])
#Set the Sprite's color(s)
Paddle1.color = color[0]
Paddle2.color = color[1]
Ball.color = color[2]
#Variables used for moving the paddles
moveY1,moveY2=0,0
#### Spot where my collision detector goes####

#### Code for drawing and moving the paddles####
    Paddle1.y += moveY1
    Paddle1.render()
    Paddle2.y += moveY2
    Paddle2.render()
    #Draw the Ball
    Ball.render()

person N.Huggett    schedule 16.05.2016    source источник
comment
какой конкретный вопрос? Вы можете сэкономить всем время, упростив код, чтобы показать только этот вопрос?   -  person rleir    schedule 16.05.2016
comment
Мой вопрос: как я могу добавить детектор столкновений в эту игру в понг с помощью pygame? Желательно без изменения имен моих классов и тому подобного.   -  person N.Huggett    schedule 17.05.2016


Ответы (1)


Это рабочий фрагмент игры Pong, которую я сделал с помощью pygame. Я также сделал второй бампер, но опустил его для экономии места, так как это почти то же самое.

def check_bumpers_collision(self):
    if (self.ball.pos_x < self.bumper_one.pos_x + self.bumper_one.width and
        self.ball.pos_x + self.ball.size > self.bumper_one.pos_x and
        self.ball.pos_y < self.bumper_one.pos_y + self.bumper_one.height and
        self.ball.pos_y + self.ball.size > self.bumper_one.pos_y):
        # Change Ball X moving direction
        self.ball.speed_x *= -1
        # Change Y ball Speed
        if self.bumper_one.moving_up:
            self.ball.speed_y -= 5
        elif self.bumper_one.moving_down:
            self.ball.speed_y += 5
person Tales Pádua    schedule 17.05.2016