如何使两次鼠标点击不相互影响?

时间:2017-08-11 09:42:19

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

我正在使用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

当它运行时,我点击按钮&#39; b开始&#39;,游戏直接切换到界面3,因为&#39; b开始&#39;和&#39; bPlay&#39;处于相同的位置。

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

1 个答案:

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