3

I'm trying to add PS4 input to my python code, so I wanted to make it whenever I'm holding down a button, it prints as long as it held down for, not just the one time. I tried many different variations of while loops but it just spams my console with text so I know im doing something wrong. Any help would be appreciated.

import pygame


BLACK    = (   0,   0,   0)
WHITE    = ( 255, 255, 255)


class TextPrint:
    def __init__(self):
        self.reset()
        self.font = pygame.font.Font(None, 25)

    def print(self, screen, textString):
        textBitmap = self.font.render(textString, True, BLACK)
        screen.blit(textBitmap, [self.x, self.y])
        self.y += self.line_height

    def reset(self):
        self.x = 30
        self.y = 30
        self.line_height = 20

    def indent(self):
        self.x += 10

    def unindent(self):
        self.x -= 10


pygame.init()


size = [800, 500]
screen = pygame.display.set_mode(size)

pygame.display.set_caption("My Game")

done = False

clock = pygame.time.Clock()

pygame.joystick.init()

textPrint = TextPrint()

while done==False:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done=True 


        if event.type == pygame.JOYBUTTONDOWN:
            print("Joystick button pressed.")
        if event.type == pygame.JOYBUTTONUP:
            print("Joystick button released.")


    screen.fill(WHITE)
    textPrint.reset()

    joystick_count = pygame.joystick.get_count()

    for i in range(joystick_count):
        joystick = pygame.joystick.Joystick(i)
        joystick.init()

        name = joystick.get_name()
        textPrint.print(screen, "Joystick name: {}".format(name) )

        buttons = joystick.get_numbuttons()
        textPrint.print(screen, "Number of buttons: {}".format(buttons) )
        textPrint.indent()

        for i in range( buttons ):
            button = joystick.get_button( i )
            textPrint.print(screen, "Button {:>2} value: {}".format(i,button) )
        textPrint.unindent()

        pygame.display.flip()

clock.tick(20)

pygame.quit ()     


Modified code from official pygame documentation
Also a side question, but its not priority: How would i know exactly which button is being pressed and use it in an if statement?

1

1 Answer 1

3

Look closely at this block:

for i in range( buttons ):
    button = joystick.get_button( i )
    textPrint.print(screen, "Button {:>2} value: {}".format(i,button) )

textPrint.print draws text with the button ID (i) and its statement (button) (0 is released, 1 is pressed). So, if you need to print some text while a button is pressed, simply add this:

if button == 1:
    print("Button "+str(i)+" is pressed")

to the block and it should work.

btw, you can use i (button ID) of this cycle to use in an if statement.

if button == 1:
    if i == 2:
        print("A is pressed")
    elif i == 1:
        print("B is pressed")

That's how the block may look after all:

for i in range( buttons ):
    button = joystick.get_button( i )
    if button == 1: #if any button is pressed
        if i == 2:  #if A button is pressed
            print("A is pressed")
        if i == 1:  #if B button is pressed
            print("B is pressed")
    textPrint.print(screen, "Button {:>2} value: {}".format(i,button) )
Sign up to request clarification or add additional context in comments.

1 Comment

This was exactly what I was looking for and it works really well, I just added some delays and it does it's job, thank you so much

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.