Blender 2.5X Game Engine Text Script! (With screenshot)

Hello all…

So I hate the way text is done in the BGE so… I designed my own way of doing it (you can implement your own if you’re witty enough :cool: ). It’s quite simply… I created a script where all you do is change the variable ‘text’ to whatever you want to see. You can change the color of the text - italic and bold are coming soon!

Also - so far all you can do is write letters… But I will add numbers and special characters soon as well. I hope you enjoy! Please comment and crit! Also - I know using an array would be faster - but hey, if you can teach me how to do it - feel free to change the script… And please - if you use it - DO NOT FORGET TO GIVE CREDITS!

Thank you!

H Y P E R R E A L

Text Script:

import bge
from bge import logic
from bge import render
from bge import events
import Rasterizer 

Rasterizer.showMouse(True)

cont = logic.getCurrentController()
object = logic.getCurrentScene().objects
obj = cont.owner

start = object['Start']
start_x = start.position.x
start_y = start.position.y

key = logic.keyboard.events
mouse = logic.mouse.events

text = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
color = [ 0, 50, 50]

if not 'init' in obj:
    obj['init'] = 0
    font = \
        [
            [.3, []],           # 20 space
            [.5, []],           #
            [.5, []],           #
            [.5, []],           #
            [.5, []],           #
            [.5, []],           #
            [.5, []],           #
            [.5, []],           #
            [.5, []],           #
            [.5, []],           #
            [.5, []],           #
            [.5, []],           #
            [.5, []],           #
            [.5, []],           #
            [.5, []],           #
            [.5, []],           #
            [.5, []],           #
            [.5, []],           #
            [.5, []],           #
            [.5, []],           #
            [.5, []],           #
            [.5, []],           #
            [.5, []],           #
            [.5, []],           #
            [.5, []],           #
            [.5, []],           #
            [.5, []],           #
            [.5, []],           #
            [.5, []],           #
            [.5, []],           #
            [.5, []],           #
            [.5, []],           #
            [.5, []],           #
            [.6, [              # 41 A
                [[0, -1], [.25, 0], [.5, -1]],
                [[.125, -.5], [.375, -.5]]
                ]],
            [.6, [              # 42 B
                [[0, -1], [0, 0], [.375, 0], [.5, -.125], [.5, -.375], [.375, -.5], [0, -.5]],
                [[.375, -.5], [.5, -.625], [.5, -.875], [.375, -1], [0, -1]]
                ]],
            [.6, [              # 43 C
                [[.5, -.125], [.375, 0], [.125, 0], [0, -.125], [0, -.875], [.125, -1], [.375, -1], [.5, -.875]]
                ]],
            [.6, [              # 44 D
                [[0, -1], [0, 0], [.375, 0], [.5, -.125], [.5, -.875], [.375, -1], [0, -1]]
                ]],
            [.6, [              # 45 E
                [[0, -1], [0, 0], [.5, 0]],
                [[0, -.5], [.375, -.5]],
                [[0, -1], [.5, -1]]
                ]],
            [.6, [              # 46 F
                [[0, -1], [0, 0], [.5, 0]],
                [[0, -.5], [.375, -.5]]
                ]],
            [.6, [              # 46 G
                [[.5, -.125], [.375, 0], [.125, 0], [0, -.125], [0, -.875], [.125, -1], [.375, -1], [.5, -.875], [.5, -.5], [.25, -.5]]
                ]],
            [.6, [              # 47 H
                [[0, -1], [0, 0]],
                [[0, -.5], [.5, -.5]],
                [[.5, 0], [.5, -1]]
                ]],
            [.45, [              # 48 I
                [[0, 0], [.35, 0]],
                [[.175, 0], [.175, -1]],
                [[0, -1], [.35, -1]]
                ]],
            [.6, [              # 49 J
                [[0, 0], [.5, 0], [.5, -.875], [.375, -1], [.125, -1], [0, -.875]]
                ]],
            [.6, [              # 50 K
                [[0, -1], [0, 0]],
                [[0, -.5], [.5, 0]],
                [[.125, -.375], [.5, -1]]
                ]],
            [.6, [              # 51 L
                [[0, 0], [0, -1], [.5, -1]]
                ]],
            [.6, [              # 52 M
                [[0, -1], [0, 0], [.25, -.5], [.5, 0], [.5, -1]]
                ]],
            [.45, [              # 53 N
                [[0, -1], [0, 0], [.35, -1], [.35, 0]]
                ]],
            [.6, [              # 54 O
                [[.5, -.125], [.375, 0], [.125, 0], [0, -.125], [0, -.875], [.125, -1], [.375, -1], [.5, -.875], [.5, -.125]]
                ]],
            [.6, [              # 55 P
                [[0, -1], [0, 0], [.375, 0], [.5, -.125], [.5, -.375], [.375, -.5], [0, -.5]]
                ]],
            [.6, [              # 56 Q
                [[.5, -.125], [.375, 0], [.125, 0], [0, -.125], [0, -.875], [.125, -1], [.375, -1], [.5, -.875], [.5, -.125]],
                [[.375, -.875], [.5, -1]]
                ]],
            [.6, [              # 57 R
                [[0, -1], [0, 0], [.375, 0], [.5, -.125], [.5, -.375], [.375, -.5], [0, -.5], [.5, -1]]
                ]],
            [.6, [              # 58 S
                [[.5, -.125], [.375, 0], [.125, 0], [0, -.125], [0, -.375], [.125, -.5], [.375, -.5], [.5, -.625], [.5, -.875], [.375, -1], [.125, -1], [0, -.875]]
                ]],
            [.6, [              # 59 T
                [[0, 0], [.5, 0]],
                [[.25, 0], [.25, -1]]
                ]],
            [.6, [              # 60 U
                [[0, 0], [0, -.875], [.125, -1], [.375, -1], [.5, -.875], [.5, 0]]
                ]],
            [.6, [              # 61 V
                [[0, 0], [.25, -1], [.5, 0]]
                ]],
            [.6, [              # 62 W
                [[0, 0], [.125, -1], [.25, -.5], [.375, -1], [.5, 0]]
                ]],
            [.6, [              # 63 X
                [[0, 0], [.5, -1]],
                [[.5, 0], [0, -1]]
                ]],
            [.6, [              # 64 Y
                [[0, 0], [.25, -.5], [.5, 0]],
                [[.25, -.5], [.25, -1]]
                ]],
            [.6, [              # 65 Z
                [[0, 0], [.5, 0], [0, -1], [.5, -1]]
                ]]
        ]

def drawText():
    drawString( text, font, start_x, start_y, 1, 1 )

def drawLine( x1, y1, x2, y2 ):
    render.drawLine( [x1, y1, 0], [x2, y2, 0], color )

def drawGlyph( g, x, y, h, v ):
    for p in g:
        for i in range( len(p) - 1 ):
            drawLine( p[i][0]*h + x, p[i][1]*v + y, p[i + 1][0]*h + x, p[i + 1][1]*v + y )

def drawString( s, f, x, y, h, v ):
    pos = x
    
    for c in s:
        info = f[ord(c) - 32]
        drawGlyph( info[1], pos, y, h, v )
        pos += info[0]*h
        
logic.getCurrentScene().post_draw = [drawText]

Here’s what it looks like in action!

Oh, wow. You’re drawing the letters by hand, aren’t you? LOL - That’s pretty crazy. Nice job on the code, particularly the drawGlyph function.

Heheh… Yes indeed - It took roughly 20 minutes to draw each letter by hand … lol. And thank you ! :slight_smile:

Forget to mention… the height and width are both extremely easily change-able, since the height and width variable are both set to one, you can use .5 , or 2 and it will be halved or doubled respectively… Also the spacing is halved and doubled as well - or not up to your preference. Unlike what’s used in the stupid other text methods which are all stupid… sigh - I spent too many hours trying to tweak them to conclude that hey - why not just spend one afternoon and make me own drawText script! And it works quite awesomely. I will soon add smaller case letters (which are kind of hard to do by hardcoding them… so I think I’ll make a script that scripts letters lol) and also numbers and special characters.