Issues running a code using Pygame - python

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!

Related

Left click detection with ursina

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.

Python Keyboard Module, wait for user

I'm currently building a python code to play Blackjack where I need to be able to call certain functions uppon keypress without necessarily being in the terminal. I found this keyboard module which helped but I came across this issue.
def hit_or_stand(deck,hand):
global playing
print ("\nWould you like to Hit or Stand? Enter 'h' or 's'")
if keyboard.is_pressed('h'):
hit(deck,hand)
if keyboard.is_pressed('s'):
print("Player stands. Dealer is playing.")
playing = False
Let me explain what this function does as I don't want to overload everyone with the entire code. Essentially this function is called in a loop until the player decides to hit s which declares playing as False and therefore stopping the loop. If the player hits h then the hit function is called which draws a card. The player can draw as many cards.
My problem right now is that this code doesn't wait for user keypress. It loops extremely fast repeating it. I only want it to loop after either h or s is pressed.
I tried using keyboard.wait('h') on the line above the first if but then this doesn't allow the user to press s and therefore declare playing as False.
What I'm looking for is something along the lines of keyboard.wait('h'or's') but I know this does not work.
Thank you for your help. I am using python 3.7.9
EDIT: Keyboard Module I am referring too:
https://pypi.org/project/keyboard/
Use keyboard.read_key() to wait for any input.
If it's not h or s ,read_key again.
import keyboard
def mywait():
keyboard.read_key()
def my_function():
print("Hello")
def my_exit():
quit()
keyboard.add_hotkey("p", my_function)
keyboard.add_hotkey("esc", my_exit)
while True:
mywait()

Copied Code works, written code does not?

New Python programmer here. I'm trying to write Snake in python without the use of tutorials. I'm now working on key binds, but I'm running into a problem
#Move snake to the left
def move_right():
right = snake.xcor()
right += 20
snake.setx(right)
#Key bind "d"
wn.listen
wn.onkeypress(move_right(), "d")
The above code is supposed to move the snake to the right when the "d" key is pressed, but it does not work. However, when copying code from a fully functioning Pong program from a tutorial on YouTube, it does work. The copied code is this:
#Function for paddle_b_up
def paddle_b_up():
y = paddle_b.ycor()
y += 20
paddle_b.sety(y)
#key bind to the up arrow key
wn.onkeypress(paddle_b_up, "Up")
I then proceeded to write out the copied code, exactly as it was written in the Pong program, into the Snake program and it does not work. I am very confused. The indenting is the same, the code is the same (save a few different, but consistent variables), and only the movement and key binding code from the Pong program was copied into the Snake program. Without changing anything else, the Pong code works, but only when I copy and paste the code into the Snake program. Any advice? Thank you.
Notice the difference between the two calls to wn.onkeypress(). In the copied code there's no () after paddle_b_up, but in your code you have move_right().
When you put () after a function name it calls it immediately. But you want the function to be called later, when the key is pressed. You need to pass a reference to the function, not the result of calling the function.
wn.onkeypress(move_right, "d")
You also need () after wn.listen so you call the function.

Tkinter, help a noob understand Tk's loops

I am Python 3 noob creating a checkers game, I have a bunch of functions... and my game's loop is as follows:
while EndGame==0:
PrintBoard()
PrintBoardGui()
print("################","\n","Player 1","\n","################","\n")
Player=1
PlayerSelectPiece()
MovePiece()
PrintBoard()
PrintBoardGui()
print("################","\n","Player 2","\n","################","\n")
Player=2
if NbrPlayer==2:
PlayerSelectPiece()
else:
AiSelectPiece()
MovePiece()
PrintBoardGui() that I run at the beggining of each turn and creates a Tkinter window and draws the board on a new Canvas in a Tkinter Frame.
After that, I have to close the window in order for the program to continue.
I know this is a sub-optimal solution.
I have looked around trying to understand Tkinter's loops and read a bit about the after() function but i really don't know how I could implement it in my code.
Ultimatly I would like my Tkinter window to stay open (mabye disabled or something) while I input stuff in the console to move the pieces. Can you help me?
At first, how do you want to interact with your game ?
With text in the console ?
With buttons ?
With keyboard/mouse event ?
The "after" method is used to refresh your screen after an amount of time.
It will call the method that you pass through the parameters.
You shouldn't have to put an infinite loop in it. But you will have to check with a simple condition the game ending, to display another screen.
If you have to use the console entry, it can be a bit hard for a beginner to manage the GUI update and the console.
I am not sure what ur question was but, If you would want to run the code forever (until you stop it). you would have to use the while loop like this:
while "thing" == True:
PrintBoard()
PrintBoardGui()
print("################","\n","Player 1","\n","################","\n")
Player=1
PlayerSelectPiece()
MovePiece()
PrintBoard()
PrintBoardGui()
print("################","\n","Player 2","\n","################","\n")
Player=2
if NbrPlayer==2:
PlayerSelectPiece()
else:
AiSelectPiece()
MovePiece()
thing = False
at the end you change thing to False otherwise it would be infinite and it would bug.

Adding an extra loop causes unresponsiveness

I'm making a simple game using Pygame where you have to get (by moving the cursor over them) all the circles that will appear on the screen (more will appear every second). The code is quite long so I made an example code. This code works fine, the Pygame window doesn't become unresponsive at all:
import pygame, random, sys
pygame.init()
window=pygame.display.set_mode((480,360))
end_program=False
while not end_program:
for event in pygame.event.get():
if event.type==pygame.QUIT or pygame.key.get_pressed()[pygame.K_ESCAPE]: #If the user either click the "x", or pressed the "esc" key
end_program=True
pass
pygame.quit()
sys.exit()
However, in my game, in order to give the user the choice to play again, I need to wrap everything inside end_program in another loop. In the example shown, this is break_from_second_loop:
import pygame, random, sys
pygame.init()
window=pygame.display.set_mode((480,360))
end_program=False
while not end_program:
for event in pygame.event.get():
if event.type==pygame.QUIT or pygame.key.get_pressed()[pygame.K_ESCAPE]: #If the user either click the "x", or pressed the "esc" key
end_program=True
break_from_second_loop=False
while not break_from_second_loop:
pass
pygame.quit()
sys.exit()
Now if this is run, the window becomes unresponsive! Anyone know why something as simple as wrapping the code in another loop (without altering the code at all) does this?
The problem is that the game can't respond, or do anything at all, if you're not running the event loop. And in the other loop, you're not running the event loop.
This is a general problem with event-loop-based programming. You can't do anything that takes a long time, and you can't do anything that has to run across multiple events.
So, you have to break your loop apart into steps, and do just one step (or a few of them) each time through the event loop.
In this particular case, it's actually pretty simple: just change that while to an if (and move the has_got_all_circles=False outside the main loop), and your logic now runs exactly once each time through the event loop.
Alternatively, change it to an if and also move it inside the for, so now it runs exactly once per event, instead of once per event loop iteration.
A third alternative is to factor out the whole thing into a function and set it as an idle or timer function, that runs whenever the event loop is idle, or once every frame, or once every 20ms, or whatever.
It's hard to know which of the three is appropriate in your case, but the basic idea is the same in all of them, so I'll just show the second one:
end_program=False
break_from_second_loop=False
while not end_program:
for event in pygame.event.get():
if event.type==pygame.QUIT or pygame.key.get_pressed()[pygame.K_ESCAPE]: #If the user either click the "x", or pressed the "esc" key
end_program=True
if not break_from_second_loop:
pass
This blog post explains the general problem in more detail—although most of it isn't really appropriate to this specific problem.
The issue you're having is that you're not nesting your event loop code within the while loop that does your game logic. Here's the general structure of what you want:
while not end_program:
while not end_game:
handle_events()
do_one_frame_of_game_logic()
offer_another_game()
It's possible that offer_another_game will also need to be run in its own loop, with its own event handling code.
Indeed, you might want to encapsulate the logic you want to use into a state machine system. You'd have states like PlayingGame, GameOver, and DoYouWantToPlayAgain, each of which would run for a while, then hand off to another state. Your main loop would then be something like:
state = StartState()
while state:
state.handle_events()
state.update()
state.draw()
state = state.next_state() # most of the time, the state will return itself

Categories