GrumpyOgre
Still Fresh
- Joined
- May 3, 2012
- Messages
- 5
Good evening fellow pygame enthusiasts. I've been working on a Ball class script. Its a fairly simple thing I think but boy o boy am I ever not seeing why my collision isn't working. I've made a constructor member function which seems to work fine. I've made an update function which also works. Then in the main game loop I create an empty array (list in python speak) of balls and when the keydown even detects a letter 'a' it calls the constructor and creates a new ball. That all works as planned.
The issue I have is when detecting collision between the balls. I simply use the distance formula to detect the distance between each ball and all the other balls in the list and if that value is ever less than the diameter of the ball then it calls up the member function that should change the direction of the two colliding balls. Seems simple enough and my distance formula is working fine but I can't get the balls to change direction. They change direction fine enough when they contact the "walls" but they don't change direction when they bump each other. After 24 hrs of trying various different things I am now out of ideas. What the heck am I doing wrong?
import pygame, sys, glob, random, math
from pygame import *
from random import *
h=600
w=800
screen = pygame.display.set_mode((w,h))
clock = pygame.time.Clock()
class ball:
def __init__(self):
# sets the x and y values of the start position for our sprite
self.size = 10
self.x = randrange(20, 780)
self.y = randrange(20, 580)
self.irection = 1
self.yDirection = 1
self.color = (randrange(0, 255),randrange(0, 255),randrange(0, 255))
self.nextx = self.x+self.irection
self.nexty = self.y+self.yDirection
self.bounce()
self.update()
self.collision()
def collision(self):
self.irection *=-1
self.yDirection *=-1
self.update()
def update(self):
self.x += self.irection
self.y += self.yDirection
pygame.draw.circle(screen, (self.color), (self.x, self.y), self.size)
def bounce(self):
if self.x > 779 or self.x < 21:
self.irection *=-1
#print BallList[0].x, BallList[0].irection
if self.y > 579 or self.y < 21:
self.yDirection *=-1
#print BallList[0].x, BallList[0].irection
def distance(obj1, obj2):
# dist formula sqrt((x2-x1)^2+(y2-y1)^2)
return math.sqrt(math.pow((obj1.nextx-obj2.nextx), 2) + math.pow((obj1.nexty-obj2.nexty),2))
BallList=[]
rungame = 1
while rungame:
screen.fill((0,0,0))
clock.tick(900)
for event in pygame.event.get():
if event.type == pygame.QUIT:
RunGame = False
elif event.type == KEYDOWN and event.key==pygame.K_a:
BallList.append(ball())
for a in range(0,len(BallList)):
for b in range(0,len(BallList)):
if BallList[a]==BallList or len(BallList)<1: continue
if distance(BallList[a],BallList)<20 :
print "boom ", BallList[0].x, BallList[0].irection
BallList[a].collision()
BallList.collision()
for x in range(0,len(BallList)):
BallList[x].update()
for x in range(0,len(BallList)):
BallList[x].bounce()
pygame.display.update()
pygame.quit()
The issue I have is when detecting collision between the balls. I simply use the distance formula to detect the distance between each ball and all the other balls in the list and if that value is ever less than the diameter of the ball then it calls up the member function that should change the direction of the two colliding balls. Seems simple enough and my distance formula is working fine but I can't get the balls to change direction. They change direction fine enough when they contact the "walls" but they don't change direction when they bump each other. After 24 hrs of trying various different things I am now out of ideas. What the heck am I doing wrong?
import pygame, sys, glob, random, math
from pygame import *
from random import *
h=600
w=800
screen = pygame.display.set_mode((w,h))
clock = pygame.time.Clock()
class ball:
def __init__(self):
# sets the x and y values of the start position for our sprite
self.size = 10
self.x = randrange(20, 780)
self.y = randrange(20, 580)
self.irection = 1
self.yDirection = 1
self.color = (randrange(0, 255),randrange(0, 255),randrange(0, 255))
self.nextx = self.x+self.irection
self.nexty = self.y+self.yDirection
self.bounce()
self.update()
self.collision()
def collision(self):
self.irection *=-1
self.yDirection *=-1
self.update()
def update(self):
self.x += self.irection
self.y += self.yDirection
pygame.draw.circle(screen, (self.color), (self.x, self.y), self.size)
def bounce(self):
if self.x > 779 or self.x < 21:
self.irection *=-1
#print BallList[0].x, BallList[0].irection
if self.y > 579 or self.y < 21:
self.yDirection *=-1
#print BallList[0].x, BallList[0].irection
def distance(obj1, obj2):
# dist formula sqrt((x2-x1)^2+(y2-y1)^2)
return math.sqrt(math.pow((obj1.nextx-obj2.nextx), 2) + math.pow((obj1.nexty-obj2.nexty),2))
BallList=[]
rungame = 1
while rungame:
screen.fill((0,0,0))
clock.tick(900)
for event in pygame.event.get():
if event.type == pygame.QUIT:
RunGame = False
elif event.type == KEYDOWN and event.key==pygame.K_a:
BallList.append(ball())
for a in range(0,len(BallList)):
for b in range(0,len(BallList)):
if BallList[a]==BallList or len(BallList)<1: continue
if distance(BallList[a],BallList)<20 :
print "boom ", BallList[0].x, BallList[0].irection
BallList[a].collision()
BallList.collision()
for x in range(0,len(BallList)):
BallList[x].update()
for x in range(0,len(BallList)):
BallList[x].bounce()
pygame.display.update()
pygame.quit()