Why do I have to press the key multiple times to move? - python

So, I am writing a game in python and pygame. I was wondering, How come I have to press the "w" key multiple times to move up? I want to be able to hold any key and that action would be repeated. Also, Why does my dagger not "swing" when the character is faced down? This site is making me mad, as I am unable to format my code correctly, so just download it please.
This is where to get all the files (including the .py file):
https://www.mediafire.com/folder/k9ai685abradg/DeadWorld_Infestation_2_18-10-39
I know how buggy it is, but I want to work on it over the summer and get some stuff done.

pygame.event.get() only notifies you if a state has changed, e.g. the state of your W-key changed to pressed. I assume that you're looking for pygame.event.get()[K_w] which tells you if a key is in a pressed-state. The details about that command can be found in the pygame docs.

Related

How to fix Python stopping when it shouldnt?

I have a program that detects when a certain key is pressed, and it will press another key. I'm not the greatest at python, but I've tried everything I could to get this to work. My game will end after the hello world statement I've added as a test. Could somebody please tell me a way to fix this? (Note: I want the code to always stay running, waiting for user input)
https://i.stack.imgur.com/QeJ3r.png

Key repeat in browser from python code

Note : I am pretty surprised I did not see that same question on this forum, if I missed it please link in comment :)
I am writing a bot in python that would eventually be able to play a browser-based video game (HTML5 canvas game, not Flash). In order to do that I need to simulate the keyboard events from the python code.
I thought pyautogui would do the work, but unfortunately it seems the only way to repeat a key is to do pyautogui.press(key) in a loop, and it is not good at all for playing games as the move is not smooth at all.
I found some solutions for Windows, but I need a solution for macOSX.

How to debug Pygame application with Visual Studio during runtime

I've been making a small game for a homework assignment using Pygame with Livewires. I've been trying to debug it, but to no success; I can't get a look at the variables I want to look at before the main loop is executed. Though I can press F10 to skip over the main loop, that just stops the Autos and Watches window from working; apparently they can only records vars when the game is paused by the debugger.
Is there a way for me to use the debugger to look at the vars in runtime? Because no matter what I do while the debugger is paused, I can't look at the data inside the game objects I want to take a look in
Thanks to Jack Zhai's suggestion, I figured it out.
When the breakpoint is hit, unpause the debugger (shortcut: F5)
Play to the point in the game you want to debug.
Repause the debugger using Break All (shortcut: Ctrl-Alt-Break)
Press F10 a few times; this makes it go a few more steps in the main livewires loop.
In the autos window, there is an entry that contains the a list of the game's objects. Look through it, until you find the one(s) you're looking for. In my case, the list of the objects was self._objects. The object I was looking for was the second, which in other words, was self._objects[1].
The dropdown arrows of the object(s) you're looking for show the object's members. If you want to look at the object's properties in a less cumbersome way, use the Interactive Debugger to assign that object to a variable. That way, you can look at its values through typing objectNameHere.objectValueHere into the interactive debug console.
In my case, I had to type in player = self._objects[1] to get it assigned, then could see the player's x position by then entering player.x into the debug console.
--
I know this answer might only work for my specific problem, so if anyone else has a better one, please post it for others' sake.

Songs events in python game

I am trying to implement a game in python and i would like to have some effects songs in my game. I have done the following:
MusicStart.play()
MusicStart.stop()
I have assign all the details that needed. I want to play the song only one time. That i have done is repeated until the user press any input button.
Does someone know how to do it?
Not completely clear about your question, but here's what I did to implement music in my game (pygame):
pygame.mixer.music.load(os.path.join('.', 'path-to', 'your-music-file.wav'))
pygame.mixer.music.play(0, 0.0)
That way, the music file plays once only. Note that path-to would be the sub-folder you have the music files in (if you have one: if the file is in the same directory as the code, skip it) and your-music-file.wav would be your file, whether it's an .mp3, .wav, etc.
You can see the documentation page on it here.

Pygame: Graphical Input + Text Input

In Pygame, how can I get graphical input(e.g. clicking exit button) and also get input from the a terminal window simultaneously?
To give you context, my game has a GUI but gets its game commands from a "input()" command. How can I look for input from the command line while also handling graphics?
I'm not sure if this is possible, but if not, what other options do I have for getting text input from the user?
Thanks in advance.
You can't do that, unless you use the input command in a different thread, but then you have to deal with syncronization (which might be what you want or don't want to do).
The way I'd implement this is to create a kind of in-game console. When a special key (e.g. '\') is pressed you make the console appear, and when your application is in that state you interpreter key pressing not as in-game commands but... well, as text. You can print them in the console (using fonts). When a key (e.g "return") is pressed you can make the console disappear and the keys take back their primary functionality.
I did this for my pet-project and it works as a charm. Plus, since you are developing in python you can accept python instructions and use exec to execute them and edit your game "on fhe fly"

Categories