【问题标题】:Python: the While loop at the end keeps generating a new random card, how can I get it to generate once and stop?Python:最后的While循环不断生成新的随机卡片,我怎样才能让它生成一次并停止?
【发布时间】:2026-02-01 07:55:02
【问题描述】:

下面的代码是骨架,它应该是纸牌游戏的开始。我生成了一张随机手牌(现在是一张牌),但我似乎无法让它只生成第一手牌一次。 while 循环重复并不断为第一手牌生成一张新的随机牌。

编辑:它不会重复生成随机卡片,但现在它崩溃了

import pygame, sys, random
from pygame.locals import*

WINWIDTH = 1200
WINHEIGHT = 800
CARDTHUMBWIDTH = 50
CARDTHUMBHEIGHT = 80
FPS = 30
FPSCLOCK = pygame.time.Clock()
DISPLAYSURF = pygame.display.set_mode((WINWIDTH,WINHEIGHT))
pygame.display.set_caption('NBA Card Game')
pygame.init()

playerHandPos = [(17, 635), (75, 635), (133, 635), (191, 635), (249, 635), (307, 635), (365, 635), (423, 635), (481, 635), (539, 635)]
lakersDeck = ['Lakers_01.png', 'Lakers_02.png', 'Lakers_03.png', 'Lakers_04.png', 'Lakers_05.png', 'Lakers_06.png', 'Lakers_07.png', 'Lakers_08.png', 'Lakers_09.png', 'Lakers_10.png']

turnCount = 0
font = pygame.font.SysFont(None, 30)
userTextPrompt = "20, 725"
playMat = pygame.image.load('playmat.png')
detailBox = pygame.image.load('detailBox.png')
DISPLAYSURF.blit(playMat, (0,0))

def terminate():
    pygame.quit()
    sys.exit()

##def drawText(text, font, surface, x, y):
##    textobj = font.render(text, 1, (0,0,0))
##    textrect = textobj.get_rect()
##    textrect.topleft = (x,y)
##    surface.blit(textobj, textrect)

def getFirstHand():
    random.shuffle(lakersDeck)
    firstHand = [lakersDeck[0], lakersDeck[1], lakersDeck[2], lakersDeck[3]]
    return firstHand

def displayHand():
    cardThumb = pygame.image.load(playerHand[0])
    cardThumb = pygame.transform.scale(cardThumb, (50,80))
    DISPLAYSURF.blit(cardThumb, playerHandPos[0])
    pygame.display.update()

def playerTurn():
    placedBench = False
    while placedBench == False:
        drawText("Choose a card for your bench.", font, DISPLAYSURF, 20, 725)
        pygame.display.update()    

def cardLakers_01():
    offense = 10
    return offense

while True:
    playerHand = []
    mouse = pygame.mouse.get_pos()
    print(mouse)
    if turnCount == 0:
        getFirstHand()
        playerHand = getFirstHand()
        turnCount += 1

    displayHand()
    playerTurn()

    for event in pygame.event.get():
        if event.type == QUIT:
            terminate()
        if event.type == KEYUP:
            if event.key == K_ESCAPE:
                terminate()
        if event.type == MOUSEMOTION:
            if 67 > mouse[0] > 17 and 710 > mouse[1] > 635:
                cardDashImage = pygame.image.load('Lakers_01.png')
                DISPLAYSURF.blit(cardDashImage, (925, 200))
                pygame.display.update()
            else:
                detailBox = pygame.image.load('detailBox.png')
                DISPLAYSURF.blit(detailBox, (900,0))
                pygame.display.update()
    FPSCLOCK
    pygame.display.update()

pygame.display.update()

【问题讨论】:

  • FPSCLOCK = pygame.time.Clock(): 那么你使用的是FPSCLOCK,它不会调用函数BTW。
  • 当然,因为每次显示时都会调用 getFirstHand 例程。随机在这里。您应该执行一次,然后存储结果。
  • 在您的 while 循环中,您有以下代码:如果 turnCount == 0: getFirstHand(),但您没有使用 getFirstHand() 函数返回的值。
  • 如果您遇到崩溃,包括回溯可以让我们更轻松地为您提供帮助。此外,最好在开始时加载一次资源,然后将它们复制到适当的表面/精灵。

标签: python loops random while-loop pygame


【解决方案1】:

您不会从 while 循环中中断,因此它将继续循环通过循环。因此,当您的代码运行时,每次更改时,它的“turnCount”都会重置回 0。您想将 turnCount 声明移到 while 循环之外,以便在程序开始时 turnCount 仅等于 0

【讨论】:

  • 谢谢,有帮助。虽然它因为其他原因而崩溃并且没有错误消息,但这只是我学习 Python 的第 3 周,所以我必须继续排除故障,直到我变得更好。
  • 是的,每个人都必须从某个地方开始,我的第一语言是 Python。如果您有错误消息,请编辑您的帖子,如果可以的话,我会提供帮助 :) 如果我的解决方案有效,请给我的答案投票
  • 我投了赞成票,但由于我的声誉太低,它说它被记录但没有公开显示:(..我对我的帖子进行了更新,我所做的只是将变量 handCount 移到顶部附近我的代码与其他变量。再次感谢!
  • 您从 GetFirstHand() 返回 playerHand 但不允许将其存储在您调用它的位置。因此,当您调用该函数时,您需要有一个地方可以返回。 variable = GetFirstHand()
  • 感谢您的帮助,我为 getFirstHand() 的返回分配了一个变量,但不知道为什么它仍然崩溃,对我的帖子进行了另一次编辑