1

I am currently creating a game in which (for now) the score gains 1 point every second.

However, with my current code (which I believe DOES change the variable every second) whenever I run the program, the text doesn't change. It just stays at 0.

Here is my code: (I've provided text in this question to explain my code, like comments.)

SECTION 1: Importing PyGame and other standard procedure code.

import sys, pygame
from pygame.locals import *
pygame.init()
size = width, height = 800,500
screen = pygame.display.set_mode(size)

SECTION 2: Setting the window caption and a color variable.

pygame.display.set_caption("One Score (Created by - Not Really Working Lamp Productions:)")
WHITE = (255,255,255)

SECTION 3: Declaring the variable "score". I've given it a separate code sample, because it's heavily involved with the problem.

score = 0

SECTION 4: Filling the screen and declaring the default font variable.

screen.fill (WHITE)
myfont = pygame.font.SysFont("monospace", 16)

SECTION 5: Disclaimer Text (Or is it claimer text, I'm not quite sure.)

disclaimertext = myfont.render("Copyright, 2013, Not Really Working Lamp Productions.", 1, (0,0,0))
screen.blit(disclaimertext, (5, 480))

SECTION 6: Adding the score text (POSSIBLY THE MOST CRUCIAL PART.)

scoretext = myfont.render("Score = "+str(score), 1, (0,0,0))
screen.blit(scoretext, (5, 10))

SECTION 7: The while loop (POSSIBLY THE MOST CRUCIAL PART.)

while 1:         
    for event in pygame.event.get():
        pygame.display.flip()
        if event.type == pygame.QUIT:sys.exit()
        pygame.time.wait(100)
        score = score + 1

So where in my code do I put what? (I need the screen to constantly update the score as it changes every second from the "while 1:" loop.)

Thank you.

Christian Ternus
  • 8,406
  • 24
  • 39
  • 1
    Drawing operations need to occur inside the loop. As it is, blitting happens exactly once, at the beginning of the program, and never again. Furthermore, all of your code in the `for event` loop should almost certainly be outside of that loop (but still inside the while), except for the `if event.type` line. – Kevin Nov 01 '13 at 18:43
  • Claimer, not disclaimer – rlms Nov 01 '13 at 18:46
  • I wrote a text rendering class that renders text if the variable changes, else uses a cached surface. http://stackoverflow.com/a/15516132/341744 – ninMonkey Nov 01 '13 at 20:24
  • 1
    I recommend not breaking the program up into parts. It makes it difficult to piece together what's happen. If the code is minimal enough, just put it all in one block and use comments. – kevintodisco Nov 01 '13 at 20:52

1 Answers1

1

I'm not sure how pygame structures its logic, but typically the while true: game loop handles a few tasks:

  • processing user input
  • updating the state of the game
  • rendering the state.

So in your while 1 loop you should do so, and in that order (the order is extremely important).

You want to ensure that you handle any input from your users, update the state of the game, and then present that to the user!

update

A basic google search tells me you should call

scoretext = myfont.render("Score = "+str(score), 1, (0,0,0))
screen.blit(scoretext, (5, 10))

every iteration of the loop

Revision

import sys
import pygame
from pygame.locals import *

pygame.init()
size = width, height = 800,500
screen = pygame.display.set_mode(size)
pygame.display.set_caption("testing")
myfont = pygame.font.SysFont("monospace", 16)
WHITE = (255,255,255)

score = 0

while True:
    pygame.display.flip()
    for event in pygame.event.get():
        # I remove the timer just for my testing
        if event.type == pygame.QUIT: sys.exit()

    screen.fill(WHITE)

    disclaimertext = myfont.render("Some disclaimer...", 1, (0,0,0))
    screen.blit(disclaimertext, (5, 480))

    scoretext = myfont.render("Score {0}".format(score), 1, (0,0,0))
    screen.blit(scoretext, (5, 10))
    score += 1

Notice that I fill the screen and redraw it each loop: https://stackoverflow.com/a/1637386/1072724

You can't undo one graphic written over the top of another graphic any more than you can undo one chalk illustration drawn over the top of another chalk illustration on the same board.

What is typically done in graphics is what you'd do with the chalkboard - clear the whole lot, and next time only redraw what you want to keep.

Community
  • 1
  • 1
Mike McMahon
  • 7,096
  • 3
  • 30
  • 42
  • But I don't know what code to add in. Or are you telling me I just move where the variable-increasing script goes? – Not Really Working Lamp Produc Nov 01 '13 at 18:46
  • @NotReallyWorkingLampProduc each iteration should render the state of your game, this means you will need to redraw something that has already been drawn (as states change rapidly in games). Your render loop doesn't care about how the score is updated (that should be handled in your logic to update the state of the game) but it does care about the drawable itself. One thing to note, putting a wait(100) inside your while loop causes the whole game to stall for a second every loop. That should be put on a timed loop where you count time between loops and increment when the time hits > 1 second. – Mike McMahon Nov 01 '13 at 18:55
  • I tried changing my code, but now the program won't run, and it's all fell apart. – Not Really Working Lamp Produc Nov 01 '13 at 19:11
  • @NotReallyWorkingLampProduc if you don't have it figured out by now, this is a working example... – Mike McMahon Nov 02 '13 at 03:56