### Thread: Python,pygame,making circle move with trigonometry,bounce problem

1. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
May 2013
Posts
5
Rep Power
0

#### Python,pygame,making circle move with trigonometry,bounce problem

hi...this is my code
class Circle():
angle = random.randrange(0,360)
def __init__(self,x,y,size):
self.x = x
self.y = y
self.size= size
self.speed = random.random()
def drawing(self):
pygame.draw.circle(screen,black,[int(self.x),int(self.y)],self.size,2)
def movement(self):
self.x = self.x + math.cos(math.radians(self.angle))*self.speed #calculating new x(function math.radians change degrees to radians)
self.y = self.y - math.sin(math.radians(self.angle))*self.speed
def boundaries(self):
if self.x >= 700 or self.x < 0: # this is my problem...iam trying to make boundaries so my circle could bounce from it...i was trying to change angle but with no succes...is there an option so my circle could bounce from boundaries?? so i want to make my circle go to opposite direction!!
thanks for any advice
2. Please search the internet for
pygame collision detection
There find tutorials and ideas.
Code:
```    def boundaries(self):
if self.x >= 700 or self.x < 0: # then negate the x velocity component
# velocity is given by polar coordinates (a bad idea since you
# recompute sines and cosines for each movement).
# On contact with wall of constant x we change the sign of the
# x component of velocity in a Cartesian system.
VxNew = -Vx
angle_in_radians = math.atan2(Vy, VxNew)
self.angle = angle_in_degrees```
3. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Oct 2012
Posts
194
Rep Power
6
Use code tags please.

python Code:
```import pygame as pg
import sys,os,random,math

class Circle():
def __init__(self,x,y,size):
self.angle = random.randrange(0,360)
self.rect = pg.Rect((0,0),size)
self.move = [x,y]
def update(self,Surf):
self.movement()
self.rect.topleft = self.move
self.boundaries(Surf.get_rect())
pg.draw.ellipse(Surf,(255,255,255),self.rect,2)
def movement(self):
self.move[0] += self.speed[0]
self.move[1] -= self.speed[1]
def boundaries(self,bounding_rect):
for i in (0,1):
if not (0 <= self.rect[i] <= bounding_rect.size[i]-self.rect.size[i]):
self.speed[i] *= -1
self.rect.clamp_ip(bounding_rect)

class Control:
def __init__(self):
os.environ["SDL_VIDEO_CENTERED"] = '1'
pg.init()
self.screen = pg.display.set_mode((700,600))
self.MyCirc = Circle(350,350,(50,50))
self.Clock = pg.time.Clock()
self.fps = 60
self.done = False
def event_loop(self):
keys = pg.key.get_pressed()
for event in pg.event.get():
if event.type == pg.QUIT or keys[pg.K_ESCAPE]:
self.done = True
def main(self):
while not self.done:
self.event_loop()
self.screen.fill(0)
self.MyCirc.update(self.screen)
pg.display.update()
self.Clock.tick(self.fps)

if __name__ == "__main__":
RunIt = Control()
RunIt.main()
pg.quit();sys.exit()```

-Mek

Edit: Restructure to demonstrate better habits.
Last edited by Mekire; May 29th, 2013 at 08:45 PM.