我对Python和编程一般都很陌生,基本上就是开始学习如何抓取。我决定学习编程,所以我尝试开发一个小骰子游戏。
主要意图(我没有成功制作像我想要的代码)是用以下规则制作游戏:
当我第一次尝试制作这段代码时,我想用类和函数来实现它,但我无法做到这一点。此外,我无法弄清楚如何为多个玩家设计游戏。我尝试使用列表和词典,但我一直在拉我的头发。所以我只是尝试为1名玩家制作游戏,目标是在最少轮次中达到100分(每次你获得积分时都会开始新一轮)。不是很有趣,但至少我让它起作用(好吧,有点)。
我非常感谢对我的小程序的任何评论。请注意它是我的第一个代码,我知道它写的很糟糕。我想听听你们怎么认为代码可以改进,以及我应该如何为多个玩家组织代码。我有兴趣在稍后阶段学习移动设备的编程,并希望最终将这个小游戏变成一个应用程序,这样我就可以和朋友一起玩(为了娱乐和学习操作方法而且#39 ; S)
希望缩进调整得当。
from random import choice # Importing the random element to create the dice
score = 0
rounds = 1
secured = 0
unsecured = 0
ask = "N/A"
while secured+unsecured < 100: # Check if the total score is below 100
a = int(choice([1,2,3,4,5,6])) # Choose a dice number
if a == 1: # Check if the dice is one
if ask == "s": # Check if the player secured the last score
print ("Good thing you just secured your score, because you now rolled a 1.")
print ("Because of this, you only lost your round, not any points! :)")
if ask =="N/A": # Check if the player rolled a one on the first throw
print ("************* Round 1 ***********")
print ("Tough luck, you rolled a ONE on your first roll. You lost one round.")
rounds +=1
if ask == "r": # Check if the player lost points that was unsecured
print ("")
print ("***** UH UH UH UH UH - YO ROLLED A ONE ******")
print ("You lost the", unsecured,"points you didn't secure")
unsecured = 0 # Player loses the unsecured points
rounds +=1 # Increase round number
else:
unsecured = unsecured + a # If the dice returned something else than one, the player wins the points
print ("")
print ("")
print ("************ Round ", rounds, "**************")
print ("")
if a > 1: # Only display the dice if the dice is higher than one (if it's one it has already been printed)
print ("You rolled a ", a)
print ("")
print ("Unsecured points: ", unsecured)
print ("Secured points:" , secured)
print ("The sum of secured and unsecured pointsare:",secured+unsecured, "points")
print ("")
ask = input ("Roll the dice or secure? (r/s) ")
if ask == "s":
secured = secured+unsecured # If player chooses to secure the points, the points are added
score = 0
rounds +=1 # Increase round
if unsecured == 0: # Check if player was an idiot and chose to secure 0 points
print ("You chose to secure zero points, not very clever! You wouldn't risk anything by rolling the dice, and you lost the round!")
else:
print ("You chose to secure your", unsecured, "points, and you have secured", secured, "points in total")
print ("")
unsecured = 0 # Reset unsecured points, since it is added to secure points
input ("Press Enter to roll the dice:")
else:
if ask == "r":
score = unsecured+a
else:
print ("Congrats! You made ",secured+unsecured, "points in", rounds , "rounds")