0

I'm making a pong in pygame, but I have problem. When the ball collides with a paddle, it doesn't bounce off it. Here's my code:

main file:

import pygame as py

import game_functions as gf
from game_objects import Platform, Platform2, Ball
from settings import Settings


def run_game():
py.init()
ai_settings = Settings()
screen = py.display.set_mode((
    ai_settings.screen_width, ai_settings.screen_height
))
py.display.set_caption('Pong')
platform = Platform(ai_settings, screen)
platform2 = Platform2(ai_settings, screen)
ball = Ball(ai_settings, screen)

while True:
    gf.check_events(platform, platform2, ball)
    platform.update(), platform2.update(), ball.update()
    gf.collide_check(ball, platform, platform2, ai_settings)
    gf.update_screen(ai_settings, screen, platform, platform2, ball)

run_game()

class for ball:

class Ball:

def __init__(self, ai_settings, screen):
    self.ai_settings = ai_settings
    self.screen = screen
    self.screen_rect = screen.get_rect()

    self.image = py.image.load('pics/ball.png')
    self.rect = self.image.get_rect()

    self.rect.centerx = self.screen_rect.centerx
    self.rect.centery = self.screen_rect.centery

    self.moving = False

def update(self):

    if self.moving:
        self.rect.centerx -= self.ai_settings.ball_speed

def draw_ball(self):

    self.screen.blit(self.image, self.rect)

and here is my function to change direction:

def collide_check(ball, platform, platform2, ai_settings):

    if ball.rect.colliderect(platform.rect):
        ball.rect.centerx += ai_settings.ball_speed
    elif ball.rect.colliderect(platform2.rect):
        ball.rect.centerx -= ai_settings.ball_speed

Thanks a lot for help :/

P.S. move flag self.moving will change to true if any key for paddle move is pressed.

Ru Chern Chong
  • 3,692
  • 13
  • 33
  • 43
Freestajlo
  • 11
  • 2
  • Just a guess as the code isn't complete, but `update` looks like it always subtracts the ball speed, so it only goes one direction. If `platform` is the one that it collides with, the ball_speed is added, but the next `update` it'll get subtracted again...effectively not going anywhere. There are plenty of free Python IDEs with debuggers so you can step through the code and look at your logic. Just watch your X and Y coordinates change. – Mark Tolonen Sep 03 '16 at 22:20
  • Try remove as much code you can to still recreate the problem. It'll make it easier to debug. You also need to include code that's part of the problem (in this case `ai_settings.ball_speed`). The code should be testable so make sure the indentions are correct and that we're provided with the code needed to recreate the problem. Also, why do you import pygame as py? It makes the code much more ambiguous and doesn't save much typing. It's understandable for your own modules with long name though. – Ted Klein Bergman Sep 04 '16 at 15:02

1 Answers1

0

Your update function is not fully complete as you need to add:

def update(self):

self.collide_check(ball, platform, platform2, ai_settings)

if self.moving:
    self.rect.centerx -= self.ai_settings.ball_speed

So that your code updates the direction of the ball. Since it is now in the update section it will check for collisions every time the update cycle happens. As a result of this you probably need to add the

collide_check() 

function into the ball class. I should also add that:

if ball.rect.colliderect(platform.rect):
   ball.rect.centerx += ai_settings.ball_speed

Should instead be:

if ball.rect.colliderect(platform.rect):
    ai_settings.ball_speed * -1

That way it makes ai_settings.ball_speed go in the opposite direction, as n * -1 = -1 and -n * -1 = 1.

ModoUnreal
  • 642
  • 5
  • 15