Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Some pygame issues

2 views
Skip to first unread message

Ryan Spencer

unread,
Dec 22, 2003, 2:59:49 AM12/22/03
to
Hello everyone,

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 Spencer

unread,
Dec 22, 2003, 3:01:09 AM12/22/03
to
Pardon me for extensive post, by the way.

~Ryan

Marcos Eimil Pardo

unread,
Dec 22, 2003, 3:37:04 AM12/22/03
to pytho...@python.org

>
>class dot:
> x = 317.5
> y = 207.5
> width = 15
> height = 15
> #dir = +1
>
# You can uncoment
dir = 1
# *or* add a variable called speed so it has clearer meaning
speed = 1

>
> #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 :)


Lee Harr

unread,
Dec 22, 2003, 4:09:17 PM12/22/03
to
On 2003-12-22, Ryan Spencer <je...@earthlink.net> wrote:
> Hello everyone,
>
> 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.
>

I use absolute value to make sure the ball goes the right way.
http://staff.easthighschool.net/lee/computers/book/chapter04/

Ryan Spencer

unread,
Dec 23, 2003, 12:06:22 AM12/23/03
to
Thanks Lee and Marcos, Got it to work,

Of course, if I have another problem I'll be sure to ask.

Talk to you later, and Merry X-mas ;)

~Ryan

0 new messages