Python

Moderators: None (Apply to moderate this forum)
Number of threads: 473
Number of posts: 1172

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
When using pygame I cannot get the balls to bounce off eachother Posted by bowlofsoup94 on 2 Dec 2012 at 10:55 PM
hello,
I am writing a really simple python program using pygame. However i cannot figure out how to make the balls bounce off eachother.
here is my code


import pygame
import random
import time
# Define some colors
black = ( 0, 0, 0)
white = ( 255, 255, 255)
green = ( 0, 255, 0)
red = ( 255, 0, 0)

pygame.init()

#creats ball class.
class Ball():
# x and y cordiates starting points before random.
x = 0
y = 0

change_x = 0
change_y = 0

size = 5

color= black
#function to move the ball on the x and y at the same time.
def move(self):
self.x = self.x + self.change_x
self.y = self.y + self.change_y
#when ball hits the wall it changes the direction of movement.
if self.x <= 0:
self.change_x = 1
if self.x == 691:
self.change_x = -1
if self.y <= 0:
self.change_y = 1
if self.y == 491:
self.change_y = -1



def draw(self, screen):
pygame.draw.circle(screen, self.color, [self.x, self.y], self.size )


# Set the width and height of the screen [width,height]
size=[700,500]
screen=pygame.display.set_mode(size)
pygame.display.set_caption("Quintens bouncing balls")

# Used to manage how fast the screen updates
clock=pygame.time.Clock()
done=False

#Loop until the user clicks the close button.
# -------- Main Program Loop -----------
#ball1 = Ball()
ball = [Ball() for i in range(0,20)]
#for loop to creat all random atribures for balls.
for i in range (0, 20):
ball[i].x = random.randrange(0,686)
ball[i].y = random.randrange(0, 486)
ball[i].color = [random.randrange(0, 256), random.randrange(0,256),random.randrange(0,256)]
ball[i].size = random.randrange(5,20)
ball[i].change_x = random.randrange(-2, 2, 1)
ball[i].change_y = random.randrange(-2, 2, 1)
print( i, ball[i].x, ball[i].y)
time.sleep(0.003)


while done==False:

# ALL EVENT PROCESSING SHOULD GO BELOW THIS COMMENT
for event in pygame.event.get(): # User did something
if event.type == pygame.QUIT: # If user clicked close
done=True # Flag that we are done so we exit this loop

# ALL EVENT PROCESSING SHOULD GO ABOVE THIS COMMENT


# ALL GAME LOGIC SHOULD GO BELOW THIS COMMENT

# ALL GAME LOGIC SHOULD GO ABOVE THIS COMMENT


# ALL CODE TO DRAW SHOULD GO BELOW THIS COMMENT

# First, clear the screen to white. Don't put other drawing commands
# above this, or they will be erased with this command.
#creates the screen and draws 20 balls.
screen.fill(white)
for i in range(20):
ball[i].move()
ball[i].draw(screen)


# Draw on the screen a green line from (0 ,0) to (100 ,100)
# 5 pixels wide .

# ALL CODE TO DRAW SHOULD GO ABOVE THIS COMMENT


# Go ahead and update the screen with what we've drawn.
pygame.display.flip()

# Limit to 20 frames per second
clock.tick(20)

# Close the window and quit.
# If you forget this line, the program will 'hang'
# on exit if running from IDLE.
pygame.quit ()



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.