python掷骰子_用于掷骰子的Python程序(2人骰子游戏)
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 randomdef roll():return random.choice([1,2,3,4,5,6])class player(object):def __init__(self, name, age, colour):self.name = nameself.age = ageself.colour = colourdef score(self, score):self.score = score def getscore(self):return self.scoredef getname(self):return self.namedef __str__(self):return 'NAME: ' + self.name + '\nCOLOUR: ' + self.colour + '\nSCORE: ' + str(self.score)class game(object):def __init__(self, playr, trails):self.trails = trailsself.playr = playrdef gaming(self):throw = 0score = 0for i in range(self.trails):throw = roll()if throw == 6:throw = throw + roll()score = throw + score return scoredef __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: 525Practice more python experiences here: python programs
在這里練習更多python經驗: python程序
翻譯自: https://www.includehelp.com/python/program-for-rolling-the-dice-2-player-dice-game.aspx
python擲骰子
總結
以上是生活随笔為你收集整理的python掷骰子_用于掷骰子的Python程序(2人骰子游戏)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 颐和园用提前预约购票吗
- 下一篇: linux 目录命令_Linux目录命令