Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I am making this game where you are moving a circle using the arrows keys and there is another circle spawning randomly on the screen. I want to make a piece of code to detect when the first circle collides with the second circle. I know that collidepoint() might come in useful but there is nothing else I really can do. Any help would really be appreciated.
while True:
#if image1 collides with image2
Score -= 1
Pygame supports collision detection between sprites using circles.
The API call you need is:
pygame.sprite.collide_circle()
You can see an example of its use in this question on game dev SE (and don't worry - the bug that question is about has been fixed).
If for some reason you are not using sprites, then circle-circle collision detection is pretty straightforward: just test if the distance between them is less than the sum of their radii.
Or rather, test if the square of the distance between them is less than the square of the sum of their radii (to avoid slow square root calculation):
def circlesCollide(x1, y1, r1, x2, y2, r2):
return (((x2-x1)**2) + ((y2-y1)**2) < ((r1+r2)**2))
Note, that this assumes your circle is not moving fast enough to pass enitrely through the other circle in one update step, in which case you need to do a swept circle collision test.
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I want to make a game in the turtle module similar to "Lunar Lander" by Atari but I'm not sure if I'll be able to make a collision system if I draw the terrain using a turtle. Any help would be appreciated.
I don't think python turtle have the collision system, but you can make the limit for the ground. Such as if the ground surface is on -40y, then you can create a variable and prevent the object from going through the ground.
groundLimit = -40 #create the limit for the object
while True:
if turtle.ycor() < 40: #check if the object hit the ground
turtle.sety(40) #make the object on the ground again
#but this method might make the object a bit weird
But if you are making a game, I would suggest using the "pygame" module. It's great for making games and better than turtle. Turtle is just for drawing, but pygame is for making games. You can find tutorials online to help you.
Good Luck!
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I wan't to know how I can make objects in pygame. For example a Rectangle. How do I draw it? And I know/heard that you need different names for different shapes for example rect for rectangle. Can you tell me wich names I should use for other shapes? For example a triangle or a circle, etc.
I really hope this question is fitting to StackOverflow and not a waste of time for you! :)
You can draw rectangle in pygame using this code line -
DISPLAY=pygame.display.set_mode((500,400),0,32)
pygame.draw.rect(DISPLAY,BLUE,(200,150,100,50))
The above code, creates a rectangle Blue in color.
Now, Let's say in future you want to make a rectangle smaller or larger, in that case, you can just remove this rectangle and redraw it using the above command.
Similarly, to draw circle , you can use something like this:-
pygame.draw.circle(DISPLAY, BLUE, (150, 50), 15, 1)
Hope this helps!
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
Using Python, I want to check whether a circle has an equal radius along all 360 degrees. Actually, I have segmented a circular shape from an image and then want to check whether it has a same-sized and equal radius along all the 360 degrees or not.
Can someone kindly help and tell how to do that?
In Python, OpenCV is a strong choice for an imaging library. You'd want to fit a contour to the circle and use moments to check the properties. You can fit an ellipse and see how close the major and minor axis of the ellipse match.
See this tutorial/docs for details.
Also, this question should help.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I am making a python game and I am not exactly sure how to make the enemy's and the player have collisions. Could someone show me a easy way to add collisions to multiple things efficiently. I want the enemy in my python game to collide with the player. Here is the code I use to move the enemy to the player. This code moves the enemy but the enemy does not collide with the player.
if self.canvas.coords(self.man)[0] > self.canvas.coords(Man1.man)[0]:
self.canvas.move(self.man,-1,0)
if self.canvas.coords(self.man)[0] < self.canvas.coords(Man1.man)[0]:
self.canvas.move(self.man,1,0)
if self.canvas.coords(self.man)[1] < self.canvas.coords(Man1.man)[1]:
self.canvas.move(self.man,0,1)
if self.canvas.coords(self.man)[1] > self.canvas.coords(Man1.man)[1]:
self.canvas.move(self.man,0,-1)
You can use the canvas.overlapping() which returns all the items in a given rectangle. Just give it the x,y coordinates of one of your objects and see if the tuple returns more than one.
if you show us some code or give more details we might be able to better help you
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I need a way to get a list of all sprites in a certain area of the screen, and I cannot find one anywhere on the internet.
Can someone please give me an example code of how to do this?
If it helps, all the sprites are in an "active" list.
Depends on whether you have obstacles or not, i.e. what the definition of "In the Area" means and also home many sprites you are dealing with.
If there's not many sprites and the distance calc is fast then brute force is probably ok.
for sprite in sprites:
if something.distance(sprite) < THRESHOLD:
do_something_with_near_sprite(sprite)
If you have a lot of sprites have a look at quadtrees and things like that. If calculating the distance is complicated then you're probably going to want to look at A* algorithm. There are libraries for these things so you shouldn't have to implement them yourself unless you want to.
This is a pretty general question (prepare to have it voted down by others :)