I have just added in some code I had tried out into my game as a scoreboard and I have encountered an error. Only my background will display. No text or anything will appear. Here is the code I am using:
class MenuScores(MenuClass):
def __init__(self, surface, engine):
MenuClass.__init__(self, surface)
self.MoonSurvival = engine
self.text = "Name"
def name():
size = 36
screen = pygame.display.set_mode((1280, 720))
name = ""
font = pygame.font.SysFont('data/fonts/score.ttf', size)
background = pygame.image.load('data/images/bg.jpg')
while True:
# readlines returns a list; having this in
# loop allows pygame to draw recently added
# string; no need to close and open a window
namefile = open('score.txt', 'r')
names = namefile.readlines()
for event in pygame.event.get():
if event.type == KEYDOWN:
if event.unicode.isalpha():
if len(name) < 4:
if event.unicode == 3:
name += 0
else:
name += event.unicode
elif event.key == K_BACKSPACE:
name = name[:-1]
elif event.key == K_RETURN:
f = open("data/scores/score.txt", "a")
f.write(str(name) + " " + str(self.MoonSurvival.total_score) + "\n")
f.close()
name = ""
self.text = ""
elif event.type == QUIT:
return
# create a Rectangle container, where yours
# text variable will be drawn
# Rect(left, top, width, height)
textrect = Rect(0, 0, 100, size)
screen.fill((0, 0, 0))
for i in names:
# iterate through lines from text file (it is stored
# in names variable and it is a list)
# create text variable and draw it to textrect
text = font.render(i[:-1], True, (255,0,0), (0,0,0))
screen.blit(text, textrect)
# change y coordinate of textrect; in next iteration
# next line will appear below the previous line
textrect.centery += size
block = font.render(name, True, (255, 255, 255))
rect = block.get_rect()
rect.center = screen.get_rect().center
screen.blit(background, (0, 0))
screen.blit(block, rect)
pygame.display.update()
pygame.display.flip()
def draw(self):
self.renderText()
self.drawHint()
self.surface.blit(self.background, (0, 0))
# update surface
pygame.display.update()
def _handleEvents(self, event):
def pressed(key):
keys = pygame.key.get_pressed()
if keys[key]:
return True
else:
return False
if pressed(pygame.K_SPACE):
self.MoonSurvival.game_state = STATE_MENU
self.MoonSurvival.level = 1
def renderText(self):
# split texts at \n (newline)
texts = self.text.split('\n')
for i in range(len(texts)):
textSurface = self.menufont.render(texts[i], 0, (255, 0, 0))
textRect = textSurface.get_rect()
textRect.centerx = SCREEN_WIDTH / 2
textRect.centery = SCREEN_HEIGHT / 2 + i * self.menufont.size(texts[i])[1]
self.surface.blit(textSurface, textRect)
def drawHint(self):
textSurface = self.menufont.render('(Press SPACE to return to menu)', 0, (255, 0, 0))
textRect = textSurface.get_rect()
textRect.centerx = SCREEN_WIDTH / 2
textRect.centery = SCREEN_HEIGHT - 50
self.surface.blit(textSurface, textRect)
I really don't know how to fix this. The text appeared fine when it was on its own (the def name()) So why does it not work here?
self.renderText()
self.drawHint()
self.surface.blit(self.background, (0, 0))
Here, you are drawing the text on self.surface, then drawing the hint on self.surface, and then painting the background image on self.surface, over top everything else.
Instead, you should draw the background first, and everything else after it.
self.surface.blit(self.background, (0, 0))
self.renderText()
self.drawHint()
Related
The program consists in generating 2 random numbers, rendering them on screen (with the '+' symbol, because it's a sum) and expecting the user to put the result of the sum on the box entry. The code already has a function that generates 2 random numbers (x and y), and another one that renders them on screen (besides the score). Here is the code:
import pygame
import random
from InputBox import InputBox
pygame.init()
clock = pygame.time.Clock()
surface = pygame.display.set_mode((600, 400))
pygame.display.set_caption("Projecte MatZanfe")
font = pygame.font.SysFont('comicsans', 50)
base_font = pygame.font.Font(None, 32)
user_text = ''
color_active = pygame.Color('lightskyblue3')
running = True
points = 0
def start_the_game():
x = random.randint(0, 10)
y = random.randint(0, 10)
is_correct = False
return x, y
def display_the_game(x, y):
# Variables
z = x + y
surface.fill((255, 70, 90))
text = font.render(str(x) + "+" + str(y), True, (255, 255, 255))
text_surface = base_font.render(user_text, True, (255, 255, 255))
surface.blit(text, (260, 120))
input_box.draw(surface)
punts = font.render("PuntuaciĆ³: " + str(points),True, (255,255,255))
surface.blit(punts, (350,30))
x, y = start_the_game()
input_box = InputBox(190, 250, 200, 32)
while running:
clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
else:
result = input_box.handle_event(event)
if result != None:
if int(result) == int(x) + int(y):
# TODO: when the user is right
points = points + 5
start_the_game()
pygame.display.update()
display_the_game(x, y)
pygame.display.update()
else:
# TODO: when the user is wrong
start_the_game
pygame.display.update()
display_the_game(x, y)
pygame.display.update()
display_the_game(x, y)
pygame.display.update()
pygame.quit()
I need the program to generate two new random numbers if the result is right and render them, apart from adding 5 points to the "PuntuaciĆ³" that appears up to the right (done). If the user is wrong, it just needs to generate 2 new random numbers, and render them (the score doesn't have to change).
Here is the code from the imported InputBox.
import pygame
pygame.init()
surface = pygame.display.set_mode((600, 400))
COLOR_INACTIVE = pygame.Color('lightskyblue3')
COLOR_ACTIVE = pygame.Color('dodgerblue2')
FONT = pygame.font.SysFont('comicsans', 32)
base_font = pygame.font.Font(None, 32)
color_active = pygame.Color('lightskyblue3')
user_text = ''
class InputBox:
def __init__(self, x, y, w, h, text=''):
self.rect = pygame.Rect(x, y, w, h)
self.color = COLOR_INACTIVE
self.text = text
self.txt_surface = FONT.render(text, True, self.color)
self.active = False
def handle_event(self, event):
if event.type == pygame.MOUSEBUTTONDOWN:
# If the user clicked on the input_box rect.
if self.rect.collidepoint(event.pos):
# Toggle the active variable.
self.active = not self.active
else:
self.active = False
# Change the current color of the input box.
self.color = COLOR_ACTIVE if self.active else COLOR_INACTIVE
if event.type == pygame.KEYDOWN:
if self.active:
if event.key == pygame.K_RETURN:
user_input = self.text
self.text = ''
self.txt_surface = FONT.render(self.text, True, self.color)
return user_input
elif event.key == pygame.K_BACKSPACE:
self.text = self.text[:-1]
else:
self.text += event.unicode
# Re-render the text.
self.txt_surface = FONT.render(self.text, True, self.color)
def update(self):
# Resize the box if the text is too long.
width = max(200, self.txt_surface.get_width()+10)
self.rect.w = width
def draw(self, screen):
# Blit the text.
screen.blit(self.txt_surface, (self.rect.x+5, self.rect.y+5))
# Blit the rect.
pygame.draw.rect(screen, self.color, self.rect, 2)
def main():
clock = pygame.time.Clock()
input_box2 = InputBox(190, 250, 200, 32)
input_boxes = [input_box2]
done = False
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
for box in input_boxes:
box.handle_event(event)
for box in input_boxes:
box.update()
surface.fill((255, 70, 90))
for box in input_boxes:
box.draw(surface)
pygame.display.flip()
clock.tick(30)
if __name__ == '__main__':
main()
pygame.quit()
All you have to do is to create new values for x and y and to reset the input box:
while running:
clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
else:
result = input_box.handle_event(event)
if result != None:
if int(result) == int(x) + int(y):
points = points + 5
# create new random numbers
x, y = start_the_game()
# reset input box (just create a new box)
input_box = InputBox(190, 250, 200, 32)
display_the_game(x, y)
pygame.display.update()
pygame.quit()
I don't know how to detect when my mouse is on 2 rect and choose (if two rect has an action) the action. For example, in Windows, when two windows are one over the other, it's the first window that will be selected. I want to do exactly the same thing.
import pygame
class Rectangle(pygame.sprite.Sprite):
def __init__(self, screen, rect, x, y, color, name):
super().__init__()
self.screen = screen
self.name = name
self.screen_str = str(screen)
self.rect = rect
self.color = color
self.x, self.y = x, y
self.r = pygame.Surface((self.rect[2], self.rect[3]), pygame.SRCALPHA)
self.rect = self.r.get_rect()
self.rect.x, self.rect.y = x, y
self.r.fill(self.color)
pygame.init()
screen = pygame.display.set_mode((1280, 720))
pygame.display.set_caption("PyStoneTest")
width, height = screen.get_size()
background_default = "image\Settings\Wallpaper\default_1.jpg"
D = {}
D["Rect2"] = Rectangle(screen, (0, 200, width, 70), 0,
50, (255, 255, 0), "Rect2")
D["Rect1"] = Rectangle(screen, (0, 100, width-200, 200), 0,
100, (255, 0, 255), "Rect1")
Programme = ["Rect1", "Rect2"]
while True:
background = pygame.image.load(background_default).convert()
background = pygame.transform.scale(background, (width, height))
for event in pygame.event.get():
x,y = pygame.mouse.get_pos()
if event.type == pygame.QUIT:
pygame.quit()
for element in Programme:
if D[element].rect.collidepoint(x,y) and event.type == pygame.MOUSEBUTTONDOWN:
del Programme[Programme.index(D[element].name)]
Programme.append(D[element].name)
print(Programme)
screen.blit(background, (0, 0))
for element in Programme:
screen.blit(D[element].r, D[element].rect)
pygame.display.update()
You should first run for-loop to check all windows and use last one which collides with mouse.
elif event.type == pygame.MOUSEBUTTONDOWN:
last = None
for element in Programme:
if D[element].rect.collidepoint(event.pos):
last = element
if last:
Programme.remove(last)
Programme.append(last)
print(Programme)
Or you would have to check in reverse order - from last to first - and break loop on first matching rectangle.
elif event.type == pygame.MOUSEBUTTONDOWN:
last = None
for element in reversed(Programme):
if D[element].rect.collidepoint(event.pos):
last = element
break
if last:
Programme.remove(last)
Programme.append(last)
print(Programme)
Minimal working code with other changes
import pygame
# --- classes ---
class Rectangle(pygame.sprite.Sprite):
# I skip `x,y` because I have it in `rect`
def __init__(self, screen, rect, color, name):
super().__init__()
self.screen = screen
self.color = color
self.name = name
self.rect = pygame.Rect(rect)
self.image = pygame.Surface(self.rect.size, pygame.SRCALPHA)
self.image.fill(self.color)
def draw(self):
self.screen.blit(self.image, self.rect)
# --- functions ---
# empty
# --- main ---
pygame.init()
screen = pygame.display.set_mode((1280, 720))
screen_rect = screen.get_rect() # it can be useful to center elements on screen - `d[name].rect.center = screen_rect.center`
pygame.display.set_caption("PyStoneTest")
# raw string
background_default = r"image\Settings\Wallpaper\default_1.jpg"
# load and rescale before `while`-loop
background = pygame.image.load(background_default).convert()
background = pygame.transform.scale(background, screen_rect.size)
d = {} # PEP8: `lower_case_names` for variable
d["Rect2"] = Rectangle(screen, (0, 0, screen_rect.width-100, 70), (255, 255, 0), "Rect2")
d["Rect2"].rect.center = screen_rect.center
d["Rect1"] = Rectangle(screen, (0, 0, 70, screen_rect.height-100), (255, 0, 255), "Rect1")
d["Rect1"].rect.center = screen_rect.center
programme = ["Rect1", "Rect2"] # PEP8: `lower_case_names` for variable
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit() # to skip rest of while-loop
elif event.type == pygame.MOUSEBUTTONDOWN:
selected = None
for name in programme:
if d[name].rect.collidepoint(event.pos):
selected = name
if selected and selected != programme[-1]:
programme.remove(selected)
programme.append(selected)
print('after replace:', programme)
screen.blit(background, (0, 0))
for name in programme:
d[name].draw()
pygame.display.update()
I cannot help with your code since I cannot understand what you are doing but I can offer my own solution. Since pygame renders thigs that are drawn later on the top, you can change the rendering order of your rectangles by checking which rectangle is being clicked and swapping it with the last rectangle in your list.
Here is an example. The colors in my example change weirdly but that's because I am generating them on the fly just to be able to tell the different between the different rects. You shouldn't have this problem.
import pygame
pygame.init()
screen = pygame.display.set_mode((1280, 720))
pygame.display.set_caption("PyStoneTest")
rects = []
for i in range(10):
rects.append(pygame.Rect(i * 25, 100, 30, 30))
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
screen.fill((0, 0, 0))
c = 1
for rect in rects:
pygame.draw.rect(screen, (c, 100, 100), rect)
c += 20
clicked = pygame.mouse.get_pressed()
x,y = pygame.mouse.get_pos()
for rect in rects:
if (rect.collidepoint(x, y) and clicked[0]):
#swap it
rects[-1][:], rect[:] = rect[:], rects[-1][:]
pygame.display.update()
This question already has answers here:
How can I create a text input box with Pygame?
(5 answers)
How to allow the user to type only under certain conditions in pygame? [duplicate]
(3 answers)
Closed 1 year ago.
I want the user to be able to type a name into each of the 2 textboxes when they click on them. Instead what happens is the next letter will always replace the previous letter instead of writing out a new letter onto the screen. For example, if you try to type "mat" then the a will replace the m and the t will replace the a. How do I fix this?
from pygame import *
screen_width = 1400
screen_height = 800
init()
screen = display.set_mode((screen_width, screen_height))
name_font = font.Font(None, 32)
name_1 = ""
name_2 = ""
class Rectangle:
def __init__(self, x, y):
self.name = ""
self.active = False
self.x = x
self.y = y
self.text_surface = name_font.render(self.name, True, (255, 255, 255))
self.rect_width = max(140, 10 + self.text_surface.get_width())
self.input_rect = Rect(x, y, self.rect_width, 32)
self.input_rect.w = self.text_surface.get_width() + 10
self.click_value = 10
def naming(self, player_name):
for e in events:
if e.type == MOUSEBUTTONDOWN:
if self.input_rect.x + self.click_value >= mx >= self.input_rect.x - 10 and self.input_rect.y + 26 >= my >= self.input_rect.y - 10:
self.active = True
else:
self.active = False
if e.type == KEYDOWN:
if self.active:
if e.key == K_BACKSPACE:
player_name = player_name[:-1]
screen.fill((0, 0, 0))
else:
player_name += e.unicode
self.click_value += 7
self.text_surface = name_font.render(player_name, True, (255, 255, 255))
def draw(self, screen):
draw.rect(screen, (0, 0, 0), self.input_rect, 0)
draw.rect(screen, (255, 255, 255), self.input_rect, 2)
self.input_rect.w = self.text_surface.get_width() + 10
screen.blit(self.text_surface, (self.input_rect.x + 5, self.input_rect.y + 5))
rect_1 = Rectangle(200, 200)
rect_2 = Rectangle(200, 400)
while True:
mx, my = mouse.get_pos()
events = event.get()
for e in events:
if e.type == QUIT:
quit()
screen.fill((0, 0, 0))
rect_1.naming(name_1)
rect_1.draw(screen)
rect_2.naming(name_2)
rect_2.draw(screen)
display.update()
time.delay(1)
I think it has something to do with the placement of the name_1 and name_2 variables but I'm not entirely sure.
Pyhton has not concept of in-out parameters. Use the self.name attribute, instead of an argument:
from pygame import *
screen_width = 1400
screen_height = 800
init()
screen = display.set_mode((screen_width, screen_height))
name_font = font.Font(None, 32)
name_1 = ""
name_2 = ""
class Rectangle:
def __init__(self, x, y):
self.name = ""
self.active = False
self.x = x
self.y = y
self.text_surface = name_font.render(self.name, True, (255, 255, 255))
self.rect_width = max(140, 10 + self.text_surface.get_width())
self.input_rect = Rect(x, y, self.rect_width, 32)
self.input_rect.w = self.text_surface.get_width() + 10
self.click_value = 10
def naming(self):
for e in events:
if e.type == MOUSEBUTTONDOWN:
if self.input_rect.x + self.click_value >= mx >= self.input_rect.x - 10 and self.input_rect.y + 26 >= my >= self.input_rect.y - 10:
self.active = True
else:
self.active = False
if e.type == KEYDOWN:
if self.active:
if e.key == K_BACKSPACE:
self.name = self.name[:-1]
screen.fill((0, 0, 0))
else:
self.name += e.unicode
self.click_value += 7
self.text_surface = name_font.render(self.name, True, (255, 255, 255))
return self.name
def draw(self, screen):
draw.rect(screen, (0, 0, 0), self.input_rect, 0)
draw.rect(screen, (255, 255, 255), self.input_rect, 2)
self.input_rect.w = self.text_surface.get_width() + 10
screen.blit(self.text_surface, (self.input_rect.x + 5, self.input_rect.y + 5))
rect_1 = Rectangle(200, 200)
rect_2 = Rectangle(200, 400)
while True:
mx, my = mouse.get_pos()
events = event.get()
for e in events:
if e.type == QUIT:
quit()
screen.fill((0, 0, 0))
name_1 = rect_1.naming()
rect_1.draw(screen)
name_2 = rect_2.naming()
rect_2.draw(screen)
display.update()
time.delay(1)
You can use this code:-
import pygame
import sys
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)
I want to make some sort of typing game in python using pygame. So, if the key pressed character is the same as the character in the word, it should return true... Is there any way to do this in python?
For example:
the word is "cat", if the user presses the key 'c', then it returns true... and so on for the rest of the characters.
here's my main.py file
from time import sleep
import pygame
import random
import winsound
from words import Words
BLACK = ( 0, 0, 0)
WHITE = (255, 255, 255)
BLUE = ( 0, 0, 255)
GREEN = ( 0, 255, 0)
RED = (255, 0, 0)
pygame.init()
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
done = False
clock = pygame.time.Clock()
screen = pygame.display.set_mode((SCREEN_WIDTH,SCREEN_HEIGHT))
w1 = Words(screen) #making a single word (for now) to see if typing works
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
screen.fill(WHITE)
w1.draw()
#attempting to write code here to compare word and user input
pygame.display.flip()
clock.tick(60)
pygame.init()
exit()
here's my words.py file
from random_words import RandomWords
import pygame
import random
from queue import *
rw = RandomWords()
class Words():
def __init__(self, screen):
self.screen = screen
self.x_point = 400
self.y_point = 400
self.word = rw.random_word() #generates a random word
self.queue = Queue() #was hoping to use the queue so that if the user types the char correctly in the right order, then the letter would change color or something (but that's further down the line)
for c in self.word: #iterate through randomized word..
self.queue.put(c) #add each char in randomized word to queue, for typing reasons
def getY(self):
return self.y_point
def draw(self):
#creates a new object
myfont = pygame.font.SysFont('Comic Sans MS' ,30)
#creates a new surface with text drawn on it
textsurface = myfont.render(self.word, False, (0,0,0))
self.screen.blit(textsurface,(self.x_point,self.y_point))
Event KEYDOWN has event.unicode, event.key, event.mod
You can compare
if event.type == pygame.KEYDOWN:
if event.unicode == "a":
or even
if event.type == pygame.KEYDOWN:
if event.unicode.lower() == "a":
to check "a" and "A"
To check char in word
if event.type == pygame.KEYDOWN:
if event.unicode.lower() in your_word.lower():
Example code use event.unicode to render text with pressed keys.
BTW: It is not some Entry widget so it doesn't delete char when you press backspace.
import pygame
# --- constants ---
BLACK = ( 0, 0, 0)
WHITE = (255, 255, 255)
BLUE = ( 0, 0, 255)
GREEN = ( 0, 255, 0)
RED = (255, 0, 0)
SCREEN_WIDTH = 300
SCREEN_HEIGHT = 200
FPS = 5 # `FPS = 25` is enough for human eye to see animation.
# If your program don't use animation
# then `FPS = 5` or even `FPS = 1` can be enough
# --- main ---
# - init -
pygame.init()
screen = pygame.display.set_mode((SCREEN_WIDTH,SCREEN_HEIGHT))
screen_rect = screen.get_rect()
# - objects -
font = pygame.font.SysFont(None, 30)
text = ""
text_image = font.render(text, True, GREEN)
text_rect = text_image.get_rect() # get current size
text_rect.center = screen_rect.center # center on screen
# - mainloop -
clock = pygame.time.Clock()
done = False
while not done:
# - events -
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
elif event.type == pygame.KEYDOWN:
text += event.unicode
text_image = font.render(text, True, GREEN)
text_rect = text_image.get_rect() # get current size
text_rect.center = screen_rect.center # center on screen
# - draws -
screen.fill(BLACK)
screen.blit(text_image, text_rect)
pygame.display.flip()
clock.tick(FPS)
# - end -
pygame.quit() # <-- quit(), not init()
I am learning about text input in pygame for a game I am creating and currently adding scores into. I have learnt how to write text to a file then read and display the text on the screen. Now I am trying to learn how I can limit names to 3 or 4 characters so scores are neatly aligned. Currently the player can input a name as long as they want. So for example they could do the ABC's but I just want 3 or 4 characters. Here is my code I am working on:
import pygame, random
from pygame.locals import *
FONT_SIZE = 60
def name():
screen = pygame.display.set_mode((1280, 720))
name = ""
lol = random.randint(0, 100)
font = pygame.font.SysFont(None, FONT_SIZE)
while True:
# readlines returns a list; having this in
# loop allows pygame to draw recently added
# string; no need to close and open a window
namefile = open('test.txt', 'r')
names = namefile.readlines()
for event in pygame.event.get():
if event.type == KEYDOWN:
if event.unicode.isalpha():
if event.unicode == 3:
name += 0
else:
name += event.unicode
elif event.key == K_BACKSPACE:
name = name[:-1]
elif event.key == K_RETURN:
f = open("test.txt", "a")
f.write(str(name) + " " + str(lol) + "\n")
f.close()
name = ""
elif event.type == QUIT:
return
# create a Rectangle container, where yours
# text variable will be drawn
# Rect(left, top, width, height)
textrect = Rect(0, 0, 100, FONT_SIZE)
screen.fill((0, 0, 0))
for i in names:
# iterate through lines from text file (it is stored
# in names variable and it is a list)
# create text variable and draw it to textrect
text = font.render(i[:-1], True, (255,0,0), (0,0,0))
screen.blit(text, textrect)
# change y coordinate of textrect; in next iteration
# next line will appear below the previous line
textrect.centery += FONT_SIZE
block = font.render(name, True, (255, 255, 255))
rect = block.get_rect()
rect.center = screen.get_rect().center
screen.blit(block, rect)
pygame.display.update()
pygame.display.flip()
if __name__ == "__main__":
pygame.init()
name()
pygame.quit()
Here is an example of what I am after:
You could do something like this
if event.unicode.isalpha():
if len(name) < 3:
if event.unicode == 3:
name += 0
else:
name += event.unicode
this way, you only add a character if the name is not to long