下面是我的代码,主要在于points函数,它不执行if里面的内容,但也没有报错
# -*- coding:utf-8 -*-
import random
import time
import sys
playerPoints = 100
playerWon = False
playerChoice = None
playerGuess = None
def easy():
## 简单难度的踩雷,在 1 ~ 100 之间随机选择雷号
global playerWon
rbnList = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,
37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,
70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100]
rbn = random.sample(rbnList,3)
if playerGuess == rbn[0] or playerGuess == rbn[1] or playerGuess == rbn[2]:
sys.stdout.write("你踩到地雷了!")
playerWon = False
points()
else:
sys.stdout.write("你躲开了地雷!")
playerWon = True
points()
def normal():
## 正常难度的踩雷,在 1 ~ 50 之间随机选择雷号
global playerWon
rbnList = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,
37,38,39,40,41,42,43,44,45,46,47,48,49,50]
rbn = random.sample(rbnList,3)
if playerGuess == rbn[0] or playerGuess == rbn[1] or playerGuess == rbn[2]:
sys.stdout.write("你踩到地雷了!")
playerWon = False
else:
sys.stdout.write("你躲开了地雷!")
playerWon = True
points()
def hard():
## 困难难度的踩雷,在 1 ~ 20 之间随机选择雷号
global playerWon
rbnList = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
rbn = random.sample(rbnList, 3)
if playerGuess == rbn[0] or playerGuess == rbn[1] or playerGuess == rbn[2]:
sys.stdout.write("你踩到地雷了!")
playerWon = False
else:
sys.stdout.write("你躲开了地雷!")
playerWon = True
points()
def points():
## 玩家分数,踩雷游戏分数系统
global playerPoints
if playerWon == True:
if playerChoice == "简单难度":
playerPoints += 5
sys.stdout.write("当前分数加5, 当前分数: %s" % playerPoints)
elif playerChoice == "正常难度":
playerPoints += 10
sys.stdout.write("当前分数加10, 当前分数: %s" % playerPoints)
elif playerChoice == "困难难度":
playerPoints += 20
sys.stdout.write("当前分数加20, 当前分数: %s" % playerPoints)
elif playerWon == False:
if playerChoice == "简单难度":
playerPoints -= 5
sys.stdout.write("当前分数减5, 当前分数: %s" % playerPoints)
if playerPoints < 0:
sys.stdout.write("分数过少,不能进行减分,请等待分数重置")
playerPoints = 0
resetPoints()
elif playerChoice == "正常难度":
playerPoints -= 7
sys.stdout.write("当前分数减7, 当前分数: %s" % playerPoints)
if playerPoints < 0:
sys.stdout.write("分数过少,不能进行减分,请等待分数重置")
playerPoints = 0
resetPoints()
elif playerChoice == "困难难度":
playerPoints -= 10
sys.stdout.write("当前分数减10, 当前分数: %s" % playerPoints)
if playerPoints < 0:
sys.stdout.write("分数过少,不能进行减分,请等待分数重置")
playerPoints = 0
resetPoints()
def resetPoints():
resetPointsTimes = 300
for i in range(resetPointsTimes):
sys.stdout.write("分数将在 {} 秒后重置为100".format(resetPointsTimes - i))
sys.stdout.flush()
time.sleep(1)
sys.stdout.write("分数成功重置为100")
GameBody()
def rules():
sys.stdout.write("==================== *游戏规则* ====================")
sys.stdout.write("本游戏分3个模式:简单模式,正常模式和困难模式")
sys.stdout.write("简单模式: 赢了得5分, 输了扣5分")
sys.stdout.write("正常模式: 赢了得10分, 输了扣7分")
sys.stdout.write("困难模式: 赢了得20分, 输了扣10分")
sys.stdout.write("祝您游戏愉快!")
def GameBody():
global playerChoice
global playerGuess
playerChoice = input("简单模式请输入'简单模式',正常模式请输入'正常模式'," +
"困难模式请输入'困难模式',查看游戏规则请输入'规则':")
if playerChoice == "简单模式":
playerGuess = int(input("请输入一个 1 ~ 100 的数字"))
easy()
elif playerChoice == "正常模式":
playerGuess = int(input("请输入一个 1 ~ 50 的数字"))
normal()
elif playerChoice == "困难模式":
playerGuess = int(input("请输入一个 1 ~ 20 的数字"))
hard()
elif playerChoice == "规则":
rules()
else:
sys.stdout.write("输入错误, 请重新输入")
GameBody()
GameBody()