Я закончил свою первую игру и хотел бы добавить рекорд на экран «игра окончена».
У меня есть переменная под названием «оценка». Эта «оценка» увеличивается по мере того, как вы попадаете в игру. Эта «оценка» также отображается на экране во время игры. Если вы набрали ie, ваш окончательный счет отобразится на экране «игра окончена». Все, что я хочу сделать, это добавить высокий балл на этот экран.
Я искал это, но почему-то не нашел ясного пользователя. Я нашел много разных awnsers, но все они слишком специфичны для кода, который использовали люди. Должен быть простой ответ на этот вопрос, верно?
Есть только 1 игрок, и я хочу показать только 1 высокий балл. Я все еще очень плохо разбираюсь в программировании, пожалуйста, сделайте это как можно проще ?
Обновлен код с awnser в нем под game_over
import pygame
import sys
import time
import random
from pygame.locals import *
pygame.init()
pygame.display.set_mode()
def main():
width = 1400
height = 800
blue = (0, 0, 255)
black = (0, 0, 0)
white = (255, 255, 255)
red = (255, 0, 0)
dino = pygame.image.load("dino.png").convert_alpha()
meteor = pygame.image.load("meteor.png").convert_alpha()
player_location = [width - 1350, height - 200]
player_size = [100, 200]
obstacle_size = [101, 101]
obstacle_location = [width - 100, height - 100]
obstacle_list = [obstacle_location]
screen = pygame.display.set_mode((width, height))
speed = 10
score = 0
clock = pygame.time.Clock()
FPS = 80
myFont = pygame.font.SysFont("monospace", 35)
run = True
def set_speed(score, speed):
if score < 5:
speed = 10
elif score < 10:
speed = 15
elif score < 20:
speed = 20
elif score < 35:
speed = 25
elif score < 50:
speed = 30
elif score < 75:
speed = 35
elif score < 100:
speed = 40
else:
speed = 50
return speed
def spawn_obstacle(obstacle_list):
delay = random.random()
dist = random.choice([100, 300])
if len(obstacle_list) < 3 and delay < 0.0075:
x_pos = width-100
y_pos = height-dist
obstacle_list.append([x_pos, y_pos])
def draw_obstacle(obstacle_list):
for obstacle_location in obstacle_list:
pygame.draw.rect(screen, white, (obstacle_location[0], obstacle_location[1], obstacle_size[0], obstacle_size[1]))
screen.blit(meteor, (obstacle_location[0], obstacle_location[1]))
def update_obstacle_positions(obstacle_list, score):
for idx, obstacle_location in enumerate(obstacle_list):
if obstacle_location[0] >= 0 and obstacle_location[0] < width:
obstacle_location[0] -= speed
else:
obstacle_list.pop(idx)
score += 1
return score
def collision_check(obstacle_list, player_location):
for obstacle_location in obstacle_list:
if detect_collision(obstacle_location, player_location):
return True
return False
def detect_collision(player_location, obstacle_location):
p_x = player_location[0]
p_y = player_location[1]
p_width = player_size[0]
p_height = player_size[1]
o_x = obstacle_location[0]
o_y = obstacle_location[1]
o_width = obstacle_size[0]
o_height = obstacle_size[1]
if (o_x >= p_x and o_x < (p_x + p_width)) or (p_x >= o_x and p_x < (o_x + o_width)):
if (o_y >= p_y and o_y < (p_y + p_height)) or (p_y >= o_y and p_y < (o_y + o_height)):
return True
return False
end_it = False
while (end_it == False):
screen.fill(black)
for event in pygame.event.get():
if event.type == MOUSEBUTTONDOWN:
end_it = True
if event.type == pygame.QUIT:
run = False
pygame.quit()
sys.exit()
myfont = pygame.font.SysFont("Britannic Bold", 40)
label = myfont.render("Click to start", 1, red)
screen.blit(label,(600,550))
label_2 = myfont.render("Hold ARROW UP to jump", 1, white)
screen.blit(label_2, (520, 380))
myfont2 = pygame.font.SysFont("Britannic Bold", 120)
label_3 = myfont.render("Hold SPACE to duck", 1, white)
screen.blit(label_3, (550, 420))
label_4 = myfont2.render("T-REX RUN", 1, white)
screen.blit(label_4, (470,200))
pygame.display.flip()
def game_over():
while game_over:
screen.fill(black)
text = "Game Over, Press SPACE to restart"
label = myFont.render(text, 1, white)
screen.blit(label, (350, 350))
end_score = "Score:" + str(score)
label_2 = myFont.render(end_score, 1, white)
screen.blit(label_2, (350, 250))
file = open("highscore.txt", "r")
content = file.read()
content = str(content)
if content < str(score):
file = open("highscore.txt", "w")
file.write(str(score))
hs = "You got a new highscore"
label_3 = myFont.render(hs, 1, white)
screen.blit(label_3, (350, 250))
pygame.display.update()
else:
hs = "Highscore: " + content
label_3 = myFont.render(hs, 1, white)
screen.blit(label_3, (350, 250))
pygame.display.update()
file.close()
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
main()
while run:
clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN:
x = player_location[0]
y = player_location[1]
p_width = player_size[0]
p_height = player_size[1]
if event.key == pygame.K_UP:
y -= 200
player_location = [x,y]
if event.type == pygame.KEYUP:
if event.key == pygame.K_SPACE or event.key == pygame.K_UP:
player_size = [100, 200]
player_location = [50, 600]
keys = pygame.key.get_pressed()
if keys[pygame.K_SPACE]:
p_height = 100
y = 700
player_size = [p_width,p_height]
player_location = [x,y]
screen.fill(white)
spawn_obstacle(obstacle_list)
score = update_obstacle_positions(obstacle_list, score)
speed = set_speed(score, speed)
text = "Score:" + str(score)
label = myFont.render(text, 1, blue)
screen.blit(label, (600, 250))
if collision_check(obstacle_list, player_location):
game_over()
draw_obstacle(obstacle_list)
pygame.draw.rect(screen, white, (player_location[0], player_location[1], player_size[0], player_size[1]))
screen.blit(dino, (player_location[0], player_location[1]-39))
pygame.display.update()
main()