I have recently been wondering how to generate rects without having to manually writing them in because currently, I am making an io-like game where you can eat food and attack other players. It would be useful for the game to be able to generate rects as then it would be possible to have many more AIs and also to generate food scattered around the map.
-
You just have to remember where each random `Rect` is located. Store them in a Python List, or make a PyGame Sprite for each one, and put it in a SpriteGroup. There's lots of ways to do this. – Kingsley Jul 18 '22 at 01:58
-
Thanks a lot, I think I’ll try the sprite option. – CheesePython Jul 18 '22 at 10:48
-
1Welcome to Stack Overflow. I cannot understand the question, or rather, I cannot understand where the difficulty comes from. Why does generating rects (I assume you mean creating new ones while the game is in progress, rather than creating them before the game starts) *cause a problem* for collision detection? Do you know how to do collision detection? Do you have a strategy for keeping track of multiple Rects? Can you figure out which ones should be tested against which other ones for collision? Why does "generating" the rects complicate any of those steps? – Karl Knechtel Jul 18 '22 at 17:13
-
I am sorry that it is complex, as I didn't really know how to phrase what I was looking for. In retrospect, it is the sprite module that I am looking for. – CheesePython Jul 23 '22 at 13:24
-
This was all I really needed, thanks for the help. – CheesePython Jul 23 '22 at 13:33
1 Answers
You would have to store said rects
in a list and iterate through the list to get the collisions. You will want the objects to check for collision for every other rect there is so iterating through the list in a for-loop is your way to go. And since there is an N amount of rects you will need to have a nested loop to check every collision for every body. The creation of rects can simply be done in a function which returns the rect and then you need to append it to your list. You provided no code but I will try to make an example for your understanding:
all_rects = []
def create_rect(x, y, width, height):
return pygame.Rect(x, y, width, height)
while game_run:
if(condition): # add your condition to create a new Rect
all_rects.append(create_rect(x, y, width, height)) # define your parameters
for i in range(len(all_rects)): # go through every body to check for collisions
for j in range(len(all_rects)): # collide with every body
if(all_rects[i] != all_rects[j]): # dont check for collision with the body itself
all_rects[i].colliderect(all_rects[j])
j += 1
i += 1
This code is not meant to be run, it is merely a framework for your implementation. I hope this helps. If you need more clarification, comment on the answer. I believe I have answered it fully. The code was also written in this StackOverflow answer box so if some indentations are wrong, my apologies.

- 78
- 9
-
and if you want them to have AI's etc. you don't store the rects in the list but rather the objects, and have the rects of them defined in the class. The list approach is still the desired approach however – eisa.exe Jul 18 '22 at 19:05