![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programming Guru
![]() |
Neat little program...
...just another snipet of my RPG. It's a module that allows easy display of a character's dialogue with little effort.
You just take the main code, add where and what color you want the text to be. Along with whatever else. Then you just simple do a little function call whenever you want the dialogue displayed. import pygame
pygame.init()
screen = pygame.display.set_mode((800, 700))
pygame.mouse.set_visible(1)
def dialogue(name, speech):
#constant variables most likely to be changed:
text_boxX = 150
text_boxY = 150
speed = 100
color = (255, 255, 255)
#/end constants
pygame.font.init()
background = pygame.Surface(screen.get_size())
background = background.convert()
background.fill((0, 0, 0))
font = pygame.font.Font(None, 17)
text = font.render(name + ":", 2, (255, 255, 255))
textpos = (text_boxX, text_boxY)
background.blit(text, textpos)
screen.blit(background, (0, 0))
pygame.display.flip()
clock = pygame.time.Clock()
font = pygame.font.Font(None, 14)
for letter in range(0, len(speech)):
clock.tick(-(speed-200))
texta = speech[letter]
text = font.render(texta, 2, color)
textpos = ( text_boxX+8*(len (name) )+5+(letter*8), (text_boxY+2) )
background.blit(text, textpos)
screen.blit(background, (0, 0))
pygame.display.flip()Then in my main program, for some dialogue, I might go: from speech_function import *
import time
background = pygame.Surface(screen.get_size())
background = background.convert()
background.fill((0, 0, 0))
dialogue("Turk", "Are we there yet?")
time.sleep(2)
background = pygame.Surface(screen.get_size())
background = background.convert()
background.fill((0, 0, 0))
dialogue("Gurshnu", "Just a little further!!")
time.sleep(2)
background = pygame.Surface(screen.get_size())
background = background.convert()
background.fill((0, 0, 0))
dialogue("Turk", "Okay...I've got an idea!...")
time.sleep(2)
for amount in range(-99, 1):
songa = str(-amount)
songb = "Bottles of beer on the wall,"
songd = "bottles of beer! Take one down, pass it around"
songe = str(-amount-1)
songf = "bottles of beer on the wall!"
background = pygame.Surface(screen.get_size())
background = background.convert()
background.fill((0, 0, 0))
dialogue("Turk and Gurshnu", songa)
time.sleep(1)
background = pygame.Surface(screen.get_size())
background = background.convert()
background.fill((0, 0, 0))
dialogue("Turk and Gurshnu", songb)
time.sleep(1)
background = pygame.Surface(screen.get_size())
background = background.convert()
background.fill((0, 0, 0))
dialogue("Turk and Gurshnu", songa)
time.sleep(1)
background = pygame.Surface(screen.get_size())
background = background.convert()
background.fill((0, 0, 0))
dialogue("Turk and Gurshnu", songd)
time.sleep(3)
background = pygame.Surface(screen.get_size())
background = background.convert()
background.fill((0, 0, 0))
dialogue("Turk and Gurshnu", songe)
background = pygame.Surface(screen.get_size())
background = background.convert()
background.fill((0, 0, 0))
dialogue("Turk and Gurshnu", songf)
time.sleep(2)
background = pygame.Surface(screen.get_size())
background = background.convert()
background.fill((0, 0, 0))
dialogue("Turk and Gurshnu", "*stabs eachother*")I'm now adding a small if statement to detect the end of a line and move down to the next... |
|
|
|
|
|
#2 |
|
Professional Programmer
Join Date: Apr 2005
Location: London, England
Posts: 459
Rep Power: 4
![]() |
You say small if statement but it's a lot trickier than that. I had to write a text area widget for a game in OpenGL.. not something I want to have to do again
![]() |
|
|
|
|
|
#3 |
|
Programming Guru
![]() |
No, it was small. I finished a while ago:
import pygame
import time
pygame.init()
screen = pygame.display.set_mode((800, 500))
pygame.mouse.set_visible(1)
#gamescreen = pygame.image.load("gamescreen.bmp")
#screen.blit(gamescreen, (50, 0) )
#gamescreen = pygame.image.load("text_box.bmp")
#screen.blit(gamescreen, (227, 327) )
#pygame.display.flip()
def dialogue(name, speech):
#constant variables most likely to be changed:
text_boxX = 235
text_boxY = 333
speed = -100
color = (255, 255, 255)
#/end constants
pygame.font.init()
font = pygame.font.Font(None, 17)
text = font.render(name + ":", 2, (255, 255, 255))
textpos = (text_boxX, text_boxY)
screen.blit(text, textpos)
pygame.display.flip()
clock = pygame.time.Clock()
font = pygame.font.Font(None, 14)
for letter in range(0, len(speech)):
clock.tick(-(speed-200))
texta = speech[letter]
text = font.render(texta, 2, color)
if (text_boxX+8*(len(name))+5+(letter*7)) > 565-14:
if letter > 70:
cursor_return = 0
while cursor_return == 0:
cursor = pygame.image.load("cursor.bmp")
screen.blit(cursor, (554, 353) )
pygame.display.flip()
time.sleep(1)
cursor = pygame.image.load("cursora.bmp")
screen.blit(cursor, (554, 353) )
pygame.display.flip()
time.sleep(1)
if (text_boxX+8*(len(name))+5+(letter*7)) > 565:
if (text_boxX+8*(len(name))+5+(letter*7)) < 571:
text_boxX = text_boxX - letter*7
text_boxY = text_boxY + 15
textpos = ( text_boxX+8*(len (name) )+5+(letter*7), (text_boxY+2) )
screen.blit(text, textpos)
pygame.display.flip()
#dialogue("Turk", "HAR HAR HAR HAR HAR HAR HAR HAR HAR HAR HAR HAR HAR HAR HAR HAR HAR")When the text gets too far it bumps down a line. Then the second time the program stops outputting, displays a blinking cursor (and now I'm going to make it wait for input from the user to clear the textbox and continue outputting)... |
|
|
|
|
|
#4 |
|
Programming Guru
![]() ![]() ![]() |
seems pretty straight forward, i really need to pick up on some python... but I'm thinking my time is spread to thin at the moment... may need to drop a few languages off the plate for now.
__________________
http://jasonpowers.net "There are a thousand hacking at the branches of evil to one who is striking at the root." |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|