为什么我的pygame几秒钟后显示没有响应?

时间:2020-07-08 22:11:15

标签: python automation pygame

请澄清一下,我绝对不知道自己在做什么,实际上我真的不知道如何做到这一点。我写了一些代码来充当Minecraft农场的自动点击器(是的,我知道您可以使用更简单的方法)。我从各种教程中学到了点点滴滴,并且自己做了一些。有点混乱。但是,一旦代码运行并第一次单击,pygame窗口将显示无响应。我不知道为什么。我将不胜感激任何建议和技巧,如果我敢打赌解决此问题,那就太好了,谢谢!

我认为有些代码不知道如何很好地使用它:(

    import pygame
    import keyboard
    import time
    import pyautogui
    pygame.init()

    def Click():
        pyautogui.doubleClick(None, None, 1)
        print('Click')
        time.sleep(3)

    def Img(x, y):
        display_surface.blit(Piglin_Img, (x, y))


    white = (255, 255, 255)
    black = (0, 0, 0)

    x = 280
    y = 10
    X = 400
    Y = 100
    Piglin_Img = pygame.image.load('Piglin.png')
    pygame.display.set_icon(pygame.image.load("Icon.png"))
    display_surface = pygame.display.set_mode((X, Y ))
    pygame.display.set_caption('Gold Farm Auto Clicker')
    font = pygame.font.Font('freesansbold.ttf', 20)


    Start_Text = font.render('Press p to start...', True, black, white)
    Run_Text = font.render('Running...', True, black, white)
    Pause_Text = font.render('Paused', True, black, white)

    Start_Text_pos = (10, 10)
    Run_Text_pos = (10, 10)
    Pause_Text_pos = (10, 10)
    Continue_pos = (10, 40)

    display_surface.fill(white)
    display_surface.blit(Start_Text, Start_Text_pos)

    Clicker = False
    running = True

    while running:
     for event in pygame.event.get():
      Img(x, y)

      pygame.event.set_blocked(pygame.MOUSEMOTION)
      pygame.event.set_blocked(pygame.MOUSEBUTTONDOWN)
      pygame.event.set_blocked(pygame.MOUSEBUTTONUP)

       if event.type == pygame.QUIT or \
          event.type == pygame.KEYDOWN and \
          event.key == pygame.K_z:
            running = False

      if event.type == pygame.KEYDOWN and \
         event.key == pygame.K_p:
         print('started')
         display_surface.fill(white)
         display_surface.blit(Run_Text, Run_Text_pos)
         Img(x, y)
         pygame.display.update()
         Clicker = True


     while Clicker:
         if event.type == pygame.KEYDOWN and \
            event.key == pygame.K_x:
            Clicker = False
            display_surface.fill(white)
            display_surface.blit(Start_Text, Continue_pos)
            display_surface.blit(Pause_Text, Pause_Text_pos)
            Img(x, y)
            pygame.display.update()

        Click()
        if event.type == pygame.KEYDOWN and \
            event.key == pygame.K_x:
            Clicker = False
            display_surface.fill(white)
            display_surface.blit(Start_Text, Continue_pos)
            display_surface.blit(Pause_Text, Pause_Text_pos)
            Img(x, y)
            pygame.display.update()



 pygame.display.update()
pygame.quit()

1 个答案:

答案 0 :(得分:1)

Clicker变为True时,它将阻止事件队列处理。因此,一旦答题器开始循环播放,就不会再处理用户输入,因为此循环永远不会重新检查队列中是否有新事件,而只会继续重新处理相同的(旧)事件结果。

您可能需要将while Clicker原因中的事件处理合并到主事件循环中。也许在这些事件上带有if Clicker

pygame.event.set_blocked(pygame.MOUSEMOTION)
pygame.event.set_blocked(pygame.MOUSEBUTTONDOWN)
pygame.event.set_blocked(pygame.MOUSEBUTTONUP)

while running:
    # handle events and user interaction
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

        if event.type == pygame.KEYDOWN :    # some key was pushed
            if event.key == pygame.K_p:
                print('started')
                Clicker = True               # start the clicker
            elif event.key == pygame.K_x:
                print('stopped')
                Clicker = False              # stop the clicker
            elif event.key == pygame.K_z:
                running = False              # Allow exit here too  


    # Update the screen
    if Clicker:
        # Click mode
        display_surface.fill(white)
        display_surface.blit(Run_Text, Run_Text_pos)
    else:
        # NOT in Click Mode
        display_surface.fill(white)
        display_surface.blit(Start_Text, Continue_pos)
        display_surface.blit(Pause_Text, Pause_Text_pos)

    Img(x, y)
    pygame.display.update()

无论如何都差不多。没有注释,就无法立即弄清楚代码的目的是什么,因此很难创建严格的解决方案。