<<<< Q.How can I disable pause() in game_loop() once game_over() function is called?
- How to disable pause() in game_loop() once game_over() function is called?
- And why does the bullet keep re-appearing even after I have changed the bullet_state to 'ready' in function game_over()?
def game_loop(): global playerX, playerY, playerX_change, playerY_change, bulletX, bulletY, bulletY_change, bullet_state global score
# backgrounddef music
mixer.music.loadgame_loop('bg music.mp3'):
mixer.music.play(-1) global playerX, playerY, playerX_change, playerY_change, bulletX, bulletY, bulletY_change, bullet_state
global score
running = True # background music
mixer.music.load('bg music.mp3')
mixer.music.play(-1)
while running: = True
# set background image(bg)
bg = pygame.image.load('bg 2.PNG')
screen.blit(bg, (0,while 0))running:
for event in pygame.event.get # set background image(bg):
if event.typebg === pygame.QUIT:
image.load('bg 2.PNG')
runningscreen.blit(bg, =(0, False0))
iffor event.type ==in pygame.KEYDOWNevent.get():
if event.keytype == pygame.K_ESCAPEQUIT:
running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
running = False
if event.key == pygame.K_p:
pause()
game_loop()
# movement of player L, R , U & D
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
playerX_change -= 7
if event.key == pygame.K_RIGHT:
playerX_change += 7
if event.key == pygame.K_UP:
playerY_change -= 7
if event.key == pygame.K_DOWN:
playerY_change += 7
# update bullet position when shift in player position + bullet sound
if event.key == pygame.K_SPACE:
if bullet_state == "ready":
bullet_sound = mixer.Sound('bullet.wav')
bullet_sound.play()
bulletX = playerX
fire_bullet(bulletX, bulletY)
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
playerX_change = 0
if event.key == pygame.K_UP or event.key == pygame.K_DOWN:
playerY_change = 0
playerX += playerX_change
# for player stay inside game window (L & R)
if playerX >= 650: # R
playerX = 650
elif playerX <= 20: # L
playerX = 20
playerY += playerY_change
# for player stay inside game window (U & D)
if playerY >= 370: # D
playerY = 370
if playerY <= 350: # U
playerY = 350
# for enemies to stay inside game window & enemies movement
for i in range(no_of_enemies):
# game over
if enemyY[i] >= 340:
for j in range(no_of_enemies):
enemyY[j] = 1000
game_over()
break
enemyX[i] += enemyX_change[i]
if enemyX[i] >= 720: # R
enemyX_change[i] -= 5
enemyY[i] += enemyY_change[i]
elif enemyX[i] <= 20: # L
enemyX_change[i] += 5
enemyY[i] += enemyY_change[i]
# collision for every enemy
result = collision(enemyX[i], enemyY[i], bulletX, bulletY)
if result:
enemy_to_blast(enemyX[i], enemyY[i])
explosion_sound = mixer.Sound('explosion.wav')
explosion_sound.play()
score += 1
bulletY = 445
bullet_state = "ready"
enemyX[i] = random.randint(20, 720)
enemyY[i] = random.randint(20, 150)
# call enemy func
enemy(enemyX[i], enemyY[i], i)
# bullet movement
if bullet_state == "fire":
fire_bullet(bulletX, bulletY)
bulletY -= bulletY_change
# re-appearance of bullet at start point
if bulletY <= 0: # U
bulletY = 445
bullet_state = "ready"
# call funcs 'player' & 'score'
player(playerX, playerY)
display_score()
display_pause()
pygame.display.update()
def pause(): global running
def pause():
global running
# background music for paused screen
mixer.music.load('start.mp3')
mixer.music.play(-1)
paused = True
while paused:
txt_on_screen(paused_text_font_1, "PROJECT ANNIHILATION", 0, (-120), white, black)
txt_on_screen(paused_text_font_2, "0N HOLD", 0, (-70), red, black)
txt_on_screen(paused_text_font_3, " RESUME >> esc ", 0, 0, black, light_purple)
txt_on_screen(start_text_font_3, " SETTINGS >> s ", 0, 50, black, light_purple)
txt_on_screen(start_text_font_4, " QUIT >> Delete ", 0, 100, black, light_purple)
for event in pygame.event.get():
if event.type == pygame.QUIT:
paused, running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE: # RESUME GAME
paused = False
mixer.music.stop()
break
if event.key == pygame.K_DELETE:
paused, running = False
# if event.type == pygame.K_s >>>>> volume settings
pygame.display.update()
def game_over(): global bullet_state # centering game-over text txt_on_screen(game_over_font, "GAME-OVER!", 0, 0, light_purple, None) # centering game-over 'instructions' text txt_on_screen(below_txt_font, "Press 'Delete' to QUIT, 'r' to RESTART", 0, 50, white, black)
def game_over():
global bullet_state
# centering game-over text
txt_on_screen(game_over_font, "GAME-OVER!", 0, 0, light_purple, None)
# centering game-over 'instructions' text
txt_on_screen(below_txt_font, "Press 'Delete' to QUIT, 'r' to RESTART", 0, 50, white, black)
# game-over sound effect
end_sound = mixer.Sound('end.wav')
end_sound.play()
mixer.music.set_volume(0)
# unable shooting bullets
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
bullet_state = "ready"
if event.key == pygame.K_r:
game_loop()