To simplify this problem, let's say I simply want to be able to detect whether my gamepad's right thumbstick is being pushed up (but not horizontally, within some lenience), pushed to the right (but not up, within some lenience) or being pushed to the left (but not up, within some lenience).
Unfortunately, the lenience is required, but I haven't found a way to do it yet. Thusfar I have been using the inputs library:
https://pypi.org/project/inputs/
But it's not super well documented and as far as I can tell it doesn't let you compare an exact angle of the thumbstick at any one time.
Using their own example code, I have this snippet of a function:
def get_true_game_input():
inputArray = [0,0,0,0,0,0,0,0,0,0,0,0,0,0]
# All the different buttons that are important to be detected, including whether the thumbstick has been moved left, right, or up
events = get_gamepad()
for event in events:
if event.code == "ABS_RY":
if event.state >= 8000:
print("top Guard!")
inputArray[5] = 1
elif event.code == "ABS_RX":
if event.state >= 8000:
inputArray[6] = 1
print("right guard!")
elif event.state <= -8000:
inputArray[4] = 1
print("left guard!")
Which works up until one moves the stick, for example, upwards, then ever so slightly to the left or right, whereupon it will detect that instead.
The problem is that, with the way Inputs seems to work, events is always either empty, or of size 1. So it will either detect a horizontal movement or a vertical movement, and one will overwrite the other.
Is there some way to solve this? I am also open to rebuilding this using a different library, but cannot find any other. I am using a dualshock 4 controller using the DS4 Tool.
Any help or advice would be greatly appreciated. Thank you.
Related
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" ;)
I am using pygame 1.9.6 and python 3.7.7. I am looking for a way to have something drawn by pygame(or maybe even an image) that stays inside the window border(and stops when I release the key. I started with the “if keys[]:” method:
if keys[pygame.K_LEFT] and x>0:
x-=speed
that made it stay in bounds, but it only moved a few pixels and then did not repeat.
Next I tried the “event.key” method:
if event.type==pygame.KEYDOWN:
if event.key==pygame.K_LEFT:
xchange=-speed
ychange=-0
but that just makes the object move forever. I tried putting “and x>0” on the same line right after the direction, an “if x>0:” before, an “if x<0:” after, but the event never updates to see that the coordinates are past the edge, even with update commands. It just keeps going and going. I also don’t know how to make the object stop moving when I release the key, since event.key’s have the event always on.
Thank you for all the help you can offer.
In order for something to stay within a frame, you must check if it's coordinates are not past the border before moving it.
keys = pygame.key.get_pressed()
if(keys[K_LEFT]):
if(square.left > 0):
square.left -= 10
if(keys[K_RIGHT]):
if(square.right < 600):
square.left += 10
if(keys[K_UP]):
if(square.top > 0):
square.top -= 10
if(keys[K_DOWN]):
if(square.bottom < 600):
square.bottom += 10
This is an example of how you would do this with a Rect object, it checks for border and then moves the object if its within those border and the appropriate key is being pressed. I can't apply this to your code as I cannot see enough of it but you just have to replace the coordinates. For example, you might have thing.x instead of square.left
I’m trying to use the same key to flip after multiple successive stimuli in PsychoPy but it keeps flipping successive screens as well then.
What I came up with was the following slight modification of one of the examples in the manual:
from psychopy import visual, core, event
win = visual.Window(monitor="testMonitor", units="deg")
stim1 = visual.TextStim(win, text="Stim 1", pos=(0,0))
stim2 = visual.TextStim(win, text="Stim 2", pos=(0,0))
stim3 = visual.TextStim(win, text="Stim 3", pos=(0,0))
while len(event.getKeys(['space'])) <= 0:
stim1.draw()
win.flip()
event.clearEvents()
print(event.getKeys()) # test if key buffer is really empty
win.flip()
# while len(event.getKeys(['return'])) <= 0:
# stim2.draw()
# win.flip()
# event.clearEvents()
# win.flip()
while len(event.getKeys(['space'])) <= 0:
stim3.draw()
win.flip()
event.clearEvents()
win.flip()
win.close()
core.quit()
However if I hit space on the first screen, it doesn’t just flip this screen. Instead it will flash stim3 for a split second and then immediately terminate. If on the other hand I use a different key for the second stimulus (replace 'space' with 'return' in the loop condition), that works fine.
My first suspicion was that event.clearEvents() somehow failed to clear the event buffer, so the space event from the first stimulus remained saved and also flipped the second stimulus. However in that case the stim3 shouldn’t be shown at all because the loop condition would immediately fail. Also, if I print out event.getKeys() at the indicated position above, it appears to be empty. Moreover, if I use a different key between the two space-triggered stimuli (remove comments from the stim2 section), suddenly all keys work just fine.
Does anybody have an idea what I’m doing wrong?
(Btw if you’re wondering: I modified the manual example by changing the loop where the program waits for input from a while True loop into while len(event.getKeys(['space'])) <= 0 which eliminates the need of using the exact same condition to break. I tested it with the original idiom just to be sure and the behaviour is the exact same as with my version.)
After some more testing found that deleting the win.flip()'s after each event.clearEvent() fixes the issue. I’m not quite sure why the additional flips (which I thought were necessary to clear the screen before drawing the new stimulus) would cause such a behaviour though. Can somebody explain?
I am trying to create a game that moves to the end of a square. Inside the game I created obstacles by making a list of true and false (false=obstacles). What I have (but did not post) is something that detects an obstacle ONLY IF the user lands on the obstacle. However, I want to find a way to detect an obstacle BEFORE the user makes its next move and make it stay in place if the next spot will be an obstacle. In other words I want to find the next index of list before proceeding. Here's some pseudo code for a better picture:
if next_left != [[False]]: # if there is no obstacle
officially_move_left
else: # if there is an obstacle
user_do_nothing
What I have (in pseudo):
def moving_pos(user):
copy_user.pos = user.pos
if copy_user.pos +1 == [True]
user.pos += 1
copy_user.pos += 1
else:
return user.pos
if (user_position == obstacle):
next_square_is_obstacle = true
user_move_backwards
ie, move the player, detect the object, move the player back again. It's very difficult to help without seeing the actual implementation
Edit: Okay, in your new edit I don't see any reason why the program should have such constraints. However, let me suggest something new-
obstacle_index = [obs1, obs2, obs3...]
if user_position+1 in obstacle_index:
do_nothing;
else
user_move_forward;
Can you post the actual minimal working code, or at least a more detailed pseudocode of the implementation?
Assuming that my character is falling, and headed toward some sprite platform, and I flag that my character has landed once a collision is detected, what is the best way to reset my character's position - so that he is above the platform's rectangle or so that he is still clipping it?
My concerns are that if my character is above the platform's rectangle, than my function fall() will be called because he isn't touching anything, and if my character is clipping the platform's rectangle, than my jump() function may be hindered, because it may think he's landed as soon as he takes off. Obviously there are work arounds for either, but I'm really just looking for the convention, if there is one.
(I can post code if necessary, but the code isn't the issue. I'm really just looking for the right method)
Thanks,
Mike
I would have him touching the platform so that fall() is not called again.
and if my character is clipping the platform's rectangle, than my jump() function may be hindered, because it may think he's landed as soon as he takes off.
In the jump() function, when it moves the character make it so it cannot stop until it moves a minimum of x pixels.
You can do this by making a boolean to False at the beginning of jump() and having a counter to make it True after x pixels have been traversed. Make sure that x is just enough to get the character to stop clipping the platform, and not too much so it can collide with other things.
Once it has moved x pixels, then allow the collision testing to commence to see when the character hits something again.
So for example:
MINIMUM_DISTANCE = x
# ^ how many pixels you want to move to stop clipping
count = 0 #used for counting
def jump():
global count
in_air = False
#do your movements here, and increase count each time you move a pixel
if count >= MINIMUM_DISTANCE: in_air=True
if in_air:
#collision detection here
#once there is a collision:
if collision:
count = 0
return
And just for future reference, its better to post the code you have, even if it isn't a code related error. just so we can maybe see and get a feel for your approach
hope I could help