This question already has answers here:
Pygame mouse clicking detection
(4 answers)
Closed 7 months ago.
I have lots of rects and each one has its own function when clicking on it.
Some of these rects are overlap sometimes. When I click somewhere, I want to execute just the above rect function (not all rects in that position).
Thanks
without a minimum working example it's difficult to gauge the exact issues but assuming you are rendering all of these rectangles in order I would check for clicks in the reverse order of rendering, that way once a click check passes you are guaranteed to stop checking any of the items that were drawn previous to the one that passed the click check, which presumably would be above.
to stop checking further you can use the break keyword like so:
for rect in my_rects[::-1]: #this loops over your rectangles in reverse
if rect.clicked(): # this is pseudocode, you'll have to write the clicked() check yourself
do clicky stuff here...
break
Give a name/var to each rectangle, then use a for loop and break once the rect is the desired one.
Related
This question already has answers here:
How can I crop an image with Pygame?
(4 answers)
Closed 1 year ago.
I am working on making a drawing program in Pygame. All features are basically complete except for the ability to save your image. Is there any built in Pygame function that does this, or will I have to find some other way (E.G. taking a screenshot or something).
screen is a Surface object which has method subsurface(Rect) to get part of surface - and this part will be also a Surface object.
And there is function pygame.image.save(surface, filename)
Other method: at start create smaller Surface and draw on this surface (instead of drawing directly on screen) and blit() this surface on screen to display it and save() this surface to file.
You can even use many surfaces and keep them on list to create function Undo. When you use new tool then you duplicate surface and put it on list. When you use Undo then you get last surface from list.
If it's a drawing game, then you'll probably find it very easy to manually make the image.
I know it sounds scary, but it really isn't, especially in python.
You can use PyPng to convert a 2D array into an image, as I assume you already use a 2D array to store the drawing
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):
# [...]
This question already has answers here:
Getting the fill color or any other property of a item drawn in a canvas in tkinter(Python)
(2 answers)
Closed 2 years ago.
I there anyway to check the state of a canvas object? I've dug through the documentation and can't seem to find anything. I am looking for something like:
canvasWidget.getState(node1)
I have a bunch of nodes that I want the user to be able to draw lines that connect the nodes, and I want to have the line follow my cursor to sort of animate the creation of the line like my picture below.
To do so, I have a hidden line that I plan on making visible and then I'll change the coordinates to following the cursor.
The method you are looking for is called itemcget
Syntax is
state = canvas.itemcget(canvasItem,'state')
This question already has an answer here:
Displaying Label on Tkinter for a fixed amount of time
(1 answer)
Closed 3 years ago.
I am really new to Python. I have a memory game assignment. I've started doing it but I am kind of stuck right now because I have to generate numbers that are gonna appear on the screen and disappear after 2 seconds. I managed to generate the numbers and I put the numbers as text on a label but can't make them disappear. Is there a function to do it?
For Tkinter there are some options for "forgetting" (making the label disappear).
label.pack_forget()
label.place_forget()
If you used pack then use pack_forget() and if your using place use place_forget(). To make them reappear, use the pack or place function again. To wait a certain amount of time you can use time.sleep but the better option is to use .after()
tk.after(10, function)
This question already has answers here:
How do I detect collision in pygame?
(5 answers)
How to detect collisions between two rectangular objects or images in pygame
(1 answer)
Closed 2 years ago.
I'm using Pygame with my group to develop a game for this semester in school. I'm responsible for building the physics engine.
My question: How do I get a player object to stop on collision with a rect object? I've scoured the internet, and this website for answers, but everything seems to complicated to grasp.
Here's the code particular block of code I'm trying to get to work:
if player.rect.left == rect.right:
player.undo_right()
This block is preceded by the game loop. All I want to do is make it so the player is consistently 20 pixels away from the box. I'll be able to apply better bounds once I get this test to work. Right now, it repels the player sporadically, and never consistently. Sometimes, I'll run it, and it'll bounce away once, and then the player will go through the rectangle freely.
I realize there are numerous links on the internet for this type of thing, but I wanted to try to get collision to work using simple code like this. If I'm not able to, then why? Can someone exaplin a simple and efficient way to do this?
Thanks.