Skip to content

Instantly share code, notes, and snippets.

@prozacgod
Created February 18, 2021 06:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save prozacgod/19d5d63c868709fb251ed66c441b1580 to your computer and use it in GitHub Desktop.
Save prozacgod/19d5d63c868709fb251ed66c441b1580 to your computer and use it in GitHub Desktop.
I remember writing games like this in rom basic, and thought "there's not enough stupidly simple text mode games around" wanted to get back to my roots a bit. I shared with with a group of young adults to see if I could inspire them to code, and it may have worked!!
#!/usr/bin/env python3
import sys
import time
import random
import select
import termios, fcntl
import atexit
import os
posx = 40
def restoreTerm():
global oldterm, inputfd
termios.tcsetattr(inputfd, termios.TCSAFLUSH, termattr)
pass
atexit.register(restoreTerm)
inputfd = sys.stdin.fileno()
termattr = termios.tcgetattr(inputfd)
termflags = fcntl.fcntl(inputfd, fcntl.F_GETFL)
attr = termios.tcgetattr(inputfd)
attr[3] = attr[3] & ~termios.ICANON
fcntl.fcntl(inputfd, fcntl.F_SETFL, termflags | os.O_NONBLOCK)
termios.tcsetattr(inputfd, termios.TCSANOW, attr)
def getKey():
if len(select.select([sys.stdin], [], [], 1/60)[0]) == 1:
return sys.stdin.read()
def createRow(offset, width):
if random.randrange(2) == 0:
if random.randrange(2) == 0:
offset -= 1
else:
offset += 1
if random.randrange(50) == 1:
width -= 1
if (offset < 0):
offset = 0
if (offset >= 80 - width):
offset = 80 - width - 1
if (width < 5):
width = 5
return (int(offset), int(width))
score = 0
playerx = int(80/2)
offset = int((80 / 2) - 5)
width = 10
state = [(offset, width) for x in range(24)]
def clrScreen():
print(chr(27)+'[2j')
print('\033x')
print('\x1bc')
def lineString(offset, width):
return ('#' * offset) + (' ' * width) + ('#' * (80 - width - offset))
def drawScreen():
print("Score: ", score)
i = 0
for (offset, width) in state:
i = i + 1
line = lineString(offset, width)
if (i == 20):
line = line[:playerx] + '@' + line[playerx +1:]
print(line)
def renderScreen():
clrScreen()
drawScreen()
def main():
global offset, width, state, playerx, score
frame = 0
while True:
k = getKey()
if k == 'a':
playerx -= 1
if k == 'd':
playerx += 1
frame += 1
if frame % 10 == 0:
(offset, width) = createRow(offset, width)
state = [(offset, width)] + state
state = state[:-1]
score = score + 1
renderScreen()
if lineString(*state[20])[playerx] == '#':
print("Crashed!!!!!")
return
main()
@prozacgod
Copy link
Author

License is public domain, take it for what it is, it was written targeting Linux - feel free to adapt it to whatever you got.

This is not sophisticated, "It's doing all the wrong things" but it's doing what it takes to make it work.

The letters 'a' and 'd' move the player '@' left in right in the playing field while the score count increments. Avoid crashing into the walls.

@prozacgod
Copy link
Author

It's almost entirely liekly that there was some original inspiration to this, but I cannot for the life of me find it? So credit to the person in the 80's who wrote it!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment