user3102886
user3102886

Reputation: 11

Pygame: Variable not updating each iteration

I'm brand new to Python, so this is probably a simple problem. I want the code to display "rotation: " followed by the value of the variable player_rotation. It does this, but the value displayed does not by 1 every iteration (as I would expect it to).

import pygame
from pygame.locals import *

pygame.init()
screen = pygame.display.set_mode((480, 480))
myfont = pygame.font.SysFont("monospace", 15)
player_rotation = 0
rotation_label = myfont.render("rotation: " + str(player_rotation), 1, (255,255,0))

while 1:

    screen.blit(rotation_label, (100,100))
    player_rotation += 1
    pygame.display.flip()

for event in pygame.event.get():
    if event.type==pygame.QUIT:
        pygame.quit()
        exit(0)

Upvotes: 0

Views: 137

Answers (3)

Yossi
Yossi

Reputation: 12110

You set the label only once and this occurs before the loop. Try moving rotation_label into your loop.

while 1:
    rotation_label = myfont.render("rotation: " + str(player_rotation), 1, (255,255,0))
    screen.blit(rotation_label, (100,100))
    player_rotation += 1
    pygame.display.flip()

Also, your code in for loop is never executed because it appears after your while 1

Upvotes: 1

Hyperboreus
Hyperboreus

Reputation: 32459

You just update the variale player_rotation, but you never render you label it anew.

Upvotes: 0

Osvaldo Costa
Osvaldo Costa

Reputation: 128

You forgot to put tab spaces in your while(1) loop. That is:

while 1:
    screen.blit(rotation_label, (100,100))
    player_rotation += 1
    pygame.display.flip()

Upvotes: 0

Related Questions