所以我用Python做了一個遊戲,儘管我很難過,但我差不多完成了。 有幾件事我想要:我想給我的程序添加一個倒數計時器
- 我想做一個30秒的計時器。玩家可以在30秒內收集儘可能多的粒子。
- 二,定時器完成後,龜不再可控。
我需要做些什麼來解決這些問題?
屏幕設置
import turtle
import math
import random
#screen
wn=turtle.Screen()
wn.bgcolor("lightblue")
speed=1
wn.tracer(2)
#Score Variable
score=0
#Turtle Player
spaceship= turtle.Turtle()
spaceship.pensize(1)
spaceship.color("red")
spaceship.penup()
turtle.delay(3)
spaceship.shapesize(1,1)
add=1
#Create Goals
maxpoints = 6
points = []
for count in range(maxpoints):
points.append(turtle.Turtle())
points[count].color("green")
points[count].shape("circle")
points[count].penup()
points[count].goto(random.randint(-300,300), random.randint(-200,200))
#Border
border = turtle.Turtle()
border.penup()
border.goto(-300,-200)
border.pendown()
border.pensize(5)
border.color("darkblue")
for side in range(2):
border.forward(600)
border.left(90)
border.forward(400)
border.left(90)
#Functions
def left():
spaceship.left(30)
def right():
spaceship.right(30)
def increasespeed():
global speed
speed += 1
def decreasespeed():
global speed
speed -= 1
def iscollision(turtle1,turtle2):
collect = math.sqrt(math.pow(turtle1.xcor()-turtle2.xcor(),2)+ math.pow(turtle1.ycor()-turtle2.ycor(),2))
if collect <20:
return True
else:
return False
鍵盤結合移動龜各地
#Keyboard Bindings
turtle.listen()
turtle.onkey(left,"Left")
turtle.onkey(right,"Right")
turtle.onkey(increasespeed ,"Up")
turtle.onkey(decreasespeed ,"Down")
turtle.onkey(quit, "q")
pen=100
while True:
spaceship.forward(speed)
#Boundary
if spaceship.xcor()>300 or spaceship.xcor()<-300:
spaceship.left(180)
if spaceship.ycor()>200 or spaceship.ycor()<-200:
spaceship.left(180)
#Point collection
for count in range(maxpoints):
if iscollision(spaceship, points[count]):
points[count].goto(random.randint(-300,300), random.randint(-200,200))
score=score+1
add+=1
spaceship.shapesize(add)
#Screen Score
border.undo()
border.penup()
border.hideturtle()
border.goto(-290,210)
scorestring = "Score:%s" %score
border.write(scorestring,False,align="left",font=("Arial",16,"normal"))
在計時結束後我的節目的最後,我想烏龜停止運行;用戶無法移動烏龜。