2014-01-12 4 views
0

나는 게임을 만들고있다. 사람들이 main.py를 실행하기를 원한다면 옵션이있는 작은 메뉴가 나타납니다. Atm 오류가 발생합니다.(Python 초보자 용 도움말) getch?

ascii.py: 

#------------------------------------------------------------------------------- 
# Name:  ascii 
# Purpose:  To create all the ascii art and graphic tools for the game: 
#    "you wake up..." 
# 
# Author:  William Bryant 
# 
# Created:  15/12/2013 
# Copyright: (c) William 2013 
#------------------------------------------------------------------------------- 

import time 
import os 

WOODEN_DOOR = """ ______________________________ 
/_____/_____/_____/_____/_____/ 
._.     ._. 
| |     | | 
|_| ______ ______ |_| 
|-| /__1__/ /__2__/ |-|   ___________ 
| |     | |   || ||| |||| 
|_|     |_|   || ||| |||| 
._.     ._.   ||_||| |||| 
| |     | |  _||_|||_||||_ 
|_| ______   |_| 
|-| /__3__/   |-| 
| |    /\ | | 
|_|    \/ |_| 
._.     ._.          _____ 
| |     | |  ____________________________|_____| 
|_| ______ ______ |_|  | |______________________________| | 
|-| /__4__/ /__5__/ |-|  | |        | | 
| |     | |  |_|        |_| 
|_|     |_|     17615.5 x 2 
    ____________________________________________________________________ 
/_____/ /_____/__/_____/ /_____/ /_____/ /_____/ /_____/ /_____/ 
/_____/ /_____/ /_____/ /_____/ /_____/ /_____/ /_____/ /_____/""" 

#Error message if user executes the wrong file. 
if __name__ == '__main__': 
    print("[ERROR]: Do not run this file. Run main.py - this file should not be executed!") 
    time.sleep(4) 
    exit() 

#Clear function 
def clear(): 
    """ 
    Clears the console screen using the built in commands on a operating 
    system (here linux and windows) 
    """ 
    os.system(['clear','cls', "^L"][os.name == 'nt']) 

#"Wake up..." title/banner at the start 
def ywu_banner(num_of_times): 
    """ 
    Prints You wake up...(the game name) in ascii code big letters into a 
    console and clears the screen using the clear function above and reprints 
    the message to make the dots at the end appear to be moving. 
    """ 
    print("__  __       _") 
    time.sleep(0.25) 
    print("\ \ //      | |") 
    time.sleep(0.25) 
    print(" \ \_/ /__ _ _ __  ____ _| | _____ _ _ _ __") 
    time.sleep(0.25) 
    print(" \ /_ \| | | | \ \ /\// _` | |//_ \ | | | | '_ \ ") 
    time.sleep(0.25) 
    print(" | | (_) | |_| | \ V V/(_| | < __/ | |_| | |_) | _ _ ") 
    time.sleep(0.25) 
    print(" |_|\___/ \__,_| \_/\_/ \__,_|_|\_\___| \__,_| .__(_|_|_)") 
    time.sleep(0.25) 
    print("             | | ") 
    time.sleep(0.25) 
    print("             |_| ") 
    time.sleep(0.25) 
    clear() 

    for foo in range(num_of_times): 
     print("__  __       _") 
     print("\ \ //      | |") 
     print(" \ \_/ /__ _ _ __  ____ _| | _____ _ _ _ __") 
     print(" \ /_ \| | | | \ \ /\// _` | |//_ \ | | | | '_ \ ") 
     print(" | | (_) | |_| | \ V V/(_| | < __/ | |_| | |_) | _ ") 
     print(" |_|\___/ \__,_| \_/\_/ \__,_|_|\_\___| \__,_| .__(_|_)") 
     print("             | | ") 
     print("             |_| ") 
     time.sleep(0.7) 
     clear() 

     print("__  __       _") 
     print("\ \ //      | |") 
     print(" \ \_/ /__ _ _ __  ____ _| | _____ _ _ _ __") 
     print(" \ /_ \| | | | \ \ /\// _` | |//_ \ | | | | '_ \ ") 
     print(" | | (_) | |_| | \ V V/(_| | < __/ | |_| | |_) | ") 
     print(" |_|\___/ \__,_| \_/\_/ \__,_|_|\_\___| \__,_| .__(_)") 
     print("             | | ") 
     print("             |_| ") 
     time.sleep(0.7) 
     clear() 

     print("__  __       _") 
     print("\ \ //      | |") 
     print(" \ \_/ /__ _ _ __  ____ _| | _____ _ _ _ __") 
     print(" \ /_ \| | | | \ \ /\// _` | |//_ \ | | | | '_ \ ") 
     print(" | | (_) | |_| | \ V V/(_| | < __/ | |_| | |_) | ") 
     print(" |_|\___/ \__,_| \_/\_/ \__,_|_|\_\___| \__,_| .___/") 
     print("             | | ") 
     print("             |_| ") 
     time.sleep(0.7) 
     clear() 

     print("__  __       _") 
     print("\ \ //      | |") 
     print(" \ \_/ /__ _ _ __  ____ _| | _____ _ _ _ __") 
     print(" \ /_ \| | | | \ \ /\// _` | |//_ \ | | | | '_ \ ") 
     print(" | | (_) | |_| | \ V V/(_| | < __/ | |_| | |_) | _ _ ") 
     print(" |_|\___/ \__,_| \_/\_/ \__,_|_|\_\___| \__,_| .__(_|_|_)") 
     print("             | | ") 
     print("             |_| ") 
     time.sleep(0.7) 
     clear() 

menu_start = """ 
_____________ _____________ 
|    | |    | 
| *start* | | exit  | 
|_____________| |_____________| 
""" 

menu_exit = """ 
_____________ _____________ 
|    | |    | 
| start | | *exit* | 
|_____________| |_____________| 
    """ 

main.py :

#------------------------------------------------------------------------------- 
# Name:  main.py 
# Purpose:  An RPG (Roll playing game) where you wake up in a room and have 
#    to figure out text based puzzles to escape. 
#    The whole game will be done in a terminal (Shell) and will be 
#    completely text and ascii code. This will be the main file. 
# 
# Author:  William Bryant 
# 
# Created:  15/12/2013 
# Copyright: (c) William Bryant 2013 
#------------------------------------------------------------------------------- 

import time 
import ascii 
import quests 
import msvcrt 

#The function that starts/restarts the game 
def start(): 
    """ 
    Calls all the functions to start the game/ restart the game 
    """ 
    ascii.clear() 
    #Display a cool banner 
    ascii.ywu_banner(2) 

    options() 
    time.sleep(0.7) 
    ascii.clear() 

    #Game text. 
    game_text() 

def init(): 
    state = "start" 
    print(ascii.menu_start) 
    move = msvcrt.getch() 
    enter = False 
    while enter == False: 

     move 

     if move == b'\r' and state == 'start': 
      enter = True 
      start() 

     elif move == b'\r' and state == 'exit': 
      enter = False 
      exit() 

     elif move == b'K' or move == b'M' and state == "start": 
      ascii.clear() 
      print(ascii.menu_exit) 
      state = "exit" 
      move 

     elif move == b'K' or move == b'M' and state == "exit": 
      ascii.clear() 
      print(ascii.menu_start) 
      state = "start" 



#The text which is the main story line after the banner which gives the player 
#A sense of what the setting is about 
def game_text(): 
    """ 
    Prints out a bit of text 2 lines down and clears the screen every 4 or so 
    seconds. 
    """ 
    time.sleep(5) 

    print("\n\nYour eyes gentally and gradually, open") 
    time.sleep(4) 
    ascii.clear() 
    time.sleep(2) 

    print("\n\nYou slowly gather your senses and sit up...") 
    time.sleep(4.5) 
    ascii.clear() 
    time.sleep(2) 

    print("\n\nYou look around the room, and try to make sense of the") 
    print("environment you are in and to your surprise, you realise your in some") 
    print("sort of a prison cell!") 
    time.sleep(12.5) 
    ascii.clear() 
    time.sleep(2) 

    print("\n\nYou place your hand on the wall...") 
    time.sleep(3) 
    ascii.clear() 
    time.sleep(2) 

    print("\n\n... It feels like some sort of metal.") 
    time.sleep(4) 
    ascii.clear() 
    time.sleep(2) 

    print('\n\n"How did I get here?", \nYou think to yourself') 
    time.sleep(4) 
    ascii.clear() 
    time.sleep(2) 

    quests.accept_quest("Find a way out. Do you accept? ") 
    time.sleep(10) 
    ascii.clear() 

    ascii.clear() 
    time.sleep(2) 

    print("\nYou see a wooden door next to your bed with 5 buttons and a handel... ") 
    time.sleep(10) 
    ascii.clear() 
    time.sleep(2) 

    print("\n\nYou the figure out that it's some sort of combination lock.") 
    time.sleep(4) 
    ascii.clear() 
    time.sleep(2) 

    quests.door_lock_quest() 
    time.sleep(2) 
    print("\nThe door makes a click, you twist the handle anti-clockwise and it slides open") 
    time.sleep(4) 
    ascii.clear() 
    time.sleep(2) 

#if the file is the main file then start the program 
if __name__ == '__main__': 
    init() 

및 quests.py : 어떤 이유

#------------------------------------------------------------------------------- 
# Name:  quests 
# Purpose:  To contain quests functions for the game "Yoy wake up..." 
# 
# Author:  William Bryant 
# 
# Created:  17/12/2013 
# Copyright: (c) William Bryant 2013 
#------------------------------------------------------------------------------- 

import time 
import ascii 
import main 

#Error message if user executes the wrong file. 
if __name__ == '__main__': 
    print("[ERROR]: Do not run this file. Run main.py - this file should not be executed!") 
    time.sleep(4) 
    exit() 

#Helper function for the quests 
def accept_quest(name): 
    """ 
    If the player doesn't accept the quest, then the player can't move on. If 
    they say yes, then they continue through the script. If they say no, then the init 
    function is called from main.py and they get a message. 
    """ 
    answered = False 
    while answered is False: 
     quest_accept = input("Quest: " + name) 
     if quest_accept.upper in ["YES", "ACCEPT"]: 
      time.sleep(2) 
      print("\nYou accepted the quest") 
      answered = True 

     elif quest_accept.upper in ["NO", "DECLINE"]: 
      answered = True 
      time.sleep(2) 
      ascii.clear() 
      print("\nYou don't live happily ever after and die.") 
      time.sleep(5) 
      ascii.clear() 
      main.init() 

     else: 
      print("Type yes or no. ") 

     time.sleep(4) 

#Function for the first quest: do you want to find a way out? 

#Function for the second quest: Mmm lets just the combination.. 
def door_lock_quest(): 
    """ 
    If the player answers the right question (35231) then they get to continue, 
    if not then they get a 'GAME OVER' message and the init() function from main 
    executes and the game restarts 
    """ 
    combination = False 
    accept_quest("Combination lock. Do you accept? ") 
    while combination is False: 
     QUEST_ANSWER2 = input(ascii.WOODEN_DOOR + "\nThe answer may be somewhere in the room, hmmm lets try this combination: ") 
     if QUEST_ANSWER2 in ["35231", 35231]: 
      combination = True 

     elif QUEST_ANSWER2 not in ["35231", 35231]: 
      combination = False 
      time.sleep(2) 
      ascii.clear() 
      print("GAME OVER!!!\nYou die of hunger and lack of water the next day because you didn't answer correctly and couldn't escape!!!") 
      time.sleep(7) 
      ascii.clear() 
      main.init() 

     time.sleep(4) 

그래서 여기

는 3 개 파일입니다. 그들은 열쇠가 작동하지 않습니다. getch()를 처음 사용할 때입니다. 나는 다소 이해하지만 실제로는 아니다.

[편집] : 오, 나는 또한 당신이

move = msvcrt.getch() 

을 설정하면 내가

+0

...? – BartoszKP

+0

[Here] (http://stackoverflow.com/users/edit/3109055) 질문의 첫 번째 줄에 입력 한 정보를 넣을 수 있습니다. – glglgl

+2

여기에 너무 많은 코드가 게시되었습니다. 문제를 재현하는 데 필요한 최소한의 관련 행을 게시하십시오. –

답변

2

파이썬 3를 사용하고 있음을 지적해야한다 당신이 move는 다음 문자을 누르면 설정합니다. 줄 move은 아무런 효과가 없습니다. 그런 다음 반복 할 때마다 동일한 문자를 사용합니다. move = msvcrt.getch()내부에 루프를 설정해야합니다.

here 좋은 getch 사례가 있습니다

당신이지고 어떤 오류
# File: msvcrt-example-2.py 

import msvcrt 
import time 

print "press SPACE to enter the serial number" 

while not msvcrt.kbhit() or msvcrt.getch() != " ": 
    # do something else while we're waiting 
    print ".", 
    time.sleep(0.1) 

print 

# clear the keyboard buffer 
while msvcrt.kbhit(): 
    msvcrt.getch() 

serial = raw_input("enter your serial number: ") 

print "serial number is", serial 
+0

오, 알았어, 타이! – gogobebe2