首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >保存高分并在屏幕上显示

保存高分并在屏幕上显示
EN

Stack Overflow用户
提问于 2020-04-05 13:06:58
回答 1查看 434关注 0票数 0

我已经完成了我的第一场比赛,我想把高分加到游戏的屏幕上。

我有一个叫做“分数”的变量。这个“分数”随着你深入到游戏中而增加。这个“分数”也显示在屏幕上,同时播放。如果你死了,你的最后分数就会出现在屏幕上的游戏上。我想做的就是把高分加到这个屏幕上。

我找过这个,但不知怎么没找到一个清晰的喷泉。我已经找到了许多不同的awnser,但是都是特定于人们使用的代码。这个问题必须有一个简单的答案,对吗?

只有一个球员,我只想表现出1个高分。我对编程还是很陌生的,请尽量简单

在game_over下更新包含awnser的代码

代码语言:javascript
复制
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()
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-04-05 13:14:24

您可以尝试创建一个名为highscore的.txt文件。首先,您可以编码(在代码的开头) newfile = open("highscore.txt", "w+")来创建您的文件。然后删除该代码,并在您的代码中(我认为这是正确的位置)有:

代码语言:javascript
复制
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 = int(content)
                file.close()
                if content > score:
                    file = open("highscore.txt", "w")
                    file.write(score)
                    hs = "You got the highscore. well done!"
                    label_3 = my_Font.render(hs, 1, white)
                    screen.blit(label_3, (350, 150)) #Or any other position you want!
                    pygame.display.update()
                else:
                    hs = "The highscore is: ", content
                    label_3 = my_Font.render(hs, 1, white)
                    screen.blit(label_3, (350, 150)) #Or any other position you want!
                    pygame.display.update()  

内容可以是您最后输出的高分,也可以是您的代码如何决定用户是否拥有新的高分。希望这能帮上忙!如果要在python之外创建文件,请记住保存文件的末尾具有文件类型(例如.txt),并将文件保存在与python程序相同的目录(文件夹)中。如果你不明白这点,请我把它简化一些。,我还没有测试过它,所以您可能会有错误需要修复,但是先尝试一下,然后告诉我代码是如何完成的!

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61043076

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档