【问题标题】:How to make two mouse clicks not interact with each other?如何使两次鼠标单击不相互交互?
【发布时间】:2017-08-11 09:42:18
【问题描述】:

我正在使用 pygame 库,我需要创建一些按钮。这是我的代码的一部分:

import pygame
pygame.init()
screenw=1000  ;screenh=550
screen = pygame.display.set_mode(( screenw ,  screenh ))
interface=1

class Button(object):
    def __init__(self, aroundimage, onimage,position):
        self.imageAround = pygame.image.load(aroundimage).convert_alpha()
        self.imageOn = pygame.image.load(onimage).convert_alpha()
        self.position = position

    def isOn(self):
        point_x,point_y = pygame.mouse.get_pos()
        x, y = self. position
        w, h = self.imageAround.get_size()

        in_x = x - w/2 < point_x < x + w/2
        in_y = y - h/2 < point_y < y + h/2
        return in_x and in_y

    def render(self):
        w, h = self.imageAround.get_size()
        x, y = self.position

        if self.isOn():
            screen.blit(self.imageOn, (x-w/2, y-h/2))
        else:
            screen.blit(self.imageAround, (x-w/2, y-h/2))



bStart = Button(r'pic\button\startAround.png',r'pic\button\startOn.png',(500,450))
bPlay = Button(r'pic\button\playAround.png',r'pic\button\playOn.png',(500,450))
button_choiceR=Button(r'pic\button\rightAround.png',r'pic\button\rightOn.png',(650,225))

KeepGoing=True
while KeepGoing:
    screen.blit(background,(0,0))
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            KeepGoing = False
    if interface==1:
        bStart.render()
        if bStart.isOn() and pygame.mouse.get_pressed()[0]:
            face=2
    if interface==2:
        bPlay.render()
        if bPlay.isOn() and pygame.mouse.get_pressed()[0]:
            face=3
        if button_choiceR.isOn() and pygame.mouse.get_pressed()[0]:
            playing=True
    if interface==3:
        if playing:
            pass #other code

当它运行时,我点击“bStart”按钮,游戏直接切换到界面3,因为“bStart”和“bPlay”在同一个位置。

前提是不移动两个按钮或删除一个界面。出于某种原因,我必须保留它们。

【问题讨论】:

  • 所以bStartbPlay 按钮应该在同一个位置,但只有bStart 应该是活动的?

标签: python python-3.x class pygame imagebutton


【解决方案1】:

我认为最简单的解决方案是删除 pygame.mouse.get_pressed()[0] 代码,因为它会不断检查是否按下了鼠标按钮,并在事件循环中使用 event.type == pygame.MOUSEBUTTONDOWN 代替(然后鼠标单击仅生成 1 MOUSEBUTTONDOWN事件)。

import sys
import pygame


pygame.init()
screen = pygame.display.set_mode((1000, 550))
interface = 1


class Button(object):

    def __init__(self, aroundimage, onimage, position):
        self.imageAround = aroundimage
        self.imageOn = onimage
        self.position = position

    def isOn(self):
        point_x, point_y = pygame.mouse.get_pos()
        x, y = self.position
        w, h = self.imageAround.get_size()

        in_x = x - w/2 < point_x < x + w/2
        in_y = y - h/2 < point_y < y + h/2
        return in_x and in_y

    def render(self):
        w, h = self.imageAround.get_size()
        x, y = self.position

        if self.isOn():
            screen.blit(self.imageOn, (x-w/2, y-h/2))
        else:
            screen.blit(self.imageAround, (x-w/2, y-h/2))


bStart_img = pygame.Surface((50, 50))
bStart_img.fill((70, 30, 160))
bPlay_img = pygame.Surface((50, 50))
bPlay_img.fill((170, 30, 160))
button_choiceR_img = pygame.Surface((50, 50))
button_choiceR_img.fill((70, 230, 160))
bStart_img2 = pygame.Surface((50, 50))
bStart_img2.fill((70, 30, 220))
bPlay_img2 = pygame.Surface((50, 50))
bPlay_img2.fill((220, 30, 220))
button_choiceR_img2 = pygame.Surface((50, 50))
button_choiceR_img2.fill((70, 230, 220))

bStart = Button(bStart_img, bStart_img2, (500, 450))
bPlay = Button(bPlay_img, bPlay_img2, (500, 450))
button_choiceR = Button(button_choiceR_img, button_choiceR_img2, (650,225))


clock = pygame.time.Clock()
KeepGoing = True

while KeepGoing:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            KeepGoing = False
        elif event.type == pygame.MOUSEBUTTONDOWN:
            if event.button == 1:
                if bStart.isOn() and interface == 1:
                    interface = 2
                elif bPlay.isOn() and interface == 2:
                    interface = 3
                elif button_choiceR.isOn() and interface == 3:
                    playing = True

    screen.fill((30, 30, 30))

    if interface == 1:
        bStart.render()
    elif interface == 2:
        bPlay.render()
    elif interface == 3:
        button_choiceR.render()

    pygame.display.flip()
    clock.tick(30)

pygame.quit()
sys.exit()

【讨论】:

    猜你喜欢
    • 2017-07-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-24
    • 1970-01-01
    • 2016-10-25
    • 2014-02-26
    • 1970-01-01
    相关资源
    最近更新 更多