<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
  <channel>
    <title>'When using pygame I cannot get the balls to bounce off eachother' Thread RSS Feed</title>
    <link>http://www.programmersheaven.com/</link>
    <description>Contains the latest posts from the thread 'When using pygame I cannot get the balls to bounce off eachother' posted on the 'Python' forum at Programmer's Heaven.</description>
    <language>en</language>
    <copyright>Copyright 2013 Programmers Heaven</copyright>
    <pubDate>Wed, 19 Jun 2013 02:26:08 -0700</pubDate>
    <lastBuildDate>Wed, 19 Jun 2013 02:26:08 -0700</lastBuildDate>
    <generator>Argotic Syndication Framework 2007.3.0.1, http://www.codeplex.com/Argotic</generator>
    <docs>http://www.rssboard.org/rss-specification</docs>
    <ttl>360</ttl>
    <image>
      <url>http://www.programmersheaven.com/images/ph.gif</url>
      <title>Programmers Heaven</title>
      <link>http://www.programmersheaven.com/</link>
      <width>88</width>
      <height>31</height>
    </image>
    <item>
      <title>When using pygame I cannot get the balls to bounce off eachother</title>
      <link>http://www.programmersheaven.com/mb/python/430477/430477/when-using-pygame-i-cannot-get-the-balls-to-bounce-off-eachother/</link>
      <description>hello,&lt;br /&gt;
I am writing a really simple python program using pygame. However  i cannot figure out how to make the balls bounce off eachother.&lt;br /&gt;
here is my code&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
import pygame&lt;br /&gt;
import random&lt;br /&gt;
import time&lt;br /&gt;
# Define some colors&lt;br /&gt;
black    = (   0,   0,   0)&lt;br /&gt;
white    = ( 255, 255, 255)&lt;br /&gt;
green    = (   0, 255,   0)&lt;br /&gt;
red      = ( 255,   0,   0)&lt;br /&gt;
&lt;br /&gt;
pygame.init()&lt;br /&gt;
&lt;br /&gt;
#creats ball class.&lt;br /&gt;
class Ball():&lt;br /&gt;
    # x and y cordiates starting points before random.&lt;br /&gt;
    x = 0&lt;br /&gt;
    y = 0&lt;br /&gt;
&lt;br /&gt;
    change_x = 0&lt;br /&gt;
    change_y = 0&lt;br /&gt;
&lt;br /&gt;
    size = 5&lt;br /&gt;
&lt;br /&gt;
    color= black&lt;br /&gt;
    #function to move the ball on the x and y at the same time.&lt;br /&gt;
    def move(self):&lt;br /&gt;
        self.x = self.x + self.change_x&lt;br /&gt;
        self.y = self.y + self.change_y&lt;br /&gt;
    #when ball hits the wall it changes the direction of movement.&lt;br /&gt;
        if self.x &amp;lt;= 0:&lt;br /&gt;
            self.change_x = 1&lt;br /&gt;
        if self.x == 691:&lt;br /&gt;
            self.change_x = -1&lt;br /&gt;
        if self.y &amp;lt;= 0:&lt;br /&gt;
            self.change_y = 1&lt;br /&gt;
        if self.y == 491:&lt;br /&gt;
            self.change_y = -1&lt;br /&gt;
            &lt;br /&gt;
&lt;br /&gt;
        &lt;br /&gt;
    def draw(self, screen):&lt;br /&gt;
        pygame.draw.circle(screen, self.color, [self.x, self.y], self.size )&lt;br /&gt;
&lt;br /&gt;
  &lt;br /&gt;
# Set the width and height of the screen [width,height]&lt;br /&gt;
size=[700,500]&lt;br /&gt;
screen=pygame.display.set_mode(size)&lt;br /&gt;
pygame.display.set_caption("Quintens bouncing balls")&lt;br /&gt;
&lt;br /&gt;
# Used to manage how fast the screen updates&lt;br /&gt;
clock=pygame.time.Clock()&lt;br /&gt;
done=False&lt;br /&gt;
&lt;br /&gt;
#Loop until the user clicks the close button.    &lt;br /&gt;
# -------- Main Program Loop -----------&lt;br /&gt;
#ball1 = Ball()&lt;br /&gt;
ball = [Ball() for i in range(0,20)]&lt;br /&gt;
#for loop to creat all random atribures for balls.&lt;br /&gt;
for i in range (0, 20):&lt;br /&gt;
    ball[i].x = random.randrange(0,686)&lt;br /&gt;
    ball[i].y = random.randrange(0, 486)&lt;br /&gt;
    ball[i].color = [random.randrange(0, 256), random.randrange(0,256),random.randrange(0,256)]&lt;br /&gt;
    ball[i].size = random.randrange(5,20)&lt;br /&gt;
    ball[i].change_x = random.randrange(-2, 2, 1)&lt;br /&gt;
    ball[i].change_y = random.randrange(-2, 2, 1)&lt;br /&gt;
    print( i, ball[i].x, ball[i].y)&lt;br /&gt;
    time.sleep(0.003)&lt;br /&gt;
    &lt;br /&gt;
&lt;br /&gt;
while done==False:&lt;br /&gt;
    &lt;br /&gt;
    # ALL EVENT PROCESSING SHOULD GO BELOW THIS COMMENT&lt;br /&gt;
    for event in pygame.event.get(): # User did something&lt;br /&gt;
        if event.type == pygame.QUIT: # If user clicked close&lt;br /&gt;
            done=True # Flag that we are done so we exit this loop&lt;br /&gt;
&lt;br /&gt;
    # ALL EVENT PROCESSING SHOULD GO ABOVE THIS COMMENT&lt;br /&gt;
  &lt;br /&gt;
  &lt;br /&gt;
    # ALL GAME LOGIC SHOULD GO BELOW THIS COMMENT&lt;br /&gt;
&lt;br /&gt;
    # ALL GAME LOGIC SHOULD GO ABOVE THIS COMMENT&lt;br /&gt;
 &lt;br /&gt;
    &lt;br /&gt;
    # ALL CODE TO DRAW SHOULD GO BELOW THIS COMMENT&lt;br /&gt;
&lt;br /&gt;
    # First, clear the screen to white. Don't put other drawing commands&lt;br /&gt;
    # above this, or they will be erased with this command.&lt;br /&gt;
    #creates the screen and draws 20 balls.&lt;br /&gt;
    screen.fill(white)&lt;br /&gt;
    for i in range(20):&lt;br /&gt;
        ball[i].move()&lt;br /&gt;
        ball[i].draw(screen)&lt;br /&gt;
    &lt;br /&gt;
&lt;br /&gt;
    # Draw on the screen a green line from (0 ,0) to (100 ,100)&lt;br /&gt;
    # 5 pixels wide .&lt;br /&gt;
    &lt;br /&gt;
    # ALL CODE TO DRAW SHOULD GO ABOVE THIS COMMENT&lt;br /&gt;
&lt;br /&gt;
     &lt;br /&gt;
    # Go ahead and update the screen with what we've drawn.&lt;br /&gt;
    pygame.display.flip()&lt;br /&gt;
 &lt;br /&gt;
    # Limit to 20 frames per second&lt;br /&gt;
    clock.tick(20)&lt;br /&gt;
&lt;br /&gt;
# Close the window and quit.&lt;br /&gt;
# If you forget this line, the program will 'hang'&lt;br /&gt;
# on exit if running from IDLE.&lt;br /&gt;
pygame.quit ()&lt;br /&gt;</description>
      <guid isPermaLink="true">http://www.programmersheaven.com/mb/python/430477/430477/when-using-pygame-i-cannot-get-the-balls-to-bounce-off-eachother/</guid>
      <pubDate>Sun, 02 Dec 2012 22:55:34 -0700</pubDate>
      <category>Python</category>
    </item>
  </channel>
</rss>