Python游戏最高分
嗨,我用Python做了一个游戏,玩家需要在时间耗尽之前输入指定的数字。我想保存前五名的高分,并在游戏结束时显示出来。我在网上找了很多解决方案,但因为我对Python还不太熟悉,所以无法把它们调整到我的代码里。我想把名字和分数保存在一行上,最好用\t来分隔它们。
任何答案都非常感谢。
游戏代码:
# number press game
import time
import random
print "Welcome to my Number Press Game"
print "the computer will display a number"
print "you will have to enter the number before the time runs out"
name = raw_input ("please enter your name: "+"\n")
print "\n"+"welcome to the game "+name
print "ready"
time.sleep (1)
print "steady"
time.sleep (1)
print "GO!"
#game
loop = -1
t = 3
while True:
loop = loop+1
x = random.randint(1,3)
print x
start = time.time()
num = input("the number is: ")
end = time.time()
elapsed = end - start
if elapsed > t and num == x:
print "CORRECT, but too slow"
break
elif num != x and elapsed < t:
print "\n"+"WRONG, but in time"
break
elif num != x and elapsed > t:
print "WRONG and too slow"
break
if num == x and elapsed< t:
print "CORRECT and in time"
if t>0.7:
t =t*0.9
print "you loose"
print name+" scored: "+str(loop)
1 个回答
0
运行这个看看是否能达到你的想法:这个代码模拟了玩游戏n次。然后会把所有的分数打印出来,显示最高的分数。我这里用了y = 5
,如果你需要5个最高分,那我想你至少需要玩5次游戏,然后把这些分数进行比较,最后把最高的分数打印出来,这个过程是在for loop
里完成的。接着,最高分会被排序,以挑选出你需要的n个最高分。不过,你应该检查一下你的逻辑,似乎有点问题。如果这个回答对你有帮助,请给个赞。谢谢!
import time
import random
high_score = []
def play_game():
print "Welcome to my Number Press Game"
print "the computer will display a number"
print "you will have to enter the number before the time runs out"
name = raw_input ("please enter your name: "+"\n")
print "\n"+"welcome to the game "+name
print "ready"
time.sleep (1)
print "steady"
time.sleep (1)
print "GO!"
loop = -1
t = 3
score = 0
while True:
loop = loop+1
x = random.randint(1,3)
print x
start = time.time()
num = input("the number is: ")
end = time.time()
elapsed = end - start
if elapsed > t and num == x:
print "CORRECT, but too slow"
break
elif num != x and elapsed < t:
print "\n"+"WRONG, but in time"
break
elif num != x and elapsed > t:
print "WRONG and too slow"
break
if num == x and elapsed< t:
print "CORRECT and in time"
score += 1
if t>0.7:
t = t*0.9
high_score.append(score)
print "you loose"
print name + " scored: "+ str(score)
print "All highest_scores =", high_score
highest_scores = sorted(high_score, reverse = True)
print "All highest_scores =",highest_scores[0:3]
y = 5
for i in range (y):
play_game()