Im trying to make a replay button for my game where when you die the game pauses until you press the space bar which resets the game so you can play again. I’m using python with pygame. If you can fix this, please do! It would make my day. If possible, please include the code for the fix and where to put it because I am new to python. The source code below is the game I’m working on.
import pygame import random import sys import time pygame.init() WIDTH = 800 HEIGHT = 600 TRASH = (110, 110, 110) RED = (135, 56, 47) player_pos = [400, 475] player_size = 50 screen = pygame.display.set_mode((WIDTH, HEIGHT)) enemy_size = 50 SPEED = 11 FPS = 120 enemy_pos = [random.randint(0, WIDTH - enemy_size), 0] enemy_list = [enemy_pos] game_over = False score = 0 clock = pygame.time.Clock() myFont = pygame.font.SysFont("monospace", 35) pygame.display.set_caption('Crab Dodge, By: ChanceMeteor515') pygame.mouse.set_visible(0) def drop_enemies(enemy_list): if len(enemy_list) < 9: x_pos = random.randint(0, WIDTH - enemy_size) y_pos = 10 enemy_list.append([x_pos, y_pos]) def draw_enemies(enemy_list): for enemy_pos in enemy_list: pygame.draw.rect(screen, TRASH, (enemy_pos[0], enemy_pos[1], enemy_size, enemy_size)) def update_enemies_pos(enemy_list, score): for idx, enemy_pos in enumerate(enemy_list): if enemy_pos[1] >= 0 and enemy_pos[1] < HEIGHT: enemy_pos[1] += SPEED else: enemy_list.pop(idx) score += 1 return score def collision_check(enemy_list, player_pos): for enemy_pos in enemy_list: if detect_collision(enemy_pos, player_pos): return True return False def edge_detection1(player_pos): if player_pos[0] > 750: return True return False def edge_detection2(player_pos): if player_pos[0] < 0: return True return False def detect_collision(player_pos, enemy_pos): p_x = player_pos[0] p_y = player_pos[1] e_x = enemy_pos[0] e_y = enemy_pos[1] if (e_x >= p_x and e_x < (p_x + player_size)) or (p_x >= e_x and p_x < (e_x + enemy_size)): if (e_y >= p_y and e_y < (p_y + player_size)) or (p_y >= e_y and p_y < (e_y + enemy_size)): return True return False while not game_over: for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() if event.type == pygame.KEYDOWN: x = player_pos[0] y = player_pos[1] if event.key == pygame.K_LEFT: x -= player_size elif event.key == pygame.K_RIGHT: x += player_size elif event.key == pygame.K_a: x -= player_size elif event.key == pygame.K_d: x += player_size elif event.key == pygame.K_q: x -= player_size elif event.key == pygame.K_ESCAPE: sys.exit() player_pos = [x, y] screen.fill((194, 178, 128)) drop_enemies(enemy_list) score = update_enemies_pos(enemy_list, score) text = "Score:" + str(score) label = myFont.render(text, 1, (0, 0, 0)) screen.blit(label, (WIDTH - 200, HEIGHT - 40)) if edge_detection1(player_pos): player_pos[0] = 0 if edge_detection2(player_pos): player_pos[0] = 750 if collision_check(enemy_list, player_pos): time.sleep(1.4) game_over = True draw_enemies(enemy_list) pygame.draw.rect(screen, RED, (player_pos[0], player_pos[1], player_size, player_size)) clock.tick(FPS) pygame.display.update()
Advertisement
Answer
OK, I’ve done what you’ve wanted but it requires Tkinter cause I was a bit lazy.
import pygame import random import sys import time import os from tkinter import * from tkinter import messagebox print(os.getcwd()) pygame.init() def game(): WIDTH = 800 HEIGHT = 600 TRASH = (110, 110, 110) RED = (135, 56, 47) player_pos = [400, 475] player_size = 50 game_over = False screen = pygame.display.set_mode((WIDTH, HEIGHT)) enemy_size = 50 SPEED = 11 FPS = 120 enemy_pos = [random.randint(0, WIDTH - enemy_size), 0] enemy_list = [enemy_pos] score = 0 clock = pygame.time.Clock() myFont = pygame.font.SysFont("monospace", 35) pygame.display.set_caption('Crab Dodge, By: ChanceMeteor515') pygame.mouse.set_visible(0) def drop_enemies(enemy_list): if len(enemy_list) < 9: x_pos = random.randint(0, WIDTH - enemy_size) y_pos = 10 enemy_list.append([x_pos, y_pos]) def draw_enemies(enemy_list): for enemy_pos in enemy_list: pygame.draw.rect(screen, TRASH, (enemy_pos[0], enemy_pos[1], enemy_size, enemy_size)) def update_enemies_pos(enemy_list, score): for idx, enemy_pos in enumerate(enemy_list): if enemy_pos[1] >= 0 and enemy_pos[1] < HEIGHT: enemy_pos[1] += SPEED else: enemy_list.pop(idx) score += 1 return score def collision_check(enemy_list, player_pos): for enemy_pos in enemy_list: if detect_collision(enemy_pos, player_pos): return True return False def edge_detection1(player_pos): if player_pos[0] > 750: return True return False def edge_detection2(player_pos): if player_pos[0] < 0: return True return False def detect_collision(player_pos, enemy_pos): p_x = player_pos[0] p_y = player_pos[1] e_x = enemy_pos[0] e_y = enemy_pos[1] if (e_x >= p_x and e_x < (p_x + player_size)) or (p_x >= e_x and p_x < (e_x + enemy_size)): if (e_y >= p_y and e_y < (p_y + player_size)) or (p_y >= e_y and p_y < (e_y + enemy_size)): return True return False while not game_over: for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() if event.type == pygame.KEYDOWN: x = player_pos[0] y = player_pos[1] if event.key == pygame.K_LEFT: x -= player_size elif event.key == pygame.K_RIGHT: x += player_size elif event.key == pygame.K_a: x -= player_size elif event.key == pygame.K_d: x += player_size elif event.key == pygame.K_q: x -= player_size elif event.key == pygame.K_ESCAPE: sys.exit() player_pos = [x, y] screen.fill((194, 178, 128)) drop_enemies(enemy_list) score = update_enemies_pos(enemy_list, score) text = "Score:" + str(score) label = myFont.render(text, 1, (0, 0, 0)) screen.blit(label, (WIDTH - 200, HEIGHT - 40)) if edge_detection1(player_pos): player_pos[0] = 0 if edge_detection2(player_pos): player_pos[0] = 750 if collision_check(enemy_list, player_pos): time.sleep(1.4) Tk().wm_withdraw() #to hide the main window game_over = True draw_enemies(enemy_list) pygame.draw.rect(screen, RED, (player_pos[0], player_pos[1], player_size, player_size)) clock.tick(FPS) pygame.display.update() ans = messagebox.askretrycancel("askretrycancel", "Try again?") root=Tk() root.destroy() return ans while True: if game() == True: pass else: break