I'm currently working on a school project and I'm stuck with this problem.
The problem is in this line of code:
if pressed[pygame.K_SPACE]:
a = 1
while a == 1:
gameDisplay.blit(bulletIMG, (x,y))
I know what that problem is, the loop will go on forever.
Is there a way to break out of this loop? or should I try a different approach.
If I understand you correctly, you want to have the user press a button and an image displays permanently:
display_image = False
while game_running:
if pressed[pygame.K_SPACE]:
display_image = True
if display_image:
gameDisplay.blit(bulletIMG, (x,y))
now the image will always be displayed because the flag will always be true once the user hits the space bar (the key is bringing the flag outside the game loop).
Related
Im trying to make a space invadors game on pygame. Once the player dies, i want to check if the retry button has been clicked. If it has, i want the game to start playing again. This is the code i currently have for when the game ends however, when i click the retry button the game doesnt play. How do i repeat my main game while loop?
for i in range(num_of_enemies):
#Game over
if enemyY[i] > 390:
for j in range(num_of_enemies):
enemyY[j] = 2000
playerY = 2000
laserX = 2000
game_over_text(0, 0)
if retry_button.draw():
running = True
I would need more of your code to know for sure, but if this function is inside of your main game loop, your main loop would probably look like while running == True: ? There is a few things that could be happening to cause your came to not run if this is the case, syntax error (example: while running = True: would run but wouldnt check if running is equal to true), you are somehow setting running to false somewhere else, or it is not recognizing your button click so if retry_button.draw(): is passed over.
I'm making a Mineclone, but am stuck in the sprinting bit.
I want code that will put the player in 'sprinting' mode when you press Ctrl, as long as you have w pressed, and exit sprinting when w is released. Releasing Ctrl while sprinting should do nothing. I tried this code among many others, but when I test it, it freezes.
def update():
global block_pick
if held_keys['control'] and held_keys['w']:
while held_keys['w']:
player.speed = 10
So the code would check every frame.
I'm not very familiar with gaming with Python, but if this is vanilla Python and not using some additional library that has to be written differently;
First your code needs to be indented under the update function.
Second the code is updating every frame, that means that the whole execution is being re-run every frame rate. So the while loop will prevent frame from updating again.
I can suggest you introduce a global boolean for sprinting, which changes based on which buttons are pressed.
sprint = False # global boolean
def update():
global block_pick
global sprint
if held_keys['control'] and held_keys['w']:
sprint = True
player.speed = 10
if sprint and not held_keys['w']: # check for button pressed while sprinting.
sprint = False
player.speed = 1
I would like to know how to break a loop as soon as its condition breaks.Its a little difficult to explain I will attach some code that will explain it well.I think I have to use things like break and continue but I am not sure where in my code should I place or maybe I need to do something else.your help would be greatly appreciated.
while 1:
if image1 appears on screen:
# do some task(it includes multiple if statements in it too)
else:
#do task 1
#do task 2
#do task 3
I want as soon as image1 appears on screen else loop should exit even if it's incomplete.like if image1 appears when else loop is doing task 2 it should exit without completing it and move to the if loop.
with my current code if image1 appears on screen the else loop only exits when all 3 tasks are done.
I want to exit the else loop not the if loop and
by exit or break I mean shift from else loop to if loop
You can use a return, like this:
if image1 appears on screen:
#do some task**
return
else:
press arrow key up for 2secs**
press arrow down key for 2sec**
UPD
I suggest this code is a part of some function.
Otherwise you will have to put it inside some function since return can be called from inside a function only.
It is also a good practice to define and use functions.
If image1 appears on screen, the else part will not execute (that's how if-else functionality works).
You can just add "break" in the end of the if (before the else)
if image1 appears on screen:
do some task**
break
else:
press arrow key up for 2secs**
press arrow down key for 2sec**
Use break where you want to stop the loop -
if image1 appears on screen:
#do some task**
break
else:
press arrow key up for 2secs**
press arrow down key for 2sec**
You can also check this
Use a break in if condition
#loop
if image1 appears on screen:
do some task**
break # this will break the loop at running iteration
else:
press arrow key up for 2secs**
press arrow down key for 2sec**
#loop
I'm new to python and i was trying to let the 1-button on the keyboard toggle fullscreen in an application using pygame. Somehow the else: statement causes it to instantly go back to windowed mode again. At least that is my understanding. Can anyone enlighten me why that happens and care to give an alternative method?
This is the code:
# screen = pygame.display.set_mode((1024, 768))
# fullscreen = False
if event.key == K_1:
if fullscreen == False:
pygame.display.set_mode((1024, 768), FULLSCREEN)
fullscreen = True
else:
pygame.display.set_mode((1024, 768))
fullscreen = False
It looks like you're checking each frame to see if your event key (K_1) is being held. Instead, you probably want to check for a KEY_UP or KEY_DOWN event. I'm not familiar with pygame - but I assume you could probably get that sort of information out of your event variable.
Alternatively, keep a variable that detects when you're still holding the key - which may look something like this:
# outside of the loop code
holding_fullscreen = false
# inside the loop code
if event.key == K_1:
if not holding_fullscreen:
holding_fullscreen = true
if not fullscreen: # more pythonic version of fullscreen == False
# set fullscreen
else:
# set windowed
else:
holding_fullscreen = False
I assume this is inside your loop. If so, every time the game refreshes, it will check for an input, and when it sees that K_1 is not pressed, fullscreen will be disabled again. You'd have to constantly hold done K_1. You need to just remove the else.
How do you make the code stop for a moment before checking for something else? (I'm new to code)
if BonusTime==True and Drop==True:
if event.type == pygame.MOUSEBUTTONDOWN:
window.blit(Fired,(mouseX-12,mouseY-12))
Cankill=True
#I want it to delay here
Cankill=False
There is a cross hair that follows the mouse and when I click it, it fires. The problem is, you can just hold the mouse down and leave the cross hair in one place. Whenever an enemy walks into the cross hair, they die. I need it so even when you hold it will only fire once. I plan to make it delay the if statement, to set "Cankill" to true and then wait a second, after waiting, it should set "Cankill" to false. I've already looked through a lot of other people's questions similar to this and haven't found something I can understand. So if you could please help me find out how to delay it in the way I need.
"Pausing" is not what you want - if you do that, your game will just freeze, and nothing will move (since you are usign the OS mouse pointer, maybe it could move).
Anyway, the function to "pause" inside a game is pygame.time.wait(1000) - the number is in miliseconds.
Waht you actually need is to mark down the time the click was made, continue with the game, and when 1 second has passed, reset the variable back to the other state.
Something along:
last_trigger = 0
while True:
# game updating code goes here (getting events, and such)
...
if Cankill and pygame.time.get_ticks() - last_trigger > 1000:
Cankill = False
if event.type == pygame.MOUSEBUTTONDOWN:
window.blit(Fired,(mouseX-12,mouseY-12))
Cankill=True
last_trigger = pygame.time.get_ticks()
The "get_ticks" call returns the number of miliseconds that passed since pygame was initialized - and is usefull for this time of timers.