Python Forum
Using Metulburr's state machine, having a lag issue with events
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Using Metulburr's state machine, having a lag issue with events
#1
Hi, I am using Metulburr's state machine for a project. I have an event that is lagging when NOT coded directly into the Control class event handler..

My code is as follows:

def get_event(self, event):
    keys = pg.key.get_pressed()
    mouse = pg.mouse.get_pos() 


    if keys[pg.K_a]:
        main_char.moveLeft(5), point.moveLeft(5)
    elif keys[pg.K_d]:
        main_char.moveRight(5), point.moveRight(5)
    if keys[pg.K_w]:
        main_char.moveUp(5), point.moveUp(5)
    elif keys[pg.K_s]:
        main_char.moveDown(5), point.moveDown(5)
        
    if keys[pg.K_SPACE]:
        self.ammo -= 1
        chooser = choice(gunshots)
        pg.mixer.Sound.play(chooser)
        collider = pg.sprite.groupcollide(dot_sprites_list, villain_sprites_list,               
                                          dokilla=False,dokillb=False)
        if collider:
            self.health -= 1
        if self.health == 0:
            collider = pg.sprite.groupcollide(dot_sprites_list, villain_sprites_list,                        
                                              dokilla=False,dokillb=True)
        elif event.type == spawn:
            testing_jack.move_pos()
When the event is in the Control class's event method, it runs smoothly. If it is as shown above, in the class which inherits from the States class, it lags and is not smooth. And just to be clear, what I mean by lag is that the sprite, controlled by the w,a,s,d keys, moves very slow and takes a second to move, as opposed to being instant when in the Control class's event handler. I'd just leave it there, but it will cause problems like that.

Any ideas? Please let me know if I can clear anything else up. Thanks!
Reply
#2
All that game event stuff should be in the events section of Game class.

Use the pygame.events.get()once per frame, in the Control class. Then it gets handed to the state's event section for processing. Look in the original template. Control gathers the events, then the individual states are coded to handle what those events do in that state.
Reply
#3
(Dec-09-2020, 05:43 AM)michael1789 Wrote: All that game event stuff should be in the events section of Game class.

Use the pygame.events.get()once per frame, in the Control class. Then it gets handed to the state's event section for processing. Look in the original template. Control gathers the events, then the individual states are coded to handle what those events do in that state.

Thanks for the reply.

That game event stuff is in the events section of that state (it is gameplay after the menu screen).
I have been using the States class as the parent to all of my game frames, be it a menu or a gameplay part... Is that not correct? The event is in that game's class, which is passed to States/Control for processing... Is there something I am not getting here?
Reply
#4
If you look in the Control events section you will see:

def event_loop(self):
        for event in pg.event.get():
            if event.type == pg.QUIT:
                self.done = True
            self.state.get_event(event)
It's a for loop. So it grabs events once, and then loops through each event sending it one at a time to the specific active state (eg, menu, game, etc). However, in your Game.get_event, you use get_pressed() as well. So you aren't using the event that Control is sending to Game.

Here is how I normally deal with it.

def get_event(self, event, dt):

    if event.type == pg.KEYDOWN:
               
                if event.key == pg.K_LEFT:
                    self.player.go_left()
                    self.player.facing = "left"
                 
                if event.key == pg.K_RIGHT:
                    self.player.go_right()
                    self.player.facing = "right"
                  
Reply
#5
I have changed the code and removed the get_pressed(). It works, however now the cursor only moves on each keystroke. It does not scroll while the key is held down, which to my understanding requires a boolean value to be used, like...

while left:
    self.moveLeft()
any way to fix this while retaining the correct form?

Another thing was that my get_event() did not have dt as a param like yours did in the example. I didn't see that in the state machine.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  wait for the first of these events Skaperen 4 1,873 Mar-07-2022, 08:46 PM
Last Post: Gribouillis
  How to get continuous movement whilst using State Machine cjoe1993 2 1,789 Dec-10-2020, 06:36 AM
Last Post: cjoe1993
  State graph Kaluss 1 2,197 Mar-18-2019, 05:29 PM
Last Post: nilamo
  Simulating events using Hawkes akshit2291 1 2,115 Sep-25-2018, 04:17 AM
Last Post: Larz60+
  win32com Events not catching dageci 0 3,720 Aug-06-2018, 03:18 PM
Last Post: dageci

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020