How can i make a score increase in pygame

Viewed 142

I am trying to make a score system so that whenever i click "j", the score in the top left of the screen, the score increases. It worked but the previous score stays on the screen, so to resolve this i decided to put a small Rect object over the score, then paint the new score over the Rect, but it is not working, please help. This is the function for the increasing score:

def score_increase():
    global score
    score += 1    
    font = pygame.font.SysFont("Comic Sans", 50, True, False)   
    surface = font.render(str(score), True, (130, 46, 75))
    pygame.draw.rect(screen, ((35, 140, 120)), (0, 0, 50, 75)) 
    screen.blit(surface, (0, 0)) 
    

This is the rest of my code if it helps:

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

pygame.init()

pygame.display.set_caption("waht da dog doin")

screen = pygame.display.set_mode((800, 600))

screen.fill((35, 140, 120))

dog = pygame.image.load("resources/dog.jpg").convert()

score = 0

font = pygame.font.SysFont("Comic Sans", 50, True, False)
surface = font.render(str(score), True, (130, 46, 75))
screen.blit(surface, (0, 0))



troll = ["corned", "balled", "chimkened", "torkelled", "kanaquined", "khazaqed", "bo burnam'd", "talal'd", "chammaked", "australiad", "lotto pie'd", "tetrised", "caca'd", "scrutinized", "bulbasaured"]
troll_item = random.choice(troll)

dog_y = random.randint(0, 400)
dog_x = random.randint(0, 500)

text_y = random.randint(0, 400)
text_x = random.randint(0, 500)

def score_increase():
    global score
    score += 1    
    font = pygame.font.SysFont("Comic Sans", 50, True, False)   
    surface = font.render(str(score), True, (130, 46, 75))
    pygame.draw.rect(screen, ((35, 140, 120)), (0, 0, 50, 75)) 
    screen.blit(surface, (0, 0)) 
    
 

def print_trolled():
    screen.fill((35, 140, 120))
    screen.blit(dog, (dog_x, dog_y))   
    font = pygame.font.SysFont("Comic Sans", 30, True, True)
    surface_text = font.render("u have been" + " " + troll_item, True, (130, 46, 75))
    screen.blit(surface_text, (text_x, text_y))  

def start_print():   
    font = pygame.font.SysFont("Comic Sans", 50, True, False)
    surface = font.render("press 'j' plsz", True, (130, 46, 75))
    screen.blit(surface, (250, 250))

#def again_print():
    font = pygame.font.SysFont("Comic Sans", 50, True, True)
    surface = font.render("do it again", True, (130, 46, 75))
    screen.blit(surface, (text_x, text_y))   

start_print()

while True:
    for event in pygame.event.get():
        if event.type == KEYDOWN:
            pass  
       
            if event.key == K_j:
                print_trolled() 
                score_increase()
                screen.blit(surface, (0, 0))
                dog_y = random.randint(0, 400)
                dog_x = random.randint(0, 500)
                text_y = random.randint(0, 400)
                text_x = random.randint(0, 500)
                troll = ["corned", "balled", "chimkened", "torkelled", "kanaquined", "khazaqed", "bo burnam'd", "talal'd", "chammaked", "australiad", "lotto pie'd", "tetrised", "caca'd", "scrutinized", "bulbasaured"]
                troll_item = random.choice(troll)
                
                

            if event.type == QUIT:
                sys.exit
            
            if event.type == K_ESCAPE:
                sys.exit
    
    pygame.display.update() 
1 Answers

You have to draw the scene in the application loop. Increment score and render a core_surface in score_increase

core_surface = None
def score_increase():
    global score, score_surface
    score += 1    
    font = pygame.font.SysFont("Comic Sans", 50, True, False)   
    score_surface = font.render(str(score), True, (130, 46, 75))

But redraw the entire scene in the a application loop in every frame:

while True:
    for event in pygame.event.get():
        if event.type == KEYDOWN:
            pass  
       
            if event.key == K_j:
                score_increase()
                dog_y = random.randint(0, 400)
                dog_x = random.randint(0, 500)
                text_y = random.randint(0, 400)
                text_x = random.randint(0, 500)
                troll = ["corned", "balled", "chimkened", "torkelled", "kanaquined", "khazaqed", "bo burnam'd", "talal'd", "chammaked", "australiad", "lotto pie'd", "tetrised", "caca'd", "scrutinized", "bulbasaured"]
                troll_item = random.choice(troll)
                
            if event.type == QUIT:
                sys.exit
            
            if event.type == K_ESCAPE:
                sys.exit
    
    screen.fill(0)
    start_print()
    print_trolled() 
    score_surface = font.render(str(score), True, (130, 46, 75))
    pygame.draw.rect(screen, ((35, 140, 120)), (0, 0, 50, 75)) 
    if score_surface:
        screen.blit(score_surface, (0, 0)) 
    pygame.display.update() 

The typical PyGame application loop has to:

Related