引用前面的帧值Python Pygame

时间:2014-12-02 01:37:00

标签: python pygame

我有一段非常简单的代码。程序循环为60 fps时我想做的是我想引用之前的鼠标点击状态。即我有'one'作为鼠标点击状态的变量,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()

1 个答案:

答案 0 :(得分: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