pyautogui and pydirectinput not working properly on Halo 5? - python

I am trying to write a script in python that would take control of Halo 5 forge in order to automatically create and script an in-game script brain object and use the game's built-in scripting system to script the object to randomize the movement of 2 agents (will be added later) in order to procedurally generate a map, saving me time in doing tedious manual work on creating a method from scratch I have repeated many times in Halo 5 Forge.
Basically I am trying to create a class representing this script brain object in python and under the init method it is supposed to follow all of the steps in Forge to create a script brain object.
At first I tried pyautogui as it initially worked previously on Halo 5. But now it doesn't seem to work. It is a known issue that pyautogui doesn't input certain things properly on directX games and was suggested pydirectinput as an alternative.
So I did and while the mouse responds properly in the game and opens the object menu using the press() method, it doesn't actually seem to respond to leftClick() for some reason, only repositioning the mouse to the correct position but not actually clicking the object menu like I had hoped.
Here is the code:
import os
import pyautogui as pygui
import pydirectinput as pydi
import time
class forgeObjectRandomizer:
def __init__(self):
#CREATE THE SCRIPT BRAIN
time.sleep(5)
screenWidth, screenHeight = pydi.size()
pydi.moveTo(round(screenWidth / 2), round(screenHeight / 2))
time.sleep(1)
# pydi.leftClick(1749, 44)
pydi.press('o')
time.sleep(0.5)
pydi.leftClick(1554, 337) # --- Extras
time.sleep(0.5)
pydi.leftClick(1605, 266) # --- Scripting
time.sleep(0.5)
pydi.leftClick(1630, 236) # --- Script Brain
time.sleep(0.5)
pydi.press('p') # --- Properties
time.sleep(0.5)
pydi.mouseDown(1886, 231) # --- Scroll down
pydi.moveTo(1886, 453) # --- Scroll down
pydi.mouseUp()
brainRandomizer = forgeObjectRandomizer()

You can use to move mouse
import win32api,win32con
def click():
win32api.mouse_event(win32con.MOUSEEVENTF_MOVE, int(x), int(y), 15, 15)
for mouse click equivalent, but I only had problems with moving the mouse

This is kinda a blind shot since u said pyautogui was working fine before on halo 5...are you running the game as administrator? If so make sure that ur code does too, because i had same problem in a game named asda global

Related

How intercept mouse device with Python for Games?

I trying to move my mouse inside an FPS game (like valorant/csgo) but doesn't work.
I used PyAutoGui, PyInput, Mouse, PyWin32, and many other libs but I just can simulate a click.
I see something about Interception (by oblitum) and years ago worked but this day doesn't. The game block this intercept.
So I guess I need to build some driver intercept to make moves in Mouse. Another trick is to use Arduino (this works these days) but for my project this isn't a solution. I need to only in python with any mouse.
This depends how you want to move your mouse. I'm assuming according to however you want to move it? If so, try the first code as the second code performs based on a series of commands
Have you tried this code by #flipeador?
#from interception import Interception, MouseFilter, KeyFilter, MouseFlags,\
# MouseState, KeyState, MapVk, Vk, map_virtual_key
RUNNING = True
TIMEOUT = 2500 # ms
interception = Interception()
interception.set_mouse_filter(MouseFilter.ButtonAll)
interception.set_keyboard_filter(KeyFilter.All)
while RUNNING:
device = interception.wait_receive(TIMEOUT)
if device:
print(f'{device.get_hardware_id()}:')
# Mouse
if device.is_mouse:
print('MouseStroke(flags={1},state={2},rolling={0.rolling},x={0.x},y={0.y},info={0.info})'
.format(device.stroke, MouseFlags(device.stroke.flags).name, MouseState(device.stroke.state).name))
# Keyboard
elif device.is_keyboard:
vk = map_virtual_key(device.stroke.code, MapVk.ScToVk)
print('KeyStroke(sc={0.code:03X},vk={2:03X},state={1},info={0.info})'
.format(device.stroke, KeyState(device.stroke.state).name, vk))
# escape = terminate
if vk == Vk.Escape:
RUNNING = False
# switch x and y
elif vk == Vk.X:
device.stroke.code = map_virtual_key(Vk.Y, MapVk.VkToSc)
elif vk == Vk.Y:
device.stroke.code = map_virtual_key(Vk.X, MapVk.VkToSc)
device.send()
print('-'*100)
Do note that you are not allowed to cheat in Valorant, as it blocks a lot of "hacking" and detect if your hacking even if the program your using is not on their block list.
I'm not here to question your morality.
Definitely go the arduino(s) route (you may need two. One to receive python commands and one to emulate the mouse events. Depends on the arduino) and be sure to use some randomization everywhere.
I do question why you'd want this.
I see no fun in cheating and I can't see what you are trying to achieve with this.
Although you said it did not work for you, the package you want to use is pynput. It is possible to simulate mouse movements and clicks as stated in the docs.
from pynput.mouse import Button, Controller
mouse = Controller()
# Move pointer relative to current position
mouse.move(5, -5)
This should give you the behaviour you want.
EDIT: with anti-cheat software in place, it may be very hard (not to say impossible) to simulate mouse movements in games. The game can distinguish organic mouse movement from simulated movement. Python alone might not be able to convince Valorant that your mouse movement is "real" ;)

Using onscreen.click() in turtle but only works at the end of the programme

This is the programme I wrote but I'm not sure what's wrong:-
import turtle
import random
bob = turtle.Turtle()
screen = turtle.Screen()
def coord(x,y):
print(x,y)
turtle.onscreenclick(None)
turtle.onscreenclick(coord)
turtle.listen()
print('hello')
turtle.done()
The programme works fine except that the print('hello') part happens first, followed by the on screen event. How do I make the onscreen event occur first before the rest of my programme?
You can simply make the rest of your code part of the coord function:
def coord():
print(x,y)
turtle.onscreenclick(None)
print("Hello")
# The rest of your program goes here.
However, a few things to note:
This isn't an amazing elegant solution, especially if you intend to set up other events further into your code. It can become quite hard to keep track of.
It's imperative that you remove the event binding (in this case the binding of coord to onscreenclick) as soon as it's been used, otherwise you could end up with multiple instances of the same code running at once if someone double-clicked the screen, for example. In your case you've already done this (with turtle.onscreenclick(None)), but it is something to keep in mind.
If you choose to go this route, don't forget to rename the coord function something more representative of what that section of your code will do.

Turtle gives error: AttributeError: 'Turtle' object has no attribute 'onkeyrelease'

I am trying to add 1 every time I release a key:
from turtle import *
import turtle
turtle1 = Turtle()
screen = turtle1.getscreen()
goPressed = False
imported Turtle...
currentWatts=0
def onaclicked():
global currentWatts
currentWatts+=1
print (currentWatts)
defined my function to be run when the key: 1, is released
turtle.onkeyrelease(onaclicked, "1")
for some reason onkeyrelease isn't there even though I imported Turtle and checked in Python documentation. It SHOULD work, shouldn't it? Did I improperly import? Can you please help me?
The reason I want it to be onkeyrelease instead of onkey, is because it is for a game. With onkey, when you hold your finger on the key, it adds 1 to currentWatts every around 0.25 seconds. You could cheat by placing something on the key so I want it only to add 1 when you release the key.
You've several problems with your code: you import turtle two different ways which confuses things; onkeyrelease() is really a method of the screen/window, not a turtle; you didn't call listen() which allows keystrokes to be processed. The following should work in Python 3:
from turtle import Turtle, Screen, mainloop
def onaclicked():
global currentWatts
currentWatts += 1
print(currentWatts)
currentWatts = 0
screen = Screen()
screen.onkeyrelease(onaclicked, "1")
screen.listen()
mainloop()
Make sure to click on the window once before you start typing to make it active.
If you're using Python 2, which I suspect from the error message you got, then replace the Python 3 alias onkeyrelease with onkey:
The method Screen.onkeypress() has been added as a complement to
Screen.onkey() which in fact binds actions to the keyrelease event.
Accordingly the latter has got an alias: Screen.onkeyrelease().
This change should work the same in both versions. Using onkeyrelease instead of onkey wasn't going fix your holding a finger on the key issue.
when you hold your finger on the key, it adds 1 to
currentWatts every around 0.25 seconds. You could cheat by placing
something on the key so I want it only to add 1 when you release
It appears that automatic key repeats are handled by the operating system and may need to be disabled external to Python, depending on the OS. Some example links:
Apple OSX: Set how quickly a key
repeats
Ubuntu: Turn off repeated key
presses
X-Windows from Python: key repeat in
tkinter

Python using turtle button

I am attempting for a homework assignment to implement Simon Says in python. I'm trying to do it using the turtle library (a requirement).
However, I've run into a stumbling block in that while I can get the screen to register click events (currently just printing the x,y coordinates) I can't get it to wait for a click event.
Specifically what I'm planning on doing is having areas on the screen that when they click within that location it is considered as if they had clicked a button. Screen clears and game does whatever.
However, in experiments in trying to get a working 'button' all that it does is set it so it prints the x,y coordinates but the rest of the program finishes. Didn't wait for the user to click anything. I tried a blocking method of...
while clicked == False:
pass
or
while clicked == False:
time.sleep(1)
but both methods hangs the program until I manually interrupt and then it'll print the clicks.
Am I missing an option somewhere?
Turtles don´t have buttons, but they do have callbacks for clicks.
Furthermore, you should use onclick for Screen to detect general clicks and onclick for turtle to detect clicking in turtles. You can, for example, make a 4 BIG turtles with different colors by using a dynamic shape.
Also, turtle is based on Tk, so you must be aware of things like mainloop()
The following program give some hints for Python 2.7.5.
import turtle as t
from random import randint
class MyTurtle(t.Turtle) :
def __init__(self,**args) :
t.Turtle.__init__(self,**args)
def mygoto(self,x,y) :
t1.goto(x,y)
print x,y
def randonics(self,x,y) :
self.left(randint(90,270))
def minegoto(x,y) :
print x,y
t1.goto(x,y)
wt=t.Screen()
t1=MyTurtle()
wt.register_shape("big",((0,0),(30,0),(30,30),(0,30)))
t1.shape("big")
wt.onclick(t1.mygoto,btn=1)
wt.onclick(minegoto,btn=2)
t1.onclick(t1.randonics,btn=3)
t1.goto(100,100)
t.mainloop()
So after extensive search there isn't necessarily a way pause execution of the code in python while using turtle to wait for some click event. Maybe in Tk I could do that but not in turtle.
However, there is a way to get around that. As an example. A method sets up the fake button on the screen, sets the click event, and terminates. The click event when clicked calls the next method needed for execution. So until the button is clicked the actual code isn't doing anything but remains in memory for use.
So more specifically.
1. Create a 'button'.
2. Have your program behave normally until it needs to wait for a click event.
3. Set up the on screen click (or on turtle) in such a way when the 'button' is clicked the next part of the code is run.
Special note. The code in question can't depend on waiting for a click event for later on in code. Instead, the click causes the next part of the execution of your code.
You can make the function registered with onclick() test the x,y position. If it is inside some region you do whatever you must.
I don´t see the difference between what you want to do and what this code does, the modification of turtle position is just an example, you can do anything when a click is captured by onclick(), even start a thread if you really need it (using Creating Threads in python)
import turtle as t
from random import randint
from threading import Thread
from time import sleep
def threaded_function(arg,t1):
for i in range(arg):
print "running",i
sleep(1)
t1.forward(i*10)
def minegoto(x,y) :
print x,y
t1.goto(x,y)
thread = Thread(target = threaded_function, args = (10,t1 ))
thread.start()
thread.join()
print "thread finished...exiting"
wt=t.Screen()
t1=t.Turtle()
wt.register_shape("big",((0,0),(30,0),(30,30),(0,30)))
t1.shape("big")
wt.onclick(minegoto,btn=1)
t1.goto(100,100)
t.mainloop()

pygame dual monitors and fullscreen

I am using pygame to program a simple behavioral test. I'm running it on my macbook pro and have almost all the functionality working. However, during testing I'll have a second, external monitor that the subject sees and the laptop monitor. I'd like to have the game so up fullscreen on the external monitor and not on the laptop's monitor so that I can monitor performance. Currently, the start of the file looks something like:
#! /usr/bin/env python2.6
import pygame
import sys
stdscr = curses.initscr()
pygame.init()
screen = pygame.display.set_mode((1900, 1100), pygame.RESIZABLE)
I was thinking of starting the game in a resizable screen, but that OS X has problems resizing the window.
Pygame doesn't support two displays in a single pygame process(yet). See the question here and developer answer immediately after, where he says
Once SDL 1.3 is finished then pygame will get support for using multiple windows in the same process.
So, your options are:
Use multiple processes. Two pygame instances, each maximized on its own screen, communicating back and forth (you could use any of: the very cool python multiprocessing module, local TCP, pipes, writing/reading files, etc)
Set the same resolution on both of your displays, and create a large (wide) window that spans them with your information on one half and the user display on the other. Then manually place the window so that the user side is on their screen and yours is on the laptop screen. It's hacky, but might a better use of your time than engineering a better solution ("If it's studpid and it works, it ain't stupid" ;).
Use pyglet, which is similar to pygame and supports full screen windows: pyglet.window.Window(fullscreen=True, screens[1])
Good luck.
I do not know if you can do this in OS X, but this is worth mentioning for the Windows users out there, if you just want to have your program to run full screen on the second screen and you are on windows, just set the other screen as the main one.
The setting can be found under Rearrange Your Displays in settings.
So far for me anything that I can run on my main display can run this way, no need to change your code.
I did something silly but it works.
i get the number of monitors with get_monitors()
than i use SDL to change the pygame window's display position by adding to it the width of the smallest screen, to be sure that the window will be positionned in the second monitor.
from screeninfo import get_monitors
numberOfmonitors = 0
smallScreenWidth = 9999
for monitor in get_monitors():
#getting the smallest screen width
smallScreenWidth = min(smallScreenWidth, monitor.width)
numberOfmonitors += 1
if numberOfmonitors > 1:
x = smallScreenWidth
y = 0
#this will position the pygame window in the second monitor
os.environ['SDL_VIDEO_WINDOW_POS'] = "%d,%d" % (x,y)
#you can check with a small window
#screen = pygame.display.set_mode((100,100))
#or go full screen in second monitor
screen = pygame.display.set_mode((0, 0), pygame.FULLSCREEN)
#if you want to do other tasks on the laptop (first monitor) while the pygame window is being displayed on the second monitor, you shoudn't use fullscreen but instead get the second monitor's width and heigh using monitor.width and monitor.height, and set the display mode like
screen = pygame.display.set_mode((width,height))
display = pyglet.canvas.get_display()
display = display.get_screens()
win = pyglet.window.Window(screen=display[1])
------------------------------------------------------
screen=display[Номер монитора]
------------------------------------------------------
display = pyglet.canvas.get_display()
display = display.get_screens()
print(display) # Все мониторы которые есть

Categories

Resources