The psudeo code I am following is:
- Have the user pick a picture from a file, and show it. Get its width (w) and height (h).
- Assign variables x and y to the center of the picture.
- Let pixelsEaten be the number of pixels the bug has eaten; initially set it to 0.
- Let hungerLevel count the number of consecutive steps the bug has eaten nothing; initially set it to 0.
Repeat the following until hungerLevel reaches 400:
- Get the pixel at position (x,y) in the picture If the color is white
- The bug has encountered a blank spot!
- add 1 to hungerLevel
else
- The bug is back on the tapestry and “eats” the pixel !
- set the pixel to white add 1 to pixelsEaten
- set hungerLevel back to zero Repaint the picture
The bug moves (up to) one pixel horizontally: add a random number (-1,0,or1) to x
The bug moves (up to) one pixel vertically: add a random number (-1,0,or1) to y
If x goes off the picture to the left, reset it to 0
If x goes off the picture to the right, reset it to w-1
Do a similar thing for y if it goes off the top or bottom
In an information box display a message showing the percentage of the tapestry that the bug ate.
I have completed everything except for the pseudo code in bold. How do I determine when the "bug" moves off the picture to the left and right / up and down?
My code so far is below:
from random import*
from time import*
def main():
showInformation(" Pick a graphics file, preferably one showing a tapestry " )
file= pickAFile()
pic= makePicture( file )
show( pic )
w= getWidth( pic )
h= getHeight( pic )
x= w/2
y= h/2
pixelsEaten= 0
hungerLevel= 0
while hungerLevel != 400:
px= getPixelAt( pic, x, y )
x= x + randrange( -1, 2 )
y= y + randrange( -1, 2 )
if px == white:
hungerLevel= hungerLevel + 1
else:
setColor( px, white )
pixelsEaten= pixelsEaten + 1
hungerLevel= 0
repaint( pic )
Aucun commentaire:
Enregistrer un commentaire