Friday, 15 August 2014

python - Rotation of a sprite on a circle -



python - Rotation of a sprite on a circle -

there's sprite moving on circle centered around screen center radius = 30 pixels. motion more or less ok, sprite doesn't rotate @ all. i've tried tutorials (e.g. how animate scale , rotate in pygame using rotozoom), , when sprite moves along line or changes trajectory randomly -there's no problem. what's wrong when it's moving along circle? here's code:

class enemiesstrong(pygame.sprite.sprite): def __init__(self): pygame.sprite.sprite.__init__(self) self.image = pygame.image.load("enemy3.png").convert_alpha() self.image2 = self.image self.rect = self.image2.get_rect() self.angle = 0 def initloc(self, pos, x_pos, y_pos): self.rect.x = pos[0]/2 + x_pos self.rect.y = y_pos def update(self, pos, x_pos, y_pos, quadrant, newcoord): #print(quadrant) self.rect.y = y_pos if (quadrant==1 , newcoord==0): self.rect.x-=1 elif (quadrant==1 , newcoord==1): self.rect.x=x_pos elif (quadrant==2): oldcenter = self.rect.center **self.image2 = pygame.transform.rotate(self.image, self.angle)** self.rect = self.image2.get_rect() self.rect.center = oldcenter self.rect.x+=1 elif (quadrant==3 , newcoord==0): self.rect.x+=1 elif (quadrant==4 , newcoord==0): self.rect.x-=1 elif (quadrant==4 , newcoord==1): self.rect.x=x_pos def turnleft(self, pos): self.angle = (self.angle + 10) % 360 print(self.angle) def whereenemy(self): homecoming (self.rect.x, self.rect.y) def orbitalcoordinates(loc, xpos, radius, quadrant): #do lots of stuff determine location of ship in screen #and homecoming y-coordinate disc = np.power(radius,2) - np.power(xpos - loc[0]/2,2) ypos = loc[1]/2 - np.sqrt(disc) homecoming ypos enemy_s = enemiesstrong() ypos = orbitalcoordinates(screenpar, xpos + screenpar[0]/2, radius,2) enemy_s.initloc(screenpar, xpos, ypos) enemiesupdate.add(enemy_s) where_strong_enemy = enemy_s.whereenemy() bg = pygame.image.load("outer_space.jpg") screenpar = [bg.get_width(), bg.get_height()] def main(): #various useless crap enemy_s in enemiesupdate: #do lots of stuff determine x- position , direction of motion of ship #and pass these values orbitalcoordinates enemy_s.turnleft(screenpar) ypos = orbitalcoordinates(screenpar, where_strong_enemy[0], radius, quadrant) enemy_s.update(screenpar, where_strong_enemy[0], ypos, quadrant, 0) where_strong_enemy = list(enemy_s.whereenemy()) #then check if y-coordinates close middle of screen tweak them #little bit screen.blit(bg, [0, 0]) enemiesupdate.draw(screen) pygame.display.flip()

i omitted chunks of code deem unnecessary. i'm sure there's stupid error i'm making.

python pygame game-physics

No comments:

Post a Comment