Linux Format forums Forum Index Linux Format forums
Help, discussion, magazine feedback and more
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Another Python Freebie Based On MikeTron...

Post new topic   Reply to topic    Linux Format forums Forum Index -> Programming
View previous topic :: View next topic  
Author Message
LXF regular

Joined: Sat Mar 21, 2009 11:16 am
Posts: 1462
Location: Loughborough

PostPosted: Thu Jun 02, 2011 6:56 pm    Post subject: Another Python Freebie Based On MikeTron... Reply with quote

Hi all...

A DEMO to show how to plot XY inside a standard text mode Python
install. MikeTron gave me the idea of making this into a dreadful game.
It is primarily to show, using an INKEY$ idea for keyboard control,
how to plot characters inside a Python shell.

It is assumed that the shell is defaulting at 80 x 24 and Python is 2.6.x
or greater although it may work in earlier versions.

The INKEY$ function is not perfect but works reasonably well considering
Python is not able to do this by default.

I take no credit for this function and the pointer to the original is inside
the code. I do take credit for the plotting method though and shows
that Python can still do things without all the glitz added.

This plotting method is going to be used in a serious project I have in

Enjoy finding simple solutions to often very difficult problems...

Read the code for more information.


# Yes I know it is not much of a game but it is intended to show how to
# "draw", AND, to use the keyboard to "draw" inside a standard text mode
# Python shell.
# Written in such a way as to easily understand how it works.
# IMPORTANT NOTE!!! This ASSUMES a standard 80 x 24 shell window.
# This working idea is copyright, (C)2011, B.Walker, G0LCU.
# NOW issued as Public Domain to LXF...
# To run at the prompt...
# >>> execfile("/full/path/to/")

def main():
   import os
   import sys
   import termios
   import tty
   import random

   # A basic clear screen and cursor removal for program start...
   os.system("setterm -cursor off")

   # Make any variables global just for this DEMO "game"...
   global screen_array
   global character
   global line
   global position
   global remember_attributes
   global plot
   global inkey_buffer
   global score

   remember_attributes="(C)2011, B.Walker, G0LCU."

   # This is a working function; something akin to the BASIC INKEY$ function...
   # Reference:-
   # Many thanks to Danny Yoo for the above code, modified to suit this program...
   # In THIS FUNCTION some special keys do a "break" similar to the "Esc" key inside the program.
   # Be aware of this...
   # An inkey_buffer value of 0, zero, generates a "" character and carries on instead of waiting for
   # a valid ASCII key press.
   def inkey():
      termios.tcsetattr(fd, termios.TCSADRAIN, remember_attributes)
      return character

   # The welcome screen.
   print ""
   print " A simple, odd style, Tron(ish) game."
   print "Another DEMO 2D animation for Linux platforms."
   print ""
   print "You control a vehicle leaving a trail behind it."
   print ""
   print "It is NOT always moving, and if it crosses any part"
   print "of the trail or border, (* characters), the game"
   print "is over. It CAN randomly move further than the key"
   print "presses so do not assume there is a bug... :)"
   print "Use the Q and A keys to change the direction to"
   print "up and down, and O and P for left and right."
   print "See how long you can survive! Score at the end."
   print ""
   character=raw_input("Hit <RETURN/ENTER> to begin... ")

   # Generate the game window as a single text string.
   # This assumes a standard 80x24 terminal text window.
   # Top line.
   while position<=78:
   # Next 20 lines.
   while line<=20:
      while position<=78:
         screen_array=screen_array+" "
   # Bottom line.
   while position<=78:

   # Store the complete string for future use.
   # End of game setup...

   # Start of game proper, set the initial position.
   while 1:
      # Standard clear the terminal window.
      print screen_array
      # Add another * when inkey_buffer=0.
      # Use the INKEY$ function to grab an ASCII key.
      if character=="a": plot=0
      if character=="A": plot=0
      if character=="q": plot=1
      if character=="Q": plot=1
      if character=="o": plot=2
      if character=="O": plot=2
      if character=="p": plot=3
      if character=="P": plot=3
      # Esc key to exit the loop...
      if character==chr(27): break
      if plot==0: position=position+80
      if plot==1: position=position-80
      if plot==2: position=position-1
      if plot==3: position=position+1
      if position>=1759: position=1759
      if position<=0: position=0

      # Check for a * character in the array and......
         # ......exit if one exists at that point.
         print "Game Over! You scored",score,"\b..."
      # Now get the whole array.
      # End of screen_array update per plot.

   # Reset the cursor, etc...
   os.system("setterm -cursor on")
   character=raw_input("Have another game? (Y/N):- ")
   if character=="y": main()
   if character=="Y": main()
   if character=="": main()


# end of game.
# Enjoy finding simple solutions to often very difficult questions.


Bazza, G0LCU...

Team AMIGA...
Back to top
View user's profile Send private message
View previous topic :: View next topic  
Display posts from previous:   
Post new topic   Reply to topic    Linux Format forums Forum Index -> Programming All times are GMT
Page 1 of 1

Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
Linux Format forums topic RSS feed 

Powered by phpBB © 2001, 2005 phpBB Group

Copyright 2011 Future Publishing, all rights reserved.

Web hosting by UKFast