Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 9th, 2005, 4:16 PM   #1
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 1,886
Rep Power: 5 Sane will become famous soon enough
Send a message via MSN to Sane
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...
Sane is offline   Reply With Quote
Old Apr 9th, 2005, 5:12 PM   #2
Cerulean
Professional Programmer
 
Cerulean's Avatar
 
Join Date: Apr 2005
Location: London, England
Posts: 459
Rep Power: 4 Cerulean is on a distinguished road
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
Cerulean is offline   Reply With Quote
Old Apr 9th, 2005, 5:16 PM   #3
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 1,886
Rep Power: 5 Sane will become famous soon enough
Send a message via MSN to Sane
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)...
Sane is offline   Reply With Quote
Old Apr 10th, 2005, 5:49 PM   #4
Infinite Recursion
Programming Guru
 
Infinite Recursion's Avatar
 
Join Date: Jul 2004
Location: United States
Posts: 3,467
Rep Power: 8 Infinite Recursion is on a distinguished road
Send a message via MSN to Infinite Recursion Send a message via Yahoo to Infinite Recursion
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."
Infinite Recursion is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 1:23 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC