mardi 20 mars 2018

Python: the While loop at the end keeps generating a new random card, how can I get it to generate once and stop?

The code below is skeletal, it is supposed to be the beginning of a card game. I generated a random hand (1 card for now) but I can't seem to get it to only generate the first hand once. The while loop repeats and constantly generates a new random card for the first hand. Thanks in advanced, first time poster so I'm sorry if this is all formatted terribly.

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

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('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']

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

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

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

while True:
    playerHand = []
    turnCount = 0
    mouse = pygame.mouse.get_pos()
    print(mouse)
    if turnCount == 0:
        getFirstHand()
    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()




Aucun commentaire:

Enregistrer un commentaire