简体   繁体   中英

python, how to show total score in turtle run game in my code?

i am making 'turtle run' game. this game is that turtle is chasing food, escaping from 'red turtle'. i almost make this game, but i cant do one thing ...that if turtle catch the food, showing numbers that turtle catch the food. how to add something in my code?

import turtle as t
import random
te=t.Turtle()
te.shape('turtle')
te.color('red')
te.speed(0)
te.up()
te.goto(0,200)

ts=t.Turtle()
ts.shape('circle')
ts.color('green')
ts.speed(0)
ts.up()
ts.goto(0,-200)

def turn_right():
    t.setheading(0)
def turn_up():
    t.setheading(90)
def turn_left():
    t.setheading(180)
def turn_down():
    t.setheading(270)

def play():
    t.forward(15)
    ang=te.towards(t.pos())
    te.setheading(ang)
    te.forward(9)
    
    
    if t.distance(ts)<12:
        star_x=random.randint(-230,230)
        star_y=random.randint(-230,230)
        ts.goto(star_x,star_y)
        
        
    if t.distance(te)>=12:
        t.ontimer(play,100)

t.setup(500,500)
t.bgcolor('orange')
t.shape('turtle')
t.speed(0)
t.up()
t.color('white')
t.onkeypress(turn_up,'Up')
t.onkeypress(turn_down,'Down')
t.onkeypress(turn_right,'Right')
t.onkeypress(turn_left,'Left')
t.listen()
play()

Go to the definition that tells if the turtle is eating something. I think it is the def distance(ts)<12: . There make it so

global points
if ts.goto(star_x, star_y):
    points += 1

This is the whole code:

import turtle as t
import random

te = t.Turtle()
te.shape('turtle')
te.color('red')
te.speed(0)
te.up()
te.goto(0, 200)
points = 0

ts = t.Turtle()
ts.shape('circle')
ts.color('green')
ts.speed(0)
ts.up()
ts.goto(0, -200)


def turn_right():
    t.setheading(0)


def turn_up():
    t.setheading(90)


def turn_left():
    t.setheading(180)


def turn_down():
    t.setheading(270)


def play():
    global points
    t.forward(15)
    ang = te.towards(t.pos())
    te.setheading(ang)
    te.forward(9)

    if t.distance(ts) < 12:
        star_x = random.randint(-230, 230)
        star_y = random.randint(-230, 230)
        ts.goto(star_x, star_y)
        if ts.goto(star_x, star_y):
            points += 1

    if t.distance(te) >= 12:
        t.ontimer(play, 100)


t.setup(500, 500)
t.bgcolor('orange')
t.shape('turtle')
t.speed(0)
t.up()
t.color('white')
t.onkeypress(turn_up, 'Up')
t.onkeypress(turn_down, 'Down')
t.onkeypress(turn_right, 'Right')
t.onkeypress(turn_left, 'Left')
t.listen()
print(points)
play()

What I added are the points variable and made it so that when the turtle is at the star it will get a point. And to show the points I added the print (points) at the end. That will make it so the points will get printed. When you close the game the total score will be the last score there. If you want to make the score to get on the screen. Ummm, I do not know how to do that. I hope this will help you further

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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