Python moving a sprite

Started by Pfaber11, May 15, 2019, 07:47:17

Previous topic - Next topic

Pfaber11

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 .
HP 15s i3 1.2 upto 3.4 ghz 128 gb ssd 16 gb ram 15.6 inch screen. Windows 11 home edition .  2Tb external hard drive dedicated to Linux Mint .
  PureBasic 6 and AppGameKit studio
ASUS Vivo book 15 16gb ram 256gb storage  cpu upto 4.1 ghz

Derron

Quoteit just doesn't move very well.


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


bye
Ron

therevills


Pfaber11

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
                 screen.blit(title,(0,0))   
                 screen.blit(b.image1,(x,y))
                 pygame.display.update() 



Hope this helps . The code does work but it only registers maybe 1 in five keystrokes .
HP 15s i3 1.2 upto 3.4 ghz 128 gb ssd 16 gb ram 15.6 inch screen. Windows 11 home edition .  2Tb external hard drive dedicated to Linux Mint .
  PureBasic 6 and AppGameKit studio
ASUS Vivo book 15 16gb ram 256gb storage  cpu upto 4.1 ghz

Derron

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 (Python) Select

        #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:
https://www.pygame.org/docs/ref/key.html
or even:
https://www.pygame.org/docs/ref/key.html#pygame.key.set_repeat

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.


bye
Ron

Pfaber11

Hi Ron excellent advice. That key repeat really helped . Been stuck on this for the last 2 days and tried everything I could think of .Thank you.
HP 15s i3 1.2 upto 3.4 ghz 128 gb ssd 16 gb ram 15.6 inch screen. Windows 11 home edition .  2Tb external hard drive dedicated to Linux Mint .
  PureBasic 6 and AppGameKit studio
ASUS Vivo book 15 16gb ram 256gb storage  cpu upto 4.1 ghz

Pfaber11

Just been reading up about key repeat key and it's turned off when pygame is initialized. I've been spending the last 2 days on this . Oh well it's something I'll never forget . Thanks again for your help. And for anybody thinking about learning python I would recommend it . I'm finding it no harder to learn than AGK2 . I've only been learning Python 9 days and I feel like I've come on quite well in that time . Thanks again Ron

HP 15s i3 1.2 upto 3.4 ghz 128 gb ssd 16 gb ram 15.6 inch screen. Windows 11 home edition .  2Tb external hard drive dedicated to Linux Mint .
  PureBasic 6 and AppGameKit studio
ASUS Vivo book 15 16gb ram 256gb storage  cpu upto 4.1 ghz

Derron

Hope I just misunderstood you ... YOU SHOULD NOT USE the set_repeat thing!

Do as they do in the example - and what I posted as snippet:
- have a main loop
- fetch an array containing the "pressed" state of all keys (keystate = pygame.key.get_pressed())
- handle your stuff according to true/false in the array

Else you _will_ run into trouble of skipped keystrokes and whatever.
I somehow think that your programming knowledge is on a level which requires a lot of reading, trying out and finding out stuff. Code little snippets instead of a complete game. Else you are having a 100% chance to run into the situation at which you have to rewrite some critical/main logic in your application and then you might rage quit for "being a complex language" or thinking of "this doesn't work".

Python is not a complex (but yet) mighty language and I am pretty sure that pygame is pretty mature too - so if stuff isn't working properly, or movement is jerky or whatever - then it is most probably your fault, not the one of Python and pygame.


Also: make sure that all you need in your "game" (or application) is possible with the engine / language of your choice:
- need android? check if you can export it (simple draw "hello world" app)
- need sound? Play it
- need streamed sound? Play it
- need 1000 images on screen at the same time? Draw them
- ...


bye
Ron

Pfaber11

Yes Ron thanks for the advice . Got sound and graphics working now .  Nothing like learning a new language when things aren't going too bad . Enjoy your day.
HP 15s i3 1.2 upto 3.4 ghz 128 gb ssd 16 gb ram 15.6 inch screen. Windows 11 home edition .  2Tb external hard drive dedicated to Linux Mint .
  PureBasic 6 and AppGameKit studio
ASUS Vivo book 15 16gb ram 256gb storage  cpu upto 4.1 ghz