簡體   English   中英

蟒蛇,如何顯示在我的代碼在龜運行游戲的總成績?

[英]python, how to show total score in turtle run game 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()

轉到說明烏龜是否在吃東西的定義。 我認為它是def distance(ts)<12: 有做起來很

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

這是整個代碼:

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()

我加入是點變量,使它這樣,當烏龜是明星就會得到一分。 並展示點我加在最后的打印(點)。 這將使它這樣的點會得到印。 當您關閉游戲時,總分將是那里的最后一個分數。 如果你想在屏幕上獲得分數。 嗯,我不知道該怎么做。 我希望這將有助於你進一步

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM