所以我创建了一个带有两个设置的弹球脚本,即简易设置(Easy_Pinball)和硬设置(Hard_Pinball)。 Hard设置继承了所有简单的设置方法。唯一的区别是硬设置有两种方法可以让你失分。我的问题是,我怎样才能激活"当玩家分数达到5或以上时,Hard_Pinball类中的两种方法?请注意,我不希望这些方法一旦被激活就会消失。例如,有人在弹球游戏中击中5分,然后激活Hard_Pinball方法,但随后他的分数降至4,硬设置中的两种方法仍应一直工作到零,这将导致玩家输掉游戏。出于某种原因,我对此感到难过。有什么帮助吗?
from random import randint
class Easy_Pinball(object):
def __init__(self, points = 0, balls = 3):
self.points = points
self.balls = balls
# When you hit this bumper, you get random number from 1 3
def first_bumper(self, winner = 20):
if self.points < winner:
self.points += randint(1,3)
if self.points >= winner:
print('you win')
elif self.points >= winner:
print ('you win')
# when you hit this bumper, you get random number from 3 6
def second_bumper(self, winner = 20):
if self.points < winner:
self.points += randint(4,6)
if self.points >= winner:
print('you win')
elif self.points >= winner:
print('you win')
# If you ball falls in hole, you lose one ball
def losing_balls(self, lost_one = 1):
if self.balls > 0:
self.balls -= lost_one
elif self.balls == 0:
print('you lose, braaahh')
class Hard_Pinball(Easy_Pinball):
# activate bumpers where you can lose points when
# your points total reaches 5
def third_bumper(self, bumper = 1):
if self.points > 0:
self.points -= bumper
elif self.points <= 0:
print('you lose, you bum')
def fourth_bumper(self, bumper = 5):
if self.points > 0:
self.points -= bumper
if self.points <= 0:
print('you lose, you bum')
elif self.points <= 0:
print('you lose, you bum')
答案 0 :(得分:0)
我只使用一个Pinball
类,并且difficulty
枚举属性在Difficulty.hard
达到5时更改为points
。然后,所有调用{{ 1}}和third_bumper
可以在应用各自的点数惩罚之前初步检查是否fourth_bumper
。
我还冒昧地使用self.difficulty == Difficulty.hard
和@property
属性上的points
简化了一些游戏逻辑,以自动触发获胜,失败和难度调整(意味着逻辑不再在你的其他方法之间展开。)
balls