game problem

If you have questions about any aspect of QBasic programming, or would like to help fellow programmers solve their problems, check out this board!

Moderators: Pete, Mods

Post Reply
iPires
Newbie
Posts: 9
Joined: Mon Jul 02, 2007 1:54 pm

game problem

Post by iPires »

hi
i made a small game (im new at this) where the player is in a square field
and he has to get to the end
and monsters randomly appear

but my problem is that when a monster appear, the battle text appears
and then when the battle is over
it returns to the field. but when it returns to the field the player is on the starting position, so its impossible to get to the end

i need help making the player return to the position where he was before the battle text appears

PS: here is what i made so that monsters randomly appear

Code: Select all

 IF INT(RND * 10) = 9 THEN GOTO begin
i dunno if the problem is here but if it is plz tell me
oh. another thing. the game is all in text. the player is the CHR$(1) :P
........
'T......\
.I.......)
.I....../
.I
,I
Patz QuickBASIC Creations
Veteran
Posts: 399
Joined: Wed Mar 02, 2005 9:01 pm
Location: Nashville, Tennessee
Contact:

Post by Patz QuickBASIC Creations »

Post what happens after the label BEGIN. That's probably around where your problem is.
iPires
Newbie
Posts: 9
Joined: Mon Jul 02, 2007 1:54 pm

Post by iPires »

begin is when the fight starts or the player goes back to the game

Code: Select all

begin:
playerhealth = 10
monsterhealth = 10
COLOR 4
PRINT "Um MONSTRO ATACOU-TE!"
COLOR 1
PRINT "(L)UTA, OU (F)OGE?"
DO
k$ = INKEY$
IF k$ = "f" OR k$ = "F" THEN GOTO wilderness
IF k$ = "l" OR k$ = "L" THEN GOTO fight
........
'T......\
.I.......)
.I....../
.I
,I
Patz QuickBASIC Creations
Veteran
Posts: 399
Joined: Wed Mar 02, 2005 9:01 pm
Location: Nashville, Tennessee
Contact:

Post by Patz QuickBASIC Creations »

OK. Try this. Say your player was located at the points (X%,Y%). Going by the other program you posted, you may have placed those default values into the start of a loop? Ex:

Code: Select all

DO
X% = 3
Y% = 2
'gameplay code
'more
'etc
LOOP
Place those default values outside of the loop. Also make sure you don't modify (X%, Y%) other than to move the character.
iPires
Newbie
Posts: 9
Joined: Mon Jul 02, 2007 1:54 pm

Post by iPires »

still doesnt work
ill poste the whole code

ps: again, all the print and input stuff are in portuguese, my language, and cause i like to show the games to my lil brother :P

EDIT: dunno why the code was all messed up after i pressed submit :?
here it is
EDIT2: ????!?! the code continues messed up!!
but anyway, i think that the parts that dont appear are just the keys to make it move or sumthing like that ...

wilderness:
DIM wild$(15)
wild$(1) = "|---------------*---------------|"
wild$(2) = "|......T...T.......TT......T....|"
wild$(3) = "|...T......T......T............T|"
wild$(4) = "|....T..........T....T.......T..|"
wild$(5) = "|T......T.....TT.......T........|"
wild$(6) = "|...............................|"
wild$(7) = "|...T.............TT............|"
wild$(8) = "|...............................|"
wild$(9) = "|..T........T..T.......T....T...|"
wild$(10) = "|.....T..........T.......T......|"
wild$(11) = "|...............................|"
wild$(12) = "|....TT.....T............T......|"
wild$(13) = "|...............................|"
wild$(14) = "|...T.............T....T......T.|"
wild$(15) = "|-------------------------------|"

row = 14
column = 3

LOCATE 17, 1
PRINT "EST?S NA FLORESTA."
PRINT "OS MONSTROS VIVEM AQUI."
PRINT "PODES TER DE ENFRENTAR ALGUNS..."

DO
CLS
LOCATE 1, 1
FOR count = 1 TO 15
PRINT wild$(count)
NEXT count
LOCATE row, column
PRINT CHR$(1)
oldRow = row
oldColumn = column

IF INT(RND * 10) = 9 THEN GOTO begin

DO
keyed$ = INKEY$
LOOP UNTIL keyed$ <SELECT>= 3 THEN
PRINT "O MONSTRO FOI ATINGIDO!"
monsterhealth = monsterhealth - 1
END IF

IF playerattack < 3 THEN PRINT "O JOGADOR FALHA O ATAQUE!"

DO
k$ = INKEY$
LOOP UNTIL k$ <PRINT>= 3 THEN
PRINT "O JOGADOR FOI ATINGIDO!"
playerhealth = playerhealth - 1
END IF

IF monsterattack < 3 THEN PRINT "O MONSTRO FALHA O ATAQUE!"

DO
k$ = INKEY$
LOOP UNTIL k$ <> ""

CLS
PRINT "JOGADOR: "; playerhealth
PRINT "MONSTRO: "; monsterhealth
IF monsterhealth = 0 THEN PRINT "JOGADOR GANHA O COMBATE!": GOTO wilderness
IF playerhealth = 0 THEN PRINT "JOGADOR PERDE O COMBATE!": END

LOOP
........
'T......\
.I.......)
.I....../
.I
,I
Ralph
Veteran
Posts: 148
Joined: Fri Feb 09, 2007 3:10 pm
Location: Katy, Texas

Post by Ralph »

I've changed your program somewhat. See if you like what I've got, and post the changes or additions you want to make to it, after trying it out in QuickBASIC. I know it is very incomplete, Here's the code:

Code: Select all

'MONSTERS, BY iPires                       '''New line by RAE
CLS                                        '''New line by RAE
''' = REMarks by RAE

wilderness:
DIM wild$(15)
 wild$(1) = "|---------------*---------------|"
 wild$(2) = "|......T...T.......TT......T....|"
 wild$(3) = "|...T......T......T............T|"
 wild$(4) = "|....T..........T....T.......T..|"
 wild$(5) = "|T......T.....TT.......T........|"
 wild$(6) = "|...............................|"
 wild$(7) = "|...T.............TT............|"
 wild$(8) = "|...............................|"
 wild$(9) = "|..T........T..T.......T....T...|"
wild$(10) = "|.....T..........T.......T......|"
wild$(11) = "|...............................|"
wild$(12) = "|....TT.....T............T......|"
wild$(13) = "|...............................|"
wild$(14) = "|...T.............T....T......T.|"
wild$(15) = "|-------------------------------|"

row = 14
column = 3
monsterhealth = 10
playerhealth = 10


DO
  CLS
  LOCATE 1, 1
  FOR count = 1 TO 15
    PRINT wild$(count)
  NEXT count
  LOCATE row, column
  PRINT CHR$(1)
  oldRow = row
  oldColumn = column

  LOCATE 16
  PRINT " EST?S NA FLORESTA.  OS MONSTROS VIVEM AQUI."
  PRINT " PODES TER DE ENFRENTAR ALGUNS..."
'''IF INT(RND * 10) = 9 THEN GOTO begin    '''Where is the label begin?

  LOCATE 19
  PRINT " Enter a number from 1 to 2, or press Esc to end game.  ";
  DO
    k$ = INKEY$
  LOOP UNTIL k$ <> ""
  IF k$ = CHR$(27) THEN END
  k = VAL(k$)
  IF k = 1 THEN
    PRINT "  O MONSTRO FOI ATINGIDO!"
    monsterhealth = monsterhealth - 1
    IF monsterattack < 3 THEN
      PRINT "  O MONSTRO FALHA O ATAQUE!"
    END IF
  END IF
 
  IF k = 2 THEN
    PRINT "  O JOGADOR FOI ATINGIDO!"
    playerhealth = playerhealth - 1
    IF playerattack < 3 THEN
      PRINT "  O JOGADOR FALHA O ATAQUE!"
    END IF
  END IF


  PRINT
  PRINT " JOGADOR: "; playerhealth
  PRINT " MONSTRO: "; monsterhealth
  IF monsterhealth = 0 THEN PRINT "JOGADOR GANHA O COMBATE!": GOTO wilderness
  IF playerhealth = 0 THEN PRINT "JOGADOR PERDE O COMBATE!": END
  PRINT " Press any key to continue"
  WHILE INKEY$ = "": WEND
LOOP
Ralph, with QuickBASIC 4.5, operating under Windows XP, wiht anHP LaserJet 4L Printer. Bilingual in English/Spanish
iPires
Newbie
Posts: 9
Joined: Mon Jul 02, 2007 1:54 pm

Post by iPires »

i tried those changes but still
doesnt work ...

dunno why before i couldnt post the whole code
but now i will post it
-------------------------

this game is giving me only troubles, maybe i should just start making one from the begining? :?
-------------------------
EDIT:
forget it ...
i cant post the whole code
ill just do nother game or sumthing
........
'T......\
.I.......)
.I....../
.I
,I
Ralph
Veteran
Posts: 148
Joined: Fri Feb 09, 2007 3:10 pm
Location: Katy, Texas

Post by Ralph »

iPeres:

The main problem I see is that we don't really know just what you are trying to accomplish with your code. I would strongly recommend that you use lots of comments as you code, to tell yourself - and us - just what each peice of code is supposed to do. With that kind of information, I'm sure many of us here could point out your problems.

In other words, use comments to document everything you want to do, just before the code. Give it a try.
Ralph, with QuickBASIC 4.5, operating under Windows XP, wiht anHP LaserJet 4L Printer. Bilingual in English/Spanish
iPires
Newbie
Posts: 9
Joined: Mon Jul 02, 2007 1:54 pm

Post by iPires »

okay

wilderness: 'ok so heres the field
DIM wild$(15)
wild$(1) = "|---------------*---------------|"
wild$(2) = "|......T...T.......TT......T....|"
wild$(3) = "|...T......T......T............T|"
wild$(4) = "|....T..........T....T.......T..|"
wild$(5) = "|T......T.....TT.......T........|"
wild$(6) = "|...............................|"
wild$(7) = "|...T.............TT............|"
wild$( = "|...............................|"
wild$(9) = "|..T........T..T.......T....T...|"
wild$(10) = "|.....T..........T.......T......|"
wild$(11) = "|...............................|"
wild$(12) = "|....TT.....T............T......|"
wild$(13) = "|...............................|"
wild$(14) = "|...T.............T....T......T.|"
wild$(15) = "|-------------------------------|"

row = 14 'this tells qhere the player is going to be when the game starts
column = 3 'same thing

LOCATE 17, 1 'this says that ur in the forest and that there are monsters
PRINT "EST?S NA FLORESTA." 'same
PRINT "OS MONSTROS VIVEM AQUI." 'same
PRINT "PODES TER DE ENFRENTAR ALGUNS..." 'same

DO 'starts the loop
CLS
LOCATE 1, 1
FOR count = 1 TO 15 'prints the map
PRINT wild$(count)
NEXT count
LOCATE row, column
PRINT CHR$(1) 'prints the player
oldRow = row
oldColumn = column

IF INT(RND * 10) = 9 THEN GOTO begin 'this makes monster appear randomly

DO
keyed$ = INKEY$
LOOP UNTIL keyed$ <> ""

SELECT CASE keyed$ 'makes the player move up, down, left, right
CASE IS = "w"
row = row - 1
CASE IS = "s"
row = row + 1
CASE IS = "a"
column = column - 1
CASE IS = "d"
column = column + 1
END SELECT

SELECT CASE MID$(wild$(row), column, 1)
CASE IS = "T"
row = oldRow
column = oldColumn
CASE IS = "-"
row = oldRow
column = oldColumn
CASE IS = "|"
row = oldRow
column = oldColumn
CASE IS = "*" 'ends the game
PRINT "CHEGASTE AO DESTINO!"
PRINT "JOGO FEITO POR JO?O PIRES"
END
END SELECT
LOOP UNTIL keyed$ = "q"

begin: 'the fight menu
playehealth = 10
monsterhealth = 10
COLOR 4
PRINT "UM MONSTRO ATACOU-TE!"
COLOR 1
PRINT "(L)UTA, OU (F)OGE?"
DO
k$ = INKEY$
IF k$ = "f" OR k$ = "F" THEN GOTO wilderness 'if the player wants to run away from the fight
IF k$ = "l" OR k$ = "L" THEN GOTO fight 'if the player wants to fight
LOOP


fight: 'the fight
DO
COLOR 15
PRINT "O JOGADOR ATACA!" 'the player attacks
playerattack = INT(RND * 10)
IF playerattack >= 3 THEN
PRINT "O MONSTRO FOI ATINGIDO!" 'the monster was attacked
monsterhealth = monsterhealth - 1
END IF

IF playerattack <3>= 3 THEN
PRINT "O JOGADOR FOI ATINGIDO!" 'the player was attacked
playerhealth = playerhealth - 1
END IF

IF monsterattack < 3 THEN PRINT "O MONSTRO FALHA O ATAQUE!" 'the monster misses the attack

DO
k$ = INKEY$
LOOP UNTIL k$ <> ""

CLS
PRINT "JOGADOR: "; playerhealth
PRINT "MONSTRO: "; monsterhealth
IF monsterhealth = 0 THEN PRINT "JOGADOR GANHA O COMBATE!": 'the player wins the fight and it goes back to the forest
GOTO wilderness
IF playerhealth = 0 THEN PRINT "JOGADOR PERDE O COMBATE!": END 'the player loses the fight and the game ends

LOOP

so here it is
the full code and with all the 'xxxxxxxxxxxxxxxxxx things
:D
........
'T......\
.I.......)
.I....../
.I
,I
Ralph
Veteran
Posts: 148
Joined: Fri Feb 09, 2007 3:10 pm
Location: Katy, Texas

Post by Ralph »

iPeres:

I've done some minor corrections and improvements, shown with '''. You must now copy it and run it, then post where an error or malfunction occurs, so that we may help you further. Until you do this, I will not respond any more.

Code: Select all

DIM wild$(15)
 wild$(1) = "|---------------*---------------|"
 wild$(2) = "|......T...T.......TT......T....|"
 wild$(3) = "|...T......T......T............T|"
 wild$(4) = "|....T..........T....T.......T..|"
 wild$(5) = "|T......T.....TT.......T........|"
 wild$(6) = "|...............................|"
 wild$(7) = "|...T.............TT............|"
 wild$(8 ) = "|...............................|"                 '''was missing 8 )
 wild$(9) = "|..T........T..T.......T....T...|"
wild$(10) = "|.....T..........T.......T......|"
wild$(11) = "|...............................|"
wild$(12) = "|....TT.....T............T......|"
wild$(13) = "|...............................|"
wild$(14) = "|...T.............T....T......T.|"
wild$(15) = "|-------------------------------|"

row = 14 'this tells where the player is going to be when the game starts
column = 3 'same thing

LOCATE 17, 1 'this says that your in the forest and that there are monsters
PRINT "EST?S NA FLORESTA." 'same
PRINT "OS MONSTROS VIVEM AQUI." 'same
PRINT "PODES TER DE ENFRENTAR ALGUNS..." 'same
 
DO 'starts the loop
  CLS
  LOCATE 1, 1
  FOR count = 1 TO 15 'prints the map
    PRINT wild$(count)
  NEXT count
  LOCATE row, column
  PRINT CHR$(1) 'prints the player
  oldRow = row
  oldColumn = column

  IF INT(RND * 10) = 9 THEN GOTO begin 'this makes monster appear randomly

  LOCATE 21, 1
  '''added next line:
  PRINT " To move, press: w(up), s(down), a(left), d(right), or q to quit"
  DO
    keyed$ = LCASE$(INKEY$)             '''added LCASE$
  LOOP UNTIL keyed$ <> ""
  SELECT CASE keyed$ 'makes the player move up, down, left, right
  CASE IS = "w"
    row = row - 1
  CASE IS = "s"
    row = row + 1
  CASE IS = "a"
    column = column - 1
  CASE IS = "d"
    column = column + 1
  '''added new CASE:
  CASE IS = "q"
    END
  END SELECT

  SELECT CASE MID$(wild$(row), column, 1)
  CASE IS = "T"
    row = oldRow
    column = oldColumn
  CASE IS = "-"
    row = oldRow
    column = oldColumn
  CASE IS = "|"
    row = oldRow
    column = oldColumn
  CASE IS = "*" 'ends the game
    '''added next line:
    LOCATE 22, 1
    PRINT "CHEGASTE AO DESTINO!"      '''You reached your goal
    PRINT "JOGO FEITO POR JO?O PIRES" '''Game made by Jo?o Pires
  '''remarked out next line:
  'END
  END SELECT
LOOP UNTIL keyed$ = "q"


begin: 'the fight or flee menu
playerhealth = 10                       '''was playehealth
monsterhealth = 10

COLOR 12                                '''was COLOR 4
'''added next line
LOCATE 18, 1
PRINT "UM MONSTRO ATACOU-TE!"
COLOR 10                                 '''was COLOR 1
PRINT "(L)UTA, OU (F)OGE?"
DO
  k$ = LCASE$(INKEY$)
  IF k$ = "l" THEN GOTO fight 'if the player wants to fight
  '''following line remarked out, as the label wilderness was not found  
  'IF k$ = "f" THEN GOTO wilderness 'if the player wants to run away from the fight
LOOP


fight: 'the fight
DO
  COLOR 15
  PRINT "O JOGADOR ATACA!" 'the player attacks
  playerattack = INT(RND * 10)
  IF playerattack >= 3 THEN
    PRINT "O MONSTRO FOI ATINGIDO!" 'the monster was attacked
    monsterhealth = monsterhealth - 1
  END IF

  '''changed this line to the next: IF playerattack <3>= 3 THEN
  IF playerattack < 3 THEN
    PRINT "O JOGADOR FOI ATINGIDO!" 'the player was attacked
    playerhealth = playerhealth - 1
  END IF

  IF monsterattack < 3 THEN PRINT "O MONSTRO FALHA O ATAQUE!" 'the monster misses the attack

  '''added next line:
  PRINT " Enter any key to continue"
  DO
    k$ = INKEY$
  LOOP UNTIL k$ <> ""

  CLS
  PRINT "JOGADOR: "; playerhealth
  PRINT "MONSTRO: "; monsterhealth
  IF monsterhealth <= 0 THEN PRINT "JOGADOR GANHA O COMBATE!": 'the player wins the fight and it goes back to the forest
  '''following line remarked out, as the label wilderness was not found  
  'GOTO wilderness
  IF playerhealth <= 0 THEN PRINT "JOGADOR PERDE O COMBATE!": END 'the player loses the fight and the game ends

LOOP
Ralph, with QuickBASIC 4.5, operating under Windows XP, wiht anHP LaserJet 4L Printer. Bilingual in English/Spanish
Erik
Veteran
Posts: 72
Joined: Wed Jun 20, 2007 12:31 pm
Location: LI, NY
Contact:

Post by Erik »

Also, you could try using SUBs or GOSUBs instead of GOTO label markers. This might prevent "fall through" that could lead to strange and hard to track errors...
rgreenlaw
Newbie
Posts: 9
Joined: Mon Aug 06, 2007 1:20 pm

Help for entering your code.

Post by rgreenlaw »

When you placed your code in the posting you entered it within the text box. Try the following to make it easier to read. Press the button above marked Code before inserting the code and pressing it again after you have finished. The difference will make the code change from this:

wild$(8)="demo"

to this:

Code: Select all

wild$(8)="demo"
This will make your code easier for others to read, copy and test.

You will then get more meaningful replies. Obviously the missing 8 was due to the insertion of emoticons :P (smilies) within your posting.
Roger Greenlaw
Post Reply