This question already has answers here:
How do I call a function from another .py file? [duplicate]
(19 answers)
Closed 1 year ago.
Recently I have been working on a videogame with Pygame, and I have already created the menu on one file, the game functions (sums) on another one, and the entry box (where the user writes the answer) on another one. I would like to start the game function (the sums) when I press the "Start" button of the menu. The names of each file are those:
Menu -
Suma -
InputBox
Here is the code of the menu.
import pygame
import pygame_menu
pygame.init()
#Size and name of the window
surface = pygame.display.set_mode((600, 400))
pygame.display.set_caption("Projecte MatZanfe")
font = pygame_menu.font.FONT_8BIT
font1 = pygame_menu.font.FONT_NEVIS
menu = pygame_menu.Menu('Projecte MatZanfe', 600, 400,
theme=pygame_menu.themes.THEME_SOLARIZED)
user_input = menu.add.text_input('User: ', font_name = font1, font_color = 'blue')
age_input = menu.add.text_input('Age: ', font_name = font1,font_color = 'Black')
menu.add.button('Start', font_name = font, font_color = 'green')
menu.add.button('Exit', pygame_menu.events.EXIT, font_name = font,font_color = 'red')
menu.mainloop(surface)
Here is the code that contains the game itself (the sums).
import pygame
import random
from InputBox import InputBox
from pygame import mixer
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))
titolsuma = font.render("SUMA (1)", True, (0,0,0))
surface.blit(titolsuma,(10,20))
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):
points = points + 5
mixer.music.load('StarPost.wav')
mixer.music.play(1)
# 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()
And finally here is the code from the imported InputBox (don't know if I actually need to use it).
import pygame
pygame.init()
surface = pygame.display.set_mode((600, 400))
COLOR_INACTIVE = pygame.Color('lightskyblue3')
COLOR_ACTIVE = pygame.Color('black')
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()
You would use
import [filename]
and then you can call functions from the other file as easy as that
Related
This question already has answers here:
How can I create a text input box with Pygame?
(5 answers)
Closed 11 months ago.
I'm trying to create an input box for one of my windows in pygame. I have created the class for it but when I tried to display the input box it gave me an error.
Here is the code for the input box:
class inp_box:
def __init__(self, xpos, ypos, width, height, text = ""):
self.rect = pygame.Rect(xpos, ypos, width, height)
self.colour = [200, 200, 200]
self.text = text
self.text_surf = FONT.render(text , True, self.colour)
self.current = False
self.colourAc = False
def enter_text(self, event):
if event.type == pygame.MOUSEBUTTONDOWN:
if self.rect.collidepoint(event.pos):
self.colourAc = not self.colourAc
else:
self.colourAc = False
if self.colourAc:
self.color = [150, 150, 150]
else:
self.color = [200, 200, 200]
if event.type == pygame.K_KEYDOWN:
if self.colourAc:
if event.key == pygame.K_RETURN:
print(self.text)
self.text = ""
elif event.key == pygame.K_BACKSPACE:
self.text = self.text[:-1]
else:
self.text += event.unicode
self.text_surf = FONT.render(self.text, True, self.colour)
def Updtext(self):
widt = max(200, self.text_surf.get_width() + 10)
self.rect.width = widt
def showBox(self, display):
display.blit(self.text_surf, (self.rect.xpos + 5, self.rect.ypos + 5))
pygame.draw.rect(display, self.colour, self.rect)
This is part of the main loop where the input box would be displayed:
elif set_wind.checkUp():
main_wind_but = return_but.hover(mouse_pos, mouse_click)
return_but.showBut(set_wind.reTitle())
enter_wind_but = enter_but.hover(mouse_pos, mouse_click)
enter_but.showBut(set_wind.reTitle())
title1_box.showBut(set_wind.reTitle())
desc1_box.showBut(set_wind.reTitle())
input_box.showBox(set_wind)
if main_wind_but:
Main = main_wind.setCurrent()
set_wind.endCurrent()
This is the error I got:
Traceback (most recent call last):
File "main.py", line 282, in <module>
input_box.showBox(set_wind)
File "main.py", line 128, in showBox
display.blit(self.text_surf, (self.rect.xpos + 5, self.rect.ypos + 5))
AttributeError: 'window' object has no attribute 'blit'
I've tried to find solutions to this problem but none of them seem to answer my problem.
I appreciate any help and thanks in advance.
I found this online
https://www.codegrepper.com/code-examples/python/how+to+make+a+text+input+box+python+pygame
import pygame as pg
pg.init()
screen = pg.display.set_mode((640, 480))
COLOR_INACTIVE = pg.Color('lightskyblue3')
COLOR_ACTIVE = pg.Color('dodgerblue2')
FONT = pg.font.Font(None, 32)
class InputBox:
def __init__(self, x, y, w, h, text=''):
self.rect = pg.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 == pg.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 == pg.KEYDOWN:
if self.active:
if event.key == pg.K_RETURN:
print(self.text)
self.text = ''
elif event.key == pg.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.
pg.draw.rect(screen, self.color, self.rect, 2)
def main():
clock = pg.time.Clock()
input_box1 = InputBox(100, 100, 140, 32)
input_box2 = InputBox(100, 300, 140, 32)
input_boxes = [input_box1, input_box2]
done = False
while not done:
for event in pg.event.get():
if event.type == pg.QUIT:
done = True
for box in input_boxes:
box.handle_event(event)
for box in input_boxes:
box.update()
screen.fill((30, 30, 30))
for box in input_boxes:
box.draw(screen)
pg.display.flip()
clock.tick(30)
if __name__ == '__main__':
main()
pg.quit()
I hope it helps!
I want to be able to make a text box pop up when I hover over the button, now the button changes colour when I hover over it but no text box is displayed how can I make that happen or do I need to something entirely different like make a new screen pop up when I hover over either way the hovering over isnt working and I dont know how to fix it and id like some help please
import pygame
pygame.init()
pygame.font.init()
colours = {"White" : (255, 255, 255), "Black" : (0, 0, 0), "Red" : (255, 0, 0), "Blue" : (0, 0, 255), "Green" : (0, 255, 0)}
orighpamt = 10
currentGoblin = 0
class Screen():
def __init__(self, title, width=400, height=600, fill=colours["White"]):
self.title=title
self.width=width
self.height = height
self.fill = fill
self.current = False
def makeCurrent(self):
pygame.display.set_caption(self.title)
self.current = True
self.screen = pygame.display.set_mode((self.width, self.height))
def endCurrent(self):
self.current = False
def checkUpdate(self):
return self.current
def screenUpdate(self):
if(self.current):
self.screen.fill(self.fill)
def returnTitle(self):
return self.screen
class Button():
def __init__(self, x, y, sx, sy, bcolour, fbcolour, font, fontsize, fcolour, text):
self.x = x
self.y = y
self.sx = sx
self.sy = sy
self.bcolour = bcolour
self.fbcolour = fbcolour
self.fcolour = fcolour
self.fontsize = fontsize
self.text = text
self.current = False
self.buttonf = pygame.font.SysFont(font, fontsize)
def showButton(self, display):
if(self.current):
pygame.draw.rect(display, self.fbcolour, (self.x, self.y, self.sx, self.sy))
else:
pygame.draw.rect(display, self.bcolour, (self.x, self.y, self.sx, self.sy))
textsurface = self.buttonf.render(self.text, False, self.fcolour)
display.blit(textsurface, ((self.x + (self.sx/2) - (self.fontsize/2)*(len(self.text)/2) - 5,(self.y + (self.sy/2) -(self.fontsize/2) - 4))))
def focusCheck(self, mousepos, mouseclick):
if(mousepos[0] >= self.x and mousepos[0] <= self.x + self.sx and mousepos[1] >= self.y and mousepos[1] <= self.y + self.sy):
self.current = True
return mouseclick
else:
self.current = False
return False
class Enemy(pygame.sprite.Sprite):
def __init__(self, dx, dy, filename):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load(filename).convert()
self.rect = self.image.get_rect()
self.rect.x = dx
self.rect.y = dy
def draw(self, screen):
screen.blit(self.image, self.rect)
menuScreen = Screen("Menu Screen")
screen2 = Screen("Screen 2")
win = menuScreen.makeCurrent()
done = False
font = pygame.font.Font('freesansbold.ttf', 32)
clickdamage = 1
hitpoints = orighpamt
DungeonButton = Button(125, 500, 150, 50, colours["Black"], colours["Blue"], "arial", 20, colours["White"], "Dungeon")
hitboxButton = Button(80, 50, 280, 400, colours["White"], colours["Red"], "arial", 20, colours["White"], "")
hitpointamount = Button(100, 0, 200, 50, colours["White"], colours["Black"], "arial", 20, colours["Black"], str(hitpoints))
shopButton = Button(125, 500, 150, 50, colours["Black"], colours["Red"], "arial", 20, colours["White"], "Shop")
Assassin_Text = Button(50, 350, 400, 50, colours["White"], colours["Red"], "arial", 18, colours["Black"], "Sharpen your sword and increase your damage per click")
Assassin_Upgrade = Button(10, 300, 125, 50, colours["Black"], colours["Red"], "arial", 15, colours["White"], "Assassin")
goblin = Enemy(0 , 20, "images\goblin-resized.png")
goblin2 = Enemy(80 , 50, "images\monster2.png")
toggle = False
while not done:
menuScreen.screenUpdate()
screen2.screenUpdate()
mouse_pos = pygame.mouse.get_pos()
keys = pygame.key.get_pressed()
mouse_click = False
for event in pygame.event.get():
if(event.type == pygame.QUIT):
done = True
if event.type == pygame.MOUSEBUTTONDOWN:
mouse_click = True
if menuScreen.checkUpdate():
screen2button = shopButton.focusCheck(mouse_pos, mouse_click)
shopButton.showButton(menuScreen.returnTitle())
if screen2button:
win = screen2.makeCurrent()
menuScreen.endCurrent()
elif screen2.checkUpdate():
returnm = DungeonButton.focusCheck(mouse_pos, mouse_click)
Assassin_Upgrade.showButton(screen2.returnTitle())
if Assassin_Upgrade.focusCheck(mouse_pos):
Assassin_Text.showButton(screen2.returnTitle())
if returnm:
win = menuScreen.makeCurrent()
screen2.endCurrent()
pygame.display.update()
pygame.quit()
Add another text attribute to your class. e.g.:
class Button():
def __init__(self, x, y, sx, sy, bcolour, fbcolour, font, fontsize, fcolour, text, tiptext):
# [...]
self.tiptextsurface = self.buttonf.render(tiptext, False, (0, 0, 0), (255, 255, 0))
Draw the text in a Button.showTip method, when self.current is set:
class Button():
# [...]
def showTip(self, display):
if self.current:
mouse_pos = pygame.mouse.get_pos()
display.blit(self.tiptextsurface, (mouse_pos[0]+16, mouse_pos[1]))
Minimal example based on your code:
I've simplified the Button class, but it behaves exactly like the class in your question.
import pygame
pygame.init()
class Button():
def __init__(self, x, y, sx, sy, bcolour, fbcolour, font, fontsize, fcolour, text, tiptext):
self.rect = pygame.Rect(x, y, sx, sy)
self.bcolour = bcolour
self.fbcolour = fbcolour
self.fcolour = fcolour
self.fontsize = fontsize
self.current = False
self.buttonf = pygame.font.SysFont(font, fontsize)
self.textsurface = self.buttonf.render(text, False, self.fcolour)
self.tiptextsurface = self.buttonf.render(tiptext, False, (0, 0, 0), (255, 255, 0))
def showButton(self, display):
color = self.fbcolour if self.current else self.bcolour
pygame.draw.rect(display, color, self.rect)
display.blit(self.textsurface, self.textsurface.get_rect(center = self.rect.center))
def showTip(self, display):
if self.current:
mouse_pos = pygame.mouse.get_pos()
display.blit(self.tiptextsurface, (mouse_pos[0]+16, mouse_pos[1]))
def focusCheck(self, mousepos, mouseclick):
self.current = self.rect.collidepoint(mousepos)
return mouseclick if self.current else True
screen = pygame.display.set_mode((400, 600))
clock = pygame.time.Clock()
font = pygame.font.Font('freesansbold.ttf', 32)
shopButton = Button(125, 500, 150, 50, "black", "red", "arial", 20, "white", "Shop", "Enter the shop")
done = False
while not done:
clock.tick(60)
for event in pygame.event.get():
if(event.type == pygame.QUIT):
done = True
if event.type == pygame.MOUSEBUTTONDOWN:
mouse_click = True
mouse_pos = pygame.mouse.get_pos()
mouse_click = False
screen2button = shopButton.focusCheck(mouse_pos, mouse_click)
screen.fill((255, 255, 255))
shopButton.showButton(screen)
shopButton.showTip(screen)
pygame.display.update()
pygame.quit()
It's easier to answer questions that provide a Minimal, Reproducible Example. Your sample has external image and font dependencies that will need to be overcome for someone to debug your code.
If your button had an associated rect, then you can tell if the mouse cursor is hovering over the button using Rect.colliderect(position).
Here's a minimal example showing some hovering text when the mouse is in a rectangle:
import pygame
WIDTH = 640
HEIGHT = 480
FPS = 30
pygame.init()
pygame.font.init()
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption(f"Color: purple")
clock = pygame.time.Clock()
# create a centered rectangle to fill with color
color_rect = pygame.Rect(100, 100, WIDTH - 200, HEIGHT - 200)
font = pygame.font.SysFont(None, 50, True)
msg = " Hovering Message "
hover_surface = font.render(msg, True, pygame.Color("chartreuse"), pygame.Color("firebrick"))
hovering = False
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# update game elements - Get mouse position
mx, my = pygame.mouse.get_pos()
# draw surface - fill background
screen.fill(pygame.Color("grey"))
## draw central rect
screen.fill(pygame.Color("purple"), color_rect)
## display the hovering text if the mouse is over the rectangle
if color_rect.collidepoint((mx, my)):
screen.blit(hover_surface, (mx, my))
# show surface
pygame.display.update()
# limit frames
clock.tick(FPS)
pygame.quit()
Your code uses a lot of raw geometric calculations where it might be easier in the long run to use some of the native pygame elements. For example, you can make your buttons immobile Sprites, and keep them in a Sprite group to make managing them easier.
Hi so I wanna limit the texts inside a rectangle and if the text surpasses the rectangle's dimension then it reduces itself to fit in. I use class to make the code easier to read and easier to add more input boxes. When I run the code, there should be different input boxes in which I can edit the text in and if the text is too long, it reduces itself( lastest character) to fit in the box. But when I run the code, the text can go over the rectangle for 1 character and for any letter after that, it deletes a whole chunk of text. Here is the code.
''' python
import pygame
import datetime
pygame.init()
clock = pygame.time.Clock()
pygame.font.init()
# Note
finish = 0
leftover = 16
# Font
numb_font = pygame.font.Font('dogicapixelbold.ttf', 14)
text_font = pygame.font.Font('dogicapixelbold.ttf', 16)
color = (233, 248, 215)
active = False
# screen resolution
Width = 800
Height = 600
bg = pygame.image.load('opennote.png')
screen = pygame.display.set_mode((Width, Height))
# Time
time_box = pygame.Rect(250, 63, 50, 30)
date_box = pygame.Rect(221, 27, 50, 30)
# boxes numb
leftover_box = pygame.Rect(265, 105, 30, 30)
finish_box = pygame.Rect(325, 105, 30, 30)
class InputBox:
def __init__(self, x, y, w, h, text=''):
self.rect = pygame.Rect(x, y, w, h)
self.color = color
self.text = text
self.txt_surface = text_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
if event.type == pygame.KEYDOWN:
if self.active:
if event.key == pygame.K_RETURN:
print(self.text)
self.text = ''
elif event.key == pygame.K_BACKSPACE:
self.text = self.text[:-1]
else:
self.text += event.unicode
# Re-render the text.
self.txt_surface = text_font.render(self.text, True, self.color)
def draw(self, screen):
# Blit the text.
screen.blit(self.txt_surface, (self.rect.x+5, self.rect.y+10))
# Blit the rect.
pygame.draw.rect(screen, self.color, self.rect, 2)
def update(self):
# Limit character
if self.txt_surface.get_width() > self.rect.w:
self.text = self.text[:-1]
def main():
clock = pygame.time.Clock()
input_box1 = InputBox(115, 170, 250, 36)
input_box2 = InputBox(115, 224, 250, 36)
input_box3 = InputBox(115, 278, 250, 36)
input_box4 = InputBox(115, 333, 250, 36)
input_box5 = InputBox(115, 386, 250, 36)
input_box6 = InputBox(115, 440, 250, 36)
input_box7 = InputBox(115, 494, 250, 36)
input_boxes = [input_box1, input_box2, input_box3, input_box4, input_box5, input_box6, input_box7]
done = False
while not done:
# Background
screen.fill((0, 0, 0))
screen.blit(bg, (0, 0))
now = datetime.datetime.now()
date_now = now.strftime("%d/%m/%Y")
time_now = now.strftime("%H:%M:%S")
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()
for box in input_boxes:
box.draw(screen)
# Real Time
# Date
pygame.draw.rect(screen, 'white', date_box, -1)
datebox_surface = numb_font.render(date_now, True, color)
screen.blit(datebox_surface, (date_box.x + 5, date_box.y + 5))
# Time
pygame.draw.rect(screen, 'white', time_box, -1)
timebox_surface = numb_font.render(time_now, True, color)
screen.blit(timebox_surface, (time_box.x + 5, time_box.y + 5))
# finish &Leftover
# finish box
pygame.draw.rect(screen, 'white', finish_box, -1)
finishbox_surface = numb_font.render(str(finish), True, color)
screen.blit(finishbox_surface, finish_box)
# Leftover box
pygame.draw.rect(screen, 'white', leftover_box, -1)
leftover_box_surface = numb_font.render(str(leftover), True, color)
screen.blit(leftover_box_surface, leftover_box)
pygame.display.update()
clock.tick(120)
if __name__ == '__main__':
main()
pygame.quit()
'''
But when the letter outside the rect's width, it deletes a whole bunch of text not just the letter that outside the width.
Switch your update and re-render contents.
if event.type == pygame.KEYDOWN:
if self.active:
if event.key == pygame.K_RETURN:
print(self.text)
self.text = ''
elif event.key == pygame.K_BACKSPACE:
self.text = self.text[:-1]
else:
self.text += event.unicode
# Limit characters -20 for border width
if self.txt_surface.get_width() > self.rect.w -20:
self.text = self.text[:-1]
def draw(self, screen):
# Blit the text.
screen.blit(self.txt_surface, (self.rect.x+5, self.rect.y+10))
# Blit the rect.
pygame.draw.rect(screen, self.color, self.rect, 2)
def update(self):
# Re-render the text.
self.txt_surface = text_font.render(self.text, True, self.color)
Edit: oh, it's 3am; I'm just about asleep by now, if I missed posting something that was edited I don't remember. Had to swap out the background for a plain black rectangle and blit that in just for testing, but I know for certain it runs OK here. Downloaded same font and everything. This is what it looks like.
#! /usr/bin/env python3
import pygame
import datetime
pygame.init()
clock = pygame.time.Clock()
pygame.font.init()
# Note
finish = 0
leftover = 16
# Font
numb_font = pygame.font.Font('dogicapixelbold.ttf', 14)
text_font = pygame.font.Font('dogicapixelbold.ttf', 16)
color = (233, 248, 215)
active = False
# screen resolution
Width = 800
Height = 600
bg = pygame .Surface( [Width, Height] )
pygame .draw .rect( bg, (0, 0, 0), [0, 0, Width, Height] )
## bg = pygame.image.load('opennote.png')
screen = pygame.display.set_mode((Width, Height))
# Time
time_box = pygame.Rect(250, 63, 50, 30)
date_box = pygame.Rect(221, 27, 50, 30)
# boxes numb
leftover_box = pygame.Rect(265, 105, 30, 30)
finish_box = pygame.Rect(325, 105, 30, 30)
class InputBox:
def __init__(self, x, y, w, h, text=''):
self.rect = pygame.Rect(x, y, w, h)
self.color = color
self.text = text
self.txt_surface = text_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
if event.type == pygame.KEYDOWN:
if self.active:
if event.key == pygame.K_RETURN:
print(self.text)
self.text = ''
elif event.key == pygame.K_BACKSPACE:
self.text = self.text[:-1]
else:
self.text += event.unicode
# Limit characters -20 for border width
if self.txt_surface.get_width() > self.rect.w -20:
self.text = self.text[:-1]
def draw(self, screen):
# Blit the text.
screen.blit(self.txt_surface, (self.rect.x+5, self.rect.y+10))
# Blit the rect.
pygame.draw.rect(screen, self.color, self.rect, 2)
def update(self):
# Re-render the text.
self.txt_surface = text_font.render(self.text, True, self.color)
def main():
clock = pygame.time.Clock()
input_box1 = InputBox(115, 170, 250, 36)
input_box2 = InputBox(115, 224, 250, 36)
input_box3 = InputBox(115, 278, 250, 36)
input_box4 = InputBox(115, 333, 250, 36)
input_box5 = InputBox(115, 386, 250, 36)
input_box6 = InputBox(115, 440, 250, 36)
input_box7 = InputBox(115, 494, 250, 36)
input_boxes = [input_box1, input_box2, input_box3, input_box4, input_box5, input_box6, input_box7]
done = False
while not done:
# Background
screen.fill((0, 0, 0))
screen.blit(bg, (0, 0))
now = datetime.datetime.now()
date_now = now.strftime("%d/%m/%Y")
time_now = now.strftime("%H:%M:%S")
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()
for box in input_boxes:
box.draw(screen)
# Real Time
# Date
pygame.draw.rect(screen, 'white', date_box, -1)
datebox_surface = numb_font.render(date_now, True, color)
screen.blit(datebox_surface, (date_box.x + 5, date_box.y + 5))
# Time
pygame.draw.rect(screen, 'white', time_box, -1)
timebox_surface = numb_font.render(time_now, True, color)
screen.blit(timebox_surface, (time_box.x + 5, time_box.y + 5))
# finish &Leftover
# finish box
pygame.draw.rect(screen, 'white', finish_box, -1)
finishbox_surface = numb_font.render(str(finish), True, color)
screen.blit(finishbox_surface, finish_box)
# Leftover box
pygame.draw.rect(screen, 'white', leftover_box, -1)
leftover_box_surface = numb_font.render(str(leftover), True, color)
screen.blit(leftover_box_surface, leftover_box)
pygame.display.update()
clock.tick(120)
if __name__ == '__main__':
main()
pygame.quit()
when ever I try to display to render this text 1 by 1 it seems to slow my game down and make everything run slow and for some reason it starts the text as well I want it to end when its finished typing the text VIDEO< as you can see in the video the text keeps playing the game keeps lagging for some reason when ever each of the text is rendered on the screen
def show_text(string):
WHITE = (255, 255, 255)
text = ''
font = pygame.font.Font("Alice.ttf", 30)
for i in range(len(string)):
text += string[i]
text_surface = font.render(text, True, WHITE)
text_rect = text_surface.get_rect()
text_rect.center = (700/2, 800/2)
window.blit(text_surface, text_rect)
pygame.display.update()
pygame.time.wait(100)
display_text_animation('Hello World!')
heres the full code there all rects execept the Alice.tff < text style
import pygame, sys
from pygame.locals import *
WINDOW_WIDTH = 500
WINDOW_HEIGHT = 500
pygame.init()
DISPLAYSURF = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
BLACK = ( 0, 0, 0)
WHITE = (255, 255, 255)
class player:
def __init__(self,x,y,height,width,color):
self.x = x
self.y = y
self.height = height
self.width = width
self.color = color
self.rect = pygame.Rect(x,y,height,width)
def draw(self):
self.rect.topleft = (self.x,self.y)
pygame.draw.rect(DISPLAYSURF,self.color,self.rect)
white = (120,120,120)
player1 = player(150,150,50,50,white)
def display_text_animation(string):
text = ''
for i in range(len(string)):
text += string[i]
font = pygame.font.Font("Alice.ttf", 30)
text_surface = font.render(text, True, white)
text_rect = text_surface.get_rect()
DISPLAYSURF.blit(text_surface, text_rect)
pygame.display.update()
pygame.time.wait(100)
def main():
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
keys = pygame.key.get_pressed()
if keys[pygame.K_d]:
player1.x += 5
if keys[pygame.K_a]:
player1.x -= 5
DISPLAYSURF.fill((0,0,0))
player1.draw()
display_text_animation('Hello World!')
main()
Do not create the pygame.font.Font object in every frame. Creating a font object is very time consuming. Every time the Font object is created, the font file ("Alice.ttf") must be read and interpreted. Create the Fontobject before the application loop during initialization.
Furthermore do not animate the text in a loop use the application loop. Use pygame.time.get_ticks() to measure the time. Calculate the number of letters to be displayed as a function of time:
font = pygame.font.Font("Alice.ttf", 30)
def display_text_animation(string, start_time):
current_time = pygame.time.get_ticks()
letters = (current_time - start_time) // 100
text = string[:letters]
WHITE = (255, 255, 255)
text_surface = font.render(text, True, WHITE)
text_rect = text_surface.get_rect()
text_rect.center = (700/2, 800/2)
DISPLAYSURF.blit(text_surface, text_rect)
def main():
text = 'Hello World!'
start_time = pygame.time.get_ticks()
clock = pygame.time.Clock()
while True:
clock.tick(60)
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
keys = pygame.key.get_pressed()
if keys[pygame.K_d]:
player1.x += 5
if keys[pygame.K_a]:
player1.x -= 5
DISPLAYSURF.fill((0,0,0))
player1.draw()
display_text_animation(text, start_time)
pygame.display.update()
When ever you want to animate a new text you just have to set text and start_time. e.g:
text = "New text"
start_time = pygame.time.get_ticks()
Minimal typewriter effect example:
import pygame
pygame.init()
window = pygame.display.set_mode((500, 150))
clock = pygame.time.Clock()
font = pygame.font.SysFont(None, 100)
background = pygame.Surface(window.get_size())
ts, w, h, c1, c2 = 50, *window.get_size(), (32, 32, 32), (64, 64, 64)
tiles = [((x*ts, y*ts, ts, ts), c1 if (x+y) % 2 == 0 else c2) for x in range((w+ts-1)//ts) for y in range((h+ts-1)//ts)]
for rect, color in tiles:
pygame.draw.rect(background, color, rect)
text = 'Hello World'
text_len = 0
typewriter_event = pygame.USEREVENT+1
pygame.time.set_timer(typewriter_event, 100)
text_surf = None
run = True
while run:
clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if event.type == typewriter_event:
text_len += 1
if text_len > len(text):
text_len = 0
text_surf = None if text_len == 0 else font.render(text[:text_len], True, (255, 255, 128))
window.blit(background, (0, 0))
if text_surf:
window.blit(text_surf, text_surf.get_rect(midleft = window.get_rect().midleft).move(40, 0))
pygame.display.flip()
pygame.quit()
exit()
def InputBox():
font = pygame.font.Font(None, 32)
inputBox = pygame.Rect(50, 50, 140, 32)
colourInactive = pygame.Color('lightskyblue3')
colourActive = pygame.Color('dodgerblue2')
colour = colourInactive
text = ''
active = False
isBlue = True
while True:
for event in pg.event.get():
if event.type == pg.QUIT:
pg.quit()
sys.exit()
if event.type == pygame.MOUSEBUTTONDOWN:
if inputBox.collidepoint(event.pos):
active = not active
else:
active = False
colour = colourActive if active else colourInactive
if event.type == pygame.KEYDOWN:
if active:
if event.key == pygame.K_RETURN:
print(text)
text = ''
elif event.key == pygame.K_BACKSPACE:
text = text[:-1]
else:
text += event.unicode
screen.fill(screenGray)
txtSurface = font.render(text, True, colour)
width = max(200, txtSurface.get_width()+10)
inputBox.w = width
screen.blit(txtSurface, (inputBox.x+5, inputBox.y+5))
pygame.draw.rect(screen, colour, inputBox, 2)
if isBlue:
color = (0, 128, 255)
else:
color = (255, 100, 0)
pg.display.flip()
clock.tick(60)
InputBox()
Above is a working function to make a screen that has a text box on it. Now, how would you make two text boxes on the same screen, without just copying and pasting the same code twice?
My idea is that the text box that is clicked on, or activated, will do the stuff in the event part, so you don't have to repeat it all twice. However, I have no idea how to implement that.
Thanks in advance
You could create a class, and instantiate it as many times as you need different input boxes.
something like this:
# pseudocode #
class InputBox(pygame.Rect):
def __init__(self, position=(50, 50, 140, 32),
font=pygame.font.Font(None, 32),
color_inactive=pygame.Color('lightskyblue3'),
color_active=pygame.Color('dodgerblue2'),
text = '',
active=False)
super().__init__(position) #<- this may need to be adjusted to pygame Rect specs
self.font = font
self.color_inactive = color_inactive
self.color_active = color_active
self.color = color_inactive
self.text = text
self.active = active
You can create class with methods which draws it and which handle events. And then you can use it many times
Full working example
import pygame
class InputBox():
def __init__(self, x, y):
self.font = pygame.font.Font(None, 32)
self.inputBox = pygame.Rect(x, y, 140, 32)
self.colourInactive = pygame.Color('lightskyblue3')
self.colourActive = pygame.Color('dodgerblue2')
self.colour = self.colourInactive
self.text = ''
self.active = False
self.isBlue = True
def handle_event(self, event):
if event.type == pygame.MOUSEBUTTONDOWN:
self.active = self.inputBox.collidepoint(event.pos)
self.colour = self.colourActive if self.active else self.colourInactive
if event.type == pygame.KEYDOWN:
if self.active:
if event.key == pygame.K_RETURN:
print(self.text)
self.text = ''
elif event.key == pygame.K_BACKSPACE:
self.text = self.text[:-1]
else:
self.text += event.unicode
def draw(self, screen):
txtSurface = self.font.render(self.text, True, self.colour)
width = max(200, txtSurface.get_width()+10)
self.inputBox.w = width
screen.blit(txtSurface, (self.inputBox.x+5, self.inputBox.y+5))
pygame.draw.rect(screen, self.colour, self.inputBox, 2)
if self.isBlue:
self.color = (0, 128, 255)
else:
self.color = (255, 100, 0)
# --- main ---
def mainloop():
# create objects
input1 = InputBox(50, 50)
input2 = InputBox(450, 50)
clock = pygame.time.Clock()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
# handle every event
input1.handle_event(event)
input2.handle_event(event)
screen.fill((128,128, 128))
# draw it
input1.draw(screen)
input2.draw(screen)
pygame.display.flip()
clock.tick(60)
pygame.init()
screen = pygame.display.set_mode((800,600))
mainloop()
BTW: for more inputs you could keep them on list (like in GUI frameworks)
def mainloop():
widgets = [
InputBox(50, 50),
InputBox(450, 50),
InputBox(50, 150),
InputBox(450, 150),
InputBox(50, 250),
InputBox(450, 250),
InputBox(50, 350),
InputBox(450, 350),
]
clock = pygame.time.Clock()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
for child in widgets:
child.handle_event(event)
screen.fill((128,128, 128))
for child in widgets:
child.draw(screen)
pygame.display.flip()
clock.tick(60)