[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

RE: [pygame] Two questions regarding a simple space shooter



Hi,

I may be wrong, but I think that if you kept a variable for horizontal 
movement that could be positive or negative, and simply updated it with the 
left key press (add -1) and right key press (add 1) then called move_ship() 
every frame, this would allow you to build up momentum (good realism) and also 
mean that after a couple of presses in any direction, your ship would be 
flying at a fair rate of knots.


while 1:

for event in pygame.event.get():
    if event.type == KEYDOWN:
        if event.key == K_LEFT:
            ship.changeSpeed(-1)
        if event.key == K_RIGHT:
            ship.changeSpeed(+1)

ship.move()


not sure about the starscape. A static screen could be used or maybe moving a 
view window over a larger image or strip of images maybe.

hope that helps,

rob


>===== Original Message From pygame-users@xxxxxxxx =====
>Hi list. :)
>Wanted to start programming with pygame while making a pacman-clone.
>But that's to hard. Now I'm creating a simple scroll-shooter with a ship
>sitting on a starfield and some enemy ships.
>
>I have a question regarding the design of this space shooter.
>
>I'm pondering how to handle the stars. For now, I just use
>screen.set_at and "overdraw" the old star with the new one.
>
>Dump question, i know.. but, is this commonly used this way?
>Or would you prefer to make a "Star" class. I thought, this would
>be - maybe - a little bit of overkill. :-)
>
>Another question is how to handle the keyboard input.
>I wanted to control the ship by the left and right arrow keys and
>as long I press $the button, the ship shall fly into this direction. :-)
>
>I have no code here, atm. But I try to remember what I've written:
>
>The following code just do nothing (I thought this would do it):
>while 1:
>...
>    pygame.event.pump()
>    keys = pygame.key.get_pressed()
>    if K_LEFT in keys:
>        ship.move_left()
>..
>
>When I print out 'keys', I see correct output since there is a 1 in the list.
>
>
>This code works, but I have to press the direction several
>times to make the ship fly (one press, one pixel):
>
>while 1:
>...
>for event in pygame.event.get():
>    if event.type == KEYDOWN:
>        if event.key == K_LEFT:
>            ship.move_left()
>...
>
>
>Thanks for you help!
>Kai