Firstly, there are a couple of similar questions posted on this. But they are not complete questions, and no one has really answered them all. I am using Python to send input to a game active in widow modes using "ctypes" package. The original idea is mentioned here
In particular, my codes are:
x=500
y=400
for i in range(10):
time.sleep(4)
if i > 0:
x=x+17
pos=(x,y)
#ctypes.windll.user32.SetCursorPos(x,y) # this does not work in game but
#outside the game window
ctypes.windll.user32.mouse_event(0,x,y,0,0)
print i
In order for the "mouse_event" to work, I tried inputting hexadecimal values but can't get it to move consistently. I tried setting up separate class & functions to import, then calling the function. The function works outside the game window but not inside the game window.
The separate class & functions are based on this question
Thank you for your help in advance.
Related
I have two modules one for the Main Menu lets call it MainMenu.py and another one for the game InGame.py, there is a button in each module that when clicked should take me to the other module:
NOTE: I always run MainMenu.py first then i open InGame using the button, I just need a way to go back to MainMenu.py then again back to a new ran InGame, I tried using classes for both modules but It didnt work because i have circular dependencies between other modules and InGame.py
# MainMenu.py
if button_clicked:
# run InGame.py
and
# InGame.py
if button_clicked:
#run MainMenu.py
I tried directly importing each module at first which obviously didnt work, it would take me from MainMenu to InGame, back to MainMenu and stops there, pressing the button in MainMenu wont do anything.
then I tried:
# MainMenu.py
if button_clicked:
if __name__ == "main":
import InGame
else:
del InGame
sys.modules.pop('InGame')
import InGame
and
# InGame.py
if button_clicked:
import MainMenu
But it also did nothing after going to MainMenu and trying to press the button to InGame, it just stops there.
I am sure this means my design is a mess but I even tried changing the whole design with no success so im looking for an easier fix.
I believe what you are trying to do is discouraged, there are a number of alternative approaches you can take which will remove the need for such cyclic references. The first immediate idea I have to solve your problem involves using an event driven design pattern. The general concept behind it is to have the MainMenu register an event that is called by your button. Your button does not need to know who is listening to the event but the main menu can still just as easily receive the message.
EDIT: The name was eluding me whilst typing the answer, its typically called the Observer Pattern.
When you run into problems like these, it generally indicates bad design, Its good that you were able to recognise that its a bit messy. I highly suggest looking into design patterns (if you dont know them already) even if they are in languages you dont know since the concept behind them is whats important.
In your case having something which mediates the flow between the two modules is the way to go, it will act as a parent in a sense and let you go between the two children
Hello I want to run a function that puts some random dots to paint but I want to make this thing for several times so ı need a multiprocessor but I doubt about it because of that, I use pyautogui to put dots to random coordinates of the paint table. So, I wonder that if I use a multiprocessor, will it just get broke or will it just work fine?
Thanks already to whoever answers this question!
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.
To cut a long story short, I've been doing an interactive GUI (tkinter) word-game program for school. At first, everything went smoothly, but having finished the code, it has started to behave in unexpected ways when I run it. Some dialog boxes (particularly the
if tkinter.messagebox.askyesno():
thingy) just rapidly answer themselves with the 'no' option, rather than waiting for user input. Sometimes, the windows close off completely and cause the whole program to quit. However, although these errors are all the same (i.e. tkinter windows closing/answering themselves/stopping the program before they should), they usually happen in different places every time. I'm not sure if that's to do with the fact that tkinter is nested, opened, re-opened and closed numerous times within other code, which is making it run messily, but I have only destroyed tkinter windows in the right places, as far as I know.
Part of my code involves a while loop - I'm not sure if that could be interfering with the mainloop()s, but I couldn't find another way to allow the user to repeat the game as many times as they want.
I know this question is vague, but I'm mainly looking for tips - if it would be easier to diagnose if I split it up into different sections and tidy it up a bit, found an alternative for the while loop, etc.
Thanks!
TKinter dialogs should be fully completed and the results stored before moving onto the next section of code.
Make sure you provide all the arguments to the dialog (your example doesn't include the parameters).
result = tkinter.messagebox.askyesno('Confirm', 'Do you want to do this')
if result == true:
I'm creating a simple two-player board game where each player must place pieces on their own boards. What I would like to do is by either:
opening a new terminal window (regardless which OS the program is run on) for both players so that the board is saved within a variable but the other player cannot scroll up to see where they placed their pieces.
clearing the current terminal completely so that neither player could scroll and see the other player's board. I am aware of the unix 'clear' command but it doesn't achieve the effect I'm after and doesn't work with all OS's (though this might be something that I'll have to sacrifice to get a working solution)
I have tried clearing the screen but haven't been able to completely remove all the text. I don't have a preference; whichever method is easier. Also, if it would be easier to use a different method that I haven't thought of, all other suggestions are welcome. Thanks in advance!
EDIT: Other solutions give the appearance that text has been cleared but a user could still scroll up and see the text that was cleared. I'd like a way to remove any way that a user could see this text.
EDIT 2: Please read the other answers and the comments as they provide a lot of information about the topic as a whole. In particular, thanks to #zondo.
Consider using a portable terminal handling library. They abstract away the system specifica of common tasks like erasing the "screen" (i.e. terminal), or placing output at a specific position on the "screen" (again, meaning the text terminal). However, to use such a library effectively, you often have to switch to its style of generating output on the screen instead of naively printing strings.
curses is one such library (based on the C library ncurses) and included in the Python standard library. To get started, be sure to have a look at the curses tutorial in the official Python documentation.
I'd personally just use this.
import os
os.system("cls" if os.name == "nt" else "clear") #"cls" for Windows, otherwise "clear"
I would recomend a simple ANSI escape code to move the cursor position, Cursor Escape Codes, to the start of the board everytime. There is also an ANSI escape code that completly clears the console though, so you can choose.
If you are on windows you must first import colorama a module that makes windows prompt be able to use the ANSI codes as such:
import colorama # OR: from colorama import init
colorama.init() # AND THEN: init()
So if your board has n rows, after the user input for their turn, you move the cursor UP n rows + however many were required for user input, so if you wrote Input row, col: ... then you would go UP n+1, etc...
A simple example:
numLines = 1
print("Hello world!")
print("\033[<{0}>A".format(numLines), "This came AFTER hello world line")
You may not like this, it's a bit higher level than a basic two player board game, but there is always using some sort of GUI.
I personally like tkinter myself.
You don't want the option of people scrolling up to see printed text, but you can't remove what has been printed, that's like asking a printer to remove ink off a page. It's going to stay there.
Research a GUI interface, and try and make the game in that. Otherwise, you could let me take a stab at creating a explanatory piece of code that shows you how to use tkinter. If you do, link me the game you have so I can understand what you want.