I want to code a bot that walks for me in a game called Minecraft To Explore All Of The Biomes. I've tried this code:
import pyautogui
import time
pyautogui.keydown("w")
time.sleep(8888)
pyautogui.keyUp("w")
print("Exploration Finished")
But it doesn't even move how can I fix it?
This is because you have inserted a time.sleep() code which will wait for 8888 seconds and then finish your code, resulting nothing. So instead, I put a for loop to fix your error:
from pyautogui import *
for i in range(8888):
keyDown("w")
keyUp("w")
print("Exploration Finished")
You can use the time.sleep() function to go to your game, start the game etc...
Please tell if this worked for you
Related
import pygame
pygame.mixer.init()
pygame.mixer.music.load("myFile.Mp3")
pygame.mixer.music.play(-1) # note -1 for playing in loops
# do whatever
# when ready to stop do:
pygame.mixer.pause()
If you want to unpause:
pygame.mixer.unpause()
I have a question about pygame!
I have the same pygame setting as above my question:
When I click the main python code How can I keep this constantly playing in the background instead of stopping when I start Main.py?
I tried the code above but:
it It starts when I boot up the Raspberry pi 4B
but Stops playing when I click Main.py.
Personally, I’d recommend you use the playsound module, as it implements this pretty easily. Run pip install playsound, then you can include this in your code:
playsound("sound_file.mp3", block=False)
Alternatively, you could make a separate thread to play the song using the builtin threading module. I believe it would look like this:
import threading
import pygame
pygame.mixer.init()
def play_song():
pygame.mixer.music.load("my_song.mp3")
threading.Thread(target=play_song).start()
I’d personally recommend the first one, however. If you want to play the song constantly in a loop, you could do something like this, with a combination of the two:
def play_song():
while True:
playsound("my_song.mp3", block=True)
song_thread = threading.Thread(target=play_song, name='Background_song')
song_thread.daemon = True # Stops the song when the main file is closed
song_thread.start()
I know I can use time.sleep(), but I need something that would affect whole script.It is automatic test homework and aplication buttons are clicked almost instantly. It is a bit anoying because I cant see if everything is working as supposed(still learning).
import pyautogui
import time
from pywinauto.application import Application
app = Application(backend="uia").start(r"C:\Users\User\Desktop\WPF_RentACar_3maj\WPFRentACar\bin\Debug\WPFRentACar.exe")
pyautogui.FAILSAFE = True
#app.LoginWIndow.print_control_identifiers()
dlg =app.LoginWindow
dlg.MaximizeButton.click()
dlg.MinimizeButton.click()
dlg.MaximizeButton.click()
dlg.Restore.click()
try:
dlg.Edit1.type_keys("123")
dlg.Edit2.type_keys("123")
dlg.LoginButton.click()
dlg.Button1.click()
finally:
print("Cant login with wrong credentials!")
time.sleep(2)
dlg.Edit1.type_keys("'^a{BACKSPACE}")
dlg.Edit2.type_keys("'^a{BACKSPACE}")
dlg.LoginButton1.click()
time.sleep(5)
time.sleep() does stop the whole script. Or are you using threading or python 2? Also can you tell us what you are also trying to automate.
I've been working on this for hours, and I've tried everything from break in an if statement to making an index variable. I can't seem to find a way to stop the sound/music from playing. My goal is to press 'Q' and stop the function and while loop to start another while loop.
import keyboard
import time
from playsound import playsound
#this handles the name that is displayed and makes it easier to input
def Song(type,opt):
time = 1
print(opt)
playsound(type)
print('''Enter Your Music Selection:
O) Overworld
F) Fighting
B) Boss-Strong Enemy
V) Victory
I) Inside-Taven-Shop
D) Dungion
L) Lose-Death
''')
while True:
while keyboard.is_pressed('O'):
Song('MusicStuff/test0.wav','Overworld Theme')
while keyboard.is_pressed('F'):
Song('MusicStuff/TrashLine.wav','Fighting Theme')
Playsound consists of a single function which only plays sound, and does nothing else. That is why you are not able to "stop" the function.
If you want more interactivity you have to use a different library. You can use pyaudio.
Check out this post.
stopping audio with playsound module
I wanted to make a simple macro to hold down 'W' for some time, but even simple script like this does not work.
import time
import pyautogui
from pynput.keyboard import Key, Controller
keyboard = Controller()
pyautogui.keyDown('w')
time.sleep(3)
pyautogui.keyUp('w')
time.sleep(5)
keyboard.press('w')
time.sleep(3)
keyboard.release('w')
If i test it in any text editor/text input window it will write one 'w' when script starts and anouther one after 8 seconds without holding/spaming it. And therefore it DOES NOT work in any games what should be the whole purpose of this script. I tried it in a huge variety of different games (Java Minecraft, source Gmod, Roblox and some unity games) and this script just was not working in any of them, but if a game has chat, search box or any other text input window, this script will write one 'w' and anouther one after some time in it.
I realy have no idea why this is happening, but i remembered that two years ago i tried to make similar script on pynput and it did work, so i tried installing old versions of pynput, but that did not help me as well...
So after a long time i could finaly take care of the issue, and i figured out that the source of the problem was in the Microsoft's DirectInput. Basically pynput and pyAutoGUI are not able to get in to DirectInput and there is no signal at all while I thought that there wwas a signal, but for so little amount of time that the games just were not able to pick it up. And the solution to this was pretty easy thanks to this guy PyAutoGUI not working? Use DirectInput. Thanks to his PyDirectInput librarry you are able to use python to emulate button presses and holdes in games!
This might be a start to work with:
#https://stackoverflow.com/questions/66284097/pynput-and-pyautogui-cant-hold-keys
#simulate keystroke for some amount of time
import msvcrt
import time
def press(char, duration, sleep, limit=None):
while True:
lim = limit
t1 = time.time()
while time.time() - t1 < duration: # do for duration specified
msvcrt.putch(char) # output character, putch() awaits a byte string
if lim: # limit specified?
lim -= 1
if lim == 0: # limit reached
break
time.sleep(SLEEP)
if msvcrt.kbhit(): # key pressed?
char = msvcrt.getch()
press(b'w', .001, 2, None)
I was making a bot that would basically keep the left mouse button clicked or unclicked based on a toggle. I get it to work but then it started lagging my entire computer so I didn't take it one step further, being scared to burn my PC, how It happened to my phone one year ago. So we are finally here asking you guys for some optimization to my project.
Code:
import keyboard
import win32api, win32con
from pynput.mouse import Button, Controller
mouse = Controller()
play=False
def toggle():
global play
if play==False:
play=True
else:
play=False
keyboard.add_hotkey('home',toggle)
played=False
while True:
if play==True and played==False:
played=True
mouse.press(Button.left)
elif play==False:
mouse.release(Button.left)
played=False
else:
pass
You should put a time.sleep() or something to wait between each loop because your while statement is running again and again without any pauses.
You can slow the while loop by inserting a sleep function in it. To do this, import the module time and call the function time.sleep(ms) in your while loop. It will be less reactive but you can set a sleep time of only a few ms and it will be better because the program will not be running at full speed all the time.