如何在游戏中添加一个重播按钮?
我正在尝试为我的游戏制作一个重播按钮,当您死亡时,游戏会暂停,直到您按空格键重置游戏,以便您可以再次玩。我正在使用 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()
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()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好的,我已经完成了您想要的工作,但它需要TKINTER,因为我有点懒。
OK, I've done what you've wanted but it requires Tkinter cause I was a bit lazy.