May 24, 2019, 11:17:51 PM

Author Topic: Python moving a sprite  (Read 102 times)

Offline Pfaber11

  • Full Member
  • ***
  • Posts: 181
Python moving a sprite
« on: May 15, 2019, 07:47:17 AM »
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 .

Offline Derron

  • Hero Member
  • *****
  • Posts: 2087
Re: Python moving a sprite
« Reply #1 on: May 15, 2019, 09:47:01 AM »
Quote
it 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

Offline therevills

  • Hero Member
  • *****
  • Posts: 540
Re: Python moving a sprite
« Reply #2 on: May 15, 2019, 11:04:03 AM »
Have you looked at the example from pygame?

https://github.com/xamox/pygame/blob/master/examples/aliens.py

Offline Pfaber11

  • Full Member
  • ***
  • Posts: 181
Re: Python moving a sprite
« Reply #3 on: May 15, 2019, 12:15:47 PM »
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 .

Offline Derron

  • Hero Member
  • *****
  • Posts: 2087
Re: Python moving a sprite
« Reply #4 on: May 15, 2019, 12:54:59 PM »
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)
  1.         #get input
  2.         for event in pygame.event.get():
  3.             if event.type == QUIT or \
  4.                 (event.type == KEYDOWN and event.key == K_ESCAPE):
  5.                     return
  6.         keystate = pygame.key.get_pressed()
  7.  
  8. #[...]
  9.  
  10.         #handle player input
  11.         direction = keystate[K_RIGHT] - keystate[K_LEFT]
  12.  

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

Offline Pfaber11

  • Full Member
  • ***
  • Posts: 181
Re: Python moving a sprite
« Reply #5 on: May 15, 2019, 01:09:21 PM »
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.

Offline Pfaber11

  • Full Member
  • ***
  • Posts: 181
Re: Python moving a sprite
« Reply #6 on: May 15, 2019, 01:43:57 PM »
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


Offline Derron

  • Hero Member
  • *****
  • Posts: 2087
Re: Python moving a sprite
« Reply #7 on: May 15, 2019, 02:02:02 PM »
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

Offline Pfaber11

  • Full Member
  • ***
  • Posts: 181
Re: Python moving a sprite
« Reply #8 on: May 15, 2019, 03:14:27 PM »
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.