TGC Codebase Backup



Easy Flashlight by MikeS

5th Feb 2005 19:20
Summary

Create a basic flashlight using the ghost object command.



Description

Create a basic flashlight using the ghost object command.



Code
                                    ` This code was downloaded from The Game Creators
                                    ` It is reproduced here with full permission
                                    ` http://www.thegamecreators.com
                                    
                                    `This tutorial will teach you how to make a basic flashlight for your games.
`It uses the ghost object command to perform a neat trick against objects it's viewed
`through.
`Tutorial made by MikeS of CurvedBasic 2-5-05

sync on : sync rate 60           `Set our refresh rate
set display mode 1024,768,32     `Set the display mode
color backdrop rgb(0,0,0)        `Color the background of our world black

make object cone 2,1             `This will be our flashlight
set object smoothing 2,100       `The cone object by itself is very blocky, so we smooth it.
ghost object on 2,-1             `This is the most important part of this flashlight snippet.
                                 `Basically, with the ghost object command set to a negative
                                 `value, it inverts all the colors around the object.
                                 `Thus, this can be used as a cheap effect for a flashlight.
xrotate object 2,-90             `We rotate our cone object now.
position object 2,0,-.1,2        `Position it a little away from us
lock object on 2                 `Now we lock the object onto our screen

for x=3 to 20                    `Here is where we create some demo objects.
 make object cube x,10           `This will give us some random objects to view with our
                                 `flashlight
 position object x,x*10,0,10
 color object x,rgb(rnd(55),rnd(55),rnd(55))
 set object light x,1
next x

`We want things dark, so we use these commands to darken the area, and set the lighting
`to a perfect black
set ambient light 20
color ambient light rgb(0,0,0)

do `Initialize our loop

text 0,0,"Use 1-5 to change the light color" `A little bit of instructions for our user. :)

`Here we have some fun by coloring our light however we like with if-endif statements.
if keystate(2)=1                 `Detect which key was pressed.
 color object 2,rgb(255,255,255) `Color the object
endif                            `End our statement
if keystate(3)=1
 color object 2,rgb(0,0,0)
endif
if keystate(4)=1
 color object 2,rgb(255,0,0)
endif
if keystate(5)=1
 color object 2,rgb(0,255,0)
endif
if keystate(6)=1
 color object 2,rgb(0,0,255)
endif

control camera using arrowkeys 0,1,1 `This handy command moves our camera.

sync `Refresh our screen
loop `Go back to our do statement.