samedi 21 février 2015

How/where to implement a randomized function

I'm making 2048 in python, and I'm having no trouble with any part of the game except initializing that next random block after each of the users turns. Creating the function that does this is easy, and only requires randomly changing one entry in the array of entries for the blocks. The issue I'm having is that it is a random function, and it is in a (game) loop. This means that it keeps getting called, and keeps giving different output, which means it keeps changing the array when it should change it only once. The game loop itself is posted below, and should be pretty self-explanatory. The function I'm concerned about is the addTile() function. I've thought of tracking when it comes in and out of the if statements, or maybe storing the output of the random function somewhere, but that doesn't seem to work. How could I selectively call this function at the times I need it?



while not done:

screen.fill((0,0,0))
board()

for event in pygame.event.get():
if event.type == pygame.QUIT:
done = 1

pressed = pygame.key.get_pressed()

if pressed[pygame.K_UP]:
state = 1
if pressed[pygame.K_DOWN]:
state = 2
if pressed[pygame.K_LEFT]:
state = 3
if pressed[pygame.K_RIGHT]:
state = 4

if state == 0:
tiles(positions)
elif state == 1:
positions = addTile(positions)
positions = up(positions)
tiles(positions)
elif state == 2:
positions = down(positions)
tiles(positions)
elif state == 3:
positions = left(positions)
tiles(positions)
elif state == 4:
positions = right(positions)
tiles(positions)

pygame.display.flip()
clock.tick(60)




Aucun commentaire:

Enregistrer un commentaire