有没有办法在满足条件后终止我的程序? (如果玩家赢得了比赛)
Is there a way to terminate my program after a condition is met? (If a player won the game)
我大约一周前刚学会 Python(我没有任何编程经验),但我试图重新创建蛇和梯子游戏。我对它进行了编码,但我不知道如果玩家赢了如何终止程序。玩家获胜后(达到 100 或任何棋盘大小),我的程序仍然要求另一个玩家掷骰子,游戏继续进行。
这是我创建的播放器 class。
您可以在 Player.turn()
.
中找到获胜条件
class Player:
def __init__(self, name):
self.name = str(name)
self.position = int()
def ask_to_roll(self):
while True:
command = str(input("Press enter to roll the dice.\n> "))
if command == "":
break
else:
print("Invalid")
continue
def roll_dice(self):
print(f'{self.name} is rolling the dice...')
time.sleep(delay)
roll_result = random.randint(1, dice_faces)
print(f"It's a {roll_result}!")
time.sleep(delay)
return roll_result
def check_for_snakes_ladders(self):
if self.position in snakes.keys():
print(random.choice(snake_bite))
time.sleep(delay)
print(f'{self.name} fell from {self.position} to {snakes[self.position]}')
time.sleep(delay)
self.position = snakes[self.position]
elif self.position in ladders.keys():
print(random.choice(ladder_jump))
time.sleep(delay)
print(f'{self.name} climbed from {self.position} to {ladders[self.position]}')
time.sleep(delay)
self.position = ladders[self.position]
else:
pass
def turn(self):
print(f"\nIt's {self.name}'s turn")
self.ask_to_roll()
roll_result = self.roll_dice()
new_position = self.position + roll_result
if new_position > board_size:
print(f"Oops you need to get {board_size - self.position}")
pass
elif new_position == board_size:
print(f'{self.name} won')*
elif new_position < board_size:
print(f'{self.name} moved from {self.position} to {new_position}')
time.sleep(delay)
self.position = new_position
self.check_for_snakes_ladders()
elif new_position == board_size:
print(f'{self.name} won')
sys.exit(0)
记得在文件顶部import sys
我大约一周前刚学会 Python(我没有任何编程经验),但我试图重新创建蛇和梯子游戏。我对它进行了编码,但我不知道如果玩家赢了如何终止程序。玩家获胜后(达到 100 或任何棋盘大小),我的程序仍然要求另一个玩家掷骰子,游戏继续进行。
这是我创建的播放器 class。
您可以在 Player.turn()
.
class Player:
def __init__(self, name):
self.name = str(name)
self.position = int()
def ask_to_roll(self):
while True:
command = str(input("Press enter to roll the dice.\n> "))
if command == "":
break
else:
print("Invalid")
continue
def roll_dice(self):
print(f'{self.name} is rolling the dice...')
time.sleep(delay)
roll_result = random.randint(1, dice_faces)
print(f"It's a {roll_result}!")
time.sleep(delay)
return roll_result
def check_for_snakes_ladders(self):
if self.position in snakes.keys():
print(random.choice(snake_bite))
time.sleep(delay)
print(f'{self.name} fell from {self.position} to {snakes[self.position]}')
time.sleep(delay)
self.position = snakes[self.position]
elif self.position in ladders.keys():
print(random.choice(ladder_jump))
time.sleep(delay)
print(f'{self.name} climbed from {self.position} to {ladders[self.position]}')
time.sleep(delay)
self.position = ladders[self.position]
else:
pass
def turn(self):
print(f"\nIt's {self.name}'s turn")
self.ask_to_roll()
roll_result = self.roll_dice()
new_position = self.position + roll_result
if new_position > board_size:
print(f"Oops you need to get {board_size - self.position}")
pass
elif new_position == board_size:
print(f'{self.name} won')*
elif new_position < board_size:
print(f'{self.name} moved from {self.position} to {new_position}')
time.sleep(delay)
self.position = new_position
self.check_for_snakes_ladders()
elif new_position == board_size:
print(f'{self.name} won')
sys.exit(0)
记得在文件顶部import sys