Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Increse number on click in pygame

I am trying to create a scoring system. So if the user clicks numbers, player 1 gets 1 point and if they click letter player 2 gets 1 point

import pygame
import pygame.freetype
import random

pygame.init()
pygame.font.init()

screen = pygame.display.set_mode((0, 0), pygame.FULLSCREEN)  # Fullscreen

Here I have created a variable to store number and changed it to display on screen.

number = 0
str_number = str(number)

def text():
game_font = pygame.freetype.SysFont("monospace", 45)
text_surface, rect = game_font.render(str_number, (0, 0, 0))
screen.blit(text_surface, (250, 700))

game_font = pygame.freetype.SysFont("monospace", 45)
text_surface, rect = game_font.render(str_number, (0, 0, 0))
screen.blit(text_surface, (1300, 700))

In my main loop, I tried to increase the number by one whenever the user press the key. but it is not working.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

running = True
while running:
screen.fill((255, 194, 102))  # RGB

    for event in pygame.event.get():  # Event handler
    ...

      if event.type == pygame.KEYDOWN:
        if event.key == pygame.K_1 or event.key == pygame.K_b and len(cards) > 0:
            cards.pop(0)
            number += 1
        if event.key == pygame.K_2 or event.key == pygame.K_n and len(cards) > 1:
            cards.pop(1)
            number += 1
        if event.key == pygame.K_3 or event.key == pygame.K_m and len(cards) > 2:
            cards.pop(2)
            number += 1

text()
pygame.display.update()

>Solution :

The rendered text does not magically change when you change the number. You have to re-render the text after increasing the number.

Do not create the pygame.font.Font object in every frame and do not render the text in every frame. Create the text Surface once at the begin of the program. Just blit the text Surface in every frame:

Simplify the code with functions. e.g.:

game_font = pygame.freetype.SysFont("monospace", 45)
text_surface, rect = game_font.render(str(number), (0, 0, 0))

def text():
    screen.blit(text_surface, (250, 700))
def increment_score():
    global number
    number += 1
    return game_font.render(str(number), (0, 0, 0))
while running:
    # [...]

    for event in pygame.event.get():
        # [...]

        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_1 or event.key == pygame.K_b and len(cards) > 0:
                cards.pop(0)
                text_surface, rect = increment_score()

            if event.key == pygame.K_2 or event.key == pygame.K_n and len(cards) > 1:
                cards.pop(1)
                text_surface, rect = increment_score()

            if event.key == pygame.K_3 or event.key == pygame.K_m and len(cards) > 2:
                cards.pop(2)
                text_surface, rect = increment_score()
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading