## text based game

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Contact:

### Re: text based game

I feel obliged to post this: ascii sector.
When I write def I mean function.

pwniknoobz
Prole
Posts: 11
Joined: Fri Jul 16, 2010 3:10 pm

### Re: text based game

Hey supertails - heres a class I made for Love2d that simulates textboxes to gather user input. It's just a basic start, but should give you a starting point for handling text based user input on the gui.

I have attached the 2 necessary lua files to be required in main.lua.

Here is a simple example of how to implement the textbox class:

Code: Select all

function love.load()

txtNewBox[1] = TextBox:new()
txtNewBox[2] = TextBox:new()
txtNewBox[0] = 2 -- INDEX 0 OF A TEXTBOX ARRAY HOLDS THE VALUE OF THE TOTAL NUMBER OF TEXT BOXES IN THE ARRAY, MANUALLY SET THIS

txtNewBox[1]:setParams (100,100,12,0,'textbox.png','textbox_selected.png',true,true,'(Enter Nick Name)')
txtNewBox[2]:setParams (100,200,5,0,'textbox.png','textbox_selected.png',true,true,'(Click to enter value)')

end

function love.draw()

drawTextBoxes(txtNewBox)

end

function love.mousepressed(x, y, button)

checkTextBoxesClicked (txtNewBox, x, y)

end

function love.keypressed(k)

updateTextBoxesText (txtNewBox, k)

end

text_box.lua is set up to handle an array of textboxes. Index[0] of each array must hold the total number of textboxes in the array (starting with 1).

Parameters:

setParams (x,y,maxChars,txtType,txtBoxImg,txtBoxImgSelected,enabled,visible,sleepText)

x: The x position of the textbox.
y: The y position of the textbox.
maxChars: The maximum number of characters allowed in the textbox.
txtType: N/A at this point, to be used with further development of the class, may leave blank for now.
txtBoxImg: A file path string to the textbox image you would like to use.
txtBoxImgSelected: A file path string to the textbox image you would like the textbox to change to when it is selected.
enabled: Self explanatory, as of now the only impact this variable makes is that if the textbox is not enabled, it will not draw, and can not be selected/typed in.
visible: Same as enabled for now.
sleepText: When a textbox loses focus, it's text will be reset to sleepText.

This class still needs some development, such as variables to control the specific placement of the cursor inside the textbox image in order to adapt to various textbox images. Also, this class does not limit what keys can be typed into it; I.E. if you press the spacebar it will type "spacebar" per Love2d's key constants.

If you would like to manually adapt the class for YOUR specific textbox image, and align the cursor into the correct position, you can play around with the love.graphics.print calls in the TextBox:draw() function. There are 3 instances of print in TextBox:draw(), such as this:

love.graphics.print(self.text, self.x + 8, self.y + self.height - 10)

To adjust the placement of the cursor, modify the "self.x + 8" and the "self.height - 10" to correctly align the cursor within your textbox image.

Let me know if this helps!
Attachments
timer.lua
text_box.lua

Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

### Re: text based game

pwniknoobz wrote:

Code: Select all

txtNewBox[0] = 2 -- INDEX 0 OF A TEXTBOX ARRAY HOLDS THE VALUE OF THE TOTAL NUMBER OF TEXT BOXES IN THE ARRAY, MANUALLY SET THIS
Why? What is wrong with #txtNewBox?