I am writing a DoodleJump game for the terminal in Python with curses. I use getch() to get the user input to know in which direction the character should move. When I get the user input my environment gets updated simultaneously but I want that it just keeps on moving and not update when I get the user input.
Does anybody know how I can achieve this? I tried using nodelay() but that still doesn't cut it. Thanks in advance
please try with:
time.sleep(seconds)
This will wait the seconds you want before continue.
Sources:
https://www.pythoncentral.io/pythons-time-sleep-pause-wait-sleep-stop-your-code/
Hope it help, have a nice day,
David.
curses refreshes the window associated with getch. You could prevent refreshes (or hide them) by turning off echo, and using a window that won't have changes made to it (even one that you create just for that purpose).
Related
I've recently realized that Python's pynput module's key click doesn't get registered in certain software. However, physical key clicks work perfectly fine. This raises the question of how are the two different methods been recognized. I thought this would've been an answered question from a long time ago but I can't seem to find any post about this.
Let's take a random game such as Genshin for example. The "w" key on the keyboard would result in the character in the game moving forward. However, a program such as the following that "holds" down the "w" key doesn't get registered by the game.
from pynput.keyboard import Key, Controller
keyboard = Controller()
while True:
keyboard.press("w")
keyboard.release("w")
The two big questions that I have are:
What is the difference between physical key clicks and the program generate key clicks that allowed these softwares to distinguish which method is used?
Based on the answer to the first question, is there a way to bypass such a difference without tempering with the software's code (aka disguise program generated key clicks as physical key clicks)?
Thanks in advance!
Edit: If anyone has any idea on how this works, please let me know. Any help would be much appreciated!
I also encountered this problem when I was trying to write a small program to help myself in Genshin. After an excruciating search, I found these two questions: question1, question2
I'm not very sure how it exactly works, so I can only give you my conclusion: When we run Genshin, we give it admin rights. Programs without admin rights sort of can't interact with it.
To solve this problem, you can merge this into your code:
import ctypes, sys
def is_admin():
try:
return ctypes.windll.shell32.IsUserAnAdmin()
except:
return False
if is_admin():
# Code of your program here
else:
# Re-run the program with admin rights
ctypes.windll.shell32.ShellExecuteW(None, "runas", sys.executable, " ".join(sys.argv), None, 1)
Then you need to make the code an executable with pyinstaller module. When you execute it, it will re-run and ask you for admin rights, then it can do its job on Genshin or other program.
There are certainly games that intentionally try to prevent the kind of cheating you're trying to do. How they do so depends on the operating system.
pynput on Windows, for example, uses the send_input API to inject keystrokes into the input system, but a sufficiently motivated application could be reading at a lower level, by using a keyboard filter driver to pull keystrokes before they get into the input system.
With such a design, there's nothing you can do in user mode to cheat the keys. You would need your own kernel driver, and even then it's not trivial to guarantee the order that the filters load.
Hi,
I am working on a project and I am monitoring the keyboard with the keyboard module.
My application works in real-time (on-the-fly) and read strings entered from user (with the keyboard module as mentioned)
What I want to do is hide user input when some specific conditions are True.
I have searched all the web and didn't manage to find something that does what I want.
To give it a more detailed explanation lets say that the user enters some text and this text string-by-string is being checked for some condition from my program.
If everything is OK, then nothing happens but if not, then I want the next user input not to be shown in the position he is writing.
I found solutions that do exactly this in the terminal like the msvcrt module (How to temporarily disable keyboard input using Python ) or do the above functionality with the input() function.
Is there something that prevent the text ,entered from the keyboard, from showing to the screen, but send it to a buffer for editing first.
Thanks in advance.
! I am on windows
I have a tkinter program written in python 3.3.3. I see myself in the need of making the the program get focus when the user unlocks the computer. I don't really know how to go ahead and start with this, users have a .exe version of the program that I created with cxfreeze. I f i need to modify the .py and create another .exe, that wouldn't be a problem.
After some research I found that one can use the ctypes module to lock the computer, but it's not very helpful because i need to know if it is locked or unlocked. I also saw commands from win32com, but i can't seem to be able to find a way to trigger a command when it gets unlocked.
What is the best way to get focus on my program after the computer is unlocked??
Any help is greatly appreciated.
I cannot answer this specifically for 'unlock' event (if there even is one in the GUI; I can't find one by cursory search.).
But I think the real answer is to change the question. Having a program simply take focus when user unlocks the display is very un-Windows-ish behavior. The Windows user expects to see the desktop just as s/he left it before the display was locked -- why does s/he want to see your program on top when unlocking, regardless of why Windows might have locked the display?
Maybe you want to recast your program as something that runs in the background and pops up a notification (in the notification area on the right side of toolbar) when it wants user to do something?
I'm writing a text based RPG, and I am using Python 3.3.4. It will be played through the Python command line, with no graphics. I'm wanting to make it so that no matter what options the user is presented with, they have the capability of typing "exit" or "help" and exiting (or getting help information) respectively at any time during the game. So if they're in the middle of fighting some monster and the only options presented to them directly would be to attack, defend, or flee, they're still able to exit or get help. Same for once they leave that function and they're just moving around the map, or talking to NPCs. From what I've found, starting a thread that waits for "exit" to be entered is the best way to do that. If I'm wrong, please let me know! If not, please explain (or show me a guide) how I'm supposed to go about this because none of my ideas have worked.
I've attempted using the threading module, and _thread. I may not have implemented them correctly, and I have no code to show for my attempts as I just trashed it when it didn't work.
Thank you in advance.
Your game doesn't have to do any work in the background, so you do not need to spawn any additional threads. The main loop of your program will listen for user input, perform the action, and then prompt the user for their next action. It should look something like this:
while True:
input = get_user_input()
if input == 'exit':
break
next_actions = modify_game_state(input)
print "You can now do: %s" % next_actions
Edit: You should not have more than one thread accept user input. It's possible for one of the threads to gobble information that was intended for the other, so no matter what solution you decide on, don't add a thread that just checks if the user input was "exit", unless it reads all of the user input.
Looking back at this, I feel like an absolute idiot.
The proper way to handle this would be calling a function that accepts input and checks it against different expected strings or numerical values.
I just thought I'd answer this in a little more clear way than Stu did.
I was wondering if it is possible to write the pygame code to a separate terminal so that you can still do things like print to the terminal. When pygame's display becomes initialized it seems to be impossible to put any input into the terminal. Any way to get around this?
I also want to know that if this is possible can one the other terminal edit the one running pygame to change certain things?
Github
Ok, once you initialize pygame and start your pygame loop in startDisplay() in Commands.py, you are essentially leaving your main() loop in game.py. So your repeated request for input won't happen again. If you want to call for input during the loop in startDisplay() you'll need to do it expressly there. As I stated above, this will pause your game until you enter a command, which obviously isn't very good. You could build a little logic around it and only request input during a break in the action or implement a Pause event (using a key event) which would subsequently call the prompt for a command.