0
votes

I'm writing code for a one paddle pong game in pygame where the ball will bounce off of the 3 walls the paddle is not on, and when it does hit the paddle's wall instead of the paddle, it makes you lose a life. My problem is that I cannot figure out the collision detection to do this. Here is what I've done so far:

import pygame, sys
from pygame.locals import *

pygame.init()

screen_width = 640
screen_height = 480
Display = pygame.display.set_mode((screen_width, screen_height), 0, 32)
pygame.display.set_caption('Lonely Pong')

back = pygame.Surface((screen_width, screen_height))
background = back.convert()
background.fill((255, 255, 255))

paddle = pygame.Surface((80, 20))
_paddle = paddle.convert()
_paddle.fill((128, 0, 0))

ball = pygame.Surface((15, 15))
circle = pygame.draw.circle(ball, (0, 0, 0), (7, 7), 7)
_ball = ball.convert()
_ball.set_colorkey((0, 0, 0))

_paddle_x = (screen_width / 2) - 40
_paddle_y = screen_height - 20
_paddlemove = 0
_ball_x, _ball_y = 8, 8
speed_x, speed_y, speed_circle = 250, 250, 250
Lives = 5

clock = pygame.time.Clock()
font = pygame.font.SysFont("verdana", 20)
paddle_spd = 5

while True:

    for event in pygame.event.get():
        if event.type == QUIT:
            sys.exit()
        if event.type == KEYDOWN:
            if event.key == K_RIGHT:
                _paddlemove = paddle_spd
            elif event.key == K_LEFT:
                _paddlemove = -paddle_spd
        elif event.type == KEYUP:
            if event.key == K_RIGHT:
                _paddlemove = 0
            elif event.key == K_LEFT:
                _paddlemove = 0

    Livespr = font.render(str(Lives), True, (0, 0, 0))
    Display.blit(background, (0, 0))
    Display.blit(_paddle, (_paddle_x, _paddle_y))
    Display.blit(_ball, (_ball_x, _ball_y))
    Display.blit(Livespr, ((screen_width / 2), 5))

    _paddle_x += _paddlemove

    passed = clock.tick(30)
    sec = passed / 1000

    _ball_x += speed_x * sec
    _ball_y += speed_y * sec

    pygame.display.update()

Another problem I've been having is that when I start it up, the ball doesn't appear at all. Could somebody please point me in the right direction?

1
I suppose your code never gets past the for event in pygame.event.get(), to the drawing code. The drawing code should be in the inner loop; fix the indentation and try again. - 9000
But its like its only skipping drawing the ball, because it draws the paddle and the lives - TheBudderBomb
Did you check that sec is not zero? Maybe sec = passed / 1000.0 could fix it. - 9000
Nah still didn't fix anything - TheBudderBomb

1 Answers

2
votes

first things first, the ball doesn't appear because you are not filling the surface, you are setting the color_key:

_ball.set_colorkey((0, 0, 0))

should be

_ball.fill((0, 0, 0))

Second, for collision detection, pygame offers some functions such as collidepoint and colliderect.

https://www.pygame.org/docs/ref/rect.html#pygame.Rect.collidepoint

which means you will need the rectangles of the surfaces. do this by:

my_surface.get_rect()

https://www.pygame.org/docs/ref/surface.html#pygame.Surface.get_rect

In order to collide with the edges of the screen, you can always do

if _ball_x <= 0 or _ball_y <= 0 or _ball_x >= 640 or _ball_y >= 480:
    # collision detected