New to QBASIC, I have a question.

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
User avatar
Austin
Coder
Posts: 36
Joined: Wed Aug 01, 2007 6:09 am
Location: Colorado
Contact:

New to QBASIC, I have a question.

Post by Austin » Wed Aug 01, 2007 6:24 am

Hello Everyone!
I'm trying to get a GOTO to work.

Code: Select all

PRINT "Logon Name"; : INPUT L$
IF L$ = "austin" THEN
PRINT "Hello Austin."
ELSE PRINT "Logon Name Incorrect."
GOTO 1
END IF
Everything is fine But It won't go back to Line 1. I also tried for it to go to line 10, that didn't work. Help would be great, I'm using QBASIC 1.1 .

Regards,
Austin

Anonymous

Post by Anonymous » Wed Aug 01, 2007 8:24 am

Hi,

QBasic does not automatically add the line numbers such as the BASIC on the commodore 64. You need to put them there yourself.

Code: Select all

1 PRINT "Logon Name"; : INPUT L$
IF L$ = "austin" THEN
    PRINT "Hello Austin."
ELSE 
    PRINT "Logon Name Incorrect."
    GOTO 1
END IF
Though I would recommend heavily against using GOTO ever, but its up to you. To do it without GOTO it would look something like this.

Code: Select all

CorrectName = 0
DO
    PRINT "Logon Name"; : INPUT L$
    IF L$ = "austin" THEN
        PRINT "Hello Austin."
        CorrectName = 1
    ELSE 
        PRINT "Logon Name Incorrect."
    END IF
LOOP UNTIL CorrectName = 1
Good luck with QBasic, hope you have fun :D

User avatar
sid6.7
Veteran
Posts: 318
Joined: Tue Jun 21, 2005 8:51 am
Location: west USA
Contact:

Post by sid6.7 » Wed Aug 01, 2007 8:38 am

generally myself if i use goto i try to use a more descriptive
goto...this helps with debugging something...and general
reading of your code by yourself or someone else.


like

Code: Select all

GOTO BEGIN
when using GOTO you'll want to remember to do it this way

Code: Select all

begin:

blah
blah
blah

GOTO begin

User avatar
Austin
Coder
Posts: 36
Joined: Wed Aug 01, 2007 6:09 am
Location: Colorado
Contact:

Post by Austin » Wed Aug 01, 2007 3:08 pm

Thanks Nixon!

The first option did great. I also tried the second option, it gets the j ob done but It feels like a lot more writing.

Regards,
Austin
Last edited by Austin on Thu Aug 02, 2007 3:09 am, edited 2 times in total.

moneo
Veteran
Posts: 451
Joined: Tue Jun 28, 2005 7:00 pm
Location: Mexico City, Mexico

Re: New to QBASIC, I have a question.

Post by moneo » Wed Aug 01, 2007 7:19 pm

Austin wrote:Hello Everyone!
I'm trying to get a GOTO to work.

Code: Select all

PRINT "Logon Name"; : INPUT L$
IF L$ = "austin" THEN
PRINT "Hello Austin."
ELSE PRINT "Logon Name Incorrect."
GOTO 1
END IF
Regards,
Austin
Hi Austin, welcome to the forum.

A little suggestion. When you're reading a name, you never know whether the user will type it using lower case, upper case, or maybe mixed. So, convert the input name to upper case, like:
INPUT L$
L$ = UCASE$(L$)

Then do the compare using upper case also, like:
IF L$ = "AUSTIN" THEN

This approach could save you problems.

Regards..... Moneo

User avatar
Austin
Coder
Posts: 36
Joined: Wed Aug 01, 2007 6:09 am
Location: Colorado
Contact:

Post by Austin » Thu Aug 02, 2007 2:53 am

Thanks moneo, I had been wondering how to do that for a while now!

Well after knowing how to do GOTO's correctly I have made my first part of my RPG called Munshkin. This is my first ever program in QBASIC 1.1! It is so far a little over 110 lines of code.

Please could some of you guys take a look, I'm not sure if it will open in 4.5 If i coded in 1.1 . So you might have to get 1.1 if you want to look.
Anyways it would be great if some of you could take a look and tell me ways of shortening my code because I'm sure I'm taking the long route.

I'm not sure how to get files linked with forums so I just hosted my .bas file at a free file hosting network.

http://www.mediafire.com/?0udmtdyjlvm

Regards,
Austin

rgreenlaw
Newbie
Posts: 9
Joined: Mon Aug 06, 2007 1:20 pm

Program works in QB4.5

Post by rgreenlaw » Mon Aug 06, 2007 4:15 pm

I just downloaded and ran your code in QB 4.5 and it works. You have a potential problem for expanding your code. As your program gets larger you will keep expanding the code by about 10 lines for each move. This program was written as if you never plan to move very far.

For screen appearance you might try the following suggestions:

1. Put the screen in an array
2. Use locate to position the cursor at the top left of the screen and re-print the screen after the player moves
3. Use something other than X as the solid locations. I personally like to use character codes 176, 177, 178 or 219. 219 is a solid block and 176-178 are shades of gray.
4. Use blank spaces or chr$(7) (dot centered in space) to represent places the player can move to.

These suggestions will make the screen look a little less cluttered. Also, you will be able to be a little creative with your screen design if you consider the following idea.

XXXXX
XXOXX
XXOXX
XXOXX
XXUXX
XXXXX
XXXXX
XXXXX

could be presented as

XXXXX
-X-X-
-X-X-
XX-XX
-XUX-
-XXX-
X----X
X-----

This posting doesn't look good, try cutting and pasting into a Qbasic file and opening it to see the effect. Also removing the - would have the effect I was trying to show.
Opens up the unused spaces if the player can't get there. This would allow the player to move onto this screen from another screen and be blocked needing to retrace his steps to find the correct path to another area.

It looks like you have a good start, I'd be interested in seeing what it becomes.

Best Regards,
Roger Greenlaw

User avatar
Austin
Coder
Posts: 36
Joined: Wed Aug 01, 2007 6:09 am
Location: Colorado
Contact:

Post by Austin » Tue Aug 07, 2007 1:38 am

Thanks! I added the new wall map layout.

Go check out the latest version-
http://www.petesqbsite.com/forum/viewto ... 4618#14618
(at bottom of page)

-Austin

rgreenlaw
Newbie
Posts: 9
Joined: Mon Aug 06, 2007 1:20 pm

Looks good!

Post by rgreenlaw » Tue Aug 07, 2007 5:02 pm

I viewed your program yesterday, but don't see my posting about it here. I'll try again.

I think the solid walls improve the program appearance, but did you do anything about the screen data?

Try this for an example:

To load the screen into an array

Code: Select all

dim scr$(10,10) ' create an array to hold data
for x = 1 to 10
  for y = 1 to 10
    read s$
    if len(s$) > 1 then scr$(x,y) = chr(val(s$)) else scr$(x,y) = s$
  next y
next x

data 179,179,179,179,179,179,179,179,179,179
data 179,032,032,032,032,G  ,032,032,032,032
data 179,032,032,032,032,032,032,032,032,179
data 179,032,032,032,032,032,032,032,032,179
data 179,032,032,032,032,032,032,032,032,179
data 179,032,032,032,032,032,032,032,032,179
data 179,032,032,032,032,032,032,032,032,179
data 179,032,032,032,032,032,032,032,032,179
data 179,032,032,032,032,P  ,032,032,032,179
data 179,179,179,179,179,179,179,179,179,179

To display the screen

Code: Select all


for y=1 to 10
  for x = 1 to 10
    print scr$(x,y);
    if scr$(x,y) = "P" then px = x:py=y
  next x
  print " "
next y


To move the player in any direction:

Code: Select all

gameover = 0
while gameover = 0
input "Enter your move ";m$

m$=ucase$(left$(m$,1))

select case m$
  case "N"
    if y >1  then             ' player is moving off-screen
    if scr$(x,y-1) = " " then 
      swap scr$(x,y-1),scr$(x,y)
    end if
    if scr$(x,y-1) = "G" then 
      gosub general
    end if
    end if
  case "S"
    if y <10 then           ' check player going off screen
      if scr$(x,y+1) = " " then 
        swap scr$(x,y+1),scr(x,y)
      end if
      if scr$(x,y+1) = "G" then 
        gosub general
      end if
    end if
  case "E"
    if x > 1  then 
      if scr(x-1,y) = " " then 
          swap scr$(x-1,y),scr$(x,y)
      end if  
      if scr(x+1,y) = "G" then 
        gosub general 
      end if
   end if
  case "W"
    if x < 10 then
      if scr$(x+1,y) = " " then 
         swap scr$(x+1,y), scr$(x.y)
      end if
      if scr$(x+1y) = "G" then
        gosub general
      end if
   end if

  case else ' to be done if no cases above match
    print "I don't understand that move"
end select
wend


system
general:
  put code for processing general interaction with player here!
return

' this part could also be coded as a subroutine!

This will allow movement in any direction and won't require a new set of prints (10 lines) for each move. You could even move the general randomly on the screen using this because you wouldn't need to know exactly where he is, only that he is on the screen.
Roger Greenlaw

rgreenlaw
Newbie
Posts: 9
Joined: Mon Aug 06, 2007 1:20 pm

Put it all together

Post by rgreenlaw » Tue Aug 07, 2007 5:53 pm

When you put it all together (and fix syntax errors) you get the following:

Code: Select all

DIM scr$(10, 10)' create an array to hold data
FOR x = 1 TO 10
  FOR y = 1 TO 10
    READ s$
    IF LEN(s$) > 1 THEN scr$(x, y) = CHR$(VAL(s$)) ELSE scr$(x, y) = s$
  NEXT y
NEXT x

DATA 177,177,177,177,177,177,177,177,177,177
DATA 177,032,032,032,032,032,032,032,032,177
DATA 177,032,177,177,177,177,177,177,032,177
DATA 177,032,177,032,G  ,177,032,177,032,177
DATA 177,032,177,032,032,177,032,032,032,177
DATA 177,032,032,032,177,177,032,032,177,177
DATA 177,177,177,177,177,032,032,032,032,177
DATA 177,032,032,032,032,032,032,032,177,177
DATA 177,032,032,032,032,P  ,032,032,032,177
DATA 177,177,177,177,177,177,177,177,177,177






gameover = 0
WHILE gameover = 0
FOR x = 1 TO 10
  FOR y = 1 TO 10
    PRINT scr$(x, y);
    IF scr$(x, y) = "P" THEN px = x: py = y
  NEXT y
  PRINT " "
NEXT x

x = px: y = py

INPUT "Enter your move "; m$

m$ = UCASE$(LEFT$(m$, 1))

SELECT CASE m$
  CASE "W"
    IF y > 1 THEN             ' player is moving off-screen
    IF scr$(x, y - 1) = " " THEN
      SWAP scr$(x, y - 1), scr$(x, y)
    END IF
    IF scr$(x, y - 1) = "G" THEN
      GOSUB general
    END IF
    END IF
  CASE "E"
    IF y < 10 THEN          ' check player going off screen
      IF scr$(x, y + 1) = " " THEN
        SWAP scr$(x, y + 1), scr$(x, y)
      END IF
      IF scr$(x, y + 1) = "G" THEN
        GOSUB general
      END IF
    END IF
  CASE "N"
    IF x > 1 THEN
      IF scr$(x - 1, y) = " " THEN
          SWAP scr$(x - 1, y), scr$(x, y)
      END IF
      IF scr$(x - 1, y) = "G" THEN
        GOSUB general
      END IF
   END IF
  CASE "S"
    IF x < 10 THEN
      IF scr$(x + 1, y) = " " THEN
         SWAP scr$(x + 1, y), scr$(x, y)
      END IF
      IF scr$(x + 1, y) = "G" THEN
        GOSUB general
      END IF
   END IF
  CASE "Q"
    SYSTEM

  CASE ELSE ' to be done if no cases above match
    PRINT "I don't understand that move"
END SELECT
WEND


SYSTEM
general:
  gameover = 1
RETURN

' this part could also be coded as a subroutine!

This will actually run in QB4.5 You might need to make some minor modifications to run it under qb 1.1 or FreeBasic.
Roger Greenlaw

Post Reply