当点达到一定的麻木度时激活弹球缓冲器五度

2024-03-29 09:34:00 发布

您现在位置:Python中文网/ 问答频道 /正文

所以我创建了一个弹球脚本,它有两个设置,即简单设置(easy\u pinball)和硬设置(hard\u pinball)。硬设置继承了所有的简单设置方法。唯一的区别是硬设置有两种方法,你可以失去分数。我的问题是,当球员得分达到5分或以上时,如何“激活”硬弹球课中的两种方法?请注意,我不希望这两种方法一旦被激活就消失。例如,有人在弹球游戏中击出5分,然后激活硬弹球方法,但随后他的得分下降到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')

Tags: 方法fromselfyouifdefwinpoints
1条回答
网友
1楼 · 发布于 2024-03-29 09:34:00

我只使用一个Pinball类,并且有一个difficulty枚举属性,当points达到5时,该属性将更改为Difficulty.hard。然后,对third_bumperfourth_bumper的所有调用都可以在应用各自的点惩罚之前首先检查self.difficulty == Difficulty.hard

我还利用pointsballs属性上的@property简化了一些游戏逻辑,以自动触发赢球、输球和难度调整(这意味着逻辑不再分散在其他方法之间)

from random import randint
from enum import Enum

class Difficulty(Enum):
    easy = 'easy'
    hard = 'hard'

class Pinball(object):

    def __init__(self, points = 0, balls = 3, winning_score = 20, hard_threshold = 5):
        self.winning_score = winning_score
        self.hard_threshold = hard_threshold
        self.difficulty = Difficulty.easy

        self.points = points
        self.balls = balls

    @property
    def points(self):
        return self._points

    @points.setter
    def points(self, v):
        self._points = v
        if v >= self.hard_threshold:
            self.difficulty = Difficulty.hard
        if v >= self.winning_score:
            self.win()

    @property
    def balls(self):
        return self._balls

    @balls.setter
    def balls(self, v):
        self._balls = v
        if v == 0:
            self.lose()

    def win(self):
        print('You win!')

    def lose(self):
        print('You lose!')

    #  When you hit this bumper, you get random number from 1 3

    def first_bumper(self):
        self.points += randint(1, 3)

    #  when you hit this bumper, you get random number from 3 6

    def second_bumper(self):
        self.points += randint(4, 6)

    #  If you ball falls in hole, you lose one ball

    def losing_balls(self, count = 1):
         self.balls -= count

    #  activate bumpers where you can lose points when
    #  your points total reaches 5

    def third_bumper(self, penalty = 1):
        if self.difficulty == Difficulty.hard:
            self.points -= penalty

    def fourth_bumper(self, penalty = 5):
        if self.difficulty == Difficulty.hard:
            self.points -= penalty

相关问题 更多 >