Qbasicnews.com

Full Version: Couple Problems with Game Code
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I'm new, and this is probably a stupid question I could fix myself, but I'm making a small RPG-like game called Battle Thing (v1.0 can be found here), and I figured out how to easily add more characters/weapons to the game without using random numbers and going IF num% = soandso Or num% = soandso THEN: pname$ = "Dude Guy": etc.

Here's the code (char select):

Code:
TYPE playerchar
charname AS STRING * 100
charpic AS INTEGER
charcolor AS INTEGER
END TYPE

DIM playerchar(5) AS playerchar

FOR num = 1 TO 5
READ name$, picnum%, colornum%
  playerchar(num).charname = name$
  playerchar(num).charpic = picnum%
  playerchar(num).charcolor = colornum%
NEXT num

DATA "Tom The Trucker", 38, 14
DATA "Zamzix From Planet Googlyeyes", 37, 10
DATA "Ivan The Gullible", 2, 5
DATA "Spagetti Man", 36, 6
DATA "Guy with a Fork Lad", 2, 9

CLS
COLOR 15
PRINT STRING$(80, CHR$(196))
PRINT "Character Select"
PRINT STRING$(80, CHR$(196))

FOR listguys = 1 TO 5
  PRINT "[" + LTRIM$(STR$(listguys)) + ".] " + RTRIM$(LTRIM$(playerchar(listguys).charname)); " [";
  COLOR playerchar(listguys).charcolor
  PRINT "" + CHR$(playerchar(listguys).charpic) + "";
  COLOR 15
  PRINT "]"
NEXT listguys
PRINT STRING$(80, CHR$(196))
INPUT "Select: ", choice%

charname$ = playerchar(choice%).charname
pcolor% = playerchar(choice%).charcolor
ppic% = playerchar(choice%).charpic

The last three lines are the variables I want to transfer to the main program. Problem is this: I want to put this code in a sub so I can call it when I need to. However, when I put in this code in a SUB or FUNCTION, QBASIC yells at me saying I used an illegal function or a DEF FN (something along those lines), and it highlights the DATA statements. How do I fix this? Or should just make it all one big program and forget the SUBS? Because I was planning on doing something like this for the weapons and the opposition selection.

Also, when you get into the battle, when it's the opponent's turn, his actions are based on random numbers, but I think I did it poorly. Sometimes he attacks with his weapon twice in a row, which I don't want, and sometimes if he attacks twice with his weapons, it hits twice, causing too much damage to the player. So far I haven't had the latter, though. I, however, have had it happen on the player's side.

Also, the damages are randomized, but I plan to fix that.

Here's the code that executes when it's the opposition's turn:

Code:
badguyturn:

badguyturn% = RND * 9
bghitprcnt% = RND * 6
ernddmg% = INT(RND * 5) + 1
ebigdmg% = INT(RND * 30) + 10

COLOR bgcolor%
IF badguyturn% = 1 OR badguyturn% = 3 OR badguyturn% = 5 OR badguyturn% = 7 THEN
narccount% = 0
LOCATE 23, 2
PRINT bgname$ + " punches you! *POW!*"
php% = php% - ernddmg%
ELSEIF badguyturn% = 2 OR badguyturn% = 4 OR badguyturn% = 6 THEN
narccount% = 0
LOCATE 23, 2
PRINT bgname$ + " kicks you! *WHACK!*"
php% = php% - ernddmg%
ELSEIF badguyturn% = 8 THEN
narccount% = 0
LOCATE 23, 2
PRINT bgname$ + " swings their " + bgweapname$ + ", and";
IF bghitprcnt% = 3 AND hitcount% = 0 THEN
   PRINT " hits!"
   php% = php% - ebigdmg%
   hitcount% = 1
ELSE
  PRINT " misses!"
  hitcount% = 0
END IF
ELSEIF badguyturn% > 8 AND narccount% = 0 THEN
LOCATE 23, 2
PRINT bgname$ + " had a bout of narcolepsy and skipped their turn!"
narccount% = 1
ELSE
GOTO badguyturn
END IF

Lastly, how would I make a buy system? Some people suggested I use text files to store the data and then have the program get the data from there, but that still doesn't solve the problem on how to list the weapons one currently has and how to let the player use a weapon from that list.

I'm not good at explaining things, but any help would be appreciated. Thanx.

EDIT: Oh, one thing I forgot to mention. It seems that the only way to win the game is to hit the opponent with your weapon at least once. Is there a way I can fix that? The player attack code basically runs on the same principles, except with the badguyturn% procedure.

Anonymous

just scanned over your code, im a lil busy atm to test and debug, couple hints:


first off, try putting the first codes in s ub, but leave the DATA statements in the module level. and DIM SHARED playerchar [etc...]


then, for the second code, i noticed the first two ranoms dont do int(rnd) =! (you know what i mean)

make them all int(rnd) + 1, and try that. hope it helps. if not ill look at it further in depth later

peace
I apologize...I don't quite understand what you mean by "module level." Do you mean put the DATA statements in the main program?

As for the other suggestion, I'll try that. But I think there was a reason I didn't do INT(RND * blah) + 1 on the badguyturn and badguyhitprcnt variables. Don't remember what it was, though.

Anonymous

module level is anything thats not a sub, or a func.

heres some code to do what you want to take most of the other code out of the main modukle and putit in a 'sub' (actually a function, so we can get a return value; the value selected by the player for the character)



Code:
DECLARE FUNCTION SelectChar! ()
TYPE playerchar
charname AS STRING * 100
charpic AS INTEGER
charcolor AS INTEGER
END TYPE

DIM SHARED playerchar(5) AS playerchar


choice% = SelectChar

charname$ = playerchar(choice%).charname
pcolor% = playerchar(choice%).charcolor
ppic% = playerchar(choice%).charpic


PRINT
PRINT "you picked "
PRINT charname$


DATA "Tom The Trucker", 38, 14
DATA "Zamzix From Planet Googlyeyes", 37, 10
DATA "Ivan The Gullible", 2, 5
DATA "Spagetti Man", 36, 6
DATA "Guy with a Fork Lad", 2, 9

FUNCTION SelectChar


FOR num = 1 TO 5
READ name$, picnum%, colornum%
  playerchar(num).charname = name$
  playerchar(num).charpic = picnum%
  playerchar(num).charcolor = colornum%
NEXT num

CLS
COLOR 15
PRINT STRING$(80, CHR$(196))
PRINT "Character Select"
PRINT STRING$(80, CHR$(196))

FOR listguys = 1 TO 5
  PRINT "[" + LTRIM$(STR$(listguys)) + ".] " + RTRIM$(LTRIM$(playerchar(listguys).charname)); " [";
  COLOR playerchar(listguys).charcolor
  PRINT "" + CHR$(playerchar(listguys).charpic) + "";
  COLOR 15
  PRINT "]"
NEXT listguys
PRINT STRING$(80, CHR$(196))
INPUT "Select: ", cho%

SelectChar = cho%


END FUNCTION

as you can see, the TYPE, the DIM SHARED and the DATA are not in the function itself. (putting this code right into qb might be easier for you to understand what it looks like in a func...)



as for your second code, its more suited to a select case. observe working code:




Code:
badguyturn:

CLS
RANDOMIZE TIMER

bgcolor% = 4
badguyturn% = INT(RND * 9) + 1
bghitprcnt% = INT(RND * 6) + 1
ernddmg% = INT(RND * 5) + 1
ebigdmg% = INT(RND * 30) + 10

COLOR bgcolor%

SELECT CASE badguyturn%

  CASE 1, 3, 5, 7

    LOCATE 23, 2
    PRINT bgname$ + " punches you! *POW!*"

    php% = php% - ernddmg%

  CASE 2, 4, 6
    LOCATE 23, 2
    PRINT bgname$ + " kicks you! *WHACK!*"

    php% = php% - ernddmg%

  CASE 8
    LOCATE 23, 2
    PRINT bgname$ + " swings their " + bgweapname$ + ", and";

    IF bghitprcnt% = 3 THEN
      PRINT " hits!"

      php% = php% - ebigdmg%

    ELSE
      PRINT " misses!"

    END IF

  CASE 9
    LOCATE 23, 2
    PRINT bgname$ + " had a bout of narcolepsy and skipped their turn!"

END SELECT


=) just a note, when you start using all those hacks like you were such as 'hitcount, narccount', you should realize that theres deeper problems that need to be addressed
Thanks. The second set of code looks a lot cleaner.

I'll be sure to integrate that in to my code and give you credits if needbe. Big Grin

As for the narccount and hitcount, those were to prevent the opposition from using their weapon twice (and hitting twice) in a row, and from skipping their turn twice in a row. It worked most of the time.