2017-04-15 1 views
0

저는 파이썬 (그리고 일반적으로 프로그래밍) 초보자이고 목록을 기반으로 랜덤 룸/만남이있는 텍스트 기반의 끝이없는 RPG를 만들려고합니다. 이 코드는 아직 완료되지 않았습니다. 테스트 용입니다. 내가 다른 평 파일에서 적 수입 참고 : 나는 인쇄 방() 목록에서 방을 선택 무작위로 "... 당신은 옆방에 입력 한"인쇄 및 소개를 인쇄하기를 원 제대로 클래스를 호출하는 방법

import Enemies 
import random  


class Room: 
# template definition to define all rooms 
    def __init__(self, intro): 
     self.intro = intro 


class VsRoom(Room): 
# room with an enemy 
    def __init__(self, enemy): 
     self.enemy = random.choice(Enemy_List) 
     super().__init__(intro = "There's an enemy here!") 


class NullRoom(Room): 
# empty, boring room 
    def __init__(self): 
     super().__init__(intro = "Nothing Interesting here.") 


Rooms = [VsRoom, NullRoom] # All future room "types" will go here 


def print_room(): 
# Tells the player which kind of room they are 
    print("You enter the next room...") 
    Gen_Room = random.choice(Rooms) 
    print(Gen_Room.intro) 

그러나 그것을 실행하려고하면 다음과 같은 결과를 얻습니다.

나는 수업의 작동 방식과 모든 도움이 저에게 큰 도움이 될 것입니다. 내가 할 수있는 한 PEP8을 따르려고 노력했으며, 성공하지 못해도 비슷한 질문을 찾으려고 노력했다. 다음과 같이 당신이 그것을 수행해야합니다 인스턴스를 만들려면

class VsRoom(Room): 
# room with an enemy 
    def __init__(self): 
     self.enemy = random.choice(Enemy_List) 
     super().__init__(intro = "There's an enemy here!") 

: 나는 당신이 적을 선택 곳의 목록을 가지고 관찰, 그래서 당신은 그 매개 변수를 입력 할 필요가 없습니다 것과

+0

무엇 Enemy_List''의 값이하고있는 당신'에서 적을 사용 데프 _init __ (자기 적) :' – eyllanesc

+0

는'Enemy_List'가리스트입니다 'Enemies'라는 또 다른 .py 파일에서 유래했습니다. 아래에서 언급했듯이'def_init____ (자기, 적) :'에 적의 존재는 필요하지 않았다. – Bernardozomer

답변

0

{class name}() 

그래서 당신은 변경해야합니다

Gen_Room = random.choice(Rooms) 

사람 :

Gen_Room = random.choice(Rooms)() 

전체 코드 :

import Enemies 
import random  

class Room: 
# template definition to define all rooms 
    def __init__(self, intro): 
     self.intro = intro 


class VsRoom(Room): 
# room with an enemy 
    def __init__(self): 
     self.enemy = random.choice(Enemy_List) 
     super().__init__(intro = "There's an enemy here!") 


class NullRoom(Room): 
# empty, boring room 
    def __init__(self): 
     super().__init__(intro = "Nothing Interesting here.") 


Rooms = [VsRoom, NullRoom] # All future room "types" will go here 


def print_room(): 
# Tells the player which kind of room they are 
    print("You enter the next room...") 
    Gen_Room = random.choice(Rooms)() 
    print(Gen_Room.intro) 
+0

고마워, 지금 일하고있어. 또한 Enemy_List 대신 Enemies.Enemy_List를 입력하는 것을 잊어 버렸습니다. – Bernardozomer

관련 문제