GAME HELP
GAME HELP
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
Re: GAME 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).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
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)
Code: Select all
GET (Mario.x, Mario.y)-(Mario.x+Mario.Width, Mario.y+Mario.Height), MarioSprite
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
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
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
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
Code: Select all
SUB ClearScreen
CLS
numSegments=0
END SUB
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.
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
Check out this link: http://www.tek-tips.com/faqs.cfm?fid=4193
Eric