Python Forum

Full Version: how to mouse click a specific item in pygame?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
hello,

I want to mouse click a specific item.
When you click Activé which is button_A this line appear: img = font.render("Veuillez entrer votre code pour armer le systeme d'alarme!", True, BLANC)
Edit:
i am now able to click at activé but text of button A appear over B

 # -*- coding: utf-8 -*-

import pygame
from pygame.locals import *
import sys
 
pygame.init()
display = pygame.display.set_mode((300, 300))
FPS_CLOCK = pygame.time.Clock()
#pygame.init()

BLEU = (0, 0, 255)
BLANC = (255, 255, 255)
ROUGE = (255, 0, 0)
GREEN = (0, 255, 0)
BLACK = (0, 0, 0)
GRIS = (200, 200, 200)
BLANC = (255, 255, 255)
NOIR = (0, 0, 0)

screen = pygame.display.set_mode([700, 500])

# Charger un police de caract�re
sysfont = pygame.font.get_default_font()
font = pygame.font.SysFont(None, 48)

def bouton_A(screen, pos, texte):
    rayon = 55
    pygame.draw.circle(screen, GREEN, pos, rayon)
    txtBtn = font.render(texte, True, NOIR)
    rectBtn = txtBtn.get_rect()
    rectBtn.center = (pos)
    screen.blit(txtBtn, rectBtn)
    
def bouton_D(screen, pos, texte):
    rayon = 55
    pygame.draw.circle(screen, ROUGE, pos, rayon)
    txtBtn = font.render(texte, True, NOIR)
    rectBtn = txtBtn.get_rect()
    rectBtn.center = (pos)
    screen.blit(txtBtn, rectBtn)
    
def bouton_nb(screen, pos, texte):
    rayon = 25
    pygame.draw.circle(screen, GRIS, pos, rayon)
    txtBtn = font.render(texte, True, NOIR)
    rectBtn = txtBtn.get_rect()
    rectBtn.center = (pos)
    screen.blit(txtBtn, rectBtn)

sysfont = pygame.font.get_default_font()
font = pygame.font.SysFont(None, 36)



running = True

while running:
   
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        if event.type == pygame.MOUSEBUTTONDOWN:
            x , y = pygame.mouse.get_pos()
            print(x,y)
            if x < 185 and y < 285:
               img = font.render("Veuillez entrer votre code pour armer le systeme d'alarme!", True, BLANC)
               screen.blit(img, (20, 100))
          
            if x < 538 and y < 292:
               img = font.render("Entrez votre code pour d�sarmer le systeme d'alarme!", True, BLANC)
               screen.blit(img, (20, 100))
          
          
    #screen.fill(BLANC)
    
    bouton_A(screen, (150, 250), "Activ�")
    bouton_D(screen, (500, 250), "D�sactiv�")
    bouton_nb(screen, (265, 200), "1")
    bouton_nb(screen, (320, 200), "2")
    bouton_nb(screen, (375, 200), "3")
    bouton_nb(screen, (265, 255), "4")
    bouton_nb(screen, (320, 255), "5")
    bouton_nb(screen, (375, 255), "6")
    bouton_nb(screen, (265, 310), "7")
    bouton_nb(screen, (320, 310), "8")
    bouton_nb(screen, (375, 310), "9")
    
    
    
    #pygame.draw.circle(screen, GRIS, (260, 250), 25)
    pygame.display.flip()

    img = font.render("Bienvenue", True, ROUGE)
    screen.blit(img, (20, 20))
    


    pygame.display.flip()
    
    pygame.display.update()
    FPS_CLOCK.tick(30)

    
pygame.quit()
    
Thank you
You need to erase the old text. You can do this by drawing a black rectangle over the text, or by redrawing the entire screen.
i have found a quick solution, not pretty but it works

if x > 104 and x < 204 and y > 196 and y < 301:
               img = font.render("Entrez votre code pour d�sarmer le systeme d'alarme!", True, NOIR)
               screen.blit(img, (20, 100))
               img = font.render("Veuillez entrer votre code pour armer le systeme d'alarme!", True, BLANC)
               screen.blit(img, (20, 100))
          
            if x < 549 and x > 444 and y > 192 and y < 309:
               img = font.render("Veuillez entrer votre code pour armer le systeme d'alarme!", True, NOIR)
               screen.blit(img, (20, 100)) 
               img = font.render("Entrez votre code pour d�sarmer le systeme d'alarme!", True, BLANC)
               screen.blit(img, (20, 100))
Hello again,

I try to ask here to avoid creating another post.
Now i am trying to create a countdown clock below the numbers.
I am king of stuck to how insert the timer in my actual panel without creating a new panel.

# -*- coding: utf-8 -*-

import pygame
from pygame.locals import *
import sys
import time
clock = pygame.time.Clock()
 
pygame.init()
display = pygame.display.set_mode((300, 300))
pygame.display.set_caption('Panneau de contr�le')
FPS_CLOCK = pygame.time.Clock()


counter, text = 10, '10'.rjust(3)
pygame.time.set_timer(pygame.USEREVENT, 1000)
font = pygame.font.SysFont('Consolas', 30)
#pygame.init()

BLEU = (0, 0, 255)
BLANC = (255, 255, 255)
ROUGE = (255, 0, 0)
GREEN = (0, 255, 0)
BLACK = (0, 0, 0)
GRIS = (200, 200, 200)
BLANC = (255, 255, 255)
NOIR = (0, 0, 0)

screen = pygame.display.set_mode([700, 500])

# Charger un police de caract�re
sysfont = pygame.font.get_default_font()
font = pygame.font.SysFont(None, 48)

def bouton_A(screen, pos, texte):
    rayon = 55
    pygame.draw.circle(screen, GREEN, pos, rayon)
    txtBtn = font.render(texte, True, NOIR)
    rectBtn = txtBtn.get_rect()
    rectBtn.center = (pos)
    screen.blit(txtBtn, rectBtn)
    
def bouton_D(screen, pos, texte):
    rayon = 55
    pygame.draw.circle(screen, ROUGE, pos, rayon)
    txtBtn = font.render(texte, True, NOIR)
    rectBtn = txtBtn.get_rect()
    rectBtn.center = (pos)
    screen.blit(txtBtn, rectBtn)
    
def bouton_nb(screen, pos, texte):
    rayon = 25
    pygame.draw.circle(screen, GRIS, pos, rayon)
    txtBtn = font.render(texte, True, NOIR)
    rectBtn = txtBtn.get_rect()
    rectBtn.center = (pos)
    screen.blit(txtBtn, rectBtn)

sysfont = pygame.font.get_default_font()
font = pygame.font.SysFont(None, 36)



running = True

while running:
   
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
    
        #mouse_presses = pygame.mouse.get_pressed()
 
        #if mouse_presses[0]:
        if event.type == pygame.MOUSEBUTTONDOWN:
            x , y = pygame.mouse.get_pos()
            print(x,y)
            if x > 104 and x < 204 and y > 196 and y < 301:
               img = font.render("Entrez votre code pour d�sarmer le systeme d'alarme!", True, NOIR)
               screen.blit(img, (20, 100))
               img = font.render("Veuillez entrer votre code pour armer le systeme d'alarme!", True, BLANC)
               screen.blit(img, (20, 100))
          
            if x < 549 and x > 444 and y > 192 and y < 309:
               img = font.render("Veuillez entrer votre code pour armer le systeme d'alarme!", True, NOIR)
               screen.blit(img, (20, 100)) 
               img = font.render("Entrez votre code pour d�sarmer le systeme d'alarme!", True, BLANC)
               screen.blit(img, (20, 100))
             
            if event.type == pygame.USEREVENT: 
                counter -= 1
                text = str(counter).rjust(3) if counter > 0 else 'boom!'
            if event.type == pygame.QUIT: 
                run = False

    screen.fill((255, 255, 255))
    screen.blit(font.render(text, True, (0, 0, 0)), (32, 48))
    #pygame.display.flip()
    clock.tick(60)
    
    #screen.fill(BLANC)
    
    bouton_A(screen, (150, 250), "Activ�")
    bouton_D(screen, (500, 250), "D�sactiv�")
    bouton_nb(screen, (265, 200), "1")
    bouton_nb(screen, (320, 200), "2")
    bouton_nb(screen, (375, 200), "3")
    bouton_nb(screen, (265, 255), "4")
    bouton_nb(screen, (320, 255), "5")
    bouton_nb(screen, (375, 255), "6")
    bouton_nb(screen, (265, 310), "7")
    bouton_nb(screen, (320, 310), "8")
    bouton_nb(screen, (375, 310), "9")
    
    
    
    #pygame.draw.circle(screen, GRIS, (260, 250), 25)
    #pygame.display.flip()

    img = font.render("Bienvenue", True, ROUGE)
    screen.blit(img, (20, 20))
    


    pygame.display.flip()
    
    pygame.display.update()
    FPS_CLOCK.tick(30)

    
pygame.quit()
    
You should ask pygame questions in the pygame section.
There is a pygame section? i have to check that.