ark.pytm
ark.pytm

Reputation: 55

While loop in pygame

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?

Upvotes: 3

Views: 3376

Answers (1)

OSA413
OSA413

Reputation: 476

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) )

Upvotes: 3

Related Questions