Left click detection with ursina - python

I'm using ursina to make a game, and I want to detect a left click so that I can shoot. So, here's my code :
def update(self):
if held_keys['t']:
print("it works !")
Whenever I press 't', it prints 'it works !', and if I hold it, as long as it's held, the message is printed. Great ! But now, if I try with 'left mouse down' for my key, it doesn't work anymore !
My code would then be :
def update(self):
if held_keys['left mouse down']:
print("it works !")
So, the problem here is clearly the 'left mouse down' argument. But I'm sure it is the correct syntax :
according to the documentation (https://www.ursinaengine.org/cheat_sheet.html#Keys)
and according to another test I made with it, where it worked (in another situation)
So, the syntax of my argument is correct, my code is correct. Where is the error located then ? Is there a specific way to handle the mouse, different than the keyboard ? I really don't think so, that's why I'm kinda confused here.

The way to debug this would be to print the held_keys dict to see what it contains. The correct name is 'left mouse'. It is that way because the mouse button names are named differently than other keys and are mostly there to make code changes easier. Mouse buttons aren't keys after all.
However, what you could do is check mouse.left instead.

Related

clearing the terminal for my python text adventure

working with the current version of python after a quick google search and a perusal of stack overflow I'm not seeing what I'm looking for. I'm trying to find a way to clear the screen with every new print to the screen. So you would get the intro text for a room and there is a monster in the room so when you press a to attack it would delete the intro text and the new text saying what weapon you used and how much damage you did would be printed. then when the monster dies you the damage prompts would go away and you would get the dead enemy empty room text with the press these keys to go a direction text. the current version of my game is here https://github.com/GusRobins60/AdventureGame.git hopefully that will give you an idea of the text i'd be displaying. thanks for any help.
For windows you can use a lambda function to easily create an anonymous function which you can use to clear the terminal again and again:
clear = lambda: os.system('cls')
clear()
Or if you're on Linux then swap 'cls' with 'clear'.
Are you using windows? If so, you can try:
import os
os.system('cls')
Or something similar after each page is complete and needs to be cleared.
To clear the terminal using ANSI escape codes:
import sys
sys.stdout.write("\33[H\33[2J") #"\33" is the ESC character
sys.stdout.flush()
\33[H moves the cursor to the top left of the terminal, and \33[2J clears the screen. Using sys.stdout.write here instead of print avoids writing a newline, so the cursor stays on the first line of the screen, instead of being on the second line when using print().

Hashtag character not displaying correctly

The following fails to print a hashtag?
import pyautogui
pyautogui.typewrite('#');
It prints '~'.
Pyautogui is keyboard layout dependent.
You could try switching your keyboard layout to the US keyboard layout (which is what Pyautogui is based on). For example, the French Canadian keyboard layout has a # where the ~ key normally is, which could explain why you're experiencing this behavior.
As Sean Kennedy said, it's a keyboard layout issue. They're still working on support for non-US-English keyboard layouts.
https://github.com/asweigart/pyautogui/issues/137
Luckily you have an English keyboard layout, so you'll have far fewer issues and you should be able to patch them. You can redefine whether certain keys need shift or not by editing _pyautogui_win.py:
def _keyDown(key):
if key not in keyboardMapping or keyboardMapping[key] is None: return
needsShift = pyautogui.isShiftCharacter(key)
# insert this code, exactly here:
if key == '#': needsShift = False
if key == '+': needsShift = False
if key == '<': needsShift = False
#print(key,needsShift) #####debug
# continue
Taken from:
https://github.com/asweigart/pyautogui/issues/46#issuecomment-132640299
I haven't looked at the code myself, but you might find that you're better or worse making the edit inside isShiftCharacter.
I should have kept reading. isShiftCharacter might become a rabbit hole:
By the way: Deleting #,+,< in the following function, did NOT work
(util.py in .\pyautogui):
def isShiftCharacter(character): return character.isupper() or
character in '~!##$%^&*()_+{}|:"<>?'
Maybe .isupper() regards these characters as upper? I didn't check
further
I think this can be an alternative if pyautogui doesn't work for #,etc
import keyboard
keyboard.write('#')

Issues running a code using Pygame

OKay, so i wrote a code to develop a game using pygame. this is the aim of the game :
there is a player (Mario) which can move only vertically. from the right side of the window, flames appear which the mario has to dodge. the game is very similar to dodger.py !
now, when i run the game, it gets stuck at "Press any key to Enter"
PLEASE HELP !
You are not doing anything in the waitforkey() function.
if event.type == KEYDOWN:
# if key exit blah blah
else:
runGame()
You could put your game in a function called runGame and that would probabaly be the easiest way of doing this. Just remember that the variables will then be local to the scope of that function and any changes won't affect the rest of the program.
Having checked the code on a PC, I have found 3 errors. Two of which are typing errors. First one is on Line 77:
playerrect.topleft = (50,window_hight/2)
Needs to be:
playerrect.topleft = (50,window_height/2)
and the second is on line 126:
WindowSurface.fill(bgcolour)
You haven't defined bgcolour (as far as I could see) so by adding the following to the variables at the top of the file:
bgcolour(255,255,255) #change to what colour you want
The third error I found was in your waitForKey() function. I don't know whether this is important to the running of the program, but you have your if event.type == "QUIT" in speech marks. Like I said, this may not matter but I thought I'd point it out. You have also done this for the other conditions in this function.
By making these changes, you get running code. However, flames do not appear and I don't have time to figure this one out. By fiddling though, I'm sure you'll figure it out!

Pygame not recognising 1 key?

I have been making a game using pygame in python and it refuses to identify the 1 key.
if event.type == KEYDOWN and event.key == K_1:
print("pass")
started = True
I added the "pass" to check if something else is wrong but nothing happens. I have used a few other keys like up, down, w, s and enter which work but it won't recognize any numbers.
If it helps, I am using a Trust Xpress wireless keyboard.
In regard to Dominic's question , I have tried both number pad and standard keys.
Your problem probably has nothing to do with your keyboard.
You probably just left numb lock off and are using the keypad.
If not, get the window to print all events and see if anything comes up when you press the 1 key.
It should be 49, but if it's not, just use whatever it is.
If nothing comes up at all, and you can't use the keyboard elsewhere, then it is something wrong with your keyboard.

(Python) Detecting arrow key(press) on Windows

I tried to search for my answer before I posted, but I wasn't able to find anything that could help me here.
I currently have this in my script:
def waitForDir(self):
while True:
if msvcrt.kbhit():
keyHit = msvcrt.getch()
print keyHit
(It's part of a larger class)
Whenever I press the arrow keys, I get a pretty weird-looking symbol that kinda reminds me of a fish (think of an infinity symbol with the right half of the second loop cut off) and a letter -- K for left, H for up, M for right, P for down. (I'm using the arrow keys typically between the numpad and the main keyboard, below the home/insert/delete/all that jazz keys)
...how can I get this to something I can use? I want to run events whenever one of the keys is pressed, but I really don't know how to do it.
I can provide more info if I need to. Thanks in advance!
EDIT: Nevermind! I figured out the solution by enclosing msvcrt.getch() with ord() to get usable numbers. Thanks anyway!

Categories