# cpy Thonny - guizero shuttle simulation - train at B
# provided by Anthony Vogelaar, Perth Scotland
# rev 0.10  2025-01-03  initial release

# tools |  manage packages | guizero : search on pypi | install
# drawing: https://lawsie.github.io/guizero/drawing/

from guizero import App, Drawing

# ==================== GUI setup =========================

app = App('Shuttle simulation', width=800, height=200)
ctx = Drawing(app, width='fill', height='fill')

# ======================= Widgets ========================

# draw track, center @ x=400, y=100
def track():
    ctx.line(10,  90, 790,  90, width=4)
    ctx.line(10, 110, 790, 110, width=4)


# draw train at position x
def train(x=0):
    x0 = 60; y0 = 100
    ctx.rectangle(x0+x-40, y0-20, x0+x+40, y0+20, color='blue')


# draw layout:  signals, track and train
def layout(train_pos):
    ctx.clear()					# clear context
    track()						# draw track
    train(train_pos)			# draw train at train_pos
    
# ======================= Timer =========================

# application sequencer called at 25 mSec = 40 Hz
# moving train, 4 px per step = 680/ 4 = 170 steps
# duration = 170 * 0.05 = 8.5 sec
train_pos  = 0
train_step = 4
def onTimer():
    global	train_pos, train_step
    
    layout(train_pos)
    train_pos += train_step
    if train_pos == 680:
        train_step = -4
    else:
        if train_pos == 0:
            train_step = 4
    
# ========================= main =========================

ctx.repeat(50, onTimer)				# sequencer @ 50 mSec
app.display()						# start GUI loop
    
# ========================== end =========================    
