I recently started up with pygame and after some general playing around
in it, I started writing a small pong game within it. It's practically
functional; it loads up a 640x480 window, plays some music, allows the
user to move his paddle up or down, and has a moving dot. Although, here's
the issue I've come to find. I wanted to get the dot simply moving back
and forth on the x-axis - once it collides with x position of the right or
left paddle, it reverses it's direction. Although, with the current setup,
it moves, hit's the right paddle, and simply gets locked at that position.
[start code]
#First Game Using Pygame (A pong type game)
import pygame
from pygame import *
import whrandom
#Classes
class resolution:
width = 640
height = 480
class color:
black = 0,0,0
white = 255,255,255
class leftpadd:
x = 10
y = 215
width = 20
height = 50
class rightpadd:
x = 610
y = 215
width = 20
height = 50
class dot:
x = 317.5
y = 207.5
width = 15
height = 15
#dir = +1
#Global Variables
done = 0
#Set the resolution and initialize the window
screen = pygame.display.set_mode((resolution.width, resolution.height))
pygame.display.init()
#toggle fullscreen
#pygame.display.toggle_fullscreen()
#Playing Sound
#Initialize Mixer, Load the audio file, and then play it
pygame.mixer.init()
pygame.mixer.music.load('bustamove.mp3')
pygame.mixer.music.play(0, 0.0)
#Enable keys to repeat
pygame.key.set_repeat(1,1)
while done == 0:
#Quit Sequence
remaining_events = pygame.event.get()
for event in remaining_events:
if event.type == QUIT:
done = 1
#Displaying the left paddle and making it move
screen.fill(color.black)
#Players Paddle (left), Computers paddle (right), and the infamous dot
screen.fill(color.white, (leftpadd.x,leftpadd.y,leftpadd.width,leftpadd.height))
screen.fill(color.white, (rightpadd.x,rightpadd.y,rightpadd.width,rightpadd.height))
screen.fill(color.white, (dot.x,dot.y,dot.width,dot.height))
#Paddle Movement
for event in remaining_events:
if event.type == KEYDOWN:
if event.key == K_DOWN:
leftpadd.y = leftpadd.y+2
screen.fill(color.white, (leftpadd.x,leftpadd.y,leftpadd.width,leftpadd.height))
elif event.key == K_UP:
leftpadd.y = leftpadd.y-2
screen.fill(color.white, (leftpadd.x,leftpadd.y,leftpadd.width,leftpadd.height))
#Collision detection
if leftpadd.y > 430:
leftpadd.y = 430
if rightpadd.y > 430:
rightpadd.y = 430
if leftpadd.y < 0:
leftpadd.y = 0
if rightpadd.y < 0:
rightpadd.y = 0
#Dot Motion
dot.x = dot.x+1
#Dot collision HAVING ISSUES HERE!
if dot.x + dot.width > rightpadd.x:
dot.x = dot.x-1
if dot.x + dot.width < leftpadd.x:
dot.x = dot.x+1
#Refresh the screen and pump the event qeue.
pygame.display.flip()
pygame.event.pump()
Any suggestions on how to get the dot to reverse it's direction? I think I
understand what's causing it, but I'm loss as to how to correct it.
Thank you,
~Ryan
~Ryan
>
> #Collision detection
> if leftpadd.y > 430:
> leftpadd.y = 430
>
> if rightpadd.y > 430:
> rightpadd.y = 430
>
> if leftpadd.y < 0:
> leftpadd.y = 0
>
> if rightpadd.y < 0:
> rightpadd.y = 0
>
> #Dot Motion
> dot.x = dot.x+1
>
# Replace the previous line with, so dot will move along direction
dot.x = dot.x + dot.dir
# or with the variable speed
dot.x = dot.x + dot.speed
>
> #Dot collision HAVING ISSUES HERE!
> if dot.x + dot.width > rightpadd.x:
> dot.x = dot.x-1
#Think that dot is not allways colliding pad so you can't do this, when dot
collides it should change the sign of it's velocity:
dot.dir = -dot.dir
# Or
dot.speed = -dot.speed
> if dot.x + dot.width < leftpadd.x:
> dot.x = dot.x+1
>
# The same for the left pad
dot.dir = -dot.dir
# Or
dot.speed = -dot.speed
> #Refresh the screen and pump the event qeue.
> pygame.display.flip()
> pygame.event.pump()
>
I see you haven't take in account the y-axis movement, it works the same way but
you should have a 2-component speed, so, the class dot will look like that:
class dot:
x = 317.5
y = 207.5
width = 15
height = 15
speedx = +1
speedy = +1
Greets, Marcos.
(And merry xmas :)
I use absolute value to make sure the ball goes the right way.
http://staff.easthighschool.net/lee/computers/book/chapter04/
Of course, if I have another problem I'll be sure to ask.
Talk to you later, and Merry X-mas ;)
~Ryan