TGC Codebase Backup



Boxes and More Boxes by Webber

10th Feb 2011 22:24
Summary

Collision, Blur effects, Particles, etc. Awesome!



Description

This program creates about 1000 boxes, all with smooth sliding object collision.

Basic Controls
You use the mouse to look around and the arrow keys to move around.
If you click on the mouse you can jump, but climbing up 1000 boxes
can take a while. But its fun skydiving off of the top.


The sync rate is only set to 20, you can change it to make the program run faster but it gets harder to control.


The object collision does not use any of the object collision commands
Instead it gets the objects positions, and the objects size to calculate the collision
The collision works very smooth, but the boxes cannot be rotated.

The Motion Blur effects are cool
Basically they work by replacing the normal camera with a ghosted sprite image of the camera. It is set up with a percent system, that can be adjusted with the "M" key but
when you bring the motion blur effect near 100% it gets so blurry that you can hardly see.

There is also another blur effect that takes the same sprite and pastes it to the screen 4
times in different directions. You can adjust this effect with the "B" key. 20 pixels is the maximum radius, but I don't recommend using a large radius for this effect. It starts to look like your seeing 4 objects instead of one.

There is also a 256 X 256 sky box in the background, you can always load your own.

The screen shot does not have the particle effect in it, but I added particles using plains. They follow the player, change to random colors, flop around, they look like confetti. And they look really cool with the blur effect on.

It's pretty amazing what you can do with a bunch of boxes, so have fun with this code.

Credit to The Game Guy would be appreciated, but is not required.



Code
                                    ` This code was downloaded from The Game Creators
                                    ` It is reproduced here with full permission
                                    ` http://www.thegamecreators.com
                                    
                                    `Project: Boxes and More Boxes
`Author: The Game Guy

set display mode 640,480,32
sync on : sync rate 20 : autocam off : set camera range 0,.1,100000
color backdrop RGB(100,133,153) : fog on : fog distance 500 : fog color RGB(100,133,153)
show mouse : set text font "modern" : set text to bold : set text size 12
hide mouse : set ambient light 10

Rem Loading Screen
set text size 48
Center text 320,200,"Loading"
sync : sync

load image "BlackNBlue.jpg",1

Rem Make The Sky Cube
SCN = 3 `Sky Cube Object Number
SLN = 7 `Sky Cube Limb Object Number (temporary object number)
STN = 2 `Sky Cube Texture Start Number (1 to 6)
SBI = 1 `Sky Background Images
Gosub Make_SkyCube:

Rem Creates the Player/Cube
make object cube 1,2 : hide object 1
position object 1,0,10,0

Rem Creates the Lighting Sphere
make object sphere 2,1 : texture object 2,1
set object cull 2,0 : ghost object on 2,2


Rem Create all of the boxes with collision
for i = 1 to 1000
obj = i+10

if i < 990 `randomly placed boxes
make object box obj,(rnd(200)+50.0)/30.0,(rnd(200)+50.0)/30.0,(rnd(200)+50.0)/30.0
position object obj,(rnd(2000)-1000)/40,rnd(3000)/2,(rnd(2000)-1000)/40
endif

Rem Creates the base objects
if i >= 990 and i <> 1000
j# = j# + .2
make object box obj,(i-980)*4,.5,(i-980)*4 : position object obj,0,-j#,0
endif

if i = 1000
j# = j# + .2
make object box obj,(i-980)*4,5000,(i-980)*4 : position object obj,0,-j#-2500,0
endif

color object obj,rgb(rnd(255),rnd(255),rnd(255)) `color the boxes randomly
next i



Rem Create the Particles
for i = 1 to 500
obj = i+10000
make object plain obj,.4,.4
position object obj,(rnd(400)-200)/4.0,(rnd(400)-200)/4.0,(rnd(400)-200)/4.0
next i


Rem Blur effects start
Mblur = 50`%
Alpha = 256-Mblur*2.5
blur = 0`0-20

Rem sets up sprite for the blur effects
set camera to image 0,1000,512,512
sprite 1000,320,240,1000 : size sprite 1000,640,480 : offset sprite 1000,320,240 : set sprite 1000,0,1




` L      OOOOO  OOOOO  PPPPP
` L      O   O  O   O  P   P
` L      O   O  O   O  PPPPP
` L      O   O  O   O  P
` LLLLL  OOOOO  OOOOO  P
do




Rem Animate The Particles
t = t + 1 `variable used to make the particles move around in a wave like pattern
for i = 1 to 500
obj = i+10000
turn object right obj,obj+.05 : roll object right obj,obj+.05 `flop the particles around
`point object obj,object position x(1),object position y(1),object position z(1)
Rem Makes the particles follow the player
if object position x(obj) < object position x(1)-50 then position object obj,object position x(obj)+100,object position y(obj),object position z(obj)
if object position x(obj) > object position x(1)+50 then position object obj,object position x(obj)-100,object position y(obj),object position z(obj)
if object position y(obj) < object position y(1)-50 then position object obj,object position x(obj),object position y(obj)+100,object position z(obj)
if object position y(obj) > object position y(1)+50 then position object obj,object position x(obj),object position y(obj)-100,object position z(obj)
if object position z(obj) < object position z(1)-50 then position object obj,object position x(obj),object position y(obj),object position z(obj)+100
if object position z(obj) > object position z(1)+50 then position object obj,object position x(obj),object position y(obj),object position z(obj)-100

color object obj,RGB(rnd(255),rnd(255),rnd(255)) `randomly colors the particles
position object obj,object position x(obj)+sin(obj+t)*.1,object position y(obj)-.2,object position z(obj)+cos(obj+t)*.1

next i


Rem Mouse controls
MMX# = MouseMoveX() : MMY# = MouseMoveY()

CAX# = CAX# + MMY#
CAY# = CAY# + MMX#
if CAX# > 85 then CAX# = 85
if CAX# < -85 then CAX# = -85

Rem turns the player
rotate object 1,0,0,0
turn object right 1,CAY#

rotate camera 0,CAX#,CAY#,0 `rotates the camera


Rem Slow the player to a stop when the player is not moving
if P1SpeedZ# > .1 then Dec P1SpeedZ#,.1
if P1SpeedZ# < -.1 then Inc P1SpeedZ#,.1
if P1SpeedX# > .1 then Dec P1SpeedX#,.1
if P1SpeedX# < -.1 then Inc P1SpeedX#,.1

if P1SpeedZ# > .01 then Dec P1SpeedZ#,.01
if P1SpeedZ# < -.01 then Inc P1SpeedZ#,.01
if P1SpeedX# > .01 then Dec P1SpeedX#,.01
if P1SpeedX# < -.01 then Inc P1SpeedX#,.01

if P1SpeedZ# > .001 then Dec P1SpeedZ#,.001
if P1SpeedZ# < -.001 then Inc P1SpeedZ#,.001
if P1SpeedX# > .001 then Dec P1SpeedX#,.001
if P1SpeedX# < -.001 then Inc P1SpeedX#,.001

if P1SpeedZ# > 0 then Dec P1SpeedZ#,.0001
if P1SpeedZ# < 0 then Inc P1SpeedZ#,.0001
if P1SpeedX# > 0 then Dec P1SpeedX#,.0001
if P1SpeedX# < 0 then Inc P1SpeedX#,.0001

if P1SpeedZ# > 0 and P1SpeedZ# <  .0002 then P1SpeedZ# = 0
if P1SpeedZ# < 0 and P1SpeedZ# > -.0002 then P1SpeedZ# = 0
if P1SpeedX# > 0 and P1SpeedX# <  .0002 then P1SpeedX# = 0
if P1SpeedX# < 0 and P1SpeedX# > -.0002 then P1SpeedX# = 0

Rem Control the movement of the player with the arrow keys
if UpKey() = 1 then Inc P1SpeedZ#,.15
if DownKey() = 1 then Dec P1SpeedZ#,.15
if RightKey() = 1 then Inc P1SpeedX#,.15
if LeftKey() = 1 then Dec P1SpeedX#,.15

Rem Puts a limit on the players speed
if P1SpeedZ# > 1 then P1SpeedZ# = 1
if P1SpeedZ# < -1 then P1SpeedZ# = -1
if P1SpeedX# > 1 then P1SpeedX# = 1
if P1SpeedX# < -1 then P1SpeedX# = -1

P1SpeedY# = P1SpeedY# -.02 `Gravity
if P1SpeedY# < -2 then P1SpeedY# = -2 `Maximum Gravity speed

Rem if the player is on solid ground and clicks the mouse then make the player jump
if mouseclick() = 1 and Ground = 1
P1SpeedY# = .75
endif

Ground = 0 `sets Ground to 0 so that the player cannot jump in midair

Rem Moves the Player
move object 1,P1SpeedZ#/2    `forwards and backwards
move object right 1,P1SpeedX#/3 `right and left
move object up 1,P1SpeedY#   `gravity

Rem positions the player at the base if the player falls off of the map
if object position y(1) < -50
position object 1,0,10,0
endif




` CCCCC  OOOOO  L      L      IIIII  SSSSS  IIIII  OOOOO  NN  N
` C      O   O  L      L        I    S        I    O   O  N N N
` C      O   O  L      L        I    SSSSS    I    O   O  N N N
` C      O   O  L      L        I        S    I    O   O  N  NN
` CCCCC  OOOOO  LLLLL  LLLLL  IIIII  SSSSS  IIIII  OOOOO  N   N
for i = 1 to 1000
obj = i+10


if object position x(1)+1 > (object position x(obj)-object size x(obj)/2)
if object position x(1)-1 < (object position x(obj)+object size x(obj)/2)
if object position y(1)+1 > (object position y(obj)-object size y(obj)/2)
if object position y(1)-1 < (object position y(obj)+object size y(obj)/2)
if object position z(1)+1 > (object position z(obj)-object size z(obj)/2)
if object position z(1)-1 < (object position z(obj)+object size z(obj)/2)


if object position y(1)+.5 > (object position y(obj)-object size y(obj)/2)
if object position y(1)-.5 < (object position y(obj)+object size y(obj)/2)
if object position z(1)+.5 > (object position z(obj)-object size z(obj)/2)
if object position z(1)-.5 < (object position z(obj)+object size z(obj)/2)
if OPX# <= object position x(obj)-object size x(obj)/2 then position object 1,object position x(obj)-object size x(obj)/2-1,object position y(1),object position z(1)
if OPX# >= object position x(obj)+object size x(obj)/2 then position object 1,object position x(obj)+object size x(obj)/2+1,object position y(1),object position z(1)
endif:endif:endif:endif


if object position x(1)+.5 > (object position x(obj)-object size x(obj)/2)
if object position x(1)-.5 < (object position x(obj)+object size x(obj)/2)
if object position z(1)+.5 > (object position z(obj)-object size z(obj)/2)
if object position z(1)-.5 < (object position z(obj)+object size z(obj)/2)
if OPY# <= object position y(obj)-object size y(obj)/2
position object 1,object position x(1),object position y(obj)-object size y(obj)/2-1,object position z(1)
P1SpeedY# = 0
endif
if OPY# >= object position y(obj)+object size y(obj)/2
position object 1,object position x(1),object position y(obj)+object size y(obj)/2+1,object position z(1)
P1SpeedY# = 0
if mouseclick() = 0 then Ground = 1
endif
endif:endif:endif:endif


if object position x(1)+.5 > (object position x(obj)-object size x(obj)/2)
if object position x(1)-.5 < (object position x(obj)+object size x(obj)/2)
if object position y(1)+.5 > (object position y(obj)-object size y(obj)/2)
if object position y(1)-.5 < (object position y(obj)+object size y(obj)/2)
if OPZ# <= object position z(obj)-object size z(obj)/2 then position object 1,object position x(1),object position y(1),object position z(obj)-object size z(obj)/2-1
if OPZ# >= object position z(obj)+object size z(obj)/2 then position object 1,object position x(1),object position y(1),object position z(obj)+object size z(obj)/2+1
endif:endif:endif:endif


endif:endif:endif:endif:endif:endif

next i



Rem gets the old object position of object 1
OPX# = object position x(1) : OPY# = object position y(1) : OPZ# = object position z(1)

position camera 0,Object position x(1),Object position y(1),Object position z(1) `positions the camera
position object 2,Object position x(1),Object position y(1),Object position z(1) `positions the lighting sphere
position object SCN,Object position x(1),Object position y(1),Object position z(1) `positions the sky cube

Rem Updates the blur effects
gosub Blur


Rem Text Stuff
set text size 12
text 10,10,"MotionBlur:"+str$(Mblur)+"%"
text 10,20,"Blur:"+str$(blur)+"-20"
text 10,30,"Press 'M' for Motion Blur"
text 10,40,"Press 'B' for Blur"
text 10,50,"Press 'Q' to Quit"
text 10,60,"FPS:"+str$(screen fps())

if inkey$() = "q" or inkey$() = "Q" then exit `exits the program

sync
loop

end




` SSSSS  K   K  Y   Y
` S      K  K   Y   Y
` SSSSS  KKK     Y Y
`     S  K  K     Y
` SSSSS  K   K    Y

Rem Create The Sky Cube
Make_SkyCube:
SkyCSize# = 10000.0 `The size of the Sky Box
select SBI
Case 1

load image "Cube_top.jpg",STN : load image "Cube_north.jpg",STN+1
load image "Cube_east.jpg",STN+2 : load image "Cube_south.jpg",STN+3
load image "Cube_west.jpg",STN+4 : load image "Cube_bottom.jpg",STN+5

endCase
endselect

Make Object Plain SCN,0,0 : Make Object Plain SLN,SkyCSize#,SkyCSize# : Make Mesh From Object SLN,SLN

Add Limb SCN,1,SLN : Offset Limb SCN,1,0,(SkyCSize#/2),0 : Rotate Limb SCN,1,270,90,0` Top
Add Limb SCN,2,SLN : Offset Limb SCN,2,0,0,(SkyCSize#/2)` Front
Add Limb SCN,3,SLN : Offset Limb SCN,3,SkyCSize#/2,0,0 : Rotate Limb SCN,3,0,90,0` Right
Add Limb SCN,4,SLN : Offset Limb SCN,4,0,0,(SkyCSize#/2-SkyCSize#) : Rotate Limb SCN,4,0,180,0` Back
Add Limb SCN,5,SLN : Offset Limb SCN,5,(SkyCSize#/2-SkyCSize#),0,0 : Rotate Limb SCN,5,0,270,0` left
Add Limb SCN,6,SLN : Offset Limb SCN,6,0,-(SkyCSize#/2),0 : Rotate Limb SCN,6,90,90,0` bottom

for i = 1 to 6 `scaling and textureing the sky cube
Scale Limb SCN,i,100.1,100.1,100.1 : Texture Limb SCN,i,i+STN-1
next i

Delete Object SLN

set object cull SCN,0 : set object filter SCN,0
set object light SCN,0 : set object ambient SCN,0 : set object fog SCN,0
Return




`BBBB   L      U   U  RRRR
`B   B  L      U   U  R   R
`BBBB   L      U   U  RRRR
`B   B  L      U   U  R  R
`BBBB   LLLLL   UUU   R   R

Rem Blur Effects
Blur:

if inkey$() <> "b" then bzero = 1 `Blur effect
if inkey$() = "b" and bzero = 1
blur = blur + 1
if blur = 21 then blur = 0
bzero = 0
endif

if inkey$() <> "m" then mzero = 1 `Motion Blur effect
if inkey$() = "m" and mzero = 1
Mblur = Mblur + 1
if Mblur = 101 then Mblur = 0
if Mblur = 0 then Alpha = 255
if Mblur > 0
Alpha = 256-Mblur*2.5
endif
mzero = 0
endif

set sprite alpha 1000,Alpha : sprite 1000,320,240,1000

set sprite alpha 1000,Alpha   : paste sprite 1000,320+blur,240
set sprite alpha 1000,Alpha/2 : paste sprite 1000,320,240+blur
set sprite alpha 1000,Alpha/4 : paste sprite 1000,320-blur,240
set sprite alpha 1000,Alpha/8 : paste sprite 1000,320,240-blur

return