1

My code consists of a joystick PyGame module that updates the variable self.mmcount when the left stick's vertical axis is moved. My issue is that it updates the mmcount variable too frequently that it's very hard to get to a specific integer. I presume the solution is to add a delay to the IF statements. For example, every two seconds check if the left stick is pointing up.

These are the IF statements that update the self.mmcount variable:

if joy1.get_button(3) == 1:
    self.mmcount -= 1
if joy1.get_button(2) == 1:
    self.mmcount += 1

Entire code:

class Menu:
    def run(self):
        self.intro = True
        self.clock = clock
        self.mmcount = 1
            while self.intro:
                self.get_joys()

    def get_joys(self):
        if joy1.get_button(3) == 1:
            self.mmcount -= 1
        elif joy1.get_button(2) == 1:
            self.mmcount += 1

        if self.mmcount > 3:
            self.mmcount = 3
        elif self.mmcount < 1:
            self.mmcount = 1

m = Menu()
while True:
    m.run()

1 Answers1

0

You need a timer to control the speed. I just use the delta time (self.dt) that clock.tick returns here to increment the self.mmcount_timer variable. After 0.2 seconds have passed, I increment the self.mmcount and reset the timer.

By the way, you can clamp the value in this way: self.mmcount = max(min(30, self.mmcount), 1).

import pygame as pg


pg.init()
screen = pg.display.set_mode((640, 480))
clock = pg.time.Clock()
BG_COLOR = pg.Color('gray12')
joysticks = [pg.joystick.Joystick(x) for x in range(pg.joystick.get_count())]
for joystick in joysticks:
    joystick.init()


class Menu:

    def run(self):
        self.intro = True
        self.clock = clock
        self.mmcount = 1
        self.mmcount_timer = 0
        self.dt = 0

        while self.intro:
            for event in pg.event.get():
                if event.type == pg.QUIT:
                    self.intro = False
            self.get_joys()

            screen.fill(BG_COLOR)
            pg.display.flip()
            self.dt = clock.tick(60) / 1000

    def get_joys(self):
        if len(joysticks) >= 1:
            if joysticks[0].get_button(3):
                # Increment the timer variable.
                self.mmcount_timer += self.dt
                # If 0.2 seconds have passed.
                if self.mmcount_timer >= .2:
                    # Decrement the count variable.
                    self.mmcount -= 1
                    # And reset the timer.
                    self.mmcount_timer = 0
            elif joysticks[0].get_button(2):
                self.mmcount_timer += self.dt
                if self.mmcount_timer >= .2:
                    self.mmcount += 1
                    self.mmcount_timer = 0
            # Clamp the value between 1 and 30.
            self.mmcount = max(min(30, self.mmcount), 1)
            # Set the title to the mmcount.
            pg.display.set_caption(str(self.mmcount))


Menu().run()
pg.quit()
skrx
  • 19,980
  • 5
  • 34
  • 48