Python 2层记分板?

2024-10-04 11:30:08 发布

您现在位置:Python中文网/ 问答频道 /正文

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

条件: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!")

Tags: ifissleepprintpressedkeyboardpoint1point2
3条回答

您正在添加2while,但实际上您必须在1while中完成所有工作。您的代码仍需要一些改进,但请使用此代码修复您遇到的问题:

while True:
    if keyboard.is_pressed('a'):
        point1 += 1
        sb()
        sleep(sleepTime)
    elif keyboard.is_pressed('b'):
        point2 += 1
        sb()
        sleep(sleepTime)
    if point1 >= winRound:
        print("Round Won!")
        break

注意 你的代码可以改进很多,但作为一名新程序员,你应该坚持你所学到的东西。但是,您应该从一开始就考虑以下事项:

  • 始终使用命名约定
  • pythonic代码,即不需要while match_active == True您只需写while match_active
  • 创建通用函数,使用参数,这样就可以重用相同的函数,只需调整一些参数即可。请参阅我的代码,其中反映了以下几点:
import keyboard
from time import sleep

#fighters
fighter1 = "Jim"
fighter2 = "Ryan"

#points
point1 = 0
point2 = 0
win_round = 3

#rounds
round1 = 0
round2 = 0
win_match = 10

#sleep time
sleep_time = 1

#defines the match being on
match_active = True
round_active = True


#scoreboard
def sb(fighter, point, round):
    print("Fighter:",fighter,'\nPoints:',point,'\nRounds:',round,'\n')

#functions

while True:
    if keyboard.is_pressed('a'):
        point1 += 1
        sb(fighter1, point1, round1)
        sleep(sleep_time)
    elif keyboard.is_pressed('b'):
        point2 += 1
        sb(fighter2, point2, round2)
        sleep(sleep_time)
    if point1 >= win_round:
        print(fighter1, "Round Won!")
        break
    elif point2 >= win_round:
        print(fighter2, "Round Won!")
        break

“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循环,该循环检查该人是否有足够的点数来完成游戏

最好将游戏逻辑全部放在主while循环中

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

首先,您没有打印玩家2的游戏统计数据,因为您只有一个函数sb()打印玩家1的统计数据。 接下来我改变的是,你增加分数,但从不增加回合数。因此,如果一个玩家达到winRound值,那么两个玩家的积分都会被删除。对于这个特定的播放器,pointX值增加1。 最后一个更改是第二个while语句,它已被if语句替换。您需要检查是否有一个玩家达到了winRound值。这是由最后一个if语句完成的。如果其中一名玩家赢了(在本例中)10轮,则打印赢家,布尔值matchActive设置为False,这将在下次比较matchActive == True时退出while循环

相关问题 更多 >