[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [pygame] animatio of drawned images
s.jol wrote:
> I would like to know how do I do animations using sprites or draw functions?
> I have tried to load sprites but it does not work, I have tried to move the
> circles I draw but even after an update there is no change. I import the
> following:
well, you're not the first person to get stuck at this point. i've been
thinking about putting together a very basic introducion/tutorial for
people like you first getting started with graphics programming. let's
look at your code...
> screen = pygame.display.set_mode((500,400))
> ship = pygame.draw.circle(screen,(20,200,123),(300,200),45,12)
> ship.move(23,34)
there is nothing really special about the "screen" surface. it just
represents pixels that are shown to the user. to make an object (like
this circle) move, we need to draw it in the new position. if we just
draw it in the new position, we will actually see two circles, the first
circle in the original position, and a second circle in the new
position. so before we redraw the circle, we'll want to erase the
original one.
the pygame.draw functions return a rectangular area of the section of
the screen that was changed. this doesn't represent anything we can
actually see, it's just some coordinates that tell us what area has
changed on the screen. in your code you create a variable named "ship"
that represents these coordinates on the screen. your call to
ship.move() is simply moving these coordinates around. again, they are
not something we can actually see, so this really won't do much.
also, just so you know, your call to ship.move() actually doesn't change
the position of the ship coordinates. the functions for Rect objects
usually just return a new copy of the rectangle with whatever changes
you have asked for. you would probably want to do something like "ship =
ship.move(23, 34)". in current versions of pygame, there are newer
"in-place" versions of the Rect functions, which return nothing, but
effect the real Rectangle, so you could also do it this way,
"ship.move_ip(23, 34)"
one other thing you'll need to know. the pygame display is double
buffered. this means what you draw on the screen isn't immediately
displayed. you'll need to use the special functions
pygame.display.flip() or pygame.display.update() to actually show what
you've drawn on the screen.
you should really look through some of the examples that come with
pygame. in fact take a look at the included "chimp" tutorial, which
covers a small example with moving images line-by-line.
http://www.pygame.org/docs/tut/ChimpLineByLine.html
taking your above program, what you are expecting the code to do should
look like this..
screen = pygame.display.set_mode((500,400))
color = 20, 200, 123
pos = 300, 200
pygame.draw.circle(screen, color, pos, 45, 12)
pygame.display.flip() #show our circle
screen.fill((0, 0, 0)) #erase the screen
pos[0] = pos[0] + 23 #move circle in x
pos[1] = pos[1] + 34 #move position in y
pygame.draw.circle(screen, color, pos, 45, 12)
pygame.display.flip() #show our new screen
this would draw a circle, then draw it in another position.
unfortunately, you probably wouldn't really see it move, since there is
no delay between drawing the first position and the second position. it
would happen faster than you could really see it, so you'll only see the
circle in the second position. perhaps call "pygame.time.delay(500)"
after calling the first flip() function, so you get a chance to see it.
this code is also very inefficient. erasing the entire screen, when we
only need to erase the circle will not help this run quickly. there are
many things you can do to optimize this and make it run faster, but
hopefully that is what the current tutorials and examples can start to
show you.
____________________________________
pygame mailing list
pygame-users@seul.org
http://pygame.seul.org