[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [pygame] moving the ship



On Tue, 08 Oct 2002 19:54:29 -0700
Pete Shinners <shredwheat@attbi.com> wrote:

> aazazel, you are probably wondering about the difference between integer
> and float numbers? computers have traditionally had two ways of
> representing numbers. the simple way is with integers, like numbers 1, 2,
> 3. but integers cannot really represent values in between the integers,
> like numbers 1.5, 2.11).

Thank you for the explanation, but I know the different between floats and 
integers :) 
I needed a way to move my ship 1 pixel for time diagonally. The problem was
that the sine and the cosine of 1 are less than 1, so the ship remained 
motionless, but now I find the way to move it. I left in memory the 
"ipotetical" movement number(that it's less than 1), and I add it to 
the movement number of the next frame, after a few frames this will surely
become bigger than 1, so the ship will be able to move.
This is my code in the ship class, it's call every frame, self.angle is the angle 
direction of the ship, self.xinc and self.yinc are variables that take the 
increment of x and y axes, self.rect is the rect of the ship, self.jump is 1.0

     xtoken = 0.0
     ytoken = 0.0
     yshift = self.jump * math.cos(rad(self.angle))
     xshift = self.jump * math.sin(rad(self.angle))
     
     if xshift < 0.0: 
       xsign = -1
     else:            
       xsign = 1  
     xshift = math.fabs(xshift)  
     
     if yshift < 0.0: 
       ysign = -1
     else:            
       ysign = 1  
     yshift = math.fabs(yshift)
     
     if xshift < 1.0:
       self.xinc += xshift
       if self.xinc > 1.0:
         self.xinc -= 1.0
	 xtoken = 1.0
     else:
       self.xinc = xshift  
       
     if yshift < 1.0:
       self.yinc += yshift
       if self.yinc > 1.0:
	 self.yinc -= 1.0
	 ytoken = 1.0
     else:
       self.yinc = yshift 
        
     self.rect.top -= int(self.yinc * ysign + ytoken * ysign)
     self.rect.left -= int(self.xinc * xsign + xtoken * xsign)

This code works very well, Have you any different idea to do it better?

thank you
____________________________________
pygame mailing list
pygame-users@seul.org
http://pygame.seul.org