1

I am trying to make a Pong clone in Pygame. I am fairly new to Python and am having some trouble. The program should make the two paddles moveable, and the ball bouncing of the edges of the screen, but the ball isn't bouncing. Why?

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=0,0
clock=pygame.time.Clock()
speed=750
movex1, movey1=0,0
movex2, movey2=0,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
    dm=seconds*speed
    x3+=dm
    y3+=dm

    if x3>1280: 
        x3+=-dm
    if y3>720:
        y3+=-dm
    if x3<0:
        x3+=dm
    if y3<0:
        y3+=dm

    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)
    label = myfont.render("Python", 1, (255,0,0))
    screen.blit(label, (494, 115))
    myfont = pygame.font.SysFont("Press Start 2P", 50)
    label = myfont.render("Pong", 1, (255,0,0))
    screen.blit(label, (544, 175))
    pygame.display.update()
user2005813
  • 31
  • 1
  • 3
  • Can you give a description of what the ball does when it is supposed to bounce ? What is it actually doing ? This will help in understanding the problem. Maybe you could also put some comments in the code as to which lines control the ball and it's bouncing. – chrisfs Jan 24 '13 at 00:39
  • I'm assuming the `if x3>1280: x3+=-dm` lines are your code to make the ball bounce? I think it doesn't bounce because on the next step, `dm` will just be added back to `x3`. – Marius Jan 24 '13 at 00:41

1 Answers1

4

disclaimer: I'm not a pyGame guy. But, I think this is a logic problem.

I'm guessing the behavior as currently coded, is that the ball moves from the top left corner (0,0) to the bottom right (1280,720), then just stays there.

You want to make sure the ball changes direction when you hit a wall, so instead of speed, you need a separate speed for x and y (which will also help later, when you want the ball to move at different angles, depending on the strike from the paddle). Then, when the ball touches a wall, you change the direction of travel.

replace this:

dm=seconds*speed
x3+=dm
y3+=dm

if x3>1280: 
    x3+=-dm
if y3>720:
    y3+=-dm
if x3<0:
    x3+=dm
if y3<0:
    y3+=dm

With something like this (with appropriate initialization of speedx,speedy):

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
if y3>720:
    y3+=-dy
    speedy = -speedy
if x3<0:
    x3+=dx
    speedx = -speedx
if y3<0:
    y3+=dy
    speedy = -speedy
John Hazen
  • 1,296
  • 1
  • 8
  • 19