2017-04-09 166 views
0

我和我的朋友目前正在編碼在學校鍛鍊,我們遇到了如何使烏龜保持正軌的困難。爲了說明,在廣場的第一面,兩隻海龜在兩側競賽。但是,當它們轉過90度時,其中的1或2個會脫離側面,這對於運動要求是不正確的。我希望你們能夠幫助我們,因爲今天是我們的到期日。下面是我們的代碼:烏龜賽車遊戲 - 無法在廣場上保持烏龜

import turtle 
from random import randint 
def read_int(prompt,first,last): 
    x = int(input(prompt)) 
    while x < first or x > last: 
     print("Not in range. Try Again!!!") 
     x= int(input(prompt)) 
    return x 

square_count = read_int("Enter your laps between 1 and 10: ",1,10) 
print(square_count) 
#def t(): 

window = turtle.Screen() 
window.bgcolor('lightblue') 
def draw_square(turtle, center, size): 
    xPt, yPt = center 
    xPt -= size/2 
    yPt += size/2 
    side = 4 
    size = 300 
    angle = 90 
    turtle.speed(0) 
    turtle.up() 
    turtle.goto(xPt, yPt) 
    turtle.down() 
    for i in range(side): 
     turtle.forward(size) 
     turtle.right(angle) 

t = turtle.Turtle() 
draw_square(t,(0,0),300) 
t.shape('turtle') 
t.color("red") 
t.pensize(5) 
t.up() 
t.goto(-150, 150) 

#def r(): 
r = turtle.Turtle() 
draw_square(r,(0,0),300) 
r.shape('turtle') 
r.color("yellow") 
r.up() 
r.pensize(5) 
r.goto(-150, 150) 

sides = 4 
size = 300 


count_int = int(square_count)* sides 
if count_int > 1: 
    for sides in range(count_int): 
      i = 0 
      e = 0 
      while i in range(0, size) or e in range(0, size): 
       t_step = randint(1, 5) 
       t.forward(t_step) 
       i = i + t_step 
       r_step = randint(1, 5) 
       r.forward(r_step) 
       e = e + r_step 

      t.right(90) 
      r.right(90) 





window.exitonclick() 

回答

0

你的錯誤在於對你的程序的結束。

while i in range(0, size) or e in range(0, size): 
       t_step = randint(1, 5) 
       t.forward(t_step) 
       i = i + t_step 
       r_step = randint(1, 5) 
       r.forward(r_step) 
       e = e + r_step 

在一個場景中可以說,我= 298,和t_step選擇一個隨機整數作爲3.龜將結束移動比300的範圍時,由此它移動所繪製的框外。 循環不停止,因爲它只是在所有代碼運行時才意識到我超出範圍。 (也就是說,龜龜在環路破裂時已經移出箱子。)這就是海龜走出屋外的原因。你可以嘗試使用'if'條件來防止這種情況。

0

你有兩種問題。
首先你應該注意你的海龜多少步驟。因爲如果一個正方形的大小爲300的可能會發生:一隻烏龜採取的步驟的數量,他們的總和不等於300,我建議你到隨機步代更改爲:

steps = random.randrange(0, 4, 2) 

在這一隻烏龜只需要0或2步就可以完成,你可以肯定這個總和會讓你達到300.
第二點可能更重要。如果像你一樣構造比賽循環,只有當另一隻烏龜到達線的末端時,烏龜纔會轉動,您應該重新組織代碼,使兩隻烏龜獨立於另一隻烏龜。

+0

好的!感謝您的建議:) –

-1

給你。有好玩 :)編碼。

import turtle 

from random import randint 

def read_int(prompt,first,last): 
    x = int(input(prompt)) 
    while x < first or x > last: 
     print("Not in range. Try Again!!!") 
     x= int(input(prompt)) 
    return x 

no_of_laps = read_int("Enter your laps between 1 and 10: ",1 , 10) 
print(no_of_laps) 

window = turtle.Screen() 

window.bgcolor('lightblue') 

def draw_square(turtle, center, size): 
    xPt, yPt = center 
    xPt -= size/2 
    yPt += size/2 
    side = 4 
    size = 300 
    angle = 90 
    turtle.speed(0) 
    turtle.up() 
    turtle.goto(xPt, yPt) 
    turtle.down() 
    for i in range(side): 
     turtle.forward(size) 
     turtle.right(angle) 

t = turtle.Turtle() 
draw_square(t,(0,0),300) 

t.shape('turtle') 
t.color("red") 
t.pensize(5) 
t.up() 
t.goto(-150, 150) 

#def r(): 
r = turtle.Turtle() 
draw_square(r,(0,0),300) 
r.shape('turtle') 
r.color("yellow") 
r.up() 
r.pensize(5) 
r.goto(-150, 150) 

sides = 4 
size = 300 

no_of_turns = int(no_of_laps) * sides 

countTurns_t = 0 
countTurns_r = 0 
t_i = 0 
r_i = 0 

while countTurns_t < no_of_turns and countTurns_r < no_of_turns: 


    t_step = randint(1, 5) 
    if t_i + t_step > size: 
     t_step = size - t_i 
     t.forward(t_step) 
     t_i = 0 
     t.right(90) 
     countTurns_t += 1 
    else: 
     t.forward(t_step) 
     t_i += t_step 

    r_step = randint(1, 5) 
    if r_i + r_step > size: 
     r_step = size - r_i 
     r.forward(r_step) 
     r_i = 0 
     r.right(90) 
     countTurns_r += 1 
    else: 
     r.forward(r_step) 
     r_i += r_step 

window.exitonclick() 

P.S.希望你能看到上面提供的代碼如何以及爲什麼按預期工作。仔細閱讀其他答案,他們指出了你錯過編寫代碼的重要問題。

+0

謝謝你,我的朋友!它有很多幫助。 –

+0

將其標記爲已接受的答案,以便任何人都可以直接看到問題已得到解答,並且未出現在尚未解答的問題列表中? – Claudio

+0

爲什麼downvote? – Claudio

0

我會採取一種稍微不同的方法 - 而不是硬編碼兩隻烏龜,設計任意數量的烏龜,然後只是比賽兩隻烏龜。這裏有一個基於任意數量的海龜的重寫,包括一些風格代碼的返工,以及@Claudio的一些代碼建議:

from random import randint 
from turtle import Turtle, Screen 
from collections import defaultdict 

SIZE = 300 
SIDES = 4 
ANGLE = 90 
MAXIMUM_STRIDE = 5 

COLORS = ('red', 'gold', 'green', 'orange', 'blue') 

MAX_RACERS = len(COLORS) 

def read_int(prompt, first, last): 
    x = int(input(prompt)) 

    while not first <= x <= last: 
     print('Not in range! Try Again.') 
     x = int(input(prompt)) 

    return x 

def draw_square(turtle, center, size): 
    xPt, yPt = center 

    xPt -= size/2 
    yPt += size/2 

    turtle.up() 
    turtle.goto(xPt, yPt) 
    turtle.down() 

    for _ in range(SIDES): 
     turtle.forward(SIZE) 
     turtle.right(ANGLE) 

lap_count = read_int('Enter number of laps (between 1 and 10): ', 1, 10) 
no_racers = read_int('Enter number of racers (between 2 and {}): '.format(MAX_RACERS), 2, MAX_RACERS) 

jockey_colors = COLORS[0:no_racers] 

window = Screen() 
window.bgcolor('lightblue') 

racers = defaultdict(dict) 

draw_square(Turtle(visible=False), (0, 0), SIZE) 

for color in jockey_colors: 
    jockey = Turtle('turtle', visible=False) 
    jockey.speed('fastest') 
    jockey.color(color) 

    jockey.up() 
    jockey.goto(-SIZE/2, SIZE/2) 
    jockey.showturtle() 

    racers[color]['jockey'] = jockey 
    racers[color]['sides'] = 0 
    racers[color]['position'] = 0 

finished = False 

while not finished: 
    for racer in racers.values(): 
     jockey = racer['jockey'] 

     step = randint(1, MAXIMUM_STRIDE + 1) 

     if racer['position'] + step > SIZE: 

      racer['sides'] += 1 

      if racer['sides'] == lap_count * SIDES: 
       finished = True 
       break 

      baby_step = SIZE - racer['position'] 
      jockey.forward(baby_step) 
      jockey.right(ANGLE) 
      racer['position'] = 0 

      step -= baby_step 

     jockey.forward(step) 
     racer['position'] += step 

window.exitonclick() 
+0

由於我的練習需要2只烏龜應該在同一個方格上跑步。因此,我無法應用此解決方案。但是,感謝您的支持。它幫助我很多! –

+0

@davidmechin,我簡化了代碼以使用單個共享方塊。 – cdlane

+0

最初提供的代碼(請參閱Claudio - 的答案中的代碼並不關心它)具有兩種隨機性來源。首先,意圖是讓步長隨機。另一種是在創建算法的過程中進行監督,根據轉折點到正方形拐角的距離縮短步長。它在那裏是因爲算法跳過了將賽車手向前移動的剩餘距離(如果有的話)。由於這個答案往往提供了一個「更好的」烏龜遊戲,我提出了一個改進版本列在這個答案中。 – Claudio