Some pygame issues

Ryan Spencer jeder at earthlink.net
Mon Dec 22 08:59:49 CET 2003


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




More information about the Python-list mailing list