引用以前的帧值Python Pygam

2022-08-10 13:06:10 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一段非常简单的代码。当程序以60 fps的速度循环时,我想做的是引用之前的鼠标点击状态。i、 e.我将“一”作为鼠标单击状态的变量,0表示未单击,1表示已单击。我希望发生的是,如果鼠标当前被单击,即1=1&;前一个值为0,即未勾选,然后保存作为鼠标坐标的mx和my的值。请参阅下面的代码:

PDimageFull = pygame.image.load('F:\Project files\coils\PDsinwaveResize.jpg')
PDresX = 300
PDresY = 720

gameDisplay = pygame.display.set_mode((Display_Width,Display_Height))


pygame.display.set_caption('PD diagnostic tool')
clock = pygame.time.Clock()

def PDimage(x, y):
    gameDisplay.blit(PDimageFull, (x, y))

# Defining our main programing loop

def mainProgram_loop():
    dx1 = (Display_Width-PDresX)
    dy1 = (Display_Height-PDresY)



    gameExit = False

# Event handling
    while not gameExit:
        mx, my = pygame.mouse.get_pos()
        one, two, three = pygame.mouse.get_pressed()
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                gameExit = True

            # This expression controls the movement of the overall PD graph
            # The section in the IF statement defines the boundary by which you can move the object
            if one == 1 and mx > dx1 and mx < dx1 + PDresX and my > dy1 and my < dy1+PDresY:

                dx1 = dx1 + (mx - PDresX)
                dy1 = dy1 + (my - PDresY)

        gameDisplay.fill(white)
        PDimage(dx1, dy1)
        pygame.display.update()
        clock.tick(60)


mainProgram_loop()

Tags: andtheloopmydisplay鼠标pygamemxgamedisplaygameexitdx1dy1pdresxpdresy
1条回答
网友
1楼 ·

只需使用另一个变量:

prev_one = one
one, two, three = pygame.mouse.get_pressed()

if prev_one == 0 and one == 1:
    print 'mouse was clicked this frame'

请注意,您必须在脚本开始时使用默认值初始化one