如何在游戏中添加重播按钮?

How Do You Add A Replay Button To A Game?

我正在尝试为我的游戏制作一个重播按钮,当您死亡时,游戏会暂停,直到您按下 space 栏,这会重置游戏,以便您可以再次玩游戏。我正在使用 python 和 pygame。如果你能解决这个问题,请做!这会让我开心。如果可能,请包括修复代码以及放置代码的位置,因为我是 python 的新手。下面的源代码是我正在开发的游戏。

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()

好的,我已经完成了你想要的,但它需要 Tkinter 因为我有点懒。

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