Undo steps in Pygame - python

I was wondering how I would undo drawing steps in Python/Pygame.
Basically I have made the mouse draw lines, and when I click my undo Rect, I want the screen to revert back to its original state (before mouse was pressed).
Here is my code but it does not work well.
copy=screen.copy()
if undoRect.collidepoint(mx,my) and mb[0]==1:
screen.blit(copy,(0,0))
if mb[0]==1 and omb[0]==1:
draw.line(screen,color,(omx,omy),(mx,my),5)
can someone who knows what I mean please give me ideas on how to make an undo last drawing button?
Thank you.
Ps. I am fairly new to pygame/python so please go a little slowly.

You just need to save the frames when the user interacts with the application.
For example in a demo paint application I wrote with opengl I used a list with 20 elements max and I was updating it when the user started a new action.
If user clicks to the screen, save the current frame to the list. If user stop clicking save the new frame. Then, when you need to go back you just have to take the last element of your list and draw it to the screen.

Related

button click in python turtle module

I am very new to this so please do not judge if my question seems too obvious.
so I am trying to make a game in the python turtle module and I would like to have different buttons for loading and quitting. I have images of these buttons and I am making these images my turtle object by using .addshape() and .shape() methods. The next step for me is to make sure that when I click any of these buttons I get the desired outcome. My issue is that I cannot figure out how to make that happen i.e how can I make my program do the thing when the mouse clicks within a certain image? I have tried using turtle.onclick() method, but I guess I am not able to utilize that either. Any help would be much appreciated.

Is there a way to make a function wait for image to show up then clicking the image while on display using PyAutoGui (Python)

I'm currently making a BTD6 Auto Farmer what this means is that it will farm the game for me and click on the screen and play the game for me and complete the maps and stuff so I can get rewards that do not give me a competitive advantage because the game is single-player.
And to complete this bot I'd have to wait a certain amount of time and then see whenever a button shows up on the screen and to accomplish this I need to make a function that will wait for an image to show up on the screen and if the image shows up the function should press that image and that's all.
I tried many things and couldn't get it to work, here is my code that doesn't work:
def click_image(img):
x = pag.locateCenterOnScreen(img, confidence=config.OPENCV_CONFIDENCE)
pag.click(x)
This didn't work and I'm not surprised because the code creates the variable then tries pressing the variable there is no check or wait and I don't know how to implement that and that's what I'm asking for, that's the answer I'm looking for :)
It seems like you want to perform an asynchronous task, you should look into the python package asyncio.

foreground extraction in opencv using python

I am trying to do foreground extraction(Grab cut algorithm) in open CV using python. I am using the code from the link . The code runs but everytime I am moving the mouse it is drawing a rectangle.
Problem: I cannot draw the touching strokes even after pressing the 0,1,2,3 keys as mentioned. Can someone please suggest how to come out of the rectangle mode and fix the rectangle after i release the mouse button.
I am new to open CV,any help would be appreciated
The code is structured very clearly. You might start by adding print statements in the onmouse method, in each of the
if event == ...
blocks, to figure out which events are correctly registered and which don't come through.

Clear previous screen to show next screen pygame

I am trying to develop GUI for a who wants to be a millionaire game using pygame. If the user is presented with the welcome screen and clicks new game, it should clear the welcome screen and bring up the main game screen.
I tried using os.system('xxx.py') but it doesnt stay on, it just flashes and goes off.
Any ideas on how to make this work?
Thanks in anticipation.
You probably shouldn't be trying to launch a new instance of your game. You should create a reset method which can be called to restart everything. It would clear all variables, set the game state back to the start, etc.

Creating multiple screens in pygame

Is there any way way to create multiple screens in Pygame without redrawing to the screen every time. For example, if we want to create a splash screen, then a main menu with buttons. On clicking the 'Start Game' button, it would go to a new screen which is the actual game. What I mean is anything similar to the 'Form' on Visual Studio or the Activites on Android.
Depends on what you mean.
If you mean making multiple windows at the same time, no, you can't do that--it's a limitation of SDL (although you CAN fake it by using multiprocessing (not multithreading)).
If you mean changing one screen around, yes, you can do that, with multiple calls to pygame.display.set_mode(...). You can change the resolution, arguments, etc. If you're doing OpenGL stuff, that will remake the context too.
If you just mean drawing different things to the same window, of course! That's sorta the point of PyGame.
Other than that, you'll have to clarify.
Just fill the screen with white and then draw the second screen onto the main screen. Then when you need the other screen, just refill the screen with black and then continue. It would help if both of your screens a function and you used a key like tab to "switch" between screens.

Categories