我试图在这个简单的记分板上实现第二层“得分”。我太迷路了。我自学成才,所以我确信我错过了重要的步骤
条件:3分=1轮。10轮=赢家
这就是我在代码中的位置。我可以通过按A或B来计算点数,但当它达到“winRound”值时,在本例中为“3”,什么都不会发生,它会继续计算(我已经在这上面花了5个多小时,我要放弃了。我知道这很简单,但它伤害了灵魂
我试图触发“回合胜利”,所以我知道这一切是如何运作的,我可以继续
import keyboard
from time import sleep
#fighters
fighter1 = "Jim"
fighter2 = "Ryan"
#points
point1 = 0
point2 = 0
winRound = 3
#rounds
round1 = 0
round2 = 0
winMatch = 10
#sleep time
sleepTime = 1
#defines the match being on
matchActive = True
roundActive = True
#scoreboard
def sb():
print("Fighter:",fighter1,'\n','Points:',point1,'\n','Rounds:',round1)
#functions
while matchActive == True:
if keyboard.is_pressed('a'):
point1 += 1
sb()
sleep(sleepTime)
elif keyboard.is_pressed('b'):
point2 += 1
sb()
sleep(sleepTime)
while matchActive == True:
if point1 >= winRound:
print("Round Won!")
老实说,我不完全理解python是如何看待所有这些代码的。这可能就是问题所在
编辑:我已经完成了所有工作,以下是最终代码:
import keyboard
from time import sleep
#fighters
fighter1 = "Mark"
fighter2 = "Jim"
fighter3 = "Tom"
fighter4 = "Rick"
#points
point1 = 0
point2 = 0
point3 = 0
point4 = 0
winRound = 3
#rounds
round1 = 0
round2 = 0
round3 = 0
round4 = 0
winMatch = 10
#sleep time
sleepTime = .25
def changeFighter1(fighter):
fighter1 = str(input("Set Fighter Name"))
#scoreboard
def sb1():
print(fighter1,":",round1,":",point1," |vs| ",point2,":",round2,":",fighter2)
def sb2():
print(fighter3,":",round3,":",point3," |vs| ",point4,":",round4,":",fighter4)
#functions
def resetScores():
f = open("fighter1points.txt", "w")
f.write(str(0))
f.close()
f = open("fighter2points.txt", "w")
f.write(str(0))
f.close()
f = open("fighter3points.txt", "w")
f.write(str(0))
f.close()
f = open("fighter4points.txt", "w")
f.write(str(0))
f.close()
#match 1 start
sb1()
while round1 < winMatch and round2 < winMatch:
if keyboard.is_pressed('r'):
resetScores()
if keyboard.is_pressed('a'):
if point1 < winRound:
point1 += 1
sb1()
sleep(sleepTime)
if point1 == winRound:
point1 = 0
point2 = 0
round1 += 1
sb1()
f = open("fighter1points.txt","w")
f.write(str(round1))
f.close()
#if round1 == winMatch:
#pass
elif keyboard.is_pressed('b'):
if point2 < winRound:
point2 += 1
sb1()
sleep(sleepTime)
if point2 == winRound:
point1 = 0
point2 = 0
round2 += 1
sb1()
f = open("fighter2points.txt", "w")
f.write(str(round2))
f.close()
#if round2 == winMatch:
#pass
#Match 1 Over
if round1 >= winMatch:
print("\n",fighter1,"has reached",winMatch,"rounds!")
print("Player 1 wins the battle!")
elif round2 >= winMatch:
print("\n",fighter2,"has reached",winMatch,"rounds!")
print("Player 2 wins the battle!")
#match 2 start
sb2()
while round3 < winMatch and round4 < winMatch:
if keyboard.is_pressed('r'):
resetScores()
if keyboard.is_pressed('a'):
if point3 < winRound:
point3 += 1
sb2()
sleep(sleepTime)
if point3 == winRound:
point3 = 0
point4 = 0
round3 += 1
sb2()
#if round1 == winMatch:
#pass
elif keyboard.is_pressed('b'):
if point4 < winRound:
point4 += 1
sb2()
sleep(sleepTime)
if point4 == winRound:
point3 = 0
point4 = 0
round4 += 1
sb2()
#if round2 == winMatch:
#pass
#Match 2 Over
if round3 >= winMatch:
matchActive = False
print("\nPlayer 3 has reached",winMatch,"rounds!")
print("Player 3 wins the battle!")
elif round4 >= winMatch:
matchActive = False
print("\nPlayer 4 has reached",winMatch,"rounds!")
print("Player 4 wins the battle!")
您正在添加2
while
,但实际上您必须在1while
中完成所有工作。您的代码仍需要一些改进,但请使用此代码修复您遇到的问题:注意 你的代码可以改进很多,但作为一名新程序员,你应该坚持你所学到的东西。但是,您应该从一开始就考虑以下事项:
pythonic
代码,即不需要while match_active == True
您只需写while match_active
“while”循环将继续运行,直到条件为false,在这种情况下,它将离开循环。因此,你的职能
将继续循环,并且永远不会下降到下一个while循环,该循环检查该人是否有足够的点数来完成游戏
最好将游戏逻辑全部放在主while循环中
首先,您没有打印玩家2的游戏统计数据,因为您只有一个函数
sb()
打印玩家1的统计数据。 接下来我改变的是,你增加分数,但从不增加回合数。因此,如果一个玩家达到winRound
值,那么两个玩家的积分都会被删除。对于这个特定的播放器,pointX值增加1。 最后一个更改是第二个while语句,它已被if语句替换。您需要检查是否有一个玩家达到了winRound
值。这是由最后一个if语句完成的。如果其中一名玩家赢了(在本例中)10轮,则打印赢家,布尔值matchActive设置为False,这将在下次比较matchActive == True
时退出while循环相关问题 更多 >
编程相关推荐