************************************************************************************************
Why won't the countdown work?
If you do it for the first time - for the pickle to set up - [*] scroll to n and enter it.
[*] Then scroll to one
You should just be able to scroll to 1 if you have done it before and not deleted the data files
Execute from terminal with sudo
Here's the code:
Code: Select all
#! /usr/bin/python
# Import modules and functions
import os , time , curses , pickle , random
from astro_pi import AstroPi
from time import sleep
import pickle
ap = AstroPi()
ap.clear() # Clear screen...
# Set the data: Variables , Lists , Dictionaries , Functions etc...
# Stored data: (in a pickle)
if not os.path.isfile( 'numberofpeople.dat' ): # If there is not the correct file already exising...
numberofpeople = 0 # Set the variable
file = open( 'numberofpeople.dat' , 'wb' ) # Set the setup variable
pickle.dump( numberofpeople , file ) # Dump the data in the file
file.close() # Close the file
else: # If the file does exist...
file = open( 'numberofpeople.dat' , 'rb' ) # Set the setup variable
numberofpeople = pickle.load( file ) # Load the data
file.close() # Close the file
if not os.path.isfile( 'besttemp.dat' ): # If there is not the correct file already exising...
besttemp = 0 # Set the variable
file = open( 'besttemp.dat' , 'wb' ) # Set the setup variable
pickle.dump( besttemp , file ) # Dump the data in the file
file.close() # Close the file
else: # If the file does exist...
file = open( 'besttemp.dat' , 'rb' ) # Set the setup variable
besttemp = pickle.load( file ) # Load the data
file.close() # Close the file
if not os.path.isfile( 'users.dat' ): # If there is not the correct file already exising...
users = [] # Set the dictionary
file = open( 'users.dat' , 'wb' ) # Set the setup variable
pickle.dump( users , file ) # Dump the data in the file
file.close() # Close the file
else: # If the file does exist...
file = open( 'users.dat' , 'rb' ) # Set the setup variable
users = pickle.load( file ) # Load the data
file.close() # Close the file
# Temporary data:
person = 0 # This will be the current person playing/reading data. Default is 0 if main menu is launched or if the administrator is running.
temporary = []
# Curses setup...
screen = curses.initscr() # Initialising...
screen.keypad(True) # Set up the arrow keys...
curses.cbreak() # Don't wait for enter to be pressed
# Functions:
def menunum(): # The number side of menu layer 1
running = True # Set running to true
file = open( 'numberofpeople.dat' , 'rb' ) # Setup variable
numberofpeople = pickle.load( file ) # Refresh variables
if numberofpeople >= 1: # If numberofpeople is or is more than one..
screenchar = 1 # Set the screen to 1
ap.show_message(str(screenchar)) # Change the screen
try:
while running:
ap.clear() # Clear the screen
char = screen.getch() # Setup variable
if char == curses.KEY_RIGHT: # If the right key is pressed
file = open( 'numberofpeople.dat' , 'rb' ) # Open the file
numberofpeople = pickle.load( file ) # Refresh the data
if (screenchar + 1) > numberofpeople: #######
menucharN() # Right arrow pressed
running = False #######
else:
screenchar += 1 #######
ap.show_message(str(screenchar)) # Right arrow pressed
elif char == curses.KEY_LEFT: #######
if screenchar == 1: #######
menucharN() # Left arrow pressed
running = False #######
else:
screenchar -= 1 #######
ap.show_message(str(screenchar)) # Left arrow pressed
elif char == 10: #######
person = screenchar #######
write() # Enter pressed
running = False #######
finally:
print('') # Just something to stop errors occuring...
else:
menucharN() # Go the new menu
running = False # Stop
def menucharN(): # The new menu item
running = True # Set running to true
ap.clear() # Clear the screen
ap.show_message('N') # Show N on the screen
try:
while running:
char = screen.getch() # Setup variable
if char == curses.KEY_RIGHT: # If the right arrow is pressed
menucharA() # Admin menu item
running = False # Stop
elif char == curses.KEY_LEFT: # If the left arrow is pressed
menunum() # Number menu item
running = False # Stop
elif char == 10: # If enter pressed
file = open( 'numberofpeople.dat' , 'rb' ) # Open the data file
numberofpeople = pickle.load( file ) # Refresh the data
currentpeople = numberofpeople # Setup variable
currentpeople += 1 # Add one to current people
numberofpeople = currentpeople # Setup variable
file = open( 'numberofpeople.dat' , 'wb' ) # Set the setup variable
pickle.dump( numberofpeople , file ) # Dump the data in the file
file = open( 'numberofpeople.dat' , 'rb' ) # Open the file for reading
numberofpeople = pickle.load( file ) # Refresh the variable
ap.show_message('New person ' + str(numberofpeople) + ' created!') # Display completion message
menunum() # Go to the number menu
running = False # Stop
finally:
print('') # Somethong to stop errors occuring with try...
def menucharA(): # The admin menu item
running = True # Set running to true
ap.clear() # Clear the screen
ap.show_message('A') # Show A on the screen
try:
while running:
char = screen.getch() # Setup variable
if char == curses.KEY_RIGHT: # If the right arrow is pressed
menunum() # Number menu item/s
elif char == curses.KEY_LEFT: # If the left key is pressed
menucharN() # New menu item
elif char == 10: # If enter key pressed
admin() # Admin enter
finally:
print('') # Just something to stop errors occuring...
def admin(): # Inside the admin menu
ap.show_message('The best overall temperature for this place to work at is... 21 degrees celcius!') # Display average message
def write(): # Inside the person menu item
ap.clear() ###########
ap.show_letter('9') #
sleep(1) #
ap.show_letter('8') #
sleep(1) #
ap.show_letter('7') #
sleep(1) #
ap.show_letter('6') #
sleep(1) #
ap.show_letter('5') # Countdown...
sleep(1) # Begin!
ap.show_letter('4') #
sleep(1) #
ap.show_letter('3') #
sleep(1) #
ap.show_letter('2') #
sleep(1) #
# ap.show_letter('1') #
sleep(1) #
ap.show_letter('0') #
sleep(1) ###########
# Begin the actual game!
R = [ 255 , 0 , 0 ] # r = red
O = [ 0 , 0 , 0 ] # o = black
dotred = [
O, O, O, O, O, O, O, O,
O, O, O, O, O, O, O, O,
O, O, O, O, O, O, O, O,
O, O, O, R, R, O, O, O,
O, O, O, R, R, O, O, O,
O, O, O, O, O, O, O, O,
O, O, O, O, O, O, O, O,
O, O, O, O, O, O, O, O
]
# Code for the red dot
ap.set_pixels(dotred) # Set the pixels to the red dot
sleep(random.random() * 10 + 3) # Wait for a random time between 3 and 13 seconds
ap.clear()
ap.clear() # Clear the screen
menunum() # Start the whole program!
