GAME 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
Quicky
Coder
Posts: 20
Joined: Wed Jun 13, 2007 10:51 am

GAME HELP

Post by Quicky »

I am writing my first game and i have the key controls finished and the AI finished the two things i cannot firgure out is 1. How to make graphics that you have draw(in screen 7) move by key controls and 2. how to make the chararter not got through a border of a level (ex. a line) and then erase it. I want it to not be able to go through the line but i cant firgure it out how someone please help
historian
Newbie
Posts: 4
Joined: Sat Jun 16, 2007 12:11 pm

Re: GAME HELP

Post by historian »

Quicky wrote:I am writing my first game and i have the key controls finished and the AI finished the two things i cannot firgure out is 1. How to make graphics that you have draw(in screen 7) move by key controls and 2. how to make the chararter not got through a border of a level (ex. a line) and then erase it. I want it to not be able to go through the line but i cant firgure it out how someone please help
You're running up against QB's limitations here with the key controls. QB's only relevant function is INKEY$, which tells you when a key has been typed (pressed and released). You can't poll the keyboard to check if a key is currently being held down unless you use machine language. (There are libraries available that contain machine-language code that can be accessed as QB functions, but I don't have any URLs).

To make the graphics move, you generally have to erase a graphic from the screen and then redraw it. This is a lot easier if you first store all your movable graphics using the GET command. At the top of your program:

Code: Select all

' Player keeps track of where a player is,
' which way he's moving, and how much space that player occupies:
TYPE Player
    x AS INTEGER
    y AS INTEGER
    xDirection AS INTEGER
    yDirection AS INTEGER
    Width AS INTEGER
    Height AS INTEGER
END TYPE
DIM SHARED Mario AS Player
DIM SHARED MarioSprite(100)
Then, immediately after entering SCREEN 7, draw Mario somewhere on the screen. Then, store the image of Mario in the MarioSprite:

Code: Select all

GET (Mario.x, Mario.y)-(Mario.x+Mario.Width, Mario.y+Mario.Height), MarioSprite
Thereafter, you can use the PUT command to draw Mario anywhere you want. Using the XOR modifier to PUT, you can draw and erase him with the same statement. The following SUB would move Mario to a given X and Y position:

Code: Select all

SUB MoveMario (x AS INTEGER, y AS INTEGER)
    ' Draw Mario in his new location
    PUT (x,y), MarioSprite, XOR
    ' Erase Mario from the old location
    PUT (Mario.x, Mario.y), MarioSprite, XOR
    ' Update Mario's position data
    Mario.x = x
    Mario.y = y
END SUB
Don't worry if Mario's new position overlaps with his old one. The XOR modifier will make this do the right thing (for a black background).

There are, of course, machine-language libraries available that handle sprite graphics and provide greater capabilities than you can get from QB by itself.

To prevent a character from falling through a line, you have to keep track of where you drew the line separately. If you limit your game to having horizontal and vertical lines, the math for doing the check is a lot easier (it's also a lot easier if a given line can only be collided with from one direction). First, a SUB to stop Mario from moving:

Code: Select all

SUB StopMario
  Mario.xDirection=0
  Mario.yDirection=0
END SUB

Code: Select all

   ' Prevent Mario from falling through a line drawn across the
   ' bottom of the screen, assuming that (Mario.X,Mario.Y) is the
   ' top-left corner of the Mario sprite:

   IF Mario.Y + Mario.Height > floor.Y THEN
        StopMario
        MoveMario(Mario.X, floor.Y - Mario.Height)
   END IF
To allow Mario to fall through a crack in the floor, you could represent the floor as an array of line segments (still assuming a non-scrolling screen):

Code: Select all

    TYPE LineSeg
        X AS INTEGER
        Y AS INTEGER
        Width AS INTEGER
        colr AS INTEGER
        ' Depth is constant
     END TYPE
     CONST maxSegments = 20
     DIM SHARED floor(maxSegments) AS LineSeg
     DIM SHARED numSegments AS INTEGER
To prevent this floor array from getting out of sync with the screen, you can create a SUB that both draws the line and records its presence in floor:

Code: Select all

' This SUB draws a horizontal line from left to right and keeps track
' of it for collision-detection purposes.
 SUB DrawHorizontalLine (x AS INTEGER, y AS INTEGER, Width AS INTEGER, colr AS INTEGER)
            LINE (x,y)-(x+Width,y), colr
            floor(numSegments).X = x
            floor(numSegments).Y = y
            floor(numSegments).Width=Width
            floor(numSegments).colr = colr
            numSegments = numSegments + 1
END SUB
Use this SUB to make sure the floor is cleared whenever the screen is cleared:

Code: Select all

SUB ClearScreen
   CLS
   numSegments=0
END SUB
Otherwise, if your game has different floor patterns in different levels, you might find that Mario can stand on thin air.

Then, in your main program loop, you can iterate through floors to detect if Mario should fall or land:

Code: Select all

     ' Move Mario according to his present velocity
     MoveMario(Mario.x+Mario.xDirection, Mario.y+Mario.yDirection)
     ' Check all drawn floor segments
     FOR seg = 0 TO numSegments-1
         IF (Mario.X +Mario.Width > floor(seg).X) AND (Mario.X <floor>= floor(seg).Y-1 THEN
                StopMario
                MoveMario(Mario.X, (floor(seg).Y-1) - Mario.Height)
                GOTO breakLoop
            END IF
         END IF
     NEXT
breakLoop:
________
APRILIA SCARABEO 500
Last edited by historian on Thu Feb 10, 2011 9:29 pm, edited 1 time in total.
Quicky
Coder
Posts: 20
Joined: Wed Jun 13, 2007 10:51 am

Post by Quicky »

Okay thanks a lot that really helped me
Eric Carr
Newbie
Posts: 5
Joined: Sun Jun 17, 2007 1:30 am

Post by Eric Carr »

When you get a little more advanced and used to QB and want to handle multiple key presses without using assembly, there are a few samples out there (including one I wrote 11 years ago).

Check out this link: http://www.tek-tips.com/faqs.cfm?fid=4193

Eric
Erik
Veteran
Posts: 72
Joined: Wed Jun 20, 2007 12:31 pm
Location: LI, NY
Contact:

Post by Erik »

I actually used your way in one of the last programs I worked on. It worked great. (And wasn't as intense as I thought it'd be once I sat down with it :P)
Quicky
Coder
Posts: 20
Joined: Wed Jun 13, 2007 10:51 am

Post by Quicky »

well i figured out how to use muilt. keys w.o asm a while ago but what i cant firgure out is how to make graphics move with key controls
Quicky
Coder
Posts: 20
Joined: Wed Jun 13, 2007 10:51 am

Post by Quicky »

p.s what i mean by key controls is a can make a letter move side to side and up and down on the screen
Post Reply