Python:最后的While循环不断生成新的随机卡,如何让它生成一次并停止?

标签 python loops random while-loop pygame

下面的代码是骨架,它应该是纸牌游戏的开始。我生成了随机手牌(目前为一张牌),但我似乎无法让它只生成第一手牌一次。 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()

最佳答案

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

关于Python:最后的While循环不断生成新的随机卡,如何让它生成一次并停止?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49393451/

相关文章:

python - 即使已经安装,也无法在 Jupyter 中导入 statsmodels?

javascript - 从循环中生成多个图像

c# - 在循环数据时创建列表列?

java - 我写了两段代码,几乎一模一样,但其中一段运行速度比另一段快得多(Java)

random - 如何使用Intel Ivy Bridge的RNG?

Python 和子进程中的别名

python - 打印带有宽度说明符的 python 列表

java - 调用方法和填充随机数组时出现问题

php - 如何使用php生成随机人名?

python - 将数组放入 WHERE 语句中 - MySQLdb、Python