Coming soon - Get a detailed view of why an account is flagged as spam!
view details

This post has been de-listed (Author was flagged for spam)

It is no longer included in search results and normal feeds (front page, hot posts, subreddit posts, etc). It remains visible only via the author's post history.

2
Help with selecting a pygame rectangle by reference to gather list data
Post Body

First, yes I'm new to this and I am obviously doing things the wrong way but will clean up the code later. right now I'm just trying to get things to function.

The Basics: I have a grid. On that grid I am placing a set of pygame rectangles at set positions with random colors. I am supposedly storing the rectangle locations to a list(tile_sprites) for later retrieval.

 for j in range(level):
      r = random.randint(0,255)
      g = random.randint(0,255)
      b = random.randint(0,255)
      color = (r,g,b)

      tile_rect = pygame.Rect(tilex, tiley, tilew, tileh)
      pygame.draw.rect(gameDisplay, color, tile_rect, 0)
      tile_sprites.append(tile_rect)
      pygame.display.update()

During the game loop, I am looking for a MOUSEBUTTONDOWN event to get the mouse position and hopefully check to see if the position collides. The code I am using was suggested for a similar example but does not work. FYI, the code is not trying to do anything at the moment except to tell me what rectangle was clicked.

 if event.type ==pygame.MOUSEBUTTONDOWN:
      for tile_rect in tile_sprites:
      pos = pygame.mouse.get_pos()
      print("the mouse pos is ",pos)
      click_idx = tile_rect.collidepoint(pygame.mouse.get_pos())
      print("Clicked on rect ",clicked)

The Results: The print function returns the correct mouse position but the click_idx is always 0 unless I click on the list[0] rectangle then I get click_idx 1.

What I need is which rectangle? Not that it was clicked, but that it was the 24th rectangle in the list so I can call back its stored position and change the color.

I have tried a few examples to no avail. Any help understanding this would be most helpful. One possible though was the rectangles are generated by a class with a Clicked method then discover which instance of the class was clicked but done know how to do that either.

EDIT:

I solved it. Used a minesweeper game tactic using numpy to pre-populate a grid array then select from the array to build tiles and detect locations.

Author
Account Strength
0%
Account Age
13 years
Verified Email
Yes
Verified Flair
No
Total Karma
16,339
Link Karma
6,192
Comment Karma
10,147
Profile updated: 6 months ago
Posts updated: 7 months ago

Subreddit

Post Details

We try to extract some basic information from the post title. This is not always successful or accurate, please use your best judgement and compare these values to the post title and body for confirmation.
Posted
7 years ago