I'm creating the base of a game that will have scrolling text and text wrapping when a line of text goes to far on the screen.
Everything works (so far) but at a certain point when using my default_display_text
function pygame
stops responding. It always stops around the "broke, slap some fancy words" part when I use my default_display_text
function with greeting as the variable.
I've been able to solve all my other problems with google but this one has got me really stuck! I'll be glad to give any other information and I hope you understand what my code does with help from the comments.
import pygame, math, pygame.display, time, pygame.mouse
from pygame.locals import *
pygame.init()
pygame.font.init()
infoObject = pygame.display.Info()
window_height = infoObject.current_w
window_width = infoObject.current_h
gameDisplay = pygame.display.set_mode((window_height, window_width), pygame.RESIZABLE)
pygame.display.set_caption('Aperture_Game')
pygame.mouse.set_visible(False)
c_font = 'C:\Windows\Fonts\Courier Regular.fon'
background = (10,10,10)
black = (0,0,0)
white = (255,255,255)
text_color = (39, 167, 216)
clock = pygame.time.Clock()
loop = True
default_font_size = 26
greeting = "Greetings! My name is Cave Johnson, CEO of Aperture Science. Our motto here is \"If it ain't broke, slap some fancy words on it and sell it for profit!\""
'''
def message_display(text,x,y,font,color,size):
fontObj = pygame.font.SysFont(font, size)
label = fontObj.render(text, 1, color)
gameDisplay.blit(label, (x,y))
'''
def default_display_text(string,x,y):
text = ''
#a tracks i's value.
a = 0
#newline helps determine when to wrap the text.
newline = 0
#ref is a reference for when to wrap the text (I only want to wrap text after a space so the wrapping doesn't split words in half).
ref = ' '
fontObj = pygame.font.SysFont(c_font, default_font_size)
text_surface = fontObj.render(text, True, text_color)
text_rect = text_surface.get_rect()
#this for loop is what displays the text one character at a time and wraps it.
for i in range(len(string)):
gameDisplay.fill(background)
if newline == 0:
gameDisplay.blit(text_surface, (x,y))
#if the text goes to far to the right and a word is complete, I want to start a new line.
if pygame.Surface.get_width(text_surface) > 1000:
#makes sure that its not in the middle of a word.
if string[a] == ref:
#making sure its not a double space
if string[a+1] != ref:
newline = 1
text = text[a:]
else:
newline = 0
#adds another character of the string to the text variable
if newline == 0:
text += string[i]
# if newline is not 0 then the start of the text is shifted down and all the previous text is deleted.
else:
y += 25
newline = 0
text += string[a:a+1]
text = text[a:]
a = 0
a += 1
pygame.display.update()
pygame.time.wait(50)
#Just your average main loop
def main():
loop = True
while loop:
for event in pygame.event.get():
if event.type == pygame.QUIT:
loop = False
if event.type == KEYDOWN:
if event.key == K_ESCAPE:
loop = False
#add controls here later
gameDisplay.fill(background)
default_display_text(greeting, 50, 50)
pygame.display.update()
clock.tick(60)
main()
pygame.quit()
quit()