Python Forum
[pyGame] How do i change text with keyboard ?...
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[pyGame] How do i change text with keyboard ?...
#1
Hey Programmers, Game Developers and Software Creators...

I try to make tekst changeble wenether you press an key... but it don't works...

This is my script:
import pygame
import sys
 
pygame.init()
 
WHITE = (255, 255, 255)
RED = (0, 255, 0)
BLACK = (0, 0, 0)
 
size = (640, 480)
screen = pygame.display.set_mode(size)
 
pygame.display.set_caption("JCads Develop")
 
 
done = False
clock = pygame.time.Clock()
   
while not done:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_r:
                pygame.string = "JamieVanCadsand"
            elif event.key == pygame.K_g:
                pygame.string = "VinnieOrgers"
            elif event.key == pygame.K_b:
                pygame.string = "LiesbethVDJagt"
            elif event.key == pygame.K_ESCAPE:
                done = True

    
    screen.fill(WHITE)
    pygame.draw.line(screen, RED, [0, 0], [100, 100], 5)


    font = pygame.font.SysFont('Calibri', 25, True, False)
    text = font.render(string, True, BLACK)
    screen.blit(text, [250, 250])
    pygame.display.flip()
 
    clock.tick(60)
pygame.quit()
sys.exit()
Can anyone help me to give my an example code, about how i can make tekst changible wenether my player
or program user press an key ?... Thanks for help,

Jamie...
Reply
#2
I would start using classes as well.

import pygame
import sys
  
pygame.init()
  
WHITE = (255, 255, 255)
RED = (0, 255, 0)
BLACK = (0, 0, 0)
  
size = (640, 480)
screen = pygame.display.set_mode(size)
screen_rect = screen.get_rect()
  
pygame.display.set_caption("JCads Develop")
  
done = False
clock = pygame.time.Clock()

def make_text(message, size, color):
    font = pygame.font.SysFont('Arial', size)
    text = font.render(message,True,color)
    rect = text.get_rect(center = (screen_rect.centerx, screen_rect.centery) )
    return text,rect

pygame_text = []
possible_outcome = ['JamieVanCadsand', 'VinnieOrgers', 'LiesbethVDJagt']
for outcome in possible_outcome:
    pygame_text.append(make_text(outcome, 15, (255,0,0)))
text,rect = pygame_text[0]#set a default
    
while not done:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_r:
                text,rect = pygame_text[0]
            elif event.key == pygame.K_g:
                text,rect = pygame_text[1]
            elif event.key == pygame.K_b:
                text,rect = pygame_text[2]
            elif event.key == pygame.K_ESCAPE:
                done = True
 
     
    screen.fill(WHITE)
    #pygame.draw.line(screen, RED, [0, 0], [100, 100], 5)
 
 
    #font = pygame.font.SysFont('Calibri', 25, True, False)
    #text = font.render(string, True, BLACK)
    screen.blit(text, [250, 250])
    pygame.display.flip()
  
    clock.tick(60)
pygame.quit()
sys.exit()
Recommended Tutorials:
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Handling multiple keyboard entries in Pygame mrcrease 2 4,338 Dec-13-2019, 01:12 AM
Last Post: Windspar
  Basically a Python Tetris game [pygame] rather keyboard arrows can be controlled with lsepolis123 9 5,095 Sep-10-2019, 08:12 PM
Last Post: metulburr
  [pyGame] More Text in one Window, example needed ! JamieVanCadsand 1 3,536 Oct-03-2017, 03:42 PM
Last Post: metulburr
  [pyGame] Render Text from Variable, examples needed ! JamieVanCadsand 1 5,927 Oct-03-2017, 09:54 AM
Last Post: metulburr
  [pyGame] Text with Input, help needed ! JamieVanCadsand 1 6,326 Oct-02-2017, 06:53 PM
Last Post: metulburr

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020