04-04-2005, 08:30 AM
Here are a couple of my old QB programs. Well... kinda old. I made them about 1-2 weeks ago. In the first one, you control a cannon and try to hit the target. It simulates actual cannon trajectory physics. Each pixel represents one meter.
The second program is a simple and rather boring game where you try to jump the obstacle as is hurtles towards you at random speeds. I pretty much just made this as a memory refresher on bitmaping, sprites, and such.
[syntax="qbasic"]
DECLARE SUB position (turretPositionX!, turretPositionY!, angleUpButton$, angleDownButton$, powerUpButton$, powerDownButton$, fireButton$, trajectory!, power!, barrelLength!, turretBase!, targetPosition!, score!, quitButton$, quit!)
DECLARE SUB fire (turretPositionX!, turretPositionY!, trajectory!, power!, barrelLength!, gravity!, turretBase!, targetPosition!, score!)
DECLARE SUB background ()
DECLARE SUB drawTarget (targetPosition!)
CLS
LET turretPositionX = 1
LET turretPositionY = 199
LET turretBase = 10
LET angleUpButton$ = "a"
LET angleDownButton$ = "d"
LET powerUpButton$ = "w"
LET powerDownButton$ = "s"
LET fireButton$ = "e"
LET quitButton$ = "q"
LET trajectory = 45
LET power = 50
LET barrelLength = 25
LET gravity = 9.8
LET score = 0
SCREEN 7, 0, 1, 0
PRINT "W - Increase Power"
PRINT "S - Decrease Power"
PRINT
PRINT "A - Increase Trajectory"
PRINT "D - Decrease Trajectory"
PRINT
PRINT "Q - Quit"
PRINT
PRINT "E - Fire!"
PRINT
PRINT
PRINT "Press any key to continue..."
PCOPY 1, 0
DO WHILE key$ = ""
LET key$ = INKEY$
LOOP
RANDOMIZE TIMER
DO WHILE quit <> 1
LET targetPosition = INT((230 * RND) + 80)
CALL position(turretPositionX, turretPositionY, angleUpButton$, angleDownButton$, powerUpButton$, powerDownButton$, fireButton$, trajectory, power, barrelLength, turretBase, targetPosition, score, quitButton$, quit)
IF quit <> 1 THEN
CALL fire(turretPositionX, turretPositionY, trajectory, power, barrelLength, gravity, turretBase, targetPosition, score)
END IF
LOOP
END
SUB background
LINE (0, 199)-(320, 199)
END SUB
SUB drawTarget (targetPosition)
LINE (targetPosition - 6, 198)-(targetPosition + 6, 198), 4
LINE (targetPosition - 4, 197)-(targetPosition + 4, 197), 4
END SUB
SUB fire (turretPositionX, turretPositionY, trajectory, power, barrelLength, gravity, turretBase, targetPosition, score)
LET pi = 3.141592652589793#
LET barrelPositionX = turretPositionX + INT(COS(trajectory * (pi / 180)) * barrelLength)
LET barrelPositionY = turretPositionY - INT(SIN(trajectory * (pi / 180)) * barrelLength)
LET timeBegin = TIMER
DO WHILE bulletY <= 199
LET timeElapsed = TIMER - timeBegin
LET bulletX = barrelPositionX + power * COS((trajectory * pi) / 180) * timeElapsed
LET bulletY = barrelPositionY - ((-1 / 2) * gravity * (timeElapsed ^ 2) + power * SIN((trajectory * pi) / 180) * timeElapsed)
CLS
PRINT "Altitude ="; INT(200 - bulletY); "meters"
PRINT "Distance ="; INT(bulletX - barrelPositionX); "meters"
CIRCLE (turretPositionX, turretPositionY), turretBase
LINE (turretPositionX, turretPositionY)-(barrelPositionX, barrelPositionY)
CALL drawTarget(targetPosition)
CALL background
PSET (bulletX, bulletY), RND * 15
PCOPY 1, 0
IF 200 - bulletY > 5 AND 200 - bulletY < 200 THEN
SOUND 20 * (200 - bulletY), 1
END IF
LOOP
IF bulletX >= targetPosition - 6 AND bulletX <= targetPosition + 6 THEN
LET score = score + targetPosition
END IF
END SUB
SUB position (turretPositionX, turretPositionY, angleUpButton$, angleDownButton$, powerUpButton$, powerDownButton$, fireButton$, trajectory, power, barrelLength, turretBase, targetPosition, score, quitButton$, quit)
LET pi = 3.141592652589793#
LET cannonFire = 0
DO WHILE cannonFire <> 1 AND quit <> 1
LET key$ = INKEY$
IF key$ = angleUpButton$ AND trajectory < 90 THEN
LET trajectory = trajectory + 1
ELSEIF key$ = angleDownButton$ AND trajectory > 0 THEN
LET trajectory = trajectory - 1
ELSEIF key$ = powerUpButton$ AND power < 100 THEN
LET power = power + 1
ELSEIF key$ = powerDownButton$ AND power > 0 THEN
LET power = power - 1
ELSEIF key$ = fireButton$ THEN
LET cannonFire = 1
ELSEIF key$ = quitButton$ THEN
LET quit = 1
END IF
CLS
PRINT "Trajectory ="; trajectory
PRINT "Initial Velocity ="; power; "meters per second"
PRINT
PRINT "Score ="; score
CIRCLE (turretPositionX, turretPositionY), turretBase
LINE (turretPositionX, turretPositionY)-(turretPositionX + INT(COS(trajectory * (pi / 180)) * barrelLength), turretPositionY - INT(SIN(trajectory * (pi / 180)) * barrelLength))
CALL background
CALL drawTarget(targetPosition)
PCOPY 1, 0
LOOP
END SUB
[/syntax]
[syntax="qbasic"]
DECLARE SUB graphicsLoader (stationaryJumper!(), airborneJumper!(), enemy!())
DECLARE SUB graphicsDisplay (graphic!(), spriteWidth!, spriteHeight!, xCoor!, yCoor!)
DIM stationaryJumper(4, 11), airborneJumper(4, 11), enemy(9, 4)
LET jumpHeight = 20
LET groundPosition = 100
LET groundColor = 6
LET skyColor = 9
LET playerPositionX = 100
LET playerPositionY = groundPosition - 12
LET enemyPositionX = 320
LET enemyPositionY = groundPosition - 5
LET enemySpeedRange = 7
LET enemySpeed = 1
LET jumpFlag = 0
LET life = 1
LET score = 0
LET total = 0
LET again = 1
RANDOMIZE TIMER
SCREEN 7, 0, 1, 0
CALL graphicsLoader(stationaryJumper(), airborneJumper(), enemy())
PRINT "Welcome to Jump!"
PRINT
PRINT "You are the red Jumper. Press Spacebar to jump and avoid the enemy."
PRINT
PRINT "Press any key to begin..."
PCOPY 1, 0
DO WHILE key$ = ""
LET key$ = INKEY$
LOOP
'main game engine
DO WHILE again = 1
LET time = TIMER
DO WHILE life = 1
LET key$ = INKEY$
IF jumpFlag = 0 THEN
IF key$ = " " THEN
LET jumpFlag = 1
END IF
ELSEIF jumpFlag = 1 THEN
IF playerPositionY <= groundPosition - 12 AND playerPositionY >= groundPosition - jumpHeight - 12 THEN
LET playerPositionY = playerPositionY - 1
ELSEIF playerPositionY <= groundPosition - jumpHeight THEN
LET jumpFlag = 2
END IF
ELSEIF jumpFlag = 2 THEN
IF playerPositionY < groundPosition - 12 THEN
LET playerPositionY = playerPositionY + 1
ELSEIF playerPositionY >= groundPosition - 12 THEN
LET playerPositionY = groundPosition - 12
LET jumpFlag = 0
END IF
END IF
IF enemyPositionX > -10 THEN
LET enemyPositionX = enemyPositionX - enemySpeed
ELSEIF enemyPositionX <= -10 THEN
LET score = score + enemySpeed
LET total = total + 1
LET enemySpeed = INT((RND * enemySpeedRange) + 1)
LET enemyPositionX = 320
END IF
IF enemyPositionX <= playerPositionX + 4 AND enemyPositionX >= playerPositionX - 9 AND enemyPositionY <= playerPositionY + 11 THEN
LET life = 0
END IF
CLS
PRINT "Score ="; score
LINE (0, 10)-(320, 200), skyColor, BF
LINE (0, groundPosition)-(320, groundPosition + 200), groundColor, BF
IF jumpFlag > 0 THEN
CALL graphicsDisplay(airborneJumper(), 5, 12, playerPositionX, playerPositionY)
ELSEIF jump = 0 THEN
CALL graphicsDisplay(stationaryJumper(), 5, 12, playerPositionX, playerPositionY)
END IF
CALL graphicsDisplay(enemy(), 10, 5, enemyPositionX, enemyPositionY)
PCOPY 1, 0
LOOP
'end main game engine
CLS
PRINT " Game over"
PRINT
PRINT " Score: "; score
PRINT " Time Lasted: "; INT(TIMER - time); "seconds"
PRINT "Total enemies avoided: "; total
PRINT
PRINT
PRINT "Press 'p' to play again."
PRINT "Press any other key to quit."
PCOPY 1, 0
LET key$ = ""
DO WHILE key$ = ""
LET key$ = INKEY$
IF key$ = "p" THEN
LET again = 1
LET life = 1
LET enemyPositionX = 320
LET score = 0
LET total = 0
LET enemySpeed = 1
ELSEIF key$ <> " " THEN
LET again = 0
END IF
LOOP
LOOP
END
'Begin DATA
'stationary jumper
DATA 1,1,5,12
DATA 09,09,09,09,09
DATA 09,09,09,09,09
DATA 09,09,04,09,09
DATA 09,04,04,04,09
DATA 09,04,04,04,09
DATA 09,09,04,09,09
DATA 09,09,04,09,09
DATA 04,09,04,09,04
DATA 04,04,04,04,04
DATA 04,09,09,09,04
DATA 09,04,09,04,09
DATA 09,04,09,04,09
'airborne jumper
DATA 1,2,5,12
DATA 09,09,04,09,09
DATA 09,04,04,04,09
DATA 09,04,04,04,09
DATA 09,09,04,09,09
DATA 09,09,04,09,09
DATA 09,09,04,09,09
DATA 09,04,04,04,09
DATA 09,04,09,04,09
DATA 09,04,09,04,09
DATA 09,04,09,04,09
DATA 09,04,09,04,09
DATA 09,04,09,04,09
'enemy
DATA 1,3,10,5
DATA 09,09,09,09,09,09,14,14,14,09
DATA 09,09,09,09,14,14,14,14,14,14
DATA 09,09,09,14,14,14,14,14,14,14
DATA 09,09,14,14,14,14,14,14,14,14
DATA 14,14,14,14,14,14,14,14,14,09
'end of data
DATA 0,0
SUB graphicsDisplay (graphic(), spriteWidth, spriteHeight, xCoor, yCoor)
'loads graphics from arrays to screen
FOR y = yCoor TO yCoor + spriteHeight - 1
FOR x = xCoor TO xCoor + spriteWidth - 1
PSET (x, y), graphic(x - xCoor, y - yCoor)
NEXT x
NEXT y
END SUB
SUB graphicsLoader (stationaryJumper(), airborneJumper(), enemy())
'loads graphics from DATA statements into arrays
LET flag = 1
LET counter = 1
DO WHILE flag = 1
CLS
READ flag
READ spriteID
IF flag = 1 THEN
READ spriteWidth
READ spriteHeight
FOR y = 0 TO spriteHeight - 1
FOR x = 0 TO spriteWidth - 1
IF spriteID = 1 THEN
READ stationaryJumper(x, y)
ELSEIF spriteID = 2 THEN
READ airborneJumper(x, y)
ELSEIF spriteID = 3 THEN
READ enemy(x, y)
END IF
NEXT x
NEXT y
END IF
LOOP
END SUB
[/syntax]
The second program is a simple and rather boring game where you try to jump the obstacle as is hurtles towards you at random speeds. I pretty much just made this as a memory refresher on bitmaping, sprites, and such.
[syntax="qbasic"]
DECLARE SUB position (turretPositionX!, turretPositionY!, angleUpButton$, angleDownButton$, powerUpButton$, powerDownButton$, fireButton$, trajectory!, power!, barrelLength!, turretBase!, targetPosition!, score!, quitButton$, quit!)
DECLARE SUB fire (turretPositionX!, turretPositionY!, trajectory!, power!, barrelLength!, gravity!, turretBase!, targetPosition!, score!)
DECLARE SUB background ()
DECLARE SUB drawTarget (targetPosition!)
CLS
LET turretPositionX = 1
LET turretPositionY = 199
LET turretBase = 10
LET angleUpButton$ = "a"
LET angleDownButton$ = "d"
LET powerUpButton$ = "w"
LET powerDownButton$ = "s"
LET fireButton$ = "e"
LET quitButton$ = "q"
LET trajectory = 45
LET power = 50
LET barrelLength = 25
LET gravity = 9.8
LET score = 0
SCREEN 7, 0, 1, 0
PRINT "W - Increase Power"
PRINT "S - Decrease Power"
PRINT "A - Increase Trajectory"
PRINT "D - Decrease Trajectory"
PRINT "Q - Quit"
PRINT "E - Fire!"
PRINT "Press any key to continue..."
PCOPY 1, 0
DO WHILE key$ = ""
LET key$ = INKEY$
LOOP
RANDOMIZE TIMER
DO WHILE quit <> 1
LET targetPosition = INT((230 * RND) + 80)
CALL position(turretPositionX, turretPositionY, angleUpButton$, angleDownButton$, powerUpButton$, powerDownButton$, fireButton$, trajectory, power, barrelLength, turretBase, targetPosition, score, quitButton$, quit)
IF quit <> 1 THEN
CALL fire(turretPositionX, turretPositionY, trajectory, power, barrelLength, gravity, turretBase, targetPosition, score)
END IF
LOOP
END
SUB background
LINE (0, 199)-(320, 199)
END SUB
SUB drawTarget (targetPosition)
LINE (targetPosition - 6, 198)-(targetPosition + 6, 198), 4
LINE (targetPosition - 4, 197)-(targetPosition + 4, 197), 4
END SUB
SUB fire (turretPositionX, turretPositionY, trajectory, power, barrelLength, gravity, turretBase, targetPosition, score)
LET pi = 3.141592652589793#
LET barrelPositionX = turretPositionX + INT(COS(trajectory * (pi / 180)) * barrelLength)
LET barrelPositionY = turretPositionY - INT(SIN(trajectory * (pi / 180)) * barrelLength)
LET timeBegin = TIMER
DO WHILE bulletY <= 199
LET timeElapsed = TIMER - timeBegin
LET bulletX = barrelPositionX + power * COS((trajectory * pi) / 180) * timeElapsed
LET bulletY = barrelPositionY - ((-1 / 2) * gravity * (timeElapsed ^ 2) + power * SIN((trajectory * pi) / 180) * timeElapsed)
CLS
PRINT "Altitude ="; INT(200 - bulletY); "meters"
PRINT "Distance ="; INT(bulletX - barrelPositionX); "meters"
CIRCLE (turretPositionX, turretPositionY), turretBase
LINE (turretPositionX, turretPositionY)-(barrelPositionX, barrelPositionY)
CALL drawTarget(targetPosition)
CALL background
PSET (bulletX, bulletY), RND * 15
PCOPY 1, 0
IF 200 - bulletY > 5 AND 200 - bulletY < 200 THEN
SOUND 20 * (200 - bulletY), 1
END IF
LOOP
IF bulletX >= targetPosition - 6 AND bulletX <= targetPosition + 6 THEN
LET score = score + targetPosition
END IF
END SUB
SUB position (turretPositionX, turretPositionY, angleUpButton$, angleDownButton$, powerUpButton$, powerDownButton$, fireButton$, trajectory, power, barrelLength, turretBase, targetPosition, score, quitButton$, quit)
LET pi = 3.141592652589793#
LET cannonFire = 0
DO WHILE cannonFire <> 1 AND quit <> 1
LET key$ = INKEY$
IF key$ = angleUpButton$ AND trajectory < 90 THEN
LET trajectory = trajectory + 1
ELSEIF key$ = angleDownButton$ AND trajectory > 0 THEN
LET trajectory = trajectory - 1
ELSEIF key$ = powerUpButton$ AND power < 100 THEN
LET power = power + 1
ELSEIF key$ = powerDownButton$ AND power > 0 THEN
LET power = power - 1
ELSEIF key$ = fireButton$ THEN
LET cannonFire = 1
ELSEIF key$ = quitButton$ THEN
LET quit = 1
END IF
CLS
PRINT "Trajectory ="; trajectory
PRINT "Initial Velocity ="; power; "meters per second"
PRINT "Score ="; score
CIRCLE (turretPositionX, turretPositionY), turretBase
LINE (turretPositionX, turretPositionY)-(turretPositionX + INT(COS(trajectory * (pi / 180)) * barrelLength), turretPositionY - INT(SIN(trajectory * (pi / 180)) * barrelLength))
CALL background
CALL drawTarget(targetPosition)
PCOPY 1, 0
LOOP
END SUB
[/syntax]
[syntax="qbasic"]
DECLARE SUB graphicsLoader (stationaryJumper!(), airborneJumper!(), enemy!())
DECLARE SUB graphicsDisplay (graphic!(), spriteWidth!, spriteHeight!, xCoor!, yCoor!)
DIM stationaryJumper(4, 11), airborneJumper(4, 11), enemy(9, 4)
LET jumpHeight = 20
LET groundPosition = 100
LET groundColor = 6
LET skyColor = 9
LET playerPositionX = 100
LET playerPositionY = groundPosition - 12
LET enemyPositionX = 320
LET enemyPositionY = groundPosition - 5
LET enemySpeedRange = 7
LET enemySpeed = 1
LET jumpFlag = 0
LET life = 1
LET score = 0
LET total = 0
LET again = 1
RANDOMIZE TIMER
SCREEN 7, 0, 1, 0
CALL graphicsLoader(stationaryJumper(), airborneJumper(), enemy())
PRINT "Welcome to Jump!"
PRINT "You are the red Jumper. Press Spacebar to jump and avoid the enemy."
PRINT "Press any key to begin..."
PCOPY 1, 0
DO WHILE key$ = ""
LET key$ = INKEY$
LOOP
'main game engine
DO WHILE again = 1
LET time = TIMER
DO WHILE life = 1
LET key$ = INKEY$
IF jumpFlag = 0 THEN
IF key$ = " " THEN
LET jumpFlag = 1
END IF
ELSEIF jumpFlag = 1 THEN
IF playerPositionY <= groundPosition - 12 AND playerPositionY >= groundPosition - jumpHeight - 12 THEN
LET playerPositionY = playerPositionY - 1
ELSEIF playerPositionY <= groundPosition - jumpHeight THEN
LET jumpFlag = 2
END IF
ELSEIF jumpFlag = 2 THEN
IF playerPositionY < groundPosition - 12 THEN
LET playerPositionY = playerPositionY + 1
ELSEIF playerPositionY >= groundPosition - 12 THEN
LET playerPositionY = groundPosition - 12
LET jumpFlag = 0
END IF
END IF
IF enemyPositionX > -10 THEN
LET enemyPositionX = enemyPositionX - enemySpeed
ELSEIF enemyPositionX <= -10 THEN
LET score = score + enemySpeed
LET total = total + 1
LET enemySpeed = INT((RND * enemySpeedRange) + 1)
LET enemyPositionX = 320
END IF
IF enemyPositionX <= playerPositionX + 4 AND enemyPositionX >= playerPositionX - 9 AND enemyPositionY <= playerPositionY + 11 THEN
LET life = 0
END IF
CLS
PRINT "Score ="; score
LINE (0, 10)-(320, 200), skyColor, BF
LINE (0, groundPosition)-(320, groundPosition + 200), groundColor, BF
IF jumpFlag > 0 THEN
CALL graphicsDisplay(airborneJumper(), 5, 12, playerPositionX, playerPositionY)
ELSEIF jump = 0 THEN
CALL graphicsDisplay(stationaryJumper(), 5, 12, playerPositionX, playerPositionY)
END IF
CALL graphicsDisplay(enemy(), 10, 5, enemyPositionX, enemyPositionY)
PCOPY 1, 0
LOOP
'end main game engine
CLS
PRINT " Game over"
PRINT " Score: "; score
PRINT " Time Lasted: "; INT(TIMER - time); "seconds"
PRINT "Total enemies avoided: "; total
PRINT "Press 'p' to play again."
PRINT "Press any other key to quit."
PCOPY 1, 0
LET key$ = ""
DO WHILE key$ = ""
LET key$ = INKEY$
IF key$ = "p" THEN
LET again = 1
LET life = 1
LET enemyPositionX = 320
LET score = 0
LET total = 0
LET enemySpeed = 1
ELSEIF key$ <> " " THEN
LET again = 0
END IF
LOOP
LOOP
END
'Begin DATA
'stationary jumper
DATA 1,1,5,12
DATA 09,09,09,09,09
DATA 09,09,09,09,09
DATA 09,09,04,09,09
DATA 09,04,04,04,09
DATA 09,04,04,04,09
DATA 09,09,04,09,09
DATA 09,09,04,09,09
DATA 04,09,04,09,04
DATA 04,04,04,04,04
DATA 04,09,09,09,04
DATA 09,04,09,04,09
DATA 09,04,09,04,09
'airborne jumper
DATA 1,2,5,12
DATA 09,09,04,09,09
DATA 09,04,04,04,09
DATA 09,04,04,04,09
DATA 09,09,04,09,09
DATA 09,09,04,09,09
DATA 09,09,04,09,09
DATA 09,04,04,04,09
DATA 09,04,09,04,09
DATA 09,04,09,04,09
DATA 09,04,09,04,09
DATA 09,04,09,04,09
DATA 09,04,09,04,09
'enemy
DATA 1,3,10,5
DATA 09,09,09,09,09,09,14,14,14,09
DATA 09,09,09,09,14,14,14,14,14,14
DATA 09,09,09,14,14,14,14,14,14,14
DATA 09,09,14,14,14,14,14,14,14,14
DATA 14,14,14,14,14,14,14,14,14,09
'end of data
DATA 0,0
SUB graphicsDisplay (graphic(), spriteWidth, spriteHeight, xCoor, yCoor)
'loads graphics from arrays to screen
FOR y = yCoor TO yCoor + spriteHeight - 1
FOR x = xCoor TO xCoor + spriteWidth - 1
PSET (x, y), graphic(x - xCoor, y - yCoor)
NEXT x
NEXT y
END SUB
SUB graphicsLoader (stationaryJumper(), airborneJumper(), enemy())
'loads graphics from DATA statements into arrays
LET flag = 1
LET counter = 1
DO WHILE flag = 1
CLS
READ flag
READ spriteID
IF flag = 1 THEN
READ spriteWidth
READ spriteHeight
FOR y = 0 TO spriteHeight - 1
FOR x = 0 TO spriteWidth - 1
IF spriteID = 1 THEN
READ stationaryJumper(x, y)
ELSEIF spriteID = 2 THEN
READ airborneJumper(x, y)
ELSEIF spriteID = 3 THEN
READ enemy(x, y)
END IF
NEXT x
NEXT y
END IF
LOOP
END SUB
[/syntax]
nd remember kids, only you can provoke forest fires!