how do you get it in pygame so that when you click on a rect it will reset the window but only when you click on that certain rect

Dani AI

Generated

The snippet in Post #5 shows the common mistakes: forgetting the parentheses on pygame.mouse.get_pos, and comparing a position to a method. As suggested, use the event system and Rect.collidepoint() so you get a discrete click and an exact hit-test. The usual, reliable pattern is to test event.type for a mouse button down and then call your_rect.collidepoint(event.pos); event.pos is already the (x,y) tuple. See the docs for the event constants and Rect.collidepoint for details (event docs, Rect.collidepoint).

A minimal example pattern (replace reset() with whatever reinitializes your game state):

def reset():
    # reinitialize variables, surfaces, etc.
    pass

while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        elif event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
            if my_rect.collidepoint(event.pos):
                reset()

Troubleshooting tips:

  • If you need to check position without events, use pygame.mouse.get_pos() (note the ()), but prefer event-based checks for single clicks to avoid multiple triggers while the button is held (mouse.get_pos).
  • Make sure the rect is in the same coordinate system as the event (if you blit to a shifted surface or are using scaling/camera offsets, translate event.pos accordingly).
  • To “reset the window,” reinitialize your game variables and redraw the initial screen. Avoid repeatedly calling pygame.display.set_mode() unless changing display mode is required.

This approach follows ’s advice and avoids the pitfalls seen in ’s example; ’s pointer to related GUI threads may help if you need a fuller UI pattern.

Recommended Answers

All 4 Replies

make it so that if the position of the mouse is equal to somewhere within the rectangle AND if the mouse is clicked then do something-(relpy to me if you want actual code)

actual code would be nice

See the yesterdays post of vegaseat in the GUI thread.

shouldn't I be able to do something like this

M = pygame.mouse.get_pos
if M == textRect.collidepoint:
    print('I win')
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.