Inteyerry
Inteyerry

Reputation: 41

How to make two mouse clicks not interact with each other?

I'm using the pygame library and I need to create some buttons. This is a part of my code:

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

When it is running and I click the button 'bStart', the game switches directly to the interface3, because 'bStart' and 'bPlay' are in the same position.

The premise is not moving the two buttons or deleting an interface. For some reason, I have to keep them.

Upvotes: 1

Views: 63

Answers (1)

skrx
skrx

Reputation: 20438

I think the simplest solution would be to remove the pygame.mouse.get_pressed()[0] code, because it continuously checks if the mouse button is pressed, and use event.type == pygame.MOUSEBUTTONDOWN instead in the event loop (then a mouse click generates only 1 MOUSEBUTTONDOWN event).

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()

Upvotes: 1

Related Questions