开发者社区> 问答> 正文

我怎样才能让我的乌龟赛跑游戏持续运行?

我是新来的编程,并已作出一个简单的海龟比赛游戏。游戏目前暂停后,一只乌龟到达终点线。然而,我很难让游戏持续运行。 我不确定在游戏循环的最佳方式,即每次刷新每只海龟的位置,但保留每只海龟的分数。任何建议将不胜感激。 代码:

# Basic Turtle Game

import time
import turtle
from turtle import *
from turtle import Turtle
from random import randint

play_again = True

#START WINDOW SET UP
#Window setup
window = turtle.Screen()
#Window Properties
window.title("Turtle Race!")
window.bgcolor("white")
window.setup(width=800, height=800)


#Start Title
title_turtle = turtle.Turtle()
title_turtle.penup()
title_turtle.speed(0)
title_turtle.setpos(-100,300)
title_turtle.color("black")
title_turtle.write("Turtle Race", font=("Arial", 25, "bold"))


#Finish Line
turtle.color("black")
turtle.penup()
turtle.speed(0)
turtle.setpos(-220,240)
turtle.pendown()
turtle.pensize(10)
turtle.forward(405)
turtle.hideturtle()


#Turtles
#Turtles 1
turtle_1 = turtle.Turtle()
turtle_1.speed(0)
turtle_1.penup()
turtle_1.setpos(-200,-120)
turtle_1.color("red")
turtle_1.shape("turtle")
turtle_1.pendown()
turtle_1.left(90)

#Turtles 2
turtle_2 = turtle.Turtle()
turtle_2.penup()
turtle_2.speed(0)
turtle_2.setpos(-140,-120)
turtle_2.color("green")
turtle_2.shape("turtle")
turtle_2.pendown()
turtle_2.left(90)

#Turtles 3
turtle_3 = turtle.Turtle()
turtle_3.penup()
turtle_3.speed(0)
turtle_3.setpos(-80,-120)
turtle_3.color("blue")
turtle_3.shape("turtle")
turtle_3.pendown()
turtle_3.left(90)

#Turtles 4
turtle_4 = turtle.Turtle()
turtle_4.penup()
turtle_4.speed(0)
turtle_4.setpos(-20,-120)
turtle_4.color("yellow")
turtle_4.shape("turtle")
turtle_4.pendown()
turtle_4.left(90)

#Turtles 5
turtle_5 = turtle.Turtle()
turtle_5.penup()
turtle_5.speed(0)
turtle_5.setpos(40,-120)
turtle_5.color("cyan")
turtle_5.shape("turtle")
turtle_5.pendown()
turtle_5.left(90)

#Turtles 6
turtle_6 = turtle.Turtle()
turtle_6.penup()
turtle_6.speed(0)
turtle_6.setpos(100,-120)
turtle_6.color("purple")
turtle_6.shape("turtle")
turtle_6.pendown()
turtle_6.left(90)

#Turtles 7
turtle_7 = turtle.Turtle()
turtle_7.penup()
turtle_7.speed(0)
turtle_7.setpos(160,-120)
turtle_7.color("orange")
turtle_7.shape("turtle")
turtle_7.pendown()
turtle_7.left(90)

#scores
turtle_1_score = 0
turtle_2_score = 0
turtle_3_score = 0
turtle_4_score = 0
turtle_5_score = 0
turtle_6_score = 0
turtle_7_score = 0

#scoarboard
pen = turtle.Turtle()
pen.speed(0)
pen.color("black")
pen.penup()
pen.hideturtle()
pen.goto(-40, -210)
pen.write(" " + str(turtle_1_score) + "     " + str(turtle_2_score) + "     " + str(turtle_3_score) + "     " + str(turtle_4_score) + "     " + str(turtle_5_score) + "     " + str(turtle_6_score) + "     " + str(turtle_7_score) + "     ", align="center", font=("Arial", 18, "bold"))

pen2 = turtle.Turtle()
pen2.hideturtle()
pen2.speed(0)
pen2.color("black")
pen2.penup()
pen2.goto(-40, -270)

#pause game for 1 second
time.sleep(1)

winner = None
#Move the turtles

while not winner:
        for i in range(1):
            turtle_1.forward(randint(1,8))
            turtle_2.forward(randint(1,8))
            turtle_3.forward(randint(1,8))
            turtle_4.forward(randint(1,8))
            turtle_5.forward(randint(1,8))
            turtle_6.forward(randint(1,8))
            turtle_7.forward(randint(1,8))
        if turtle_1.ycor() > 220:
            turtle_1_score += 1
            pen.clear()
            pen.write(" " + str(turtle_1_score) + "     " + str(turtle_2_score) + "     " + str(
                turtle_3_score) + "     " + str(turtle_4_score) + "     " + str(turtle_5_score) + "     " + str(
                turtle_6_score) + "     " + str(turtle_7_score) + "     ", align="center", font=("Arial", 18, "bold"))
            winner = "Turtle 1"
        elif turtle_2.ycor() > 220:
            turtle_2_score += 1
            winner = "Turtle 2"
            pen.clear()
            pen.write(" " + str(turtle_1_score) + "     " + str(turtle_2_score) + "     " + str(
                turtle_3_score) + "     " + str(turtle_4_score) + "     " + str(turtle_5_score) + "     " + str(
                turtle_6_score) + "     " + str(turtle_7_score) + "     ", align="center", font=("Arial", 18, "bold"))
        elif turtle_3.ycor() > 220:
            turtle_3_score += 1
            winner = "Turtle 3"
            pen.clear()
            pen.write(" " + str(turtle_1_score) + "     " + str(turtle_2_score) + "     " + str(
                turtle_3_score) + "     " + str(turtle_4_score) + "     " + str(turtle_5_score) + "     " + str(
                turtle_6_score) + "     " + str(turtle_7_score) + "     ", align="center", font=("Arial", 18, "bold"))
        elif turtle_4.ycor() > 220:
            turtle_4_score += 1
            winner = "Turtle 4"
            pen.clear()
            pen.write(" " + str(turtle_1_score) + "     " + str(turtle_2_score) + "     " + str(
                turtle_3_score) + "     " + str(turtle_4_score) + "     " + str(turtle_5_score) + "     " + str(
                turtle_6_score) + "     " + str(turtle_7_score) + "     ", align="center", font=("Arial", 18, "bold"))
        elif turtle_5.ycor() > 220:
            turtle_5_score += 1
            winner = "Turtle 5"
            pen.clear()
            pen.write(" " + str(turtle_1_score) + "     " + str(turtle_2_score) + "     " + str(
                turtle_3_score) + "     " + str(turtle_4_score) + "     " + str(turtle_5_score) + "     " + str(
                turtle_6_score) + "     " + str(turtle_7_score) + "     ", align="center", font=("Arial", 18, "bold"))
        elif turtle_6.ycor() > 220:
            turtle_6_score += 1
            winner = "Turtle 6"
            pen.clear()
            pen.write(" " + str(turtle_1_score) + "     " + str(turtle_2_score) + "     " + str(
                turtle_3_score) + "     " + str(turtle_4_score) + "     " + str(turtle_5_score) + "     " + str(
                turtle_6_score) + "     " + str(turtle_7_score) + "     ", align="center", font=("Arial", 18, "bold"))
        elif turtle_7.ycor() > 220:
            turtle_7_score += 1
            winner = "Turtle 7"
            pen.clear()
            pen.write(" " + str(turtle_1_score) + "     " + str(turtle_2_score) + "     " + str(
                turtle_3_score) + "     " + str(turtle_4_score) + "     " + str(turtle_5_score) + "     " + str(
                turtle_6_score) + "     " + str(turtle_7_score) + "     ", align="center", font=("Arial", 18, "bold"))
        else:
           continue

pen2.write("{} won the race!".format(winner),align="center", font=("Arial", 18, "bold"))

turtle.exitonclick()

#Keeps Screen on when code finishes
turtle.done()*

问题来源StackOverflow 地址:/questions/59380419/how-can-i-make-my-turtle-race-game-run-continuously

展开
收起
kun坤 2019-12-28 14:18:18 489 0
1 条回答
写回答
取消 提交回答
  • 技术架构师 阿里云开发者社区技术专家博主 CSDN签约专栏技术博主 掘金签约技术博主 云安全联盟专家 众多开源代码库Commiter

    赞一个,算法不错

    2020-01-04 22:46:10
    赞同 展开评论 打赏
问答地址:
问答排行榜
最热
最新

相关电子书

更多
云端设计,与时间赛跑 立即下载
白帽子的成长独白-消失的那几年 立即下载
让世界没有陌生的角落 共享单车时代的快与慢 立即下载