简体   繁体   English

如何为乌龟游戏添加高分系统?

[英]How do I add a high score system to my turtle game?

I'm still pretty new to Python but I've been working on a game using turtle where you have to click a bunch of circles within a certain timeframe for a score. 我对Python还是很陌生,但是我一直在使用Turtle进行游戏,在这种游戏中,您必须在一定时间内单击一堆圆圈以获得得分。 I want to create a high score system where people can decide on their usernames as well but everywhere I google is too complicated for me to understand, so I have no idea how to implement it into my code. 我想创建一个高分系统,人们也可以决定他们的用户名,但是我在Google上遇到的每个地方都太复杂了,以至于我无法理解,因此我不知道如何在我的代码中实现它。

import turtle
from random import random, randint
import time

CURSOR_SIZE = 20
score=0

while True:
    diffSetting=int(input("Set the difficulty(1-5,1 being easiest and 5 being hardest): "))
    if diffSetting == 1:
        difficulty = 5
        break
    elif diffSetting == 2:
        difficulty = 8
        break
    elif diffSetting == 3:
        difficulty = 12
        break
    elif diffSetting == 4:
        difficulty = 16
        break
    elif diffSetting == 5:
        difficulty = 20
        break
    else:
        print("Please choose a difficulty setting between 1 to 5.")


def addscore():
    global score
    score += 1

def my_circle(color):

    if diffSetting==1:
        radius = (50)
    elif diffSetting==2:
        radius = (40)
    elif diffSetting==3:
        radius = (30)
    elif diffSetting==4:
        radius = (20)
    elif diffSetting==5:
        radius = (10)


    circle = turtle.Turtle('circle', visible=False)
    circle.shapesize(radius / CURSOR_SIZE)
    circle.color(color)
    circle.penup()

    while True:
        nx = randint(2 * radius - width // 2, width // 2 - radius * 2)
        ny = randint(2 * radius - height // 2, height // 2 - radius * 2)

        circle.goto(nx, ny)

        for other_radius, other_circle in circles:
            if circle.distance(other_circle) < 2 * max(radius, other_radius):
                break
        else:
            break

    circle.showturtle()
    circle.onclick(lambda x,y,t=circle: (circle.hideturtle(), addscore()))


    return radius, circle



screen = turtle.Screen()
screen.bgcolor("lightgreen")
screen.title("Speed Clicker")

width, height = screen.window_width(), screen.window_height()

circles = []

gameLength = 30

startTime = time.time()
while True:
    time.sleep(1/difficulty)

    rgb = (random(), random(), random())

    timeTaken = time.time() - startTime

    circles.append(my_circle(rgb))
    screen.title('SCORE: {}, TIME LEFT: {}'.format(score,int(round(gameLength - timeTaken,0))))

    if time.time() - startTime > gameLength:
        for turtle in screen.turtles():
            turtle.reset()
        break


screen.title('GG! FINAL SCORE: {}'.format(score))

screen.mainloop()

I've been trying to keep the score in a text file but how would I keep the usernames? 我一直试图将分数保存在文本文件中,但是如何保留用户名?

  1. You have to to store score in data base or file which you are moat prefer 您必须将分数存储在您喜欢的数据库或文件中
  2. After completing your game you must have to check you current score with stored score 完成游戏后,您必须使用存储的分数来检查当前分数
  3. If your current score is less then your stored score do nothing or restart game 如果您的当前分数小于该分数,则您存储的分数不执行任何操作或重新启动游戏
  4. Else you have to update your high score with previous stored score 否则,您必须使用先前存储的分数来更新您的高分数

You Must Have Knowledge About Store Data 您必须具有有关商店数据的知识

  1. Create a file in the same folder as this game. 在与此游戏相同的文件夹中创建一个文件。
  2. Open the file and store the high score as a variable in your Python program. 打开文件并将高分作为变量存储在Python程序中。 (0 if file is empty) (如果文件为空,则为0)
  3. Every time the current score is greater than the high score, update the file. 每次当前得分大于最高得分时,请更新文件。

I'll write a small snippet, extend it to your wish. 我会写一个小片段,将其扩展到您的愿望。

First, create a file named 'scoreboard.txt' in the same folder as your game. 首先,在与游戏相同的文件夹中创建一个名为“ scoreboard.txt”的文件。 Do the following to get the details before starting the game. 在开始游戏之前,请执行以下操作以获取详细信息。

f = open('scoreboard.txt', 'r')
name = f.readline()
high_score = int(f.readline())
f.close()

Check if they've got a highscore and get username (new_name) too: 检查他们是否有高分并获得用户名(new_name):

if current_score > high_score:
    f = open('scoreboard.txt', 'w')
    f.write(new_name + '\n' + str(current_score))
    f.close()

NOTE: For the first time, fill the file with some random data like 'BOB' (press enter key), '24'. 注意:第一次用一些随机数据填充文件,例如“ BOB”(按Enter键),“ 24”。

Hope it helps! 希望能帮助到你! Comment if there's any doubts! 如有任何疑问,请发表评论!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM