Languages & Coding > Others

Python moving a sprite

(1/2) > >>

Could anybody tell me the best way of moving a sprite in Python. I'm using pygame but would be happy to install another module . It's not that I can't get the sprite to move it just doesn't move very well . Been playing with code for ma couple of days on this problem so figured I'd ask .


--- Quote ---it just doesn't move very well.
--- End quote ---

This is the perfect reason to post a piece of code to see how you coded your movement.


Have you looked at the example from pygame?

Yes I just looked at the example . Here's an piece of my code .
def left1():
    global x
    global y
    for event in pygame.event.get():
        key = pygame.key.get_pressed()
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:   
                 x = x - 1

Hope this helps . The code does work but it only registers maybe 1 in five keystrokes .

First of all: wrap your code in {code}...yourcode...{/code} blocks (use the "hash"-icon in the post-form-toolbar).

Now to your problem: you expect it to send out a "keydown" event every frame?
Did you check the example therevills linked for you?

I did:

--- Code:  (Unknown Language) ---        #get input        for event in pygame.event.get():            if event.type == QUIT or \                (event.type == KEYDOWN and event.key == K_ESCAPE):                    return        keystate = pygame.key.get_pressed() #[...]         #handle player input        direction = keystate[K_RIGHT] - keystate[K_LEFT] 
See the difference?
As said the difference is that you only handle on "keydown" events (which is ... only happening when hitting the key initially - or if repeating keys was set).

For more check this:
or even:

So if there is some code line in your project which enables "set_repeat" and eg. sets it to 100, then it will emit an "KEYDOWN" event every 100ms as long as you kept your key pressed. Else only a single KEYDOWN event is emit - when you intially started pressing the key.



[0] Message Index

[#] Next page

Go to full version