代码之家  ›  专栏  ›  技术社区  ›  ark.pytm

pygame中的While循环

  •  3
  • ark.pytm  · 技术社区  · 6 年前

    我试图将PS4输入添加到我的python代码中,所以我希望每当我按下按钮时,它都会打印,只要按住一次,而不仅仅是一次。我尝试了许多不同的while循环,但它只是向我的控制台发送文本,所以我知道我做错了什么。任何帮助都将不胜感激。

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


    官方修改代码 pygame documentation
    这也是一个附带问题,但不是重点: 我如何确切地知道正在按下哪个按钮并在if语句中使用它?

    1 回复  |  直到 6 年前
        1
  •  3
  •   OSA413    6 年前

    请仔细查看此块:

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

    textPrint.print 使用按钮ID绘制文本( i )及其声明( button )(释放0,按下1)。因此,如果您需要在按下按钮时打印一些文本,只需添加以下内容:

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

    到街区去,它应该会起作用。

    顺便说一句,你可以使用 (按钮ID)以在if语句中使用。

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

    这就是街区的样子:

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