if direction == 'right': mowerText = MOWER_RIGHT elif direction == 'left': mowerText = MOWER_LEFT for i in range(MOWER_LEN): if 0 <= mowerx + i < WIDTH: bext.goto(x + i, mowery) print(mowerText[i], end='') # Draw the initially uncut grass field: bext.clear() if sys.platform == 'win32': bext.hide() # Currently, hiding the cursor only works on Windows. bext.fg('green') for i in range(HEIGHT): print(';' * WIDTH) print('Press Ctrl-C to quit.') # mowerx and mowery refer to the left edge of the lower, despite direction. mowerx = -MOWER_LEN mowery = 0 mowerDirection = 'right' growMode = False while True: # Main program loop. # Draw the mower: drawMower(mowerx, mowery, mowerDirection)
def drawMower(x, y, direction): bext.fg('red') if direction == 'right': mowerText = MOWER_RIGHT elif direction == 'left': mowerText = MOWER_LEFT for i in range(MOWER_LEN): if 0 <= mowerx + i < WIDTH: bext.goto(x + i, mowery) print(mowerText[i], end='') # Draw the initially uncut grass field: bext.clear() bext.hide() bext.fg('green') for i in range(HEIGHT): print(';' * WIDTH) print('Press Ctrl-C to quit.') # mowerx and mowery refer to the left edge of the lower, despite direction. mowerx = -MOWER_LEN mowery = 0 mowerDirection = 'right' growMode = False while True: # Draw the mower: drawMower(mowerx, mowery, mowerDirection)
def main(): # Draw the initially uncut grass field: bext.clear() if sys.platform == 'win32': bext.hide() # Currently, hiding the cursor only works on Windows. bext.fg('green') for i in range(HEIGHT): print(';' * WIDTH) print('Press Ctrl-C to quit.') # mowerx and mowery refer to the left edge of the lower, despite direction. mowerx = -MOWER_LEN mowery = 0 mowerDirection = 'right' growMode = False while True: # Main program loop. # Draw the mower: drawMower(mowerx, mowery, mowerDirection) # Draw the cut grass: if (mowerDirection == 'right') and (mowerx - 1 >= 0): bext.goto(mowerx - 1, mowery) bext.fg('green') print(',', end='') elif (mowerDirection == 'left') and (mowerx < WIDTH - MOWER_LEN): bext.goto(mowerx + MOWER_LEN, mowery) bext.fg('green') print(',', end='') # Move the mower: if mowerDirection == 'right': mowerx += 1 # Move the mower right. if mowerx > WIDTH: # After going past the right edge, # change position and direction. mowerDirection = 'left' mowery += 1 if mowery == HEIGHT: # Done mowing, let the grass grow back: growMode = True elif mowerDirection == 'left': mowerx -= 1 # Move the mower left. if mowerx < -MOWER_LEN: # After going past the left edge, # change position and direction. mowerDirection = 'right' mowery += 1 if mowery == HEIGHT: # Done mowing, let the grass grow back. growMode = True sys.stdout.flush() # (Required for bext-using programs.) time.sleep(MOWING_PAUSE) # Pause after mowing. if growMode: # Let the grass grow back one at a time: mowerx = -MOWER_LEN # Reset mower position. mowery = 0 # Reset mower position. bext.fg('green') # Create a set of all the places the grass needs to grow: grassToGrow = set() for x in range(WIDTH): for y in range(HEIGHT): grassToGrow.add((x, y)) # Grow the grass: while len(grassToGrow) > 0: x, y = random.sample(grassToGrow, 1)[0] grassToGrow.remove((x, y)) bext.goto(x, y) print(';') try: time.sleep(GROWING_PAUSE) # Pause after growing. except KeyboardInterrupt: sys.exit() # When Ctrl-C is pressed, end the program. growMode = False # Done growing grass.