My first project needs a li'l help

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
Zambaku
Newbie
Posts: 2
Joined: Sat Nov 19, 2016 10:49 am

My first project needs a li'l help

Post by Zambaku »

Hi everyone, nice to meet ya! I'm fairly new to qbasic and I'm doing my first "game" to figure things out and I must say it's alot of fun, hard but fun. Anyway, I'm doing a simple virtual pet, so far the things I've got down is a small selection of randomly chosen names of the "pet", as well as a randomly selected appearance. The first thing I'd like some pointers on is how do I make this creature move about on it's own? I have a vague idea about it involving a randomized timer but I can't get the hang of it, am I right in my assumption that I need to do something like this?:

Code: Select all

num3! = INT(RND * 3) + 1
IF num3! = 1 then LOCATE 18, 40
IF num3! = 2 then LOCATE 18, 40 + 1
IF num3! = 3 then LOCATE 18, 40 - 1 
[Yes, I know it's not the correct syntax at all, I'm just asking if my general idea is right]


This is what my code is so far:

Code: Select all

CLS

num1! = INT(RND * 10) + 1

PRINT "NAME:";
IF num1! = 1 THEN PRINT "BOB"
IF num1! = 2 THEN PRINT "JOE"
IF num1! = 3 THEN PRINT "RICHARD"
IF num1! = 4 THEN PRINT "WILL"
IF num1! = 5 THEN PRINT "QBERT"
IF num1! = 6 THEN PRINT "ROGER"
IF num1! = 7 THEN PRINT "SVEN"
IF num1! = 8 THEN PRINT "GUNTHER"
IF num1! = 9 THEN PRINT "BARNEY"
IF num1 = 10 THEN PRINT "FRED"

num2! = INT(RND * 10) + 1

LOCATE 18, 40 

IF num2! = 1 THEN PRINT "(`-`)"
IF num2! = 2 THEN PRINT "(^o^)"
IF num2! = 3 THEN PRINT "(-_-)"
IF num2! = 4 THEN PRINT "(>_<)"
IF num2! = 5 THEN PRINT "(T_T)"
IF num2! = 6 THEN PRINT "(X_X)"
IF num2! = 7 THEN PRINT "(x_X)"
IF num2! = 8 THEN PRINT "(~_~)"
IF num2! = 9 THEN PRINT "(`o`)"
IF num2! = 10 THEN PRINT "(p_-)"
Best regards // Zambaku
User avatar
burger2227
Veteran
Posts: 2466
Joined: Mon Aug 21, 2006 12:40 am
Location: Pittsburgh, PA

Re: My first project needs a li'l help

Post by burger2227 »

Try using SELECT CASE for multiple IF statements

Code: Select all

RANDOMIZE TIMER
num% = INT(RND * 10) + 1

SELECT CASE num%
  CASE 1: name$ = "Bob"
  CASE 2: name$ = "Joe"

END SELECT 
PRINT name$
By using a variable the program can display the name$ anywhere later.
Please acknowledge and thank members who answer your questions!
QB64 is a FREE QBasic compiler for WIN, MAC(OSX) and LINUX : https://www.qb64.org/forum/index.php
Get my Q-Basics demonstrator: https://www.dropbox.com/s/fdmgp91d6h8ps ... s.zip?dl=0
Erik
Veteran
Posts: 72
Joined: Wed Jun 20, 2007 12:31 pm
Location: LI, NY
Contact:

Re: My first project needs a li'l help

Post by Erik »

Expanding on the previous post, below is a way to go about making the pets randomly walk around the screen.

I used a TYPE struct to hold all the information related to the pet under one "animal" type. You could also just use independent variables if you wanted but I like keeping related variables housed together.

Comments in code. Written in QB4.5. (Never used QB64 so I don't know if they syntax is different)

Code: Select all

CLS

'arbritrary max screen positions
CONST MAXX = 40
CONST MAXY = 20

RANDOMIZE TIMER

'create type struct to hold pet info
TYPE animal
  petName AS STRING * 8
  gfxString AS STRING * 5
  x AS INTEGER
  y AS INTEGER
END TYPE


DIM pet AS animal

'set initial pet location
pet.x = 15
pet.y = 15

'random number used to define pet
num% = INT(RND * 4 + 1)

'define pet based on num%
SELECT CASE num%
  CASE 1:
    pet.petName = "BOB"
    pet.gfxString = "('-')"
  
  CASE 2:
    pet.petName = "Joe"
    pet.gfxString = "(^o^)"

  CASE 3:
    pet.petName = "RICHARD"
    pet.gfxString = "(-_-)"

  CASE 4:
    pet.petName = "WILL"
    pet.gfxString = "(>_<)"

  'etc..

END SELECT


'display pet name in upper left corner
LOCATE 1, 1
PRINT pet.petName

'move pet 15 times and quit.
FOR i% = 0 TO 15 STEP 1

  'mask old draw location with black text
  LOCATE pet.x, pet.y
  COLOR 0
  PRINT pet.gfxString
 

  'set potential new pet xy location.
  'pet new location can be +- 3 spots.

  pet.x = pet.x + (INT(RND * 6) - 3)
  pet.y = pet.y + (INT(RND * 6) - 3)

  'error checking to see if they went off screen
  IF (pet.x <= 0) THEN pet.x = 1
  IF (pet.x >= MAXX) THEN pet.x = MAXX - 3
  IF (pet.y <= 0) THEN pet.y = 1
  IF (pet.y >= MAXY) THEN pet.y = MAXY - 3

  'draw pet at new location
  LOCATE pet.x, pet.y
  COLOR 15
  PRINT pet.gfxString

  'wait a second before next loop
  SLEEP 1

NEXT i%

END

Zambaku
Newbie
Posts: 2
Joined: Sat Nov 19, 2016 10:49 am

Re: My first project needs a li'l help

Post by Zambaku »

Thanks, dude =)
Post Reply