2017-04-17 81 views
1

所以我用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")) 

在計時結束後我的節目的最後,我想烏龜停止運行;用戶無法移動烏龜。

回答

-1

您可以使用Pygame使您的工作更輕鬆。

while x < 30: 
    x+=1 
    pygame.time.delay(1000) 
    if x == 30: 
     speed == 0 

這是一個計時器,倒計數到三十。 Pygame.time.delay以毫秒爲單位進行計數,因此您在一秒鐘內使用1000。我寫了if聲明,因爲你希望在定時器完成後烏龜無法控制,我相信這是你用來使烏龜移動。如果沒有找到你用來控制烏龜,並停止它。

0

,要實現這一點的另一個簡單的方法是做到以下幾點:

  1. 添加保存要尊重內超時當比賽開始
  2. 總結了代號爲時間戳的變量在當前時間和開始時間之間的差異小於您的時間限制時運行的while循環。

例如:

import time 
starting_time = time.time() 
time_limit = 30 

while (time.time() - starting_time) < time_limit: 
    # YOUR GAME LOGIC HERE 
0

我去給你一些烏龜ontimer()事件代碼來處理你的倒計時,但實現你的代碼不正確地組織來處理它。例如。你無限的while True:循環有可能會阻止一些射擊事件,並且它會明確地阻止你的邊界和碰撞代碼運行。即使沒有,你的邊界和碰撞代碼也只能在需要運行每一次宇宙飛船運動時運行一次。

我已將ontimer()事件的代碼重新組織爲運行飛船,另一個運行倒計時器。我已經修復了邊界和碰撞代碼以基本工作。我省略了一些功能(如spaceship.shapesize(add))來簡化這個例子:

from turtle import Turtle, Screen 
import random 

MAXIMUM_POINTS = 6 
FONT = ('Arial', 16, 'normal') 
WIDTH, HEIGHT = 600, 400 

wn = Screen() 
wn.bgcolor('lightblue') 
# Score Variable 
score = 0 

# Turtle Player 
spaceship = Turtle() 
spaceship.color('red') 
spaceship.penup() 

speed = 1 

# Border 
border = Turtle(visible=False) 
border.pensize(5) 
border.color('darkblue') 

border.penup() 
border.goto(-WIDTH/2, -HEIGHT/2) 
border.pendown() 

for _ in range(2): 
    border.forward(WIDTH) 
    border.left(90) 
    border.forward(HEIGHT) 
    border.left(90) 

border.penup() 
border.goto(-WIDTH/2 + 10, HEIGHT/2 + 10) 
border.write('Score: {}'.format(score), align='left', font=FONT) 

# Create Goals 
points = [] 

for _ in range(MAXIMUM_POINTS): 
    goal = Turtle('circle') 
    goal.color('green') 
    goal.penup() 
    goal.goto(random.randint(20 - WIDTH/2, WIDTH/2 - 20), \ 
     random.randint(20 - HEIGHT/2, HEIGHT/2 - 20)) 
    points.append(goal) 

# 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): 
    return turtle1.distance(turtle2) < 20 

# Keyboard Bindings 
wn.onkey(left, 'Left') 
wn.onkey(right, 'Right') 
wn.onkey(increasespeed, 'Up') 
wn.onkey(decreasespeed, 'Down') 
wn.onkey(wn.bye, 'q') 

wn.listen() 

timer = 30 

def countdown(): 
    global timer 

    timer -= 1 

    if timer <= 0: # time is up, disable user control 
     wn.onkey(None, 'Left') 
     wn.onkey(None, 'Right') 
     wn.onkey(None, 'Up') 
     wn.onkey(None, 'Down') 
    else: 
     wn.ontimer(countdown, 1000) # one second from now 

wn.ontimer(countdown, 1000) 

def travel(): 
    global score 

    spaceship.forward(speed) 

    # Boundary 
    if not -WIDTH/2 < spaceship.xcor() < WIDTH/2: 
     spaceship.left(180) 

    if not -HEIGHT/2 < spaceship.ycor() < HEIGHT/2: 
     spaceship.left(180) 

    # Point collection 
    for count in range(MAXIMUM_POINTS): 
     if iscollision(spaceship, points[count]): 
      points[count].goto(random.randint(20 - WIDTH/2, WIDTH/2 - 20), \ 
       random.randint(20 - HEIGHT/2, HEIGHT/2 - 20)) 
      score += 1 
      # Screen Score 
      border.undo() 
      border.write('Score: {}'.format(score), align='left', font=FONT) 

    if timer: 
     wn.ontimer(travel, 10) 

travel() 

wn.mainloop() 
-1

我的東西是用time.time()是有幫助的。如果您在代碼的開頭設置了一個變量(如startTime)到time.time(),則可以在主遊戲循環中添加一個if。 if將檢查是否有足夠的時間過去。例如:

if time.time() > startTime + 30: #whatever you do to end the game

time.time()給出秒當前的時間,因此,如果你在代碼開頭的變量設置爲time.time(),該變量會給你在比賽開始的時間。在if中,檢查當前時間是否已達到您的遊戲開始時間加上30秒。

我希望這有助於;如果它確實是upvote! :)

相關問題