def main(): import _Getch game = Game() while not game.lost: os.system('cls') game.prettyPrint() key = _Getch.getch() if key == 'w': game.turn("up") elif key == 's': game.turn("down") elif key == 'a': game.turn("left") elif key == 'd': game.turn("right") elif key == 'q': break else: continue print "So long sucker!"
def start_test(phrase): start = time.time() print(phrase) while len(phrase) > 0: key = getch() if key == phrase[0]: phrase = phrase[1:] print('\n\n' + phrase) end = time.time() print('WPM: ' + str((end - start) / 60 * 100)) return (end - start) / 60 * 100
def makeMenu(options, directInput=True): sortedKeys = sorted(options) for key in sortedKeys: print "%5s %s" % (key, options[key]["name"]) if directInput: print "Selection: ", selection = _Getch.getch() print else: selection = raw_input("Selection (commit with Enter): ") while not selection in options: print "Invalid selection" if directInput: print "New selection: ", selection = _Getch.getch() print else: selection = raw_input("New Selection: ") if "arg" in options[selection]: options[selection]["function"](*options[selection]["arg"]) else: options[selection]["function"]()
def play(): """ The primary method which incorporates other methods to play the game. """ os.system('cls' if os.name == 'nt' else 'clear') # Initialize starting snake and food item. # snake = [[10, 8], [10, 9], [10, 10], [10, 11], [10, 12]] food = generate_food(snake) print_board(snake, food) while not lose(snake): key = getch() if move(key, snake, food): food = generate_food(snake) os.system('cls' if os.name == 'nt' else 'clear') print_board(snake, food) print('You\'ve lost the game!') return None
# Initialize starting snake and food item. # snake = [[10, 8], [10, 9], [10, 10], [10, 11], [10, 12]] food = generate_food(snake) print_board(snake, food) while not lose(snake): key = getch() if move(key, snake, food): food = generate_food(snake) os.system('cls' if os.name == 'nt' else 'clear') print_board(snake, food) print('You\'ve lost the game!') return None ############################################ PLAY GAME ############################################# with open('intro.txt') as file: os.system('cls' if os.name == 'nt' else 'clear') print(file.read()) time.sleep(1) play() print('Press y to play again.') while getch() == 'y': os.system('cls' if os.name == 'nt' else 'clear') play() print('Press y to play again.') print('Thanks for playing!') time.sleep(1) os.system('cls' if os.name == 'nt' else 'clear')
def play(): """ This method simply employs the previously defined methods to play a game of Hangman. A word is first randomly selected from the provided word list, after which it is hidden from the player and illustrated as a string of '_', above which is an illustration of the gallows in the ground state. The game starts with the player making a guess, which results in the following outcomes: I. The player makes a correct guess: - The string of '_' representing the unknown characters is updated to reflect known characters. For example, if our word is given as _ _ _ _ , and a guess of 'a' correctly identifies the second character of the mystery word, then the unknown characters are updated to _ a _ _ . The guess is appended to a list containing unique previous guesses to prevent the player from making the same guess later on in the game. If the player makes a guess that pushes the game into a solved state, the player is congratulated, and the game ends. The illustration of the gallows remains the same. II. The player makes an incorrect guess: - The guess is appended to a list containing unique previous guesses, as discussed in (I), and the gallows is incremented to the next state. If the player makes a guess that pushes the game into the final state, the illustration of the gallows is incremented to the final state, and the player is informed that he has lost, and the game subsequently ends. III. The player repeats a previous guess: - The player is informed that he has already made the guess, and is then prompted to make another valid guess. """ # Generating word and hidden string of '_' . # word = generator(DICTIONARY) hidden_word = list('_' * len(word)) # The list and integer representing unique character guesses and the number of bad guesses # # respectively. # guessed_chars = [] bad_guesses = 0 # The ground state of the game is illustrated first. # gallows(0) print('\n' + char_string(hidden_word) + '\n') print('Guessed letters: ') while not won(hidden_word): print('Enter a guess: ') char_guess = getch().lower() # Checking outcome (III) # if guessed_chars.__contains__(char_guess): print('You already guessed that.\n') elif len(char_guess) > 1 or not char_guess.isalpha(): print('Illegal character. Please choose an alphabet character.') # Checking outcome (I) or (II) # else: os.system('cls' if os.name == 'nt' else 'clear') guessed_chars.append(char_guess) # Checking outcome (II) # if not word.__contains__(char_guess): bad_guesses += 1 # Final state # if bad_guesses == 8: gallows(bad_guesses) print('\nGame over. You lost.\nThe word was: ' + word) return None gallows(bad_guesses) print('\n' + char_string(hidden_word) + '\n') print('Guessed letters: ' + char_string(guessed_chars)) # Checking outcome (I) # else: gallows(bad_guesses) print('\n' + char_string(guess(char_guess, hidden_word, word)) + '\n') print('Guessed letters: ' + char_string(guessed_chars)) # The while loop is active only when the game is not won, thus a win naturally results when # # we exit this loop. # print('\nCongrats. You\'ve won.') return None