2017-10-21 89 views
0

我正在寫一個小程序,旨在繪製一個25像素的地方,無論在哪裏,randrange會給它它的點。我還有4個紅色盒子,充當炸彈或地雷。當該行的x,y通過getColor函數爲紅色時,var'color'將變爲紅色。因此停止while循環,這將停止繼續。對於我在比賽場上繪製的藍點,這也是相同的期望功能。我發現我的程序不能以這種方式運行。關於我如何解決這個問題的任何建議?雖然循環不正確停止,因爲它應該

from random import * 
def main(): 
    #draw 
    pic = makeEmptyPicture(600, 600, white) 
    show(pic) 

    #for the 4 boxes 
    boxCount = 0 
    #while statement to draw 
    while boxCount < 4: 
     addRectFilled(pic, randrange(0,576), randrange(0,576), 25, 25, red) 
     addArcFilled(pic, randrange(0,576), randrange(0,576), 10, 10, 0, 360, blue) 
     boxCount = boxCount + 1 
    repaint(pic) 

    #vars for while statement 
    newX = 0 
    newY = 0 
    oldX = 0 
    oldY = 0 
    robotcount = 0 
    finished = 0 
    safe = 0 
    triggered = 0 
    #while loop, stops @ step 750, or when a px == red/blue 
    while robotcount < 750 or color == red or color == blue: 

     oldX = newX 
     oldY = newY 
     #how to generate a new line poing +25/-25 
     newX = newX + randrange(-25, 26) 
     newY = newY + randrange(-25, 26) 
     #if statements to ensure no x or y goes over 599 or under 0 
     if newX > 599 or newX < 0: 
      newX = 0 
     if newY > 599 or newY < 0: 
      newY = 0 
     #functions to get pixel color of x,y 
     px = getPixel(pic, newX, newY) 
     color = getColor(px) 
     #draw the line from old to new, and also add +1 count for robot's steps 
     addLine(pic, oldX, oldY, newX, newY, black) 
     robotcount = robotcount + 1 

    #if statement to determine why the while loop stops 
    if color == red: 
     triggered = 1 
     printNow("trig") 
    if color == blue: 
     safe = 1 
     printNow("safe") 
    if robotcount == 750: 
     finished = 1 
     printNow("Fin") 
+0

如何'red'和'blue'界定? – Iguananaut

+1

您是否嘗試過自己調試此問題?我打賭一些printf調試可以做到這一點。 https://ericlippert.com/2014/03/05/how-to-debug-small-programs/ – jdv

+0

@Iguananaut更像是「顏色」定義在哪裏...... –

回答

0

你想實現這一點:

#while loop, stops @ step 750, or when a px == red/blue 

這不起作用:

while robotcount < 750 or color == red or color == blue: 

這將是簡單的使用for循環,而不是:

for robotcount in range(750): 
    if color == red or color == blue: 
     break 

您也可以使用while循環,修復你的條件(注意!=):

while robotcount < 750 or color != red or color != blue: 
+0

當通過紅色框繪製線條時,它仍然不會停止,並且每個最後一個if then語句都會輸出一個不同的字符串。 – ohGosh

+0

還需要使用while語句,但尚未使用for循環。 – ohGosh

+0

用'while'添加一個選項。 –