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