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.
Related
I am making turn based game in python using Pyglet. The game has a player-vs-AI mode in which the bot calculates a move to play against the player. However, the function which calculates the bot's move takes around 3-5 seconds to run, blocking the game's UI. In order to get around this, I am running the bot's calculation on a second process using multiprocessing.Process. I got it to work well without blocking the UI, however every time I open the second process to run the function a new Pyglet window opens, then closes again when the process is closed. Is there any way to open a second process in a Pyglet program without a second window opening? Let me know if examples of my code is required, and I will try to come up with similar code to share. Thanks in advance to anyone who can help.
You can fix the problem by moving the initialization of the window inside of the main block
I want to make a program stop running for a certain amount of time to save CPU power. I have a self.after function that I want to run to make the program stop running for a set amount of time. However, it always crashes the program.
self.after(int(self.timeSleep*1000),print("Sleeping."))
The word Sleeping. is printed but the window crashes. I thought that Sleeping. should only be printed after the "sleep" was done. I'm not sure what I'm doing wrong and I couldn't find another question like this. Forgive me if it's a stupid mistake as it's my first time using TKinter.
Thanks in advance.
EDIT:
I am not getting any errors in my terminal. By crash I mean the window stops responding.
Your code has the same effect as below:
execute print("Sleeping.")
execute self.after(int(self.timeSleep*1000), None) which is the same as time.sleep(int(self.timeSleep*1000)).
You need to change it to use lambda:
self.after(int(self.timeSleep*1000), lambda: print("Sleeping."))
I found a solution that doesn't require using self.after. Instead, you can use a thread to run this specific method, allowing you to use while loops and time.sleep without crashing the main GUI program.
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).
So I've been attempting to get a script to type out a string of text in a video game (Guild Wars 2). Mainly I'm using pyautogui and for the most part it works fine. My issue is it seems I can't get the game to recognize 'enter'. For example if I have the code:
import pyautogui, time
time.sleep(2) #to allow me to have time to switch windows
pyautogui.press('enter')
pyautogui.typewrite("This is a test")
pyautogui.press('enter')
The two "press enter" function will not open and submit the text. If however I manually hit enter, the 3rd line types things out just fine.
I've also tried replacing press('enter') with keyDown followed by keyUp, but with still no results.
I've managed to create a workaround by having python hit F10, and then a separate Autohotkey script hitting enter when F10 is hit, but that is far from ideal. Are there any suggestions?
Extra note from comments: The script by itself works fine in other programs such as notepad. It seems to fail exclusively for the game client.
For anyone finding their way to this post. The answer is essentially that this can't be done in this fashion due to how most games interpret keypresses. A working system is shown here: Simulate Python keypresses for controlling a game
None of \n nor return works to me, so I have to use pydirectinput: https://github.com/learncodebygaming/pydirectinput
Install it using pip install pydirectinput, then import it and use as the README. Note that the file also needs to run as admin.
I'd like to know how to have a program wait for another program to finish a task. I'm not sure what I'd look for for that...
Also, I'm using a mac.
I'd like to use Python or perhaps even applescript (I could just osascript python if the solution if for applescript anyway)
Basically this program "MPEGstreamclip" converts videos, and it opens what appears to be 2 new windows while it's converting. One window is a conversion progress bar, and the other window is a preview of the conversion. (Not sure if these actually count as windows)
(Also, MPEGstreamclip does not have an applescript dictionary, so as far as I know, it can't listen for certain window names existence)
But basically I want my program to listen for when MPEGstreamclip is done, and then run its tasks.
If it helps, when the conversion is done, the mpegstreamclip icon in the dock bounces once. I'm not sure what that means but I'd think you could use that to trigger something couldn't you?
Thanks!
I realized GUI applescript was the answer in this scenario. With it I could tell the PROCESS to get every window, and that worked. However, I'm leaving this up because I'd like to know other ways. I'm sure this GUI workaround won't work for everything.
If the MPEGstreamclip actually ends when it is done, you could wrap the whole thing up in a python script using various techniques already discussed in another post. Just be sure to wait for the external process to end before continuing with your other steps.