如何解决得到一个{pyFlakes}未定义名称'self'红色下划线
im使用python(和通过repl.it进行即时编码)开发基于文本的小型rpg游戏。 在代码的一部分中,我声明了我的3个类(战士,法师,牧师)的统计信息,但是当我写它时,它用红色下划线标出,并给我一个错误(悬停在上面时)[pyFlakes]未定义名称“ self”
import cmd
import textwrap
import sys
import os
import time
import random
screen_width = 100
### PLAYER SETUP ###
class player:
def __init__(self):
self.name = ''
self.hp = ''
self.mp = ''
self.status_effects = []
self.location = 'start'
self.game_over = False
self.job = ''
myPlayer = player()
### TITLE SCREEN ###
def title_screen_selections():
option = input("> ")
if option.lower() == ("play"):
setup_game() #placeholder for Now
elif option.lower() == ("help"):
help_menu()
elif option.lower() == ("quit"):
sys.exit()
while option.lower() not in ['play','help','quit']:
print("please enter a valid command")
option = input("> ")
if option.lower() == ("play"):
setup_game() #placeholder for Now
elif option.lower() == ("help"):
help_menu()
elif option.lower() == ("quit"):
sys.exit()
def title_screen():
os.system('clear')
print('##################################')
print('# Welcome to the text based RPG! #')
print('##################################')
print(' -Play- ')
print(' -Help!- ')
print(' -Quit- ')
print(' -copyright 2020 DIvanov.co- ')
title_screen_selections()
def help_menu():
print('##############################################')
print('# Welcome to the text based RPG! Help! menu # ')
print('##############################################')
print(' - How to play?- ')
print(' -Use up,down,left,right to move- ')
print(' - Type your commands to do them - ')
print(' -Use "look" to inspect something- ')
title_screen_selections()
### MAP ###
#PLAYER STARTS AT B2
#a1 a2 a3 a4
#-------------
#| | | | | a4
#-------------
#| | | | | b4
#-------------
#| | | | | c4
#-------------
#| | | | | d4
#-------------
#SETTING CONSTANT VARIABLES
ZONENAME = ''
DESCRIPTION = 'description'
EXAMINATION = 'examine'
SOLVED = False
UP = 'up','north'
DOWN = 'down','south'
LEFT = 'left','west'
RIGHT = 'right','east'
solved_places = {'a1': False,'a2': False,'a3': False,'a4':False,'b1': False,'b2': False,'b3': False,'b4':False,'c1': False,'c2': False,'c3': False,'c4':False,'d1': False,'d2': False,'d3': False,'d4':False,}
zonemap = {
'a1': {
ZONENAME: "Town Market",DESCRIPTION: 'description',EXAMINATION: 'examine',SOLVED: False,UP: '',DOWN: 'b1',LEFT: '',RIGHT: 'a2',},'a2': {
ZONENAME : "Town Entrance",DESCRIPTION : 'description',EXAMINATION : 'examine',SOLVED : False,UP : '',DOWN : 'b2',LEFT : 'a1',RIGHT : 'a3','a3': {
ZONENAME : "Town Square",DOWN : 'b3',LEFT : 'a2',RIGHT : 'a4','a4': {
ZONENAME : "Town Hall",DOWN : 'b4',LEFT : 'a3',RIGHT : '','b1': {
ZONENAME : "",UP : 'a1',DOWN : 'c1',LEFT : '','b2': {
ZONENAME : "Home",DESCRIPTION : 'This is your home',EXAMINATION : 'nothing has changed it is your home',UP : 'a2',DOWN : 'c2',LEFT : 'b1',RIGHT : 'b3',}
### GAME INteraCTIVITY ###
def print_location():
print('\n' + ('#' * (4 + len(myPlayer.location))))
print('#' + myPlayer.location.upper() + '#')
print('#' + zonemap[myPlayer.position][DESCRIPTION]+ '#')
print('\n' + ('#' * (4 + len(myPlayer.location))))
def prompt():
print("\n" + "==========================================")
print("What would you like to do?")
action = input("> ")
acceptable_actions = ['move','go','travel','walk','quit','examine','inspect','interact','look' ]
while action.lower() not in acceptable_actions:
print("UnkNown action,please try again. \n")
action = input("> ")
if action.lower() == 'quit':
sys.exit
elif action.lower() in ['move','walk']:
player_move(action.lower())
elif action.lower() in ['examine','look']:
player_examine(action.lower())
def player_move(myAction):
ask = "Where would you like to move to?\n"
dest = input(ask)
if dest in ['up','north']:
destination = zonemap[myPlayer.location][UP]
movement_handler(destination)
elif dest in ['left','west']:
destination = zonemap[myPlayer.location][LEFT]
movement_handler(destination)
elif dest in ['east','right']:
destination = zonemap[myPlayer.location][RIGHT]
movement_handler(destination)
elif dest in ['south','down']:
destination = zonemap[myPlayer.location][DOWN]
movement_handler(destination)
def movement_handler(destination):
print("\n" + "You have moved to the " + destination + ".")
myPlayer.location = destination
print_location()
def player_examine(action):
if zonemap[myPlayer.location][SOLVED]:
print("You have already exhuasted this zone.")
else:
print("You can trigger a puzzle here")
### GAME FINCTIONALITY ###
def main_game_loop():
while myPlayer.game_over == False:
prompt()
def setup_game():
os.system('clear')
##NAME COLLECTOR
question1 = "Hello,whats your name?\n"
for character in question1:
sys.stdout.write(character)
sys.stdout.flush()
time.sleep(0.05)
player_name = input("> ")
myPlayer.name = player_name
#ROLE HANDLING
question2 = "Hello,what role do you want to play as?\n"
question2added = "(The roles available are warrior,mage or a priest)\n "
for character in question2:
sys.stdout.write(character)
sys.stdout.flush()
time.sleep(0.05)
for character in question2added:
sys.stdout.write(character)
sys.stdout.flush()
time.sleep(0.1)
player_job = input("> ")
valid_jobs = ['warrior','mage','priest' ]
if player_job.lower() in valid_jobs:
myPlayer.job = player_job
print("You are Now a " + player_job + "!\n")
while player_job.lower() not in valid_jobs:
player_job = input("> ")
if player_job.lower() in valid_jobs:
myPlayer.job = player_job
print("Congrats! You are Now a " + player_job + "!\n")
if myPlayer.job is 'warrior':
self.hp = 140
self.mp = 40
elif myPlayer.job is 'mage':
self.hp = 70
self.mp = 140
elif myPlayer.job is 'priest':
self.hp = 80
self.mp = 80
### INTRODUCTION
question3 = "Welcome!," + player_name + " the great " + player_job + "!.\n"
for character in question3:
sys.stdout.write(character)
sys.stdout.flush()
time.sleep(0.05)
player_name = input("> ")
player_name.name = player_name
speech1 = "Welcome to this fantasy world!"
speech2 = "I hope that it greets you well!"
speech3 = "Just make sure that you dont get too lost yourself..."
speech4 = "Hehehehe..."
for character in speech1:
sys.stdout.write[character]
sys.stdout.flush()
time.sleep(0.03)
for character in speech2:
sys.stdout.write[character]
sys.stdout.flush()
time.sleep(0.03)
for character in speech3:
sys.stdout.write[character]
sys.stdout.flush()
time.sleep(0.1)
for character in speech4:
sys.stdout.write[character]
sys.stdout.flush()
time.sleep(0.2)
os.system('clear')
print("#################################################")
print("# Lets start Now! #")
print("#################################################")
main_game_loop()
title_screen()
但是当我回叫.location之类的东西时,似乎就可以了。我的代码中可能有错误还是只是repl.it?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。