Results 1 to 2 of 2

Thread: Bouncy ball (pygame)

  1. #1
    Join Date
    Feb 2012
    Joplin, MO
    Ubuntu Gnome 16.04 Xenial Xerus

    Bouncy ball (pygame)

    I am making a pong game and am putting it together a piece at a time. Right now I am working on a simple bouncy ball. I can make it move, but it wont bounce back. Basically I don't know how to set up walls . Here is my code:

    import pygame
    import sys
    from pygame.locals import *
    x_vel = 1
    y_vel = 1
    x = 1
    y = 1
    screen = pygame.display.set_mode((600,600),0,32)
    screen.blit(screen, (0,0))
    ball_img = pygame.image.load('ball.png')
    ball_cover = pygame.image.load('blank_ball.png')
    clock = pygame.time.Clock() 
    ball_rect = ball_img.get_rect()
    while True:
         for event in pygame.event.get():
              if event.type == QUIT:
         bounds_rect = pygame.Rect(x,y,0,0)
         x += x_vel
         y += y_vel
         screen.blit(ball_img, ball_rect)
         screen.blit(ball_cover, ball_rect)

  2. #2
    Join Date
    May 2007
    Leeds, UK
    Ubuntu 13.10 Saucy Salamander

    Re: Bouncy ball (pygame)

    I don't know pygame but I've done this sort of thing before. All you need to do is work out the where your horizontal and vertical walls are. Lets say x=0, x=800 and y=0, y=600. (I don't know the coordinate system for pygame, but basically you want the limits of the window/screen). Then you have to work out when your ball will collide with a wall. It will be if any of these conditions are true:

    a. if the centre x + radius >= 800
    b. if the centre x - radius <= 0
    c. if the centre y + radius >= 600
    d. if the centre y - radius <= 0

    When it collides, you just reverse the x or y velocity depending on which wall you hit. So if it was a wall based on the x coordinate, you'd reverse the x velocity and vice versa.

    This only works if you start your ball in the middle so that it's not colliding of course.
    Please create new threads for new questions.
    Please wrap code in code tags using the '#' button or enter it in your post like this: [code]...[/code].


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts