How do you make text appear when you click a key in pygame

Question:
how do you make text appear when you click a key
Repl link:
https://replit.com/@sanstheskeleton22/RowdyLargeVirtualmemory#main.py

EDIT: lol this is a pygame repl, nvm

thank you for your time and ur help

well u spent time to write that so thanks anyway

1 Like

wow so wholesome :blush: thanks

@bigminiboss can u answer my question?

i see on your homepage that your last follower was SaniyaAhmad

is anyone here skilled in pygame

this is hard man like coding itself is hard right?

I’ve used pygame before, and there are a few ways you can detect keypresses.

Method 1

The first way you can detect keypresses is to activate when the key is pressed down. What the code will do is detect a keypress event and it will see if it is the correct key, and if so, then it activates the code.

running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_SPACE:
                # do something when the spacebar is pressed
            elif event.key == pygame.K_a:
                # do something when a is pressed

Method 2

The second method for detecting keypresses is almost the same, but you can use it in more situations. What it does is keep track of which keys are actually being pressed, and not just when they got presses. This is useful because it allows to to track holding down a key.

keyboard = {}

def get_key(key):
    try:
        return keyboard[key]
    except KeyError:
        return False

running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        elif event.type == pygame.KEYDOWN:
            keyboard[event.key] = True
        elif event.type == pygame.KEYUP:
            keyboard[event.key] = False
    
    if get_key(pygame.K_a):
        # do some code here

What this allows you to do is run a piece of code repeatedly while the key is held down. The get_key() function uses a try, except statement, because if you havent pressed the key yet, it will give an error when you try to get it from the dictionary.

Differences

The main difference between methods 1 and 2, is that 1 runs the code when you press the key, and method 2 runs the code while the key is pressed. Both are useful in different situations, so pick whichever you need.

1 Like

why did it get deleted?

Question, are you asking how to detect keypress, or how to add text to the screen?

add text to the screen when you press a key

do you know how to do it?

Well to start off, I would have some sort of boolean, maybe called keypressed, or something related to your program, and only draw the text if that is true. There are 2 things you need to do now. First, you need to detect the keypress and make this boolean true, and then you need to draw the text.

import pygame

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

screen = pygame.display.set_mode((800, 600))
font = pygame.font.Font(None)

text = font.render("Draw this text.", True, "black", fontsize=50)
keypressed = False

running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running  = False
        if event.type == pygame.KEYUP:
            if event.key == pygame.K_SPACE:
                keypressed = False
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_SPACE:
                keypressed = True
        
    screen.fill("white")
    if keypressed:
        screen.blit(text, (0, 0))

    pygame.display.flip()
1 Like

theres a problem i already have this thing called window to show the width and height

this is hurting my brain

Ok, well where do you need to implement this in your code?

ill tell u the line its on

line 41 to 42 there is my text

it is drawing your text but only for 1 frame