Drawing text through bge module - Drawing several words

by Mr. Baldan   Last Updated January 14, 2018 05:15 AM

The code of my script:

import drawtext
drawtext.chain = 'Hello World!'
drawtext.positionx = 0
drawtext.positiony = 0

The code of the file drawtext.py:

# import game engine modules
from bge import render, logic
# import stand alone modules
import bgl, blf

# create a new font object, use external ttf file
font_path = logic.expandPath('C://Documents and Settings//Guillermo//Mis documentos//Google Drive//Gproyectos//Proyectos con Blender//arial.ttf')
# store the font indice - to use later
font_id = blf.load(font_path)
chain = 'this is the message'
positionx = 0.5
positiony = 0.5

def write():
    """write on screen"""
    width = render.getWindowWidth()
    height = render.getWindowHeight()
    # OpenGL setup
    bgl.glMatrixMode(bgl.GL_PROJECTION)
    bgl.glLoadIdentity()
    bgl.gluOrtho2D(0, width, 0, height)
    bgl.glMatrixMode(bgl.GL_MODELVIEW)
    bgl.glLoadIdentity()

    # BLF drawing routine
    blf.position(font_id, (width * positionx), (height * positiony), 0)
    blf.size(font_id, 50, 72)
    bgl.glColor4f(1.0, 0.0, 0.0, 1.0)
    blf.draw(font_id,chain)

def update(cont):
    own = cont.owner
    chain = own['msg']

# set the font drawing routine to run every frame
scene = logic.getCurrentScene()
scene.post_draw = [write]

when you run the game engine, you see this: enter image description here

And what I want to do is to obtein this: enter image description here

How can I change my code?



Related Questions



How do I make editable text in the game engine?

Updated January 03, 2018 02:15 AM

How to draw a text using the bge module

Updated January 07, 2018 22:15 PM

How to draw a text using the bge module II

Updated January 08, 2018 09:15 AM

Text object alpha BGE python to show/hide 2.79

Updated November 15, 2017 20:15 PM