In this second half of this two-part World of Code episode in which we dive into actual code and the experience of coding, we’ll take our BASIC light-switch program and re-implement it in a modern programming language, Python, on a modern computer, my M3 MacBook Air.
The main takeaway from the experience, I hope, will be a sense of how the ever-increasing power and capabilities of computer technology leads inevitably to more complex code, which then makes necessary the development of new tools that help us to manage the increasing complexity and to take advantage of the increasing power and capabilities the new computer technology. If we track this in terms of the computer technologies and programming languages we’ve covered or alluded to in the World of Code arc of the Geek Orthodox podcast so far, a simplified timeline might look something like this:
computers are hard-wired to do specific tasks
binary code (machine/assembly language) simplifies the process of reprogramming computers
higher-level languages (C, BASIC, etc.) simplify coding and control of computers
operating systems (DOS and then Mac/Windows) simplify control and increase the capabilities of computers but make programming more complicated
modern programming languages are themselves programs, and use Integrated Development Environments (like IDLE) to simplify programming
AI may make programming more accessible (simpler?) by allowing us to “program” computers using natural language (prompts) → see next episode…
For those interested in the technical details, here are the four “light-switch” programs we made (two in BASIC, two in Python) over the course of these two episodes:
Basic Light-Switch (On/Off) in BASIC:
10 CLS(0)
20 I$=INKEY$
30 IF I$="" THEN GOTO 20
40 IF I$=CHR$(94) THEN CLS(5)
50 IF I$=CHR$(10) THEN CLS(0)
60 GOTO 20Notes: In TRS-80 Color Computer BASIC (every early version of BASIC was slightly different, as each version was customized to control the computer it ran on), there are nine colours, 0 = black, 5 = white, the CLS command clears the screen, the INKEY$ command polls the keyboard, and CHR$ refers to characters by their ASCII values, in which 94 = up arrow and 10 = down arrow.
Advanced Light-Switch in BASIC:
10 CLS(0)
15 T=5
20 I$=INKEY$
30 IF I$="" THEN GOTO 20
40 IF I$=CHR$(94) THEN CLS(T)
50 IF I$=CHR$(10) THEN CLS(0)
60 IF I$=CHR$(8) AND T>3 THEN T=T-2:CLS(T)
70 IF I$=CHR$(9) AND T<7 THEN T=T+2:CLS(T)
80 GOTO 20Notes: Early versions of BASIC only allowed one- or two-character variable names, so here T = color temperature; ASCII values 8 and 9 indicate the right- and left-arrow keys, respectively; the colon (:) in BASIC allows multiple commands to be strung together on a single line, with all commands on the same line following an IF statement only being executed if the statement is true; and colours 3 and 7 on the TRS-80 are magenta and dark blue, respectively.
Hidden Display Layer and Functions Starting Both of the Light-Switch Python Programs:
import pygame
# pygame setup
pygame.init()
screen = pygame.display.set_mode((1280, 720))
UP = pygame.K_UP
DOWN = pygame.K_DOWN
LEFT = pygame.K_LEFT
RIGHT = pygame.K_RIGHT
def theUserClosedTheWindow():
# poll for events
# pygame.QUIT event means the user clicked X to close your window
for event in pygame.event.get():
if event.type == pygame.QUIT:
return(True)
return(False)
def cls(colour):
screen.fill(colour)
# flip() the display to put your work on screen
pygame.display.flip()
def inkey():
return(pygame.key.get_pressed())Notes: Imports the pygame library to detect key-presses and display coloured rectangles, sets up a 720p screen, defines the UP, DOWN, LEFT, and RIGHT keys as nicely named constants, defines a function that determines when the user closes the window, and implements the BASIC commands CLS and INKEY to make clearing the screen and detecting keystrokes look more familiar.
Basic Light-Switch (On/Off) in Python:
running = True
colour = "black"
cls(colour)
while running:
if theUserClosedTheWindow():
running = False
i = inkey()
if i[UP]:
cls("white")
if i[DOWN]:
cls("black")
pygame.quit()Notes: Follows and depends upon the first portion of the program, above. Turns the screen “on” (white) when the up-arrow is pressed and “off” (black) when the down-arrow is pressed.
Advanced Light-Switch in Python:
r = 255
g = 255
b = 255
running = True
colour = (r,g,b)
cls("black")
while running:
if theUserClosedTheWindow():
running = False
i = inkey()
if i[UP]:
cls((r,g,b))
if i[DOWN]:
cls("black")
if i[LEFT] and r > 0:
if b < 255:
b = b + 1
else:
r = r - 1
cls((r,g,b))
if i[RIGHT] and b > 0:
if r < 255:
r = r + 1
else:
b = b - 1
cls((r,g,b))
pygame.quit()Notes: Replaces the “basic switch” code above, and likewise follows and depends upon the “hidden display layer” code. Represents the “coolest” colour temperature as light blue (cyan), which it moves towards as the left arrow key is pressed (r,g,b = 0,255,255), and the “warmest” colour temperature as yellow (r,g,b = 255,255,0), which it moves towards as the right arrow key is pressed.









