Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/280.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 2层记分板?_Python_Python 3.x - Fatal编程技术网

Python 2层记分板?

Python 2层记分板?,python,python-3.x,Python,Python 3.x,我试图在这个简单的记分板上实现第二层“得分”。我太迷路了。我自学成才,所以我确信我错过了重要的步骤 条件:3分=1轮。10轮=赢家 这就是我在代码中的位置。我可以通过按A或B来计算点数,但当它达到“winRound”值时,在本例中为“3”,什么都不会发生,它会继续计算(我已经在这上面花了5个多小时,我要放弃了。我知道这很简单,但它伤害了灵魂 我试图触发“回合胜利”,所以我知道这一切是如何运作的,我可以继续 import keyboard from time import sleep #fig

我试图在这个简单的记分板上实现第二层“得分”。我太迷路了。我自学成才,所以我确信我错过了重要的步骤

条件: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!")
导入键盘
从时间上导入睡眠
#战士
fighter1=“标记”
fighter2=“吉姆”
fighter3=“汤姆”
fighter4=“瑞克”
#要点
点1=0
点2=0
点3=0
点4=0
winRound=3
#轮
第1轮=0
第2轮=0
第3轮=0
第四轮=0
winMatch=10
#睡眠时间
睡眠时间=.25
def更换战斗机1(战斗机):
fighter1=str(输入(“设置战斗机名称”))
#记分牌
def sb1():
打印(fighter1,“:”,round1,“:”,point1,“| vs |”,point2,“:”,round2,“:”,fighter2)
def sb2():
打印(fighter3,“:”,第三轮,“:”,第三点,“| vs |”,第四点,“:”,第四轮,“:”,第四轮)
#功能
def resetScores():
f=打开(“fighter1points.txt”,“w”)
f、 写入(str(0))
f、 关闭()
f=打开(“fighter2points.txt”、“w”)
f、 写入(str(0))
f、 关闭()
f=打开(“fighter3points.txt”、“w”)
f、 写入(str(0))
f、 关闭()
f=打开(“fighter4points.txt”,“w”)
f、 写入(str(0))
f、 关闭()
#第一场比赛开始
sb1()
当第1轮=winMatch:
打印(“\n”,fighter1,“已到达”,winMatch,“回合!”)
打印(“玩家1赢得战斗!”)
elif round2>=winMatch:
打印(“\n”,fighter2,“已到达”,winMatch,“回合!”)
打印(“玩家2赢得战斗!”)
#第二场比赛开始
sb2()
当第3轮=winMatch:
匹配活动=错误
打印(“\n第3层已到达”,winMatch,“回合!”)
打印(“玩家3赢得战斗!”)
elif round4>=winMatch:
匹配活动=错误
打印(“\n第4层已到达”,winMatch,“回合!”)
打印(“玩家4赢得战斗!”)

您在添加2
的同时
,但实际上您必须在1
的同时
完成所有工作。您的代码仍需要一些改进,但使用此代码可以解决您遇到的问题:

为True时:
如果按了键盘上的“a”:
点1+=1
sb()
睡眠(睡眠时间)
elif键盘。按下按钮('b'):
点2+=1
sb()
睡眠(睡眠时间)
如果point1>=winRound:
打印(“圆赢!”)
打破
注意 您的代码可以改进很多,但作为一名新程序员,您应该坚持所学内容。但是,您应该从一开始就考虑一些问题:

  • 始终使用命名约定
  • 编写
    pythonic
    code,即在match\u active==True时不需要
    您只需在match\u active时编写
  • 创建通用函数,使用参数,这样您就可以重用相同的函数,只需调整一些参数即可。请参阅我的代码,其中反映了以下几点:
导入键盘
从时间上导入睡眠
#战士
fighter1=“吉姆”
fighter2=“Ryan”
#要点
点1=0
点2=0
赢一轮=3
#轮
第1轮=0
第2轮=0
赢得比赛=10
#睡眠时间
睡眠时间=1
#定义正在启用的匹配项
match_active=True
四舍五入激活=真
#记分牌
def sb(战斗机、点、轮):
打印(“战斗机:”,战斗机,“\n点:”,点,“\n圈:”,圆形,“\n”)
#功能
尽管如此:
如果按了键盘上的“a”:
点1+=1
sb(战斗1,第1点,第1轮)
睡眠(睡眠时间)
elif键盘。按下按钮('b'):
点2+=1
sb(战斗2,第2点,第2轮)
睡眠(睡眠时间)
如果第1点>=赢得一轮:
打印(fighter1,“圆赢!”)
打破
elif point2>=赢得一轮:
打印(fighter2,“圆赢!”)
打破

while循环将继续运行,直到条件为false,在这种情况下,它将离开循环。因此,您的函数

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:
    # condition that will result in game finishing
    if point1 >= winRound:
        print("Round Won!")
        [.. quit out of the game..]

    elif keyboard.is_pressed('a'):
        point1 += 1
        sb()
        sleep(sleepTime)
    elif keyboard.is_pressed('b'):
        point2 += 1
        sb()
        sleep(sleepTime)
#!/usr/bin/env python3
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 sb1():
    print("Fighter:",fighter1,'\n','Points:',point1,'\n','Rounds:',round1)
def sb2():
    print("Fighter:",fighter2,'\n','Points:',point2,'\n','Rounds:',round2)

#functions

while matchActive == True:
    if keyboard.is_pressed('a'):
        point1 += 1
        sb1()
        sleep(sleepTime)
    elif keyboard.is_pressed('b'):
        point2 += 1
        sb2()
        sleep(sleepTime)

    if point1 == 3:
        round1 += 1
        point1 = 0
        point2 = 0
    elif point2 == 3:
        round2 += 1
        point1 = 0
        point2 = 0

    if round1 == winMatch or round2 == winMatch:
        print("Player #1 wins..." if round1 > round2 else "Player #2 wins...")
        matchActive = False