How can I make a rectangle in pygame be able to check for other rectangles around it? - python

As part of my current pygame project (I am very new to pygame), I have created multiple rectangles that move at random around the screen. As one of the features of this game, I want to make it so if one rectangle is close enough to another one, it moves towards it. Another feature I want to add is that rectangles cannot collide with each other, but I don't want to just do the regular
if rectangle1.colliderect(rectangle2):
rectange.x -= 10 # That is, it's previous position
because it will make the animations look odd.
The main way I can see to solve these problems is to use some sort of function that could check if a rectangle.x - 30 is another rectangle (or something similar), but I am unaware of one that exists.
I have attempted to look through google, but I haven't found anything as all the posts are different problems that aren't quite the same.
Thank you for any responses!

Use inflate to create a rectangle that is larger than the original one and surrounds it. Use this rectangle to find other rectangles in range:
test_rect = rect1.inflate(dist, dist)
if test_rect.colliderect(rect2):
# [...]

Related

What's the easiest way to shrink and move an entire turtle drawing?

I'm messing around with turtle and I drew something but I want to be able to scale down it's size and put it in a different part of the screen.
For example let's say you have a stickman made up of five rectangles and one circle as the head, when drawing the stickman I had to give the x and y positions for each rectangle and also the length/width of each one but now it's too big and I want the entire stickman smaller.
Is there a way to easily scale down the stickman I already drew? and also be able to assign the entire object different x/y cords as a whole so I can place him somewhere else?
Let me know if anyone can help, I'm trying to just make a simple stick man game and this is really crucial to know so sorry if it's a very basic question.

Containing a Rectangle inside another Rectangle

I didn't find a previous question that seemed to suit what I am asking, but I may not be asking the right thing either.
I have two rectangles, one larger and one smaller. The smaller one can be moved and resized, but I need it contained inside the larger one. But I am having a hard time keeping it inside of the larger one. I can detect when it moves outside of the larger rectangle, but I cannot figure out the method needed to keep it contained without moving outside of the bounding box.
I am currently working python/pyqt4 in a qgraphicsview/scene.
For a working example: In the program gimp, the cropping tool box cannot move outside of the image that is displayed. But it can be moved and resized up to the image's size. That's what I'm aiming for.

Python - Pygame - Rotation not very smooth? [I know why, just can't think of solution]

I'm making a simple game to get my knowledge of Python and Pygame going, but, since I haven't used rotation before, I am encountering a problem. Every time my rectangle rotates, it gets bigger and smaller, and my game needs a centered rotating object.
I have two solutions, maybe three. Will any of these work? If not, do you have a solution of your own?
Move the coods of my rectangle back and forth depending on the angle - This would be hard work.
Is there a way to blit an object by the middle of it, rather than the top left corner? This would be perfect
Use sprites??? I'm not sure if it would help at all, I haven't looked into or learnt anything about sprites at all yet.
It would clarify your question if you post some code. Not having seen your code, I suggest the following: draw the rectangle as usual onto a Surface and then rotate the Surface using pygame.transform.rotate

Implementation of collide method in terms of ratio

I would like to create a list of objects that collide with the user. However, I don't want to use the sprite.collide_rect_ratio() method because it creates a rectangular area that is too big for the collision (i.e. the objects seem to collide even though they are not really touching). I want to use the pygame.sprite.collide_rect_ratio(ratio): to fix the problem. How do I implement the method so that it returns a list of objects the user collides with?
It would implement the same code except with a smaller collision area as the following code:
sprite_list = pygame.sprite.spritecollide(myself, all_sprites_list, False)
Thank you.
This may be of use to you,
What you may want to look into is "Per Pixel Collision", which will first use the bounding box of the object (what i suspect the collide_rect function does).
What you will need to do is find where the rectangles collide and how far within each other they are. You then check to see if there are any opaque pixels from one sprite touching any opaque pixels from the other sprite...
This Link may be of use to you, its a very well done tutorial for a C++ framework similar to pygame.
The Per Pixel Collision code is half way down the page, and acts how I describe above.
Hopefully this is useful to you as it negates the need for the rectangle ratios due to 'invisible collisions'.
a quick google search may help you more with this type of collision detection.
For a bit of a boost heres some sample pygame code:
for s in sprites:
# if no intersection then 'intersection' will be of size 0
intersection = s.Rect.clip(user.rect)
if intersection.width != 0 and intersection.height != 0:
# perform collision detection
Here is an already written and tested version From the pygame wiki. Reading every thing on that page will give you a good knowledge on pixel collision and some good sample code which you can use straight away.
apologies if this was too far off topic but I feel this could be very useful to you as ratios (i feel) would not perform well for collision detection.
You may also want to look at Rectangle documentation in pygame.
As for your question, looping through all sprites and using the collide_rect_ratio method would be the only way of using such a method to get a list of colliding sprites as far as i know.

Native PyGame method for automatically scaling inputs to a surface resolution?

This question is related to this other one.
In my program (which uses pygame to draw objects on the video) I have two representation of my world:
A physical one that I use to make all the calculations involved in the simulation and in which objects are located on a 1000x1000 metres surface.
A visual one which I use to draw on the screen, in which my objects are located in a window measuring 100x100 pixels.
What I want to achieve is to be able to pass to my pygame drawing functions (which normally accept inputs in pixels) my physical/real-word coordinates. In other words, I would like to be able to say:
Draw a 20m radius circle at coordinates (200m, 500m)
using the precise pygame syntax:
pygame.draw.circle(surface, (255,255,255), (200,500), 20)
and get my circle of 2px radius at centred on pixels (20,50).
Please note that this question is about a native pygame way to do this, not some sort of workaround to achieve that result (if you want to answer that, you should take a look to the question I already mentioned) instead.
Thanks in advance for your time and support.
There is no native pygame way to do this.
You may be misunderstanding the function of pygame. It is not for drawing vector objects. It is for writing pixels into video surfaces.
Since you have vector objects, you must define how they will be converted into pixels. Doing this is not a workaround - it's how you are intended to use pygame.
Since it seems that PyGame developers do not hang around here too much, I brought the question to the Pygame mailing list where it originated a monster thread and the issue has been debated at large.
The summary would be:
At present there is not such a feature.
There is interest to implement it, or at least to try to implement it...
...although is not a priority of the core devs in any way
There is more than one way to skin a cat:
should be the scaling happen both ways (inputting coordinates and reading them)?
how to deal with lines that have no thickness but that should be visible?
how to deal with visibility of objects at the edge of the image? which of their points should be taken as reference to know if a pixel should be lit or not for them?
and more (see linked thread).

Categories