forums

TNT Basic Forums > Programming Q&A
Good-old 2D spaceship game, dif' flight modes
< Last Thread     Next Thread >
Author
Thread        Post A Reply

08-23-2009 01:57

Posted by:
Destro

Location:
LBC (langley British Columbia)

Find more posts by Destro

Hi people, so I've been programming for a week now, lots of help from friends and from this forum. Shepherd150's flight physics make an appearance, thanks for that. And thanks for being in grade 10 and being way flippin smarter than me.
So I've got it set up that when you start the game, (not much of a game yet) your spaceship is controlled under arcade asteroids physics, astmode =1.
When astmode=0, normal control of the spaceship is assumed, where we'll pretend gravity is compensated for by your ship's computer and so flight feels smoother as if in an atmosphere (shephard150's asteroid clone controls).

Press F to enter regular flight mode, G to return to asteroids mode (astmode).

While in regular flight mode, holding the apple key will initiate strafe mode, where the up and down keys now control the left and right directions.
Also, holding shift lets you bypass the maximum speed in regular flight mode.

So now the tricky part... when you are hurdling through space in regular flight mode and you press G, your ship should continue to hurdle through space in the same direction and at the same speed until your next actions modify this path.

CODE:

graphics mode hardware 640,480
hide mouse

global float width=640, height=480
global float yposship= 320, xposship=350
global float shippic=0
global float rotateship=0
global float player=0
global float playerstrafe=0
global float playerxdir, playerydir, playerspeed=0

global int astmode=1
global float thrust=0
global float xvelocity=0
global float yvelocity=0

'draw player
set pen colour make colour (140,154,154)
line 1,4 to 1,6
line 2,1 to 2,7
line 3,1 to 3,8
line 4,2 to 4,5
line 5,6 to 5,9
line 6,4 to 6,6
line 7,7 to 7,12
line 8,4 to 8,7
line 8,10 to 8,13
line 9,4 to 9,6
line 9,11 to 9,13
line 10,4 to 10,7
line 10,10 to 10,3
line 11,7 to 11,12
line 13,4 to 13,6
line 14,6 to 14,9
line 15,2 to 15,5
line 16,1 to 16,8
line 17,1 to 17,7
line 18,4 to 18,6
line 4,3 to 5,3
line 7,4 to 8,4
line 11,4 to 12,4
line 14,3 to 15,3
set pen colour blue
line 4,6 to 4,10
line 5,4 to 5,5
line 6,7 to 6,11
line 7,5 to 7,6
line 11,5 to 11,6
line 12,7 to 12,11
line 13,4 to 13,5
line 14,6 to 14,10
set pen colour yellow
line 8,8 to 8,9
line 8,9 to 9,10
set pen colour make colour (100,80,35) 'orange?
line 9,7 to 9,9
line 10,8 to 10,9
copy image 0,0,18,13,-1, true to player
set image offsets player,9.5,7
set sprite x scale player,2
set sprite y scale player,2

open canvas 1,640,480
set pen colour black
paint canvas
DrawStarfield()

draw frame
set pen colour yellow
frame rect 0,0 to 640,480

draw frame

while not mouse button 'main loop

sprite player, xposship, yposship, shippic

if left
rotateship = rotateship-2.25
else if right
rotateship = rotateship+2.25
end if
set sprite rotation player,rotateship

rotateship= sprite rotation (player)
playerxdir= -sin(rotateship)
playerydir= cos(rotateship)

if up
if playerspeed < 4.25 then playerspeed=playerspeed+0.15
end if
if down
if playerspeed = 0 then playerspeed= playerspeed-2
if playerspeed > 0 then playerspeed=playerspeed-0.325
end if
if pressed (56) 'shift = overdrive
if up then playerspeed=playerspeed+0.15
end if

if pressed (55) ''''''''''strafe mode
rotateship= sprite rotation (player)
playerxdir= Cos(rotateship)
playerydir= sin(rotateship)
if up and playerspeed< 3.5 then playerspeed=playerspeed+0.325
if down and playerspeed> -3.5 then playerspeed=playerspeed-0.325
end if


if xposship<0 'returns ship to opposite coordinates on screen
xposship= width
else if xposship>width
xposship=0
end if

if yposship<0
yposship= height
else if yposship>height
yposship= 0
end if

if pressed (3)'F '''''''''Asteroids physics mode starts here'''''''''
astmode = 0
else if pressed (5)'G
'ship continues to travel in last direction before pressing G, and now thrust
'should be equal to last speed clocked before pressing G...
astmode = 1
end if

if astmode = 1
xvelocity=xvelocity-sin(rotateship)*thrust
yvelocity=yvelocity+cos(rotateship)*thrust
xposship=xposship+xvelocity
yposship=yposship+yvelocity
if up
thrust = 0.0375
else
thrust = 0
end if
else
yposship= yposship-(playerydir*-playerspeed)
xposship= xposship+(playerxdir*playerspeed)
end if
draw frame
wend
'procedures
procedure DrawStarfield()
int star

for star=1 to 450
set pixel colour random(0,640),random(0,480),random(black,white)
next star
end proc


I'd eventually like a similiar motion effect when letting go of shift (coming out of strafe mode, ship should continue moving in strafe pattern until the course is altered by player). Also If you feel like explaining to me why the randomly coloured stars are all green, that would be cool too.
Thanks for your time people, you keep this stuff fun and not always frustrating.

08-25-2009 14:21

Posted by:
Jacob

Location:
San Francisco, CA

Click Here to Email Jacob   Find more posts by Jacob

For the all green random stars, you can't just to random(black, white).
Replace:
set pixel colour random(0,640),random(0,480),random(black,white)
With:
set pixel colour random(0,640),random(0,480),make color(random(0,255), random(0,255), random(0,255))

For the other thing, I'm not sure what you want...
Maybe when you let go of Shift, astmode is set to 2, and then when checking the ships movment, just do if astmode > 0, but once you press another control, astmode is set to 0 if it equals 2.

09-10-2009 01:28

Posted by:
Destro

Location:
LBC (langley British Columbia)

Find more posts by Destro

Thanks for the colour fix!
I got all the speed transitions to work...

sprite player,xposship,yposship,001

xposship=xposship+xvelocity-(playerxdir*-playerspeedmono)+(playerxdirstf*straferspeedmono)
yposship=yposship+yvelocity+(playerydir*playerspeedmono)+(playerydirstf*straferspeedmono)

Here there's three speeds affecting the position of the ship.
Depending on which of the two flight-modes you are using, your speed is determined by different variables that affect your position.
If strafing within either flightmode, a different variable is again used, so that when pressing the strafe button the ship doesn't take the ship's current speed and immediately start strafing with it. The speeds instead overlap between all flight-mode changes and allow for creative flying.

Other code slows all of these speeds down except the relevant one so that braking to a complete stop allows you to stop, not resume a different, inactive speed.

But now I've thrown in a planet sprite with gravity that draws the player towards it. When the ship crosses the X or Y zero axis' it hits a speedbump. So the ship isn't making B-lines for the the planet even in complete stop. The ship drifts at a 45 to the nearest x or y zero axis and then travels the remaining distance to the planet in a vertical or horizontal line.

Any thoughts?

09-10-2009 02:54

Posted by:
Destro

Location:
LBC (langley British Columbia)

Find more posts by Destro

the actual stuff. I tried >='s and tried putting the x and y together with else's but it's still wanting something else

sprite player, xposship, yposship, 001
sprite 004,xposplanet,yposplanet,004

if xposship > xposplanet then xposship = xposship - 6.75
if xposship < xposplanet then xposship = xposship + 6.75
if xposship = xposplanet then xposship = xposship
if yposship > yposplanet then yposship = yposship - 6.75
if yposship < yposplanet then yposship = yposship + 6.75
if yposship = yposplanet then yposship = yposship

All times are GMT        Post A Reply

Forum Jump:
< Last Thread     Next Thread >

< Contact Us - TNT Basic >

Powered by: vBulletin Lite Version 1.0.1 Lite
Copyright © Jelsoft Enterprises Limited 2000.