Saturday, September 2, 2023

TI-84 Plus CE Python: Drawing Random Paths

TI-84 Plus CE Python: Drawing Random Paths



Introduction



The Python script PATHS draws a path from the left side to one of the other three sides of the screen:  bottom, right, or up.  


The path is laid on a 70 x 70 grid, consisting on 10 x 10 blocks.  Due to the how the screen is set up, the grid does not look like squares.   A random number between 1 and 3 is picked and the path moves in one of three ways:


1:  The path moves up

2:  The path moves right

3:  The path moves down


Three modules are used:


>  random module

>  ti_draw module (exclusive to TI):  drawing commands

>  time module (exclusive to TI):  used for the sleep command



TI-84 Plus CE Python Script:  paths.py


This code should work on the TI-83 Premium CE and the TI-Nspire CX II.  


# draw a random path

# EWS 2023-06-19


from ti_draw import *

from random import *

from time import *



clear()

set_window(0,70,0,70)


# initialization

set_color(0,128,0)

x=0

y=40

# draw using upper left corner

fill_rect(x,y,10,10)


# main loop

while x<60 and y>10 and y<60:

  r=randint(1,3)

  if r==1:

    y+=10

    set_color(0,0,224)

  if r==2:

    x+=10

    set_color(0,128,0)

  if r==3:

    y-=10

    set_color(255,165,0)

  fill_rect(x,y,10,10)

  sleep(.25)


# set end indicator

set_color(0,0,0)

fill_rect(0,40,10,10)

fill_rect(x,y,10,10)


# text - allow for text height

# have to play around

set_color(255,255,255)

draw_text(2,42,"BGN")

draw_text(x+2,y+2,"END")

show_draw()


Notes:


>  The show_draw() command on the end let's us see all the steps.   A sleep command (time module) at the end of the loop to slow down between each execution of the loop.

>  The fill_rect both take coordinates to be the upper-left hand corner of box.  

>  The draw_text works similarly to fill_rect except we have to account for the size of the font.   

>  The set_window command allows us to set the coordinates for the window.  If there is no set_window command, the screen would operate by pixels instead. The TI-84 Plus CE screen is 319 x 209 pixels.  


Three example paths are shown below:  






Eddie 


All original content copyright, © 2011-2023.  Edward Shore.   Unauthorized use and/or unauthorized distribution for commercial purposes without express and written permission from the author is strictly prohibited.  This blog entry may be distributed for noncommercial purposes, provided that full credit is given to the author. 


Spotlight: Akron Brass FireCalc Pocket Computer

Spotlight: Akron Brass FireCalc Pocket Computer Welcome to a special Monday Edition of Eddie’s Math and Calculator blog. Thi...