2d Ship Driving - Printable Version +- Qbasicnews.com (http://qbasicnews.com/newforum) +-- Forum: QBasic (http://qbasicnews.com/newforum/forum-4.html) +--- Forum: QB Projects (http://qbasicnews.com/newforum/forum-12.html) +--- Thread: 2d Ship Driving (/thread-5507.html) |
2d Ship Driving - Meg - 12-30-2004 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] 2d Ship Driving - Ryan - 12-30-2004 Wow Meg, very cool. It's pretty much what I've always wondered how to do in a well done nicely documented format. (The problem for me has always been forgetting a lot of math since high school.. been a few years.) Thanks for showing us! 2d Ship Driving - Meg - 12-30-2004 I should point out that when I run this from the IDE, after the program ends, my left shift key is toggled "on" and I have to tap it once to turn it off. Not sure why this happens, but it's because of something the keyhandler does. 2d Ship Driving - Mech1031 - 12-30-2004 very awesome program indeed meg. i remember trying to write something very similar to this, but as always, i procrastinated until there was nothing left keep up the good work! Awesome job overall, Meg!! ^_-=b ! - Adigun A. Polack - 12-30-2004 To Meg: I must say up front that you overall have done a splendid job on your program, really! ! You know what, it truly reminds me of an original âhoming missilesâ program that was apparently part of Amarillionâs original circles tutorial for Allegro (located here for you!) if my very own memory serves me correctly, Meg. The only problem I had was that when I first ran your program at face value (using âQB /lâ as my startup settings for QuickBASIC IDE), it did not work. HOWEVER, on one of the subs, I had to change the name of it from: [syntax="QBasic"]SUB Boost (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 [/syntax] .....to: [syntax="QBasic"]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 [/syntax] ......and then, I pressed the F5 key, and it ran beautifully!! :wtnod:=b ! Like I have said, splendid job on your program!! See you now. ENCOURAGING YOU TO JUST KEEP UP THE AWESOME WORK, Adigun Azikiwe Polack One of the Founders of âAura Flowâ Continuing Developer of âFrantic Journeyâ Current Developer of âStar Angelic Sluggerâ Webmaster of the âAAP Official Projects Squadâ 2d Ship Driving - Meg - 12-30-2004 Oops. Yeah, typo. All fixed. Meg, I am glad you noticed. ;*) - Adigun A. Polack - 12-30-2004 To Meg again: I just saw the little adjustment there in your original code presented at the very top of your thread here, and having checked that code now, it clearly ran like a dream like it was supposed to in the first place!!! ! In close of my post #400 here, I must say that you *are* good on that work and then some, you know that? =b Talk to you later. - Adigun Azikiwe Polack One of the Founders of âAura Flowâ Continuing Developer of âFrantic Journeyâ Current Developer of âStar Angelic Sluggerâ Webmaster of the âAAP Official Projects Squadâ |