12-30-2004, 08:33 PM
This is a little program I started working on yesterday. I can see a lot of places this could go..
*peace*
Meg.
[syntax="QBASIC"]
'THIS PROGRAM FLIES A CIRCULAR SHIP AROUND ON SCREEN 12. THE ROUTINE THAT
'DOES MULTI-KEY HANDLING WAS NOT WRITTEN BY ME. IN ORDER FOR THIS PROGRAM
'TO RUN YOU NEED TO LOAD QB WITH /L IN THE COMMAND LINE.
'
' written 12/29/2004 by mb
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
'sub declarations
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
DECLARE FUNCTION MULTIKEY (T) 'Milo's keyhandler routine
DECLARE SUB BoostShip (power!) 'Accelerate
DECLARE SUB DrawShip (c%) 'Draw the ship in color c%
DECLARE SUB TurnShip (a%) 'Rotate the ship a% degrees
DECLARE SUB MoveShip () 'Move the ship
DECLARE SUB Plot (x!, y!, c%) 'Draw dot on screen
DECLARE SUB PlotLine (x1!, y1!, x2!, y2!, c%) 'Draw line on screen
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
'constant declarations & environmental variables
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
CONST BoostSpeed = .05 'Acceleration of ship
CONST TurnSpeed = 3 'Rotation of ship
CONST MaxVelocity = 5 'Maximum speed of ship
CONST Friction = .993 'coefficient of friction
CONST Walls = 1 '1=Bounce, 0=Wrap-Around
CONST Pi = 3.1415926535# 'pi for trig calculations
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
'precalculate sine and cosine tables for all 360 angles
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
DIM SHARED cosn(0 TO 359) AS SINGLE
DIM SHARED sine(0 TO 359) AS SINGLE
FOR i% = 0 TO 359
cosn(i%) = COS(i% * (Pi / 180))
sine(i%) = SIN(i% * (Pi / 180))
NEXT i%
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
'the player's ship
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
TYPE ShipType
x AS SINGLE 'x coordinate
y AS SINGLE 'y coordinate
a AS INTEGER 'angle ship is facing
vx AS SINGLE 'x velocity
vy AS SINGLE 'y velocity
END TYPE
DIM SHARED ship AS ShipType
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
'change to graphics mode [640x480]
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
SCREEN 12
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
'start keyhandler
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
Z = MULTIKEY(-1)
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
'the main program loop, exits when escape is hit
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
DO
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
'erase the ship
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
DrawShip 0
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
'react to keypresses
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
IF MULTIKEY(72) THEN BoostShip BoostSpeed 'speed up ship
IF MULTIKEY(80) THEN BoostShip -BoostSpeed 'slow down ship
IF MULTIKEY(77) THEN TurnShip TurnSpeed 'turn clockwise
IF MULTIKEY(75) THEN TurnShip -TurnSpeed 'turn counterclockwise
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
'move the ship and draw it on the screen
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
MoveShip
DrawShip 15
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
'wait until ready for next frame
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
WAIT &H3DA, 8
WAIT &H3DA, 8, 8
LOOP UNTIL MULTIKEY(1) = 1
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
'clear keyhandler--this line is necessary ot else your computer will need to
'be reset after running the program.
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
Z = MULTIKEY(-2)
SYSTEM
SUB BoostShip (power!)
ship.vx = ship.vx + power! * cosn(ship.a)
ship.vy = ship.vy + power! * sine(ship.a)
IF ship.vx > MaxVelocity THEN ship.vx = MaxVelocity
IF ship.vx < -MaxVelocity THEN ship.vx = -MaxVelocity
IF ship.vy > MaxVelocity THEN ship.vy = MaxVelocity
IF ship.vy < -MaxVelocity THEN ship.vy = -MaxVelocity
END SUB
SUB DrawShip (c%)
CIRCLE (320 + ship.x, 240 + ship.y), 5, c%
offx! = ship.x + 5 * cosn(ship.a)
offy! = ship.y + 5 * sine(ship.a)
PlotLine ship.x, ship.y, offx!, offy!, c%
END SUB
SUB MoveShip
ship.x = ship.x + ship.vx
ship.y = ship.y + ship.vy
ship.vx = ship.vx * Friction
ship.vy = ship.vy * Friction
IF Walls = 1 THEN
'bounce
IF ABS(ship.x) > 314 THEN
ship.x = SGN(ship.x) * 314
ship.vx = -ship.vx
END IF
IF ABS(ship.y) > 234 THEN
ship.y = SGN(ship.y) * 234
ship.vy = -ship.vy
END IF
ELSE
'wrap
IF ship.x > 320 THEN ship.x = ship.x - 640
IF ship.x < -320 THEN ship.x = ship.x + 640
IF ship.y > 240 THEN ship.y = ship.y - 480
IF ship.y < -240 THEN ship.y = ship.y + 480
END IF
END SUB
FUNCTION MULTIKEY (T)
STATIC kbcontrol%(), kbmatrix%(), Firsttime, StatusFlag
IF Firsttime = 0 THEN
DIM kbcontrol%(128)
DIM kbmatrix%(128)
code$ = ""
code$ = code$ + "E91D00E93C00000000000000000000000000000000000000000000000000"
code$ = code$ + "00001E31C08ED8BE24000E07BF1400FCA5A58CC38EC0BF2400B85600FAAB"
code$ = code$ + "89D8ABFB1FCB1E31C08EC0BF2400BE14000E1FFCFAA5A5FB1FCBFB9C5053"
code$ = code$ + "51521E560657E460B401A8807404B400247FD0E088C3B700B0002E031E12"
code$ = code$ + "002E8E1E100086E08907E4610C82E661247FE661B020E6205F075E1F5A59"
code$ = code$ + "5B589DCF"
DEF SEG = VARSEG(kbcontrol%(0))
FOR i% = 0 TO 155
d% = VAL("&h" + MID$(code$, i% * 2 + 1, 2))
POKE VARPTR(kbcontrol%(0)) + i%, d%
NEXT i%
i& = 16
N& = VARSEG(kbmatrix%(0)): l& = N& AND 255: h& = ((N& AND &HFF00) \ 256): POKE i&, l&: POKE i& + 1, h&: i& = i& + 2
N& = VARPTR(kbmatrix%(0)): l& = N& AND 255: h& = ((N& AND &HFF00) \ 256): POKE i&, l&: POKE i& + 1, h&: i& = i& + 2
DEF SEG
Firsttime = 1
END IF
SELECT CASE T
CASE -1
IF StatusFlag = 0 THEN
DEF SEG = VARSEG(kbcontrol%(0))
CALL ABSOLUTE(0)
DEF SEG
StatusFlag = 1
END IF
CASE -2
IF StatusFlag = 1 THEN
DEF SEG = VARSEG(kbcontrol%(0))
CALL ABSOLUTE(3)
DEF SEG
StatusFlag = 0
END IF
CASE 1 TO 128
MULTIKEY = kbmatrix%(T)
CASE ELSE
MULTIKEY = 0
END SELECT
END FUNCTION
SUB Plot (x!, y!, c%)
PSET (320 + x!, 240 + y!), c%
END SUB
SUB PlotLine (x1!, y1!, x2!, y2!, c%)
LINE (320 + x1!, 240 + y1!)-(320 + x2!, 240 + y2!), c%
END SUB
SUB TurnShip (angle%)
ship.a = ship.a + angle%
IF ship.a > 359 THEN ship.a = ship.a - 360
IF ship.a < 0 THEN ship.a = ship.a + 360
END SUB
[/syntax]
*peace*
Meg.
[syntax="QBASIC"]
'THIS PROGRAM FLIES A CIRCULAR SHIP AROUND ON SCREEN 12. THE ROUTINE THAT
'DOES MULTI-KEY HANDLING WAS NOT WRITTEN BY ME. IN ORDER FOR THIS PROGRAM
'TO RUN YOU NEED TO LOAD QB WITH /L IN THE COMMAND LINE.
'
' written 12/29/2004 by mb
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
'sub declarations
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
DECLARE FUNCTION MULTIKEY (T) 'Milo's keyhandler routine
DECLARE SUB BoostShip (power!) 'Accelerate
DECLARE SUB DrawShip (c%) 'Draw the ship in color c%
DECLARE SUB TurnShip (a%) 'Rotate the ship a% degrees
DECLARE SUB MoveShip () 'Move the ship
DECLARE SUB Plot (x!, y!, c%) 'Draw dot on screen
DECLARE SUB PlotLine (x1!, y1!, x2!, y2!, c%) 'Draw line on screen
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
'constant declarations & environmental variables
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
CONST BoostSpeed = .05 'Acceleration of ship
CONST TurnSpeed = 3 'Rotation of ship
CONST MaxVelocity = 5 'Maximum speed of ship
CONST Friction = .993 'coefficient of friction
CONST Walls = 1 '1=Bounce, 0=Wrap-Around
CONST Pi = 3.1415926535# 'pi for trig calculations
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
'precalculate sine and cosine tables for all 360 angles
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
DIM SHARED cosn(0 TO 359) AS SINGLE
DIM SHARED sine(0 TO 359) AS SINGLE
FOR i% = 0 TO 359
cosn(i%) = COS(i% * (Pi / 180))
sine(i%) = SIN(i% * (Pi / 180))
NEXT i%
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
'the player's ship
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
TYPE ShipType
x AS SINGLE 'x coordinate
y AS SINGLE 'y coordinate
a AS INTEGER 'angle ship is facing
vx AS SINGLE 'x velocity
vy AS SINGLE 'y velocity
END TYPE
DIM SHARED ship AS ShipType
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
'change to graphics mode [640x480]
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
SCREEN 12
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
'start keyhandler
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
Z = MULTIKEY(-1)
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
'the main program loop, exits when escape is hit
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
DO
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
'erase the ship
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
DrawShip 0
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
'react to keypresses
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
IF MULTIKEY(72) THEN BoostShip BoostSpeed 'speed up ship
IF MULTIKEY(80) THEN BoostShip -BoostSpeed 'slow down ship
IF MULTIKEY(77) THEN TurnShip TurnSpeed 'turn clockwise
IF MULTIKEY(75) THEN TurnShip -TurnSpeed 'turn counterclockwise
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
'move the ship and draw it on the screen
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
MoveShip
DrawShip 15
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
'wait until ready for next frame
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
WAIT &H3DA, 8
WAIT &H3DA, 8, 8
LOOP UNTIL MULTIKEY(1) = 1
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
'clear keyhandler--this line is necessary ot else your computer will need to
'be reset after running the program.
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
Z = MULTIKEY(-2)
SYSTEM
SUB BoostShip (power!)
ship.vx = ship.vx + power! * cosn(ship.a)
ship.vy = ship.vy + power! * sine(ship.a)
IF ship.vx > MaxVelocity THEN ship.vx = MaxVelocity
IF ship.vx < -MaxVelocity THEN ship.vx = -MaxVelocity
IF ship.vy > MaxVelocity THEN ship.vy = MaxVelocity
IF ship.vy < -MaxVelocity THEN ship.vy = -MaxVelocity
END SUB
SUB DrawShip (c%)
CIRCLE (320 + ship.x, 240 + ship.y), 5, c%
offx! = ship.x + 5 * cosn(ship.a)
offy! = ship.y + 5 * sine(ship.a)
PlotLine ship.x, ship.y, offx!, offy!, c%
END SUB
SUB MoveShip
ship.x = ship.x + ship.vx
ship.y = ship.y + ship.vy
ship.vx = ship.vx * Friction
ship.vy = ship.vy * Friction
IF Walls = 1 THEN
'bounce
IF ABS(ship.x) > 314 THEN
ship.x = SGN(ship.x) * 314
ship.vx = -ship.vx
END IF
IF ABS(ship.y) > 234 THEN
ship.y = SGN(ship.y) * 234
ship.vy = -ship.vy
END IF
ELSE
'wrap
IF ship.x > 320 THEN ship.x = ship.x - 640
IF ship.x < -320 THEN ship.x = ship.x + 640
IF ship.y > 240 THEN ship.y = ship.y - 480
IF ship.y < -240 THEN ship.y = ship.y + 480
END IF
END SUB
FUNCTION MULTIKEY (T)
STATIC kbcontrol%(), kbmatrix%(), Firsttime, StatusFlag
IF Firsttime = 0 THEN
DIM kbcontrol%(128)
DIM kbmatrix%(128)
code$ = ""
code$ = code$ + "E91D00E93C00000000000000000000000000000000000000000000000000"
code$ = code$ + "00001E31C08ED8BE24000E07BF1400FCA5A58CC38EC0BF2400B85600FAAB"
code$ = code$ + "89D8ABFB1FCB1E31C08EC0BF2400BE14000E1FFCFAA5A5FB1FCBFB9C5053"
code$ = code$ + "51521E560657E460B401A8807404B400247FD0E088C3B700B0002E031E12"
code$ = code$ + "002E8E1E100086E08907E4610C82E661247FE661B020E6205F075E1F5A59"
code$ = code$ + "5B589DCF"
DEF SEG = VARSEG(kbcontrol%(0))
FOR i% = 0 TO 155
d% = VAL("&h" + MID$(code$, i% * 2 + 1, 2))
POKE VARPTR(kbcontrol%(0)) + i%, d%
NEXT i%
i& = 16
N& = VARSEG(kbmatrix%(0)): l& = N& AND 255: h& = ((N& AND &HFF00) \ 256): POKE i&, l&: POKE i& + 1, h&: i& = i& + 2
N& = VARPTR(kbmatrix%(0)): l& = N& AND 255: h& = ((N& AND &HFF00) \ 256): POKE i&, l&: POKE i& + 1, h&: i& = i& + 2
DEF SEG
Firsttime = 1
END IF
SELECT CASE T
CASE -1
IF StatusFlag = 0 THEN
DEF SEG = VARSEG(kbcontrol%(0))
CALL ABSOLUTE(0)
DEF SEG
StatusFlag = 1
END IF
CASE -2
IF StatusFlag = 1 THEN
DEF SEG = VARSEG(kbcontrol%(0))
CALL ABSOLUTE(3)
DEF SEG
StatusFlag = 0
END IF
CASE 1 TO 128
MULTIKEY = kbmatrix%(T)
CASE ELSE
MULTIKEY = 0
END SELECT
END FUNCTION
SUB Plot (x!, y!, c%)
PSET (320 + x!, 240 + y!), c%
END SUB
SUB PlotLine (x1!, y1!, x2!, y2!, c%)
LINE (320 + x1!, 240 + y1!)-(320 + x2!, 240 + y2!), c%
END SUB
SUB TurnShip (angle%)
ship.a = ship.a + angle%
IF ship.a > 359 THEN ship.a = ship.a - 360
IF ship.a < 0 THEN ship.a = ship.a + 360
END SUB
[/syntax]