News:

Building a 3D Ray Tracer  By stevmjon

Main Menu

gun and ufo

Started by stef, December 14, 2005, 01:33:41 PM

Previous topic - Next topic

stef

Hi!

This code shows the tech of a simple 2D shooter
You can vary manually some parameters at the begin of the code.

Greetings
stef

PS
It seems there isn't much interest to post ideas or techs in form of sourcecodes.
In this way this will be my last sourcecode for a while.
Will post more compiled programs.


; PROJECT : gun and ufo3
; AUTHOR  : stef
; CREATED : 30.11.2005
; EDITED  : 16.12.2005
; ---------------------------------------------------------------------
OpenScreen 800,600,16,2

framerate=200

SetFPS framerate

ufoposx=0; position of ufo
ufoposy=100
acc=2; velocity of ufo
angle#=0
uforotangle#=0

;---------------------------------------------------------------------important parameters
maxbull=6;(possible) maximum of existing bullets -1 (at the same time)
shotrange#= 500; shotrange of gun in pixel
vel# =4; velocity of bullets
tim#=200; minimum time between 2 shots
susfir=0; sustained fire, off with susfir=0, on with susfir=1
;----------------------------------------------------------------------------------------

Gosub mousedraw
Gosub gundraw
Gosub bulletdraw
Gosub ufodraw

Type shooting
exist
xbull#
ybull#
anglebull#
EndType

Dim bullets(maxbull) As shooting


Dim bulletsp(maxbull)

For b= 0 To maxbull
bulletsp(b)=GetFreeSprite()
CreateSprite bulletsp(b)
SpriteImage bulletsp(b),bullet
SpriteDrawMode bulletsp(b), 0
CenterSpriteHandle bulletsp(b)
SpriteCollision bulletsp(b),On
Next
;-------------------------------------------------------------------------------------mainloopstart
Do

For shot = 0 To maxbull

If bullets(shot).exist=1
   bullets(shot).xbull#=bullets(shot).xbull#+ (Cos(bullets(shot).anglebull#))*vel#
   bullets(shot).ybull#=bullets(shot).ybull#+(Sin(bullets(shot).anglebull#))*vel#
   bulletrange# = GetDistance2D(400,550, bullets(shot).xbull#, bullets(shot).ybull#)
   If bulletrange# >=shotrange# Then bullets(shot).exist=0
EndIf
Next

angle#=GetAngle2D(400,550, MouseX(),MouseY())


If MouseY() <450 And LeftMouseButton() = True
 
For shot = 0 To maxbull

 If bullets(shot).exist=0
  time2#=Timer()

   If time2#-time1# > tim#
     bullets(shot).exist=1
     time1# =Timer()

     bullets(shot).anglebull#= angle#
     bullets(shot).xbull#= 400+(Cos(angle#))*55
     bullets(shot).ybull#= 550+(Sin(angle#))*55
    EndIf
 EndIf
Next
If susfir=0 Then FlushMouse
EndIf


RenderToScreen
Cls RGB(0,0,0)


RotateSprite ufosp,uforotangle#
PositionSprite ufosp,ufoposx,ufoposy
DrawSprite ufosp

For shot=0 To maxbull
If bullets(shot).exist=1
 PositionSprite bulletsp(shot),bullets(shot).xbull#,bullets(shot).ybull#
 
 If SpritesOverlap(ufosp, bulletsp(shot))=True
 
  CircleC bullets(shot).xbull#,bullets(shot).ybull#,50,1,RGB(255,255,255)
 
  bullets(shot).exist=0
  ufoposx =0
  ufoposy=RndRange(50,400)
 EndIf
 
 
 DrawSprite bulletsp(shot)
EndIf
Next

PositionSprite carriagesp,350,496
DrawSprite carriagesp


RotateSprite barrelsp,angle#+90
PositionSprite barrelsp,400,550
DrawSprite barrelsp

If MouseY() <450 Then DrawImage Mouse1,MouseX()-20,MouseY()-20,1
DrawImage Mouse2,MouseX()-20,MouseY()-20,1

Print "Use mouse and left mouse button"
Print "FPS:"+Str$(FPS())+"  FPS set to "+Str$(framerate)
Print "Maximum bullets on screeen: "+Str$(maxbull+1)
Print "Shotrange: "+Str$(shotrange#)
Print "Velocity of bullets: "+Str$(vel#)
Print "Minimum time betweeen 2 shots: "+Str$(tim#)
Print "sustained fire: "+ Str$(susfir)
Sync


ufoposx= ufoposx + acc
If ufoposx> 800
ufoposx =0
ufoposy=RndRange(50,400)
EndIf

Inc uforotangle#
If uforotangle#>360 Then uforotangle#=0

If EscKey()= True Then End

Loop
;---------------------------------------------------------------------mainloopend


ufodraw:
ufo=GetFreeImage()
CreateImage ufo, 80,40

RenderToImage ufo


CircleC 40,20,40,1,RGB(100,100,180)
CircleC 40,20,35,1,RGB(0,0,0)
BoxC 0,18,80,22,1,RGB(100,100,180)
CircleC 40,20,20,1,RGB(60,60,120)

CircleC 40,20,12,1,RGB(140,0,50)

For x= 10 To 1 Step -3
CircleC 40,20,x,0,RGB(20,20,20)
Next
For x= 0 To 360 Step 30
LineC 40,20,40- 12*Cos(x),20-12*Sin(x),RGB(20,20,20)
Next
PrepareFXImage ufo
ufosp=GetFreeSprite()
CreateSprite ufosp
SpriteImage ufosp,ufo
SpriteDrawMode ufosp,2
CenterSpriteHandle ufosp
SpriteCollision ufosp,On

Return


gundraw:

carriage=GetFreeImage()
CreateImage carriage, 100,100
RenderToImage carriage

BoxC 0,95,100,100,1,RGB(100,100,100)
BoxC 47,50,53,100,1,RGB(100,100,100)

carriagesp= 2
CreateSprite carriagesp
SpriteImage carriagesp,carriage
SpriteDrawMode carriagesp,0

barrel=GetFreeImage()
CreateImage barrel, 20,110
RenderToImage barrel

For x= 1 To 4
BoxC 4+x,-1,16-x,80,1,RGB(30+20*x,30+20*x,30+20*x)
BoxC 2+x,50,18-x,60,1,RGB(30+30*x,30+30*x,30+30*x)
BoxC 0+x,70,20-x,80,1,RGB(20+20*x,20+20*x,20+20*x)
Next
CircleC 10,55,4,1,RGB(50,50,50)
barrelsp= 3
CreateSprite barrelsp
PrepareFXImage barrel

SpriteImage barrelsp,barrel
CenterSpriteHandle barrelsp
SpriteDrawMode barrelsp,2

Return

mousedraw:
mouse1=GetFreeImage()
CreateImage mouse1, 40,40
RenderToImage mouse1

LineC 20,0,20,40,RGB(250,0,0)
LineC 0,20,40,20,RGB(250,0,0)
CircleC 20,20,8,1,RGB(0,0,0)
CircleC 20,20,14,0,RGB(250,0,0)

mouse2=GetFreeImage()
CreateImage mouse2, 40,40
RenderToImage mouse2

CircleC 20,20,2,1,RGB(200,200,200)

Return

bulletdraw:
bullet=GetFreeImage()
CreateImage bullet, 10,10
RenderToImage bullet

CircleC 5,5,5,1,RGB(255,0,0)
CircleC 5,5,3,1,RGB(255,180,0)

Return

tomazmb

Hello,

I like your ideas very much Stef. Please continue to post. There is much to learn from your code.

Have a nice day,

Tomaz
My computer specification:

AMD Athlon 64 2800+
MB ASUS K8V Socket 754 VIA K8T800
SB Audigy 2
3 GB RAM DDR 400 MHz PQI
AGP NVIDIA GeForce 7600GT 256 MB-Club 3D
Windows XP Pro SP2
DirectX 9.0c

stef

#2
Hi!

Thanks for reply.
I would say one of the best way to learn is learning by doing.
(First I really wanted to make this code like a step by step tutorial)

But would like to see some other codes for inspiration.

( Hm?!  What about your codes/codesnippets etc.?)

Have a nice day too

greetings
stef

PS
Made Made some changes in the code above
You can see now the actual parameters on screen