TGC Codebase Backup



2D Cube Catcher by Libervurto

21st Jul 2007 13:29
Summary

I made this game using my 2D 3D world. Try to catch the cubes as they drop! Move your box around the floor using the arrow keys. Get three in a row and your box will grow! Use the



Description

I started this project to find a way of detecting 3D collisions for 2D sprites (for the game Dark Knights, see Community Project in the DBC forum). It then got turned into an entry for the DBC challenge, and my future plans are to build an entirely functional 3D viewport, with rotating objects etc.
If I can achieve that, expect to see some awesome vector graphics games from yours truly :).
WARNING: THIS IS NOT ACCURATE 3D, IT IS MERELY IMITATING PERSPECTIVE.



Code
                                    ` This code was downloaded from The Game Creators
                                    ` It is reproduced here with full permission
                                    ` http://www.thegamecreators.com
                                    
                                    `--------------------------------
`2D CUBE CATCHER!
`--------------------------------
`by OBese87
`--------------------------------

`--------------------------------
` SETUP
`--------------------------------
randomize timer()
hide mouse

`world Boundaries
Wx1 = 15 : Wx2 = 320
Wy1 = 200 : Wy2 = 30
Wz1 = 0 : Wz2 = 160
`perspective
Vx# = 0.5 : Vy# = 1-Vx#

`Cube position
Cx = rnd(Wx2-Wx1)+Wx1
Cy# = Wy2
Cz = rnd(Wz2-Wx1)+Wx1
Cs# = 1

`Cube volume
Cvx = 10
Cvy = 10
Cvz = 10

`player plain
Px = Wx1
Py = Wy1
Pz = Wz1
`plain volume
Pvx = 0
Pvz = 0

sTime# = timer() : `start time

`--------------------------------
` MAIN LOOP
`--------------------------------
sync on

DO
cls
gosub Axis
gosub Cube
gosub Player
gosub control
gosub cube_fall
gosub bind
gosub HUD

sync
LOOP

`--------------------------------
` GOSUBS
`--------------------------------
axis:
`x-line
ink rgb(255,0,0),0
line 15,200,320,200
`y-line
ink rgb(0,255,0),0
line 15,200,15,20
`z-line (X+Y/2)
ink rgb(0,0,255),0
line 15,200,15+(145*Vx#),200-(145*Vy#)
RETURN

`------

cube:
`Colour the cube to plain position
if Cy# < Wy1-5
   ink rgb(255,255,255),0
else
   if ABS( (Px+(pvx/2)) - (Cx+(Cvx/2)) ) < Pvx and ABS( (Pz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if downkey() = 1 then dec Pz,1

`Change perspective
if Shiftkey() = 1 then inc Vx#,0.1
if Controlkey() = 1 then dec Vx#,0.1
if Vx# < 0.0 then Vx# = 0.0
if Vx# > 1.0 then Vx# = 1.0
Vy# = 1-Vx#

RETURN

`------

cube_fall:
inc Cy#,Cs#
if Cy# >= Wy1
   if ABS( (Px+(pvx/2)) - (Cx+(Cvx/2)) ) < Pvx and ABS( (Pz+(pvz/2)) - (Cz+(Cvz/2)) ) < Pvz
      inc Pscore,1
      inc roll,1
      if roll = 3 then onfire = 1
   else
      roll = 0
      onfire = 0
   endif
   Cx = rnd(Wx2-Wx1)+Wx1
   Cy# = Wy2
   Cz = rnd(Wz2-Wx1)+Wx1
   Cvx = rnd(10)+5 : Cvy = rnd(10)+5 : Cvz = rnd(10)+5
   Cs# = rnd(15)
   Cs# = (Cs# /10)+0.5
else
   if onfire = 0
      if (Pvx-Cvx) <> 0 then dec Pvx,(Pvx-Cvx)/ABS(Pvx-Cvx)
      if (Pvz-Cvz) <> 0 then dec Pvz,(Pvz-Cvz)/ABS(Pvz-Cvz)
   else
      Pvx = 30 : Pvz = 15
   endif
endif
RETURN

`------

bind:
if Cx < Wx1 then Cx = Wx1
if Cx > Wx2 then Cx = Wx2
if Cy# > Wy1 then Cy# = Wy1
if Cy# < Wy2 then Cy# = Wy2
if Cz < Wz1 then Cz = Wz1
if Cz > Wz2 then Cz = Wz2

if Px < Wx1 then Px = Wx1
if Px > Wx2 then Px = Wx2
if Pz < Wz1 then Pz = Wz1
if Pz > Wz2 then Pz = Wz2
RETURN

`------

HUD:
set cursor 20,320
rTime# = 60-(Timer()-sTime#)/1000
print "Time Remaining: "; rTime#
print "Score: "; Pscore
if onfire = 1 then print "YOU'RE ON FIRE!!!"
print "Cube Speed: "; Cs#

if rTime# <= 0
   cls
   center text 320,240,"You scored " + str$(Pscore) + "pts!"
   wait key
   sync
   end
endif
RETURN