Python/Pygame How to make my Score Text update its

2019-08-05 05:26发布

问题:

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.

回答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.