I'm writing a code that reads the value of a joystick axis and then sends this value to an arduino. It works, however I do not want it constantly print 0 to the LXTerminal when nothing is being pressed.
The code prints: SDL_JoystickGetAxis value:0: repeatably when the axis is not being pressed.
It then prints:
SDL_JoystickGetAxis value:-32768:
SDL_JoystickGetAxis value:0:
SDL_JoystickGetAxis value:-32768:
The left drive is
-1.0
Repeatably when the axis is being pulled all the way down, or a number between -1 and 1 depending where the joystick is.
I only want it to print The left drive is <leftDrive> when the left axis is being pressed, and then nothing when it is not. How do I achieve this?
Here is my code:
import pygame
pygame.init()
pygame.joystick.init()
joystick = pygame.joystick.Joystick(0)
joystick.init()
leftDrive = 0
rightDrive = 0
while True:
try:
pygame.event.pump()
joyCheckLeft = joystick.get_axis(1)
joyCheckRight = joystick.get_axis(2)
if joyCheckLeft != 0:
pygame.event.pump()
leftDrive = joystick.get_axis(axisLeftDrive)
print 'The left drive is'
print leftDrive
writeNumber(int(2+(leftDrive*-10)))
pygame.event.pump()
time.sleep(0.1)
except KeyboardInterrupt:
quit()
Note: The code doesn't work without pygame.event.pump(). This is needed so that "get_axis()" actually returns the value of the axis.
This is related to this question Disable the Pygame console Output and the thread: http://archives.seul.org/pygame/users/Aug-2009/msg00110.html This seems to have been fixed in the bitbucket source https://bitbucket.org/pygame/pygame/downloads you could try downloading the version from there.
Related
I want to get the x, y position of the mouse (in windows 11) and use this position in the rest of the code.
I have tried two different modules but neither seem to work.
pyautogui (for a mouse click or button press)
keyboard (for a button press)
So far, i am able to get the current position (with pyautogui), but i cannot break out of the while loop to proceed to the next piece of code or even return the function.
Here is the function with my attempts:
import time
import pyautogui
import keyboard
def spam_ordinates():
''' function to determin the mouse coordinates'''
print('press "x" key to lock position...')
while True:
# Check if the left mouse button is clicked
time.sleep(0.1)
print(pyautogui.displayMousePosition())
# various methods i have tried ...
if keyboard.is_pressed('x'):
print('x key pressed...')
break
if pyautogui.mouseDown():
print("Mouse clicked!")
break
if pyautogui.keyDown('x'):
print('x key pressed (autogui)...')
break
# Get the current mouse position
x, y = pyautogui.position()
print(f'spam at position: {x}, {y}')
return x, y
# call function
ords = spam_ordinates()
i see answers like this:
Python get mouse x, y position on click, but unfortunately it doesn't actually return a value on the mouse click or button press.
So, how can i break out of the while loop such that the function returns the x, y position of the mouse?
update
it appears as though print(pyautogui.displayMousePosition()) was preventing the code from breaking out of the while loop.
I am not sure why, but commenting out that line corrected the issue.
I noticed that for some reason the print(pyautogui.displayMousePosition()) line of code was creating problems from breaking out of the loop.
when the above print statement was removed, i was able to use any of the modules:
pynput
keyboard
so this code works with the `keyboard module:
def spam_ordinates():
''' function to determin the mouse coordinates'''
print('press "x" key to lock position...')
while True:
# Check if x key is pressed
time.sleep(0.1)
if keyboard.is_pressed('x'):
print('x key pressed...')
break
# Get the current mouse position
x, y = pyautogui.position()
print(f'spam at position: {x}, {y}')
return x, y
I cannot explain completely why print(pyautogui.displayMousePosition()) caused this error, other than it must have been blocking the if statements that would have evoked the break.
I post this answer in case anybody else encounters the same.
I've seen other solutions to this problem say that you either need to call the pygame.event.pump() or initialize the joystick outside of the while loop. However, even with these solutions, I'm getting 0's for the joystick axes values.
If I uncomment just the pygame.display.set_mode((1, 1)), then the code works as expected, and the values are output to the console.
Is there a way to still get the axes values without having to create the extra window?
Also, I am running python 3.6 on Windows 10.
import pygame
FRAMES_PER_SECOND = 20
pygame.init()
pygame.joystick.init()
# pygame.display.set_mode((1,1))
# Used to manage how fast the screen updates.
clock = pygame.time.Clock()
xboxController = pygame.joystick.Joystick(0)
xboxController.init()
# Loop until the user presses menu button
done = False
print('Found controller\nStarting loop...')
while not done:
pygame.event.pump()
for event in pygame.event.get():
if event.type == pygame.JOYBUTTONDOWN and event.button == 7:
print(f'Exiting controller loop')
done = True
for i in range(xboxController.get_numaxes()):
print(f'Axis {i}: {xboxController.get_axis(i)}')
# pygame.display.flip()
clock.tick(FRAMES_PER_SECOND)
Output:
pygame 1.9.6
Hello from the pygame community. https://www.pygame.org/contribute.html
Found controller
Starting loop...
Axis 0: 0.0
Axis 1: 0.0
Axis 2: 0.0
Axis 3: 0.0
Axis 4: 0.0
.
.
.
I would probably move away from Pygame unless you need the whole underlaying GL features, as the library is meant for 2D/3D game development. Although it might be possible to use it for these purposes, issues down the line is more or less unavoidable. A perhaps simpler approach would be to go with python's input library, which can handle gamepads (joysticks).
from inputs import get_gamepad
while True:
events = get_gamepad()
for event in events:
if event.ev_type == 'Absolute':
if event.code == 'ABS_X':
print(f'Left joystick x: {event.state}')
elif event.code == 'ABS_Y':
print(f'Left joystick y: {event.state}')
elif event.code == 'ABS_RX':
print(f'Right joystick x: {event.state}')
elif event.code == 'ABS_RY':
print(f'Right joystick y: {event.state}')
Alright found the answer 5 minutes after I posted this. The problem was that I was using pygame 1.9.6 instead of 2.0.0.dev8. After updating, I'm getting the console output without the display window.
If the pygame program is just a basic entity you can move normally with arrow keys, how could i make it so that if space is pressed, based on the arrow key that was being held at the moment of pressing, a player dashes slightly to the specified direction? My idea is that when the space is pressed, program checks if other keys are being held down and based on that it rapidly increases the x and/or y coordinate and then stops at specific time, but I don't know how to make it stop as all of this is happening inside a main game loop. Any insight is highly appreciated.
You can use time.perf_counter.
So for using that function you will need to import module time
import time
And now you can store the value of time.perf_counter in a variable when space is pressed.
import time
jumping = False
start_time = 0
while True:
if ("space is pressed"): # Replace condition with what you want
jumping = True
start_time = time.perf_counter()
# Add your code here
if jumping:
# change of x, y and other jumping mechanism code here
if jumping and time.perf_counter() - start_time > 1: # Replace 1 with the amount you want this if condition to trigger, also add value in seconds
jumping = False
I'm not sure what your code is like, but this is how I'd go about it:
def dash(self):
keys = pygame.keys.get_pressed()
if keys[pygame.K_SPACE] and not self.dashing and self.jumping:
# ^^ Can be any key
if self.facing_right:
self.x += 20
if self.facing_left:
self.x -= 20
self.dashing = True
Make sure that when you hit the ground it does self.dashing = False else you will be able to dash forever.
Just put this function in the player class if you are doing it OOP.
Else if you are not using classes, put it anywhere in the file and take out all self..
I'm making a program to control the mouse with WASD but whenever I press the button the mouse just moves to the top of the screen when I need it to move slightly then be able to move again if I press the key again
I've tried to have the loop turn off then back on but it just ends the loop before it can switch back on.
import keyboard
from pynput.mouse import Controller
mouse = Controller()
repeat = True
while repeat:
if keyboard.is_pressed('w'):
mouse.move(0, -5)
if keyboard.is_pressed('s'):
mouse.move(0, 5)
I need it to move up slightly when I press w but stop when it isn't pressed so I can press it again.
You might want your keyboard not to be so sensitive, so you may need to sample the pressing time. sleep can help in this case. In the code below, the program will sleep for 0.2 seconds every time it enters while loop.
import keyboard
import time
repeat = True
while repeat:
time.sleep(0.2)
if keyboard.is_pressed('w'):
mouse.move(0, -5)
if keyboard.is_pressed('s'):
mouse.move(0, 5)
# do something
EDIT: I was misunderstanding what you want, just sleep is enough for this. The problem is because your computer does everything too fast, so a while loop might be completed in a microsecond or less. So it sees that you are pressing the key for too long (though you think you did fast) then it just repeats the loop until you release the key. That's why your mouse always moves to the top.
Sampling is the way to tell computer "if you've done, then sleep, don't do anything else".
Another way is to count how many times it loops (ie, scaling), but it looks like dump since the computer has to do worthless things.
import keyboard
from pynput.mouse import Controller
mouse = Controller()
repeat = True
w_count, s_count = 0, 0
while repeat:
if keyboard.is_pressed('w'):
w_count += 1
if w_count == 10: # i'm scaling it
mouse.move(0, -5)
w_count = 0
if keyboard.is_pressed('s'):
s_count += 1
if s_count == 10: # i'm scaling it
mouse.move(0, 5)
s_count = 0
There are also other ways: measure the pressing time, edge detection (detect key release)... You can search about "software debouncing" for more ideas and find one best fits your requirement.
I'm trying to make Connect 4 in python, but I can't figure out how to get the coordinates of the screen click so I can use them. Right now, I want to draw the board, then have someone click, draw a dot, then go back to the top of the while loop, wipe the screen and try again. I've tried a couple different options but none have seemed to work for me.
def play_game():
"""
When this function runs, allows the user to play a game of Connect 4
against another person
"""
turn = 1
is_winner = False
while is_winner == False:
# Clears screen
clear()
# Draws empty board
centers = draw_board()
# Decides whose turn it is, change color appropriately
if turn % 2 == 0:
color = RED
else:
color = BLACK
# Gets coordinates of click
penup()
onscreenclick(goto)
dot(HOLE_SIZE, color)
turn += 1
As well intentioned as the other answers are, I don't believe either addresses the actual problem. You've locked out events by introducing an infinite loop in your code:
is_winner = False
while is_winner == False:
You can't do this with turtle graphics -- you set up the event handlers and initialization code but turn control over to the main loop event handler. My following rework show how you might do so:
import turtle
colors = ["red", "black"]
HOLE_SIZE = 2
turn = 0
is_winner = False
def draw_board():
pass
return (0, 0)
def dot(color):
turtle.color(color, color)
turtle.stamp()
def goto(x, y):
global turn, is_winner
# add code to determine if we have a winner
if not is_winner:
# Clears screen
turtle.clear()
turtle.penup()
# Draws empty board
centers = draw_board()
turtle.goto(x, y)
# Decides whose turn it is, change color appropriately
color = colors[turn % 2 == 0]
dot(color)
turn += 1
else:
pass
def start_game():
"""
When this function runs, sets up a new
game of Connect 4 against another person
"""
global turn, is_winner
turn = 1
is_winner = False
turtle.shape("circle")
turtle.shapesize(HOLE_SIZE)
# Gets coordinates of click
turtle.onscreenclick(goto)
start_game()
turtle.mainloop()
Run it and you'll see the desired behavior you described.
I'm assuming that your using Turtle in python(hence the name.)
If that's the case, Here's a link to a helpful post: Turtle in python- Trying to get the turtle to move to the mouse click position and print its coordinates
I know, i know. I hate just link answers as much as the next guy. But The post I gave a link to can probably do a much better job of answering your question than I can.
~Mr.Python
Assuming you're using turtle as mentioned in your title:
>>> import turtle
>>> help(turtle.onscreenclick)
Help on function onscreenclick in module turtle:
onscreenclick(fun, btn=1, add=None)
Bind fun to mouse-click event on canvas.
Arguments:
fun -- a function with two arguments, the coordinates of the
clicked point on the canvas.
num -- the number of the mouse-button, defaults to 1
Example (for a TurtleScreen instance named screen)
>>> onclick(goto)
>>> # Subsequently clicking into the TurtleScreen will
>>> # make the turtle move to the clicked point.
>>> onclick(None)
That means that your callback function, which you have apparently named goto, will take two parameters, an X and Y location.
import turtle
def goto(x, y):
print('Moving to {}, {}'.format(x,y))
turtle.goto(x, y)
turtle.onscreenclick(goto)
turtle.goto(0,0)
Each click that you make will move the turtle to a different position. Note that turtle already has an event loop - you don't need one of your own. Just respond to the clicks.
basically, you need to add an 'x' and 'y' parameter for the onclick and onscreenclick functions. You don't need to use them, they're just dummy params. After filling those out the clicks will work no problem:
window = turtle.Screen()
This function uses the x, y params because i'm saving the clicks in order to specify an area to fill with turtles
def on_left_click_save_coordinates(x, y):
global counter, Fill_COORS1, Fill_COORS2
counter += 1
print(x, y)
if counter == 1:
Fill_COORS1 = (x, y)
elif counter == 2:
Fill_COORS2 = (x, y)
counter = 0
This one doesn't use the x,y params because they are dummies, this one is used to allow multiple options, one of which exits, another tells the turtle to fill in the specified area saved in clicks above.
def on_right_click_open_options(x, y):
global going
last_color = options(window, filler, Fill_COORS1, Fill_COORS2, LAST_BLOCK_USED)
if type(Last_COLOR) == type(bool):
going = True
window.onscreenclick(on_click, btn=1)
window.onscreenclick(open_options, btn=3)
This is an example of a snippet of my code. hope this helps.
btn 3 refers to the right click
btn 1 is the default and isn't necessary to specify and refers to left click
btn 2 is the scroll wheel click, not scroll.
and sorry if this isn't formatted the best, it's my first time posting to stackoverflow. Hope it helps nonetheless