我在自己想出的程序构想中练习类和继承。基本上,我制作了一个可以玩两种模式的街机游戏菜单模拟器,即单人游戏和多人游戏。每次输入一个选项1或2时,菜单都会显示几次,然后继续接受输入,我只希望菜单显示一次。这是我的代码:
# Suppose you are at an arcade and you and your friend want to play a multiplayer game that requires UI.
# Make the game ask for the users name and age to see if they can play, make the program so that it can add a friend.
# If any of the players are under the age of 18, they are not allowed to play, otherwise proceed.
# **EXTRA CREDIT** --> Add a functionality which adds the players to a list until the list reaches 4 players, then stop adding to the list.
# arcade_game.py
import sys
# give the user a greeting
import self as self
lst = []
class menu:
def __init__(self, ready):
self.ready = ready
#display menu
@classmethod
def display_menu(self):
print("Pick from one of the choices below, type in the corressponding number")
print("1. single player \n"
"2. Multiplayer")
choice = int(input("Enter your choice here: "))
return choice
# ready or not function to see if the user is ready to play
def ready_or_not(self):
# see if user types 1 or 2 with try & except
try:
# ask user if they are ready
self.ready = int(input("Are you ready to play? Type 1 for yes, 2 for no"))
self.display_menu()
except ValueError:
print("You did not type 1 or 2, please try again!")
# add players class
class player(menu):
# add a default player to __init__(), **(since there has to be at least one player)**
def __init__(self, ready, player1):
super().__init__(ready)
self.player1 = player1
# single player method
def set_name(self):
self.player1 = input("Enter your name for single player mode")
print("Lets play! ", self.player1)
# multiplayer method
def set_names(self):
try:
self.player1 = input("Enter your name to begin")
lst.append(self.player1)
# add another player to continue
while len(lst) <= 4:
add = input("Add player here: ")
lst.append(add)
if len(lst) == 4:
print("Player limit reached!")
break;
except ValueError:
print("You didnt enter valid input, please try again")
# get the names of the players only if 1 is picked from display_menu() above, including player1
def check_choice(self):
if self.display_menu() == 1:
self.set_name()
elif self.display_menu() == 2:
self.set_names()
else:
print("Exiting....")
print("Goodbye!")
sys.exit(0)
m = menu("yes")
m.ready_or_not()
p = player("yes", "test")
p.check_choice()
答案 0 :(得分:0)
ready_or_not
呼叫self.display_menu()
:
def ready_or_not(self):
# see if user types 1 or 2 with try & except
try:
# ask user if they are ready
self.ready = int(input("Are you ready to play? Type 1 for yes, 2 for no"))
self.display_menu()
except ValueError:
print("You did not type 1 or 2, please try again!")
check_choice
还会调用self.display_menu()
至少一次,如果您第一次键入除1
以外的其他值,则两次调用两次:
def check_choice(self):
if self.display_menu() == 1:
self.set_name()
elif self.display_menu() == 2:
self.set_names()
else:
print("Exiting....")
print("Goodbye!")
sys.exit(0)
您的顶级代码在一个菜单实例上调用ready_or_not()
:
m = menu("yes")
m.ready_or_not()
…和check_choice()
在另一个:
p = player("yes", "test")
p.check_choice()
因此,您的程序将菜单显示两次,如果您键入除1
以外的任何内容,则将显示第三次。
如果您不希望菜单显示两次或三次,请不要调用方法两次或三次。
如果您只想显示菜单一次并记住选择,而不是显示两次或三遍,则需要使用在self.ready
中创建的ready_or_not
属性,而不是调用再次使用该方法。
但是,这仍然无法按原样工作,因为您的类设计很奇怪。您已经创建了两个单独的实例m
和p
,每个实例都有自己的独立属性。我不确定为什么player
首先从menu
继承(或者为什么display_menu
是@classmethod
,或者为什么它调用其参数self
而不是为什么)而不是cls
(如果是一个,以及其他各种东西),但是,鉴于设计中的player
是menu
,您可能只需要一个player
实例,就像这样:
p = player("yes", "test")
p.ready_or_not()
p.check_choice()
然后,您可以像这样更改check_choice
:
def check_choice(self):
if self.choice == 1:
self.set_name()
elif self.choice == 2:
self.set_names()
else:
print("Exiting....")
print("Goodbye!")
sys.exit(0)
答案 1 :(得分:0)
花点时间找出答案,但是看来
>display_menu()
的通话ready_or_not()
已经结束,因此您需要将display_menu()
从准备就绪状态中删除,否则就不这样
# ready or not function to see if the user is ready to play
def ready_or_not(self):
# see if user types 1 or 2 with try & except
try:
# ask user if they are ready
self.ready = int(input("Are you ready to play? Type 1 for yes, 2 for no"))
# self.display_menu()
except ValueError:
print("You did not type 1 or 2, please try again!")
参加聚会似乎很晚