python掷骰子
Here, we will be going to design a very simple and easy game and implement it using abstract data class. The code consists of two different classes (The base of the whole program). The one will be the class for the player and others will be for the game.
在这里,我们将设计一个非常简单的游戏,并使用抽象数据类实现它。 该代码包含两个不同的类(整个程序的基础)。 一个将是玩家的职业,其他将是游戏的职业。
The formal code structure is as:
正式的代码结构如下:
player() class: This player class will be storing player name, its age and its color code for the game. There is a method called score which stores the attribute score associated with the player. Another method getscore() for calling the value of score stored.
player()类 :此玩家类将存储玩家名称,年龄和游戏的颜色代码。 有一种称为得分的方法,该方法存储与玩家关联的属性得分。 另一个方法getscore()用来调用存储的分数值。
game() class: This class represents the game and take input as the player (class type) and the number of trails. The method __init__() defines the attribute associated with the class type game. The method gaming() is consisting of the whole game.
game()类 :此类表示游戏,并接受输入作为玩家(类类型)和步数。 方法__init __()定义与类类型游戏关联的属性。 game ()方法由整个游戏组成。
dice() function: The function dice just give output as a random value from the number set [1,2,3,4,5,6]. This uses random.choice() function for performing this task.
dice()函数 :函数dice只是将输出值设为[1,2,3,4,5,6]中的一个随机值。 这使用random.choice()函数执行此任务。
Game Rules:
游戏规则:
Player will throw a dice and the output will be added to the current scores of the player (initially equal to zero). If the dice had output 6 then it would be thrown again (one dice: 6, one more turn: 4. Then the total would be 6+4 = 10). The sum of total will be throwing id the total score of the player with a particular number of trials.
玩家将掷出一个骰子,输出将被添加到该玩家的当前分数中(最初等于零)。 如果骰子的输出为6,则将其再次抛出(一个骰子:6,再转一圈:4。则总数为6 + 4 = 10)。 总数的总和等于具有特定次数的尝试的玩家总得分。
So let us get to the code:
因此,让我们看一下代码:
import random
def roll():
return random.choice([1,2,3,4,5,6])
class player(object):
def __init__(self, name, age, colour):
self.name = name
self.age = age
self.colour = colour
def score(self, score):
self.score = score
def getscore(self):
return self.score
def getname(self):
return self.name
def __str__(self):
return 'NAME: ' + self.name + '\nCOLOUR: ' + self.colour + '\nSCORE: ' + str(self.score)
class game(object):
def __init__(self, playr, trails):
self.trails = trails
self.playr = playr
def gaming(self):
throw = 0
score = 0
for i in range(self.trails):
throw = roll()
if throw == 6:
throw = throw + roll()
score = throw + score
return score
def __str__(self):
return self.playr.getname() + str(self.score)
tri = 123
zack = player('zack', 24, 'green')
johny = player('johny', 25, 'yellow')
kina = player('kina', 14, 'red')
usher = player('usher', 13, 'blue')
print("-----------LETs PLAy THIs GAMe--------------\n" )
#zack.score(88)
#print(zack)
zackscr = game(zack, tri)
johnyscr = game(johny, tri)
kinascr = game(kina, tri)
usherscr = game(usher, tri)
scr = []
scr.append(zackscr.gaming())
scr.append(johnyscr.gaming())
scr.append(kinascr.gaming())
scr.append(usherscr.gaming())
scrsort = sorted(scr)
for el in scrsort:
print(el)
zack.score(scr[0])
usher.score(scr[3])
kina.score(scr[2])
johny.score(scr[1])
#players = []
#players.append(zack.getscore())
#players.append(usher.getscore())
#players.append(kina.getscore())
#players.append(johny.getscore())
# =============================================================================
# =============================================================================
# =============================================================================
# # # = = = = = = == = = = == = = == = = = == = = == = = == = = == == = == == =
#for el in players:
# print('--', el)
#print(scr[0])
print(zack, '\n')
print(kina, '\n')
print(johny, '\n')
print(usher, '\n')
Output
输出量
-----------LETs PLAy THIs GAMe--------------
485
489
491
525
NAME: zack
COLOUR: green
SCORE: 485
NAME: kina
COLOUR: red
SCORE: 491
NAME: johny
COLOUR: yellow
SCORE: 489
NAME: usher
COLOUR: blue
SCORE: 525
Practice more python experiences here: python programs
在这里练习更多python经验: python程序
翻译自: https://www.includehelp.com/python/program-for-rolling-the-dice-2-player-dice-game.aspx
python掷骰子