while...: #your loop button = pygame.Rect(cordX, cordY, width, height) #your bytton pygame.draw.rect(win, (0, 0, 0), button) #show button mx, my = pygame.mouse.get_pos() #mouse pos if button.collidepoint((mx, my)): #cheak if mouse on button if pygame.mouse.get_pressed(): #cheak if mouse is preesed # writh here what you want to happend pygame.display.update()
Here is what the above code is Doing:
1. It’s creating a button with the dimensions of width=100, height=50, cordX=100, cordY=100.
2. It’s drawing the button on the screen.
3. It’s getting the mouse position.
4. It’s checking if the mouse is on the button.
5. If the mouse is on the button, it’s checking if the mouse is pressed.
6. If the mouse is pressed, it’s printing “You pressed the button!”
7. It’s updating the display.
You can use this code to create a button that does anything you want.