我如何给我的乌龟游戏装上最高分系统?

问题描述 投票:2回答:1
import turtle
import time
import winsound
import random
from tkinter import Tk, Frame, Menu

win = turtle.Screen()
win.title("Collect Balls! v2.0.20")
win.bgcolor("#64E15E")
win.setup(600, 700)
win.tracer(0)


#Timer
class Timer(turtle.Turtle):
    def __init__(self, x, y, c, p, sec, action):
    turtle.Turtle.__init__(self)
    self.sec = 0
    self.pensize = "30"
    self.action = action
    self.ht()
    self.color("red")
    self.penup()
    self.goto(0, -260)
    self.write(time.strftime("%H:%M:%S", time.gmtime(self.sec)), False
               , align="center", font=("Arial", self.pensize, "bold"))
def start(self):
    self.clear()
    self.write(time.strftime("%H:%M:%S", time.gmtime(self.sec)), False
               , align="center", font=("Arial", self.pensize, "bold"))
    self.sec += 1

    if self.sec != -1:
        win.ontimer(self.start, 1000)
    else:
        self.action()
def stop(self):
    self.clear()
def restart(self):
    self.clear()
    self.sec = 0
    self.write(time.strftime("%H:%M:%S", time.gmtime(self.sec)), False, align="center", font=("Arial", self.pensize, "bold"))
def testaction():
    timer.restart()

timer = Timer(0, 0, "red", 100, 15, testaction)
timer.start()

##ACTUAL GAME CODES##

delay = 0.1
score = 0


#Head
head = turtle.Turtle()
head.speed(0)
head.shape("square")
head.color("black")
head.penup()
head.goto(0, 0)
head.direction = "stop"

#Balls
food = turtle.Turtle()
food.speed(0)
food.shape("square")
food.color("white")
food.penup()
food.goto(0, 50)

#Pen
pen = turtle.Turtle()
pen.speed(0)
pen.color("black")
pen.penup()
pen.hideturtle()
pen.goto(0, 260)
pen.write("Balls Collected: 0", align="center", font=("Ariel", 24, "normal"))

peen = turtle.Turtle()
peen.speed(0)
peen.color("black")
peen.penup()
peen.hideturtle()
peen.goto(0, 230)
peen.write("Top Score: ", align="center", font=("Ariel", 20, "bold"))



#Functions
def go_up():
    head.direction="up"

def go_down():
    head.direction="down"

def go_left():
    head.direction="left"

def go_right():
    head.direction="right"


def move():
    if head.direction == "up":
        y = head.ycor()
        head.sety(y + 20)

if head.direction == "down":
    y = head.ycor()
    head.sety(y - 20)

if head.direction == "left":
    x = head.xcor()
    head.setx(x - 20)

if head.direction == "right":
    x = head.xcor()
    head.setx(x + 20)

# Keyboard
win.listen()
win.onkeypress(go_up, "Up")
win.onkeypress(go_down, "Down")
win.onkeypress(go_left, "Left")
win.onkeypress(go_right, "Right")





while True:
    win.update()

    if head.xcor()>290 or head.xcor()<-290 or head.ycor()>320 or head.ycor()<-320:
        time.sleep(1)
        head.goto(0,0)
        food.goto(0,50)
        head.direction = "stop"
        pen.clear()
        pen.write("Balls Collected: 0".format(score), align="center", font=("Ariel", 24, "normal"))
        peen.clear()
        peen.write("Top Score: {}".format(score), align="center", font=("Ariel", 20, "bold"))
        score = 0
        timer.stop()
        timer.restart()

    if head.distance(food) < 20:
        x = random.randint(-290, 290)
        y = random.randint(-290, 290)
        score += 1
        pen.clear()
        pen.write("Balls Collected: {}".format(score), align="center", font=("Ariel", 24, "normal"))
        food.goto(x,y)
        winsound.PlaySound("smw_coin.wav", winsound.SND_ASYNC)


    move()

    time.sleep(delay)

win.mainloop()

我有一个有点像最高分系统的系统,但它不是最高分,而是写最新的分数。我也创建了一个.txt文件来存储数据,但我无法让它工作。我试着看了其他文章,但也不行。请帮助我

python turtle-graphics
1个回答
1
投票

添加一个变量 topscore:

delay = 0.1
score = 0
topscore = 0

最高分是最高分(max)的 score 和当前的 topscore:

topscore = max(topscore, score)
peen.write("Top Score: {}".format(topscore), align="center", font=("Ariel", 20, "bold")) 
© www.soinside.com 2019 - 2024. All rights reserved.