Not sure if this is what you're looking for, but I would also use cos for the x coordinate as suggested. If:
self.rect.x = self.radius * math.cos(self.angle) + self.center_x self.rect.y = self.radius * math.sin(self.angle) + self.center_y Take a derivative (assume: angle += angular_vel * dt for every increment in time dt), so that: self.rect.x += -self.radius * math.sin(self.angle) * self.angular_vel * dt self.rect.y += self.radius * math.cos(self.angle) * self.angular_vel * dt After a while, it won't be a perfect circle anymore (because of accumulating errors), or the center may move across the screen, but it may work for quite some time. Best, Lucas On Tue, Apr 28, 2015 at 9:44 PM, diliup gabadamudalige <dili...@gmail.com> wrote: > I am pasting the code of the complete program of around 145 lines below > with the part that works correctly and the part that doesn't marked. > > Why does the object NOT move in a circle when implemented as a class? > > import sys, os, pygame, itertools > from math import sin,cos,pi, radians > from pygame.locals import * > > os.environ['SDL_VIDEO_WINDOW_POS'] = "%d,%d" % (50,50) #Set window position > > pygame.init() > clock = pygame.time.Clock() > FPS = 1000 > > SCREENW = 800 #screen width > SCREENH = 740 #screen height > > BLACK = (0, 0, 0) > BLUE = (0, 0, 255) > ORANGE = (128, 100, 30) > FONT1= "Cookie-Regular.ttf" > > SCREEN = pygame.display.set_mode((SCREENW, SCREENH), 0, 32) #display screen > clock = pygame.time.Clock() > > > #------------------------------------------------------------------------------- > def maketext(msg,fontsize, colour = ORANGE, font = FONT1): > mafont = pygame.font.Font(font, fontsize) > matext = mafont.render(msg, True, colour) > matext = matext.convert_alpha() > return matext > > > #------------------------------------------------------------------------------- > def print_info(): > """""" > textcos = maketext(str(round(obj.rect.x, 2)) + " " + > str(round(obj.rect.y, 2)), 30) > SCREEN.blit(textcos, (obj.rect.x, obj.rect.y + 30)) > > > #------------------------------------------------------------------------------- > class object_factory(pygame.sprite.Sprite): > > def __init__(self, imagelist, xpos, ypos): > """Constructor""" > pygame.sprite.Sprite.__init__(self) > self.name = "" > self.frame = 0 > self.imagelist = imagelist > self.image = imagelist[self.frame] > self.mask = pygame.mask.from_surface(self.image) # pixelmask > self.rect = self.image.get_rect() > self.rect.x = xpos > self.rect.y = ypos > self.timer = 0 > self.timerlimit = 10 > > > #---------------------------------------------------------------------- > #def move(self): # wallsprites, Herosprite, looptime > #self.rect.x += self.speedx > #self.rect.y += self.speedy > > #---------------------------------------------------------------------- > def update(self): > """""" > self.image = self.imagelist[self.frame] > if self.timer >= self.timerlimit: > self.frame += 1 > if self.frame >= len(self.imagelist): > self.frame = 0 > self.timer = 0 > self.timer += 1 > > plat = pygame.image.load("plt0.png").convert_alpha() > star = pygame.image.load("gemp0.png").convert_alpha() > > ## declare the sprite group > platforms = pygame.sprite.Group() > boxes = pygame.sprite.Group() > > rotcenx = SCREENW/2 > rotceny = SCREENH/2 > > radius = 200 > angle = radians(90) #pi/4 # starting angle 45 degrees > omega = radians(5) #Angular velocity > > m = rotcenx + radius * cos(angle) #Starting position x > n = rotceny - radius * sin(angle) #Starting position y > > for _ in itertools.repeat(None, 1): > midx = SCREENW/2 > midy = SCREENH/2 > > radius1 = 200 > angle1 = radians(180) #pi/4 # starting angle 45 degrees > angular_v = radians(5) #Angular velocity > > a = midx + (radius1 * cos(angle1)) #Starting position x > b = midy - (radius1 * sin(angle1)) #Startinh position y > > plat = object_factory([plat], a, b) > plat.radius = radius1 > plat.angle = angle1 > plat.angular_vel = angular_v > > > platforms.add(plat) > > while True: > ms = clock.tick(FPS) # milliseconds passed since last frame > > SCREEN.fill((BLACK)) > > ## THIS CODE WORKS MOVES THE OBJECT IN A CIRCLE > > SCREEN.blit(star, (m, n)) # Draw current x,y > > angle = angle + omega # New angle, we add angular velocity > > m = m + radius * omega * cos(angle + pi / 2) # New x > > n = n - radius * omega * sin(angle + pi / 2) # New y > > ##--------------------------------------------------------------- > # show object anchored to center of rotation > pygame.draw.line(SCREEN, ORANGE, (rotcenx, rotceny), (m, n)) > > text = maketext(str(radius), 30) > SCREEN.blit(text, (m, n - 40)) > > text = maketext((str(round(m, 2)) + " " + str(round(n, 2))), 30) > SCREEN.blit(text, (m, n + 40)) # Draw current x,y > > ## THIS CODE DOES NOT MOVE THE OBJECT IN A CIRCLE > > for plat in platforms: > > plat.angle = plat.angle + plat.angular_vel > > plat.rect.x = plat.rect.x + plat.radius * plat.angular_vel * > cos(plat.angle + pi / 2) > > plat.rect.y = plat.rect.y - plat.radius * plat.angular_vel * > sin(plat.angle + pi / 2) > > > ##------------------------------------------------------------------------ > > pygame.draw.line(SCREEN, ORANGE, (midx, midy), (plat.rect.x, > plat.rect.y)) > pygame.draw.circle(SCREEN, BLUE, (rotcenx, rotceny), radius1, 2) > > platforms.update() > platforms.draw(SCREEN) > > pygame.event.pump() > keys = pygame.key.get_pressed() > > for event in pygame.event.get(): > if event.type == QUIT or (event.type == KEYDOWN and event.key == > K_ESCAPE): > pygame.quit() > sys.exit() > > pygame.display.update() > pygame.time.wait(100) > > > On Tue, Apr 28, 2015 at 6:22 PM, diliup gabadamudalige <dili...@gmail.com> > wrote: > >> Looking at the code on this page lines 47 & 48 >> >> >> http://programarcadegames.com/python_examples/show_file.php?file=sprite_circle_movement.py >> >> is there a way to do >> self.rect.x +*= some value* >> self.rect.y += some value >> >> rather than >> >> self.rect.x = self.radius * math.sin(self.angle) + self.center_x >> self.rect.y = self.radius * math.cos(self.angle) + self.center_y >> >> ? >> >> I tired it in the code attached and strangely it works ok but only >> OUTSIDE THE class. When I implement the exact code in a class it does >> something weird. I can't find where I have gone wrong. >> >> Please help. >> >> Many thanks in advance. >> >> Diliup Gabadamudalige >> >> >> >> ********************************************************************************************** >> This e-mail is confidential. It may also be legally privileged. If you >> are not the intended recipient or have received it in error, please delete >> it and all copies from your system and notify the sender immediately by >> return e-mail. Any unauthorized reading, reproducing, printing or further >> dissemination of this e-mail or its contents is strictly prohibited and may >> be unlawful. Internet communications cannot be guaranteed to be timely, >> secure, error or virus-free. The sender does not accept liability for any >> errors or omissions. >> >> ********************************************************************************************** >> >> > > > -- > Diliup Gabadamudalige > > http://www.diliupg.com > http://soft.diliupg.com/ > > > ********************************************************************************************** > This e-mail is confidential. It may also be legally privileged. If you are > not the intended recipient or have received it in error, please delete it > and all copies from your system and notify the sender immediately by return > e-mail. Any unauthorized reading, reproducing, printing or further > dissemination of this e-mail or its contents is strictly prohibited and may > be unlawful. Internet communications cannot be guaranteed to be timely, > secure, error or virus-free. The sender does not accept liability for any > errors or omissions. > > ********************************************************************************************** > > -- """ lucas o. wagner, PhD theoretical physics/chemistry lowagner.github.io l.o.wag...@vu.nl """