Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

add text to pygame window

import pygame, sys
from pygame.locals import *

pygame.init()
window = pygame.display.set_mode((600,400))
pygame.display.set_caption('Test')

fontSize = 40
font = pygame.font.SysFont('your font name', fontSize) #if you want to use the font you download use Font(), if not use SysFont()

def draw_text(text,x,y,color):
  label = font.render(text, True, color)
  
  window.blit(label, (x,y))

while True:
  window.fill((50,50,50)) #you can put any color here
  
  draw_text('your text',50,50,(255,255,255))
  
  for event in pygame.event.get():
	if event.type == QUIT:
      pygame.quit()
      sys.exit()
  
  pygame.display.update()
  
Comment

how to add textbox in pygame window

# import sys module
import pygame
import sys
  
  
# pygame.init() will initialize all
# imported module
pygame.init()
  
clock = pygame.time.Clock()
  
# it will display on screen
screen = pygame.display.set_mode([600, 500])
  
# basic font for user typed
base_font = pygame.font.Font(None, 32)
user_text = ''
  
# create rectangle
input_rect = pygame.Rect(200, 200, 140, 32)
  
# color_active stores color(lightskyblue3) which
# gets active when input box is clicked by user
color_active = pygame.Color('lightskyblue3')
  
# color_passive store color(chartreuse4) which is
# color of input box.
color_passive = pygame.Color('chartreuse4')
color = color_passive
  
active = False
  
while True:
    for event in pygame.event.get():
  
      # if user types QUIT then the screen will close
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
  
        if event.type == pygame.MOUSEBUTTONDOWN:
            if input_rect.collidepoint(event.pos):
                active = True
            else:
                active = False
  
        if event.type == pygame.KEYDOWN:
  
            # Check for backspace
            if event.key == pygame.K_BACKSPACE:
  
                # get text input from 0 to -1 i.e. end.
                user_text = user_text[:-1]
  
            # Unicode standard is used for string
            # formation
            else:
                user_text += event.unicode
      
    # it will set background color of screen
    screen.fill((255, 255, 255))
  
    if active:
        color = color_active
    else:
        color = color_passive
          
    # draw rectangle and argument passed which should
    # be on screen
    pygame.draw.rect(screen, color, input_rect)
  
    text_surface = base_font.render(user_text, True, (255, 255, 255))
      
    # render at position stated in arguments
    screen.blit(text_surface, (input_rect.x+5, input_rect.y+5))
      
    # set width of textfield so that text cannot get
    # outside of user's text input
    input_rect.w = max(100, text_surface.get_width()+10)
      
    # display.flip() will update only a portion of the
    # screen to updated, not full area
    pygame.display.flip()
      
    # clock.tick(60) means that for every second at most
    # 60 frames should be passed.
    clock.tick(60)
Comment

PREVIOUS NEXT
Code Example
Python :: python print for loop one line 
Python :: create 3x3 numpy array 
Python :: how to do date time formatting with strftime in python 
Python :: How to return images in flask response? 
Python :: python program for printing fibonacci numbers 
Python :: python list comprehension with if 
Python :: python obtain data from pandas dataframe without index name 
Python :: finding the index of an item in a pandas df 
Python :: how to save a neural network pytorch 
Python :: how to close a webpage using selenium driver python 
Python :: pyqt5 button example 
Python :: pyspark check all columns for null values 
Python :: django drop database postgres 
Python :: how to read text frome another file pythion 
Python :: sum of column in 2d array python 
Python :: django check if queryset is empty 
Python :: python restart script 
Python :: how to transpose a 2d list in python 
Python :: get column number in dataframe pandas 
Python :: converting binary to octal in python 
Python :: how to import a python function from another file 
Python :: measure cell execution time in jupyter notebook 
Python :: Simple pagination wrapper for discord.py. 
Python :: drop column with nan values 
Python :: how to import date python 
Python :: install flask on linux mint for python3 
Python :: fastest clicker python 
Python :: what does ^ do python 
Python :: playsound python 
Python :: how to get the first few lines of an ndarray 3d 
ADD CONTENT
Topic
Content
Source link
Name
9+2 =