我正在写一个小程序,打算画一条线25像素的地方,随机范围将给它的点。我也有4个红色的盒子,充当炸弹或地雷。当getColor函数将行的x,y设置为红色时,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")
您希望实现以下目标:
这不起作用:
使用
for
循环会更简单:您还可以使用
while
循环并修复您的条件(注意!=
):相关问题 更多 >
编程相关推荐