Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Idea for cutting down program size in games!!
#1
One of the things I've found writing games in QBasic is that you can run out of room for stuff really fast. The more text you have in your program, i.e. every single

Code:
PRINT "You enter the room.  Nothing important happens."

and every

Code:
PRINT MonsterName$ + " hit you for" + damage + " damage!"

uses up space in your program. Eventually, bad things will start happening.

So I suggest that if you're ever planning on writing game in QBasic, keep as much stuff out of the actual code as humanly possible!

Some things I did:

1. As early as possible, write a subroutine to print your text for you. Pass this routine a filename, and it will print the file. That way, ALL of your game text can be external to the program. Here's an example of one I used:

Code:
SUB TT (filename$, n)
     OPEN "text\" + filename$ FOR INPUT AS #9
          count = 0
          DO
               COLOR 15, 0
               LINE INPUT #9, text$
               skip = 0
               count = count + 1
               FOR t = 1 TO LEN(text$)
                    IF MID$(text$, t, 1) = "^" THEN 'change colors
                         COLOR(VAL(MID$(text$, t + 1, 2)), 0), 0
                         t = t + 2
                    ELSEIF MID$(text$, t, 1) = "&" THEN 'display special
                         SELECT CASE MID$(text$, t + 1, 3)
                              CASE "DAT" 'don't display this line of text (it's just a remark line)
                                   count = count - 1
                                   skip = 1
                                   EXIT FOR
                              CASE "ITM" 'print the nth item in your inventory
                                   PRINT itm$(i(n), id(n));
                              CASE "HPC" 'print current Health
                                   PRINT MID$(STR$(PC.HP), 2);
                              CASE "HPM" 'print max health
                                   PRINT MID$(STR$(PC.HPMax), 2);
                              CASE "SPC" 'print current magic
                                   PRINT MID$(STR$(PC.SP), 2);
                              CASE "SPM" 'print max magic
                                   PRINT MID$(STR$(PC.SPMax), 2);
                              CASE "NUM" 'print n (passed argument)
                                   PRINT MID$(STR$(n), 2);
                              CASE "ROM" 'print current room
                                   PRINT MID$(STR$(PC.Room), 2);
                              CASE "MOV" 'print remaining movement points
                                   PRINT MID$(STR$(PC.Moves), 2);
                              CASE "MON" 'print name of current monster
                                   PRINT mon$(m);
                              CASE "MHP" 'print monster's health
                                   PRINT MID$(STR$(MonHP), 2);
                              CASE "NAM" 'print character name
                                   PRINT SB$(PC.CName);
                              CASE "GPC" 'print current gold amount
                                   PRINT MID$(STR$(PC.Gold), 2);
                              CASE "LEV" 'print current level
                                   PRINT MID$(STR$(PC.Level), 2);
                              CASE "EXP" 'print experience points
                                   PRINT MID$(STR$(PC.XP), 2);
                         END SELECT
                         t = t + 3
                    ELSE
                         PRINT MID$(text$, t, 1);
                    END IF
               NEXT t
               IF skip = 0 THEN
                 PRINT
                 COLOR 15, 0
               END IF

               IF count = 21 THEN 'pause the screen
                    DO: LOOP UNTIL INKEY$ <> ""
                    count = 0
               END IF

          LOOP UNTIL EOF(9)
     CLOSE #9
END SUB

This may look a bit confusing, but here's what it essentially does:

1. opens up the file whose name is passed to it (filename$)
2. reads in the first line of text from the file (line input #9, text$)
3. goes through text$ one letter at a time, doing the following:
if the letter = "^" then change the COLOR of the text to whatever the two digits immediately following the "^" are. For example, "^01this will be in blue ^04but this will be in red"
if the letter = "&" then read in the three characters immediately after it, and print some value out. For example, &GPC will print the amount of gold your player is carrying. &NAM will print the character's name. &MON will print the name of the monster you're fighting. &NUM will print the number (n) that gets passed to the TT routine. This is useful for those random numbers, such as damage:

damagetaken = INT(RND * 10) + 1
TT "yourehit", damagetaken

and the file "yourehit" might look like this:

=================================
^04&MON ^07hits you for ^04&NUM ^07damage!
You have &HPC hit points remaining.
=================================

if the letter is anything other than "^" or "&" then print it to the screen without a carriage return.

4. at the end of each line, PRINT so that it goes to the next line
5. pause after each 21 lines (so that you can read text before it scrolls off the screen!)
6. continue reading lines until you reach the end of the file

Alright, now this may not be the fastest way to display text, but it gets ALL your text into external files, and it makes changing colors and displaying stats and so forth a real piece of cake!!

*peace*

Meg.
Reply
#2
p.s. i copied that code from my game, and changed some things because mine used routines to send text over a modem instead of to the screen. so there might be some silly syntax errors.

Meg.
Reply
#3
That routine works, but is not efficient, especially not for fast games.

Take a look at Corazon, at http://www.harsoftware.cjb.net which is a language utility. You can save text strings in them and read them. Without the HD rattling all the time when you print a page full of text.
Reply
#4
I would have to say , that reading from a file , and outputting something to screen isn't a very good procedure.
If you need speed , you will find yourself using ram rather then harddisk.
So my advise would be to store the whole file into ram before
doing anything to screen ,
Another thing is the way you are using the file
Using those 3char masks isn't very powerfull nor small
since you have somewhat like 9 cases , you would rather use
a byte , gives you 256 diffrent possibilities , so a binary file would be
much more efficient.

Now about those CASE's , it liturally means having a HUGE if , if else else block, in your program. making your cpu jump like an idiot.

You might want to create some structure , holding the values gotten from the file. So you eliminate the need of a file read every frame ?
Smile

Scorp.
Reply
#5
I certainly understand that this is not a very efficient way of coding this type of thing. However, for the purpose of just sending text to the screen, I don't notice any speed problems at all, even on my slow computer (pII 350).

*peace*

Meg.
Reply
#6
this was really code more for the ease with which you can create text for the game, more than for speed's sake Smile I like being able to write a text file and just throw in a &MON whenever I want the monster's name, etc. It lets me add so much to the game, quickly.

*peace*

Meg.
Reply
#7
For that^^, you could write some editor , that would allow you to edit the files and convert &MON , to a byte ,
then outputting the file in binary. This might speed up your games,
give some more protection , and allow you to re-use that editor in
some new game Smile .
Reply
#8
Quote:even on my slow computer (pII 350).

*Sigh* the times we live in... *goes back to playing super pacman on his cgi tandy*
i]"I know what you're thinking. Did he fire six shots or only five? Well, to tell you the truth, in all this excitement, I've kinda lost track myself. But being as this is a .44 Magnum ... you've got to ask yourself one question: 'Do I feel lucky?' Well, do ya punk?"[/i] - Dirty Harry
Reply
#9
What you're saying sounds interesting, but I don't think I have enough experience to implement it. Could you give me some more information on how to do something like this?

I would like to have the text go a bit faster since it's being sent over a modem, using the EasyDoor library.

*peace*

Meg.
Reply
#10
cgi tandy?
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)