我正在寫一個小程序,旨在繪製一個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")
如何'red'和'blue'界定? – Iguananaut
您是否嘗試過自己調試此問題?我打賭一些printf調試可以做到這一點。 https://ericlippert.com/2014/03/05/how-to-debug-small-programs/ – jdv
@Iguananaut更像是「顏色」定義在哪裏...... –