【问题标题】:How to make MOUSEBUTTONDOWN to only detect the left clicks? [duplicate]如何让 MOUSEBUTTONDOWN 只检测左键点击? [复制]
【发布时间】:2021-05-19 19:59:48
【问题描述】:

MOUSEBUTTONDOWN 检测 Left、Right 和 MouseWheel 事件。我想让它只检测左键。

源代码:

import pygame, sys, time
from pygame.locals import *
from millify import millify,prettify

pygame.mixer.init()
pygame.init()
pygame.mixer.music.load("soundtrack.wav")
WHITE = 255,255,255
font = pygame.font.SysFont(None, 44)
cpsecond = open("clickpersecond.txt", "r+")
cps = int(cpsecond.read())
baltotal = open("totalbal.txt", "r+")
totalbal = int(baltotal.read())
totalbalM = prettify(totalbal, '.')
clock = pygame.time.Clock()
w = 800
h = 600
screen = pygame.display.set_mode((w,h))
pygame.display.set_caption('Tap Simulator')
Loop = True
background = pygame.image.load("Background.jpg")
egg = pygame.image.load("egg.png")
resized_egg = pygame.transform.scale(egg, (282, 352))
text = font.render(f'Your total clicks are {totalbalM}', True, WHITE)
pygame.mixer.music.play(-1,0.0)
while Loop: # main game loop
    for event in pygame.event.get():
        if event.type == QUIT:
            Loop = False


        if event.type == MOUSEBUTTONDOWN: #I want it to only detect left click
            egg_rect = resized_egg.get_rect(topleft = (260,150))
            if egg_rect.collidepoint(event.pos):
                totalbal += cps
                totalbalM = prettify(totalbal, '.')
                text = font.render(f'Your total clicks are {totalbalM}', True, WHITE)
                print("Your total clicks are", totalbalM, end="\r")

    #print(pygame.mouse.get_pos()) #to get mouse pos
    screen.blit(background, (0,0))
    screen.blit(text, (235,557))
    screen.blit(resized_egg, (260,150))
    pygame.display.flip()
    pygame.display.update()
    clock.tick(30)

with open("totalbal.txt", "w") as baltotal:
    baltotal.write(str(totalbal))
baltotal.close

pygame.quit()
sys.exit()

【问题讨论】:

    标签: python python-3.x pygame


    【解决方案1】:

    MOUSEBUTTONDOWN 生成的pygame.event.Event() 对象具有两个提供有关鼠标事件信息的属性。 pos 是一个存储被点击位置的元组。 button 存储被点击的按钮。 button 属性的值为 1, 2, 3, 4, 5 分别代表鼠标左键、鼠标中键、鼠标右键、鼠标滚轮向上和鼠标滚轮向下。

    因此,如果你想检测左键点击,你需要检查event.button == 1

    while Loop: # main game loop
        for event in pygame.event.get():
            if event.type == QUIT:
                Loop = False
    
    
            if event.type == MOUSEBUTTONDOWN: #I want it to only detect left click
       
                if event.button == 1: # 1 == left 
    
                    egg_rect = resized_egg.get_rect(topleft = (260,150))
                    if egg_rect.collidepoint(event.pos):
                        totalbal += cps
                        totalbalM = prettify(totalbal, '.')
                        text = font.render(f'Your total clicks are {totalbalM}', True, WHITE)
                        print("Your total clicks are", totalbalM, end="\r")
    
        # [...]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-07-21
      • 1970-01-01
      • 2021-11-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多