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

Re: [pygame] moving the ship



Jeroen Vloothuis wrote:
> just use floats for all calculations. Then in your drawing routine cast them 
> to int's: int(x), int(y) That way your game internally works with the correct 
> value's and the screen is merely a slightly less precise view on the game 
> world.

actually, there's no need to do the casting. pygame's functions will accept
floating point numbers. so you can send them straight to functions like
blit or rect..
    screen.blit(ship, (10.3, 100.66))


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).

when mixing two integer numbers with some math, you traditionally get an
integer result. computers just "truncate" or "round down" from the floating
numbers to get an integer. when you use floating point numbers in an
equation, you will get a floating point result.

this is all how python works too. in the interactive python interpreter you
can see these results.

    >>> 5/2
    2
    >>> 5.0/2
    2.5

the problem you are probably seeing with your ship movement, is that
integers cannot really make small changes. you probably set the initial
position to some integer value, like "pos = 100, 10". try switching the
position to floating point numbers and see how you get more precision.
"pos = 100.0, 10.0"

this should all be explained relatively well in any sort of programming
book. because this can be a confusing issue to new programmers, python will
actually be changing soon so that divide operations between to integers
will give you a floating point number as a result. so 5/2 will really be 2.5





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