Skip to content

Instantly share code, notes, and snippets.

@zeimusu
Created November 1, 2014 10:17
Show Gist options
  • Save zeimusu/fd7571a278900c037d3d to your computer and use it in GitHub Desktop.
Save zeimusu/fd7571a278900c037d3d to your computer and use it in GitHub Desktop.
Usbourne Computer Battlegames was a type-in computer games book from the 1980s. This is a version of "Battle at Traitors Castle" game. Original BASIC by Daniel Isaaman and Jenny Tyler
#!/usr/bin/env python3
#Usbourne Computer Battlegames was a type-in computer games book
#from the 1980s.
#This is a version of "Battle at Traitors Castle" game.
#Original BASIC by Daniel Isaaman and Jenny Tyler
import curses
import random
stdscr = curses.initscr()
curses.noecho()
curses.cbreak()
def pressenter(stdscr,message="Press Enter to exit."):
"""Prints a message and pauses"""
curses.nocbreak()
stdscr.nodelay(0)
stdscr.addstr(message)
stdscr.refresh()
stdscr.getch()
def turnend(message,score):
"""Reports if a hit has been made, and updates the score"""
stdscr.move(6,4)
stdscr.addstr(message)
stdscr.move(8,4)
stdscr.addstr("Score: "+str(score))
def app(stdscr):
stdscr.addstr(
"""
Traitor's castle
----------------
Your computer will print a row containing eight dots and an O.
The number keys 1 to 9 correspond to the position of the O
in the row. You have a short time to press the correct key, and hit
the O, before it disappears. How many of the Baron's men
can you hit?
""")
pressenter(stdscr,"Press enter to start\n")
stdscr.clear() #Clears the screen
curses.halfdelay(25) # How long, in tenths of a second to wait
score = 0 # Sets score to zero at the start
for turn in range(10): # Loop to give you 10 goes
t = random.randint(1,9)
r = "."*(t-1) + "O" + "."*(9-t)
# These lines make a string with an 'O' in
# a random position
stdscr.move(4,4)
stdscr.addstr(r) # Prints the string
r = stdscr.getch() - 48 # Checks to see if you are pressing a key
# 48 is ascii for 0, so if you press one (ascii 49)
# this gets the number 1
if r == t: # checks if you pressed the right key
score += 1 # increases score by one
turnend("You got one",score)
else:
# or tells you you missed
turnend("You missed ",score)
stdscr.move(10,4)
pressenter(stdscr,"Press enter to exit")
curses.wrapper(app) #runs the program
#Challenge: Can you change the game so there are Ordinary soldiers "O"
#and special crack troops "S". You get 1 point for an ordinary, and 5
#for a special
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment