Main Menu

Shooter example

Started by ATLUS, August 31, 2012, 08:50:40 AM

Previous topic - Next topic

ATLUS

dirty and not optimized code
controls:
w/s/a/d - move
leftmouse - fire
1 - pistol
2 - assault rifle

* i not add bullets counter for each weapon. and primitive enemy.


PlayBASIC Code: [Select]
; PROJECT : Project1
; AUTHOR : ATLUS
; CREATED : 31.08.2012
; ---------------------------------------------------------------------


setfps 60
mouse off
loadfont "Times New Romans",2,17,1
setfont 2




dist#=0
mvtime#=0

type weapon
name as string
num as integer
v# as float
bullets_interval as integer
accuracy_min# as float
accuracy_max# as float
magazin as integer
reaload as integer
endtype

type player_st
x# as float
y# as float
hp as integer
speed# as float
status as integer // 1 - stay, 2 - shot, 3-reaload, 4 - damage, 5 - 0 hp
cur_bullets as integer
cur_weapon as integer
endtype

dim player as player_st
player.x# = 400
player.y# = 300
player.hp = 60
player.speed# = 7
player.status = 1
cur_weapon = 0
cur_bullets = 7

///////pistol
dim wep(7) as weapon
wep(0).name = "Pistol" //name
wep(0).num=0 // pistol
wep(0).v#=16.7 //bullets speed
wep(0).bullets_interval=200 //bullets_interval
wep(0).accuracy_min# = 0
wep(0).accuracy_max# = 0 // 0-1 - good, 2-3-normal, 3-4 bad, 5~n very bad
wep(0).magazin = 7 //shots in one magazin
wep(0).reaload = 300 //ms

/////storm rifle
wep(1).name = "AK-47" //name
wep(1).num=1 // pistol
wep(1).v#=21.7 //bullets speed
wep(1).bullets_interval=90 //bullets_interval
wep(1).accuracy_min# = -1.8
wep(1).accuracy_max# = 1.8 // 0-1 - good, 2-3-normal, 3~ very bad
wep(1).magazin = 30
wep(1).reaload = 700


bullets_max=50

global num_bul=7
global cw=0


dim bullets(50,4)

time=timer()
global rtime=timer()



do

cls rgb(0,0,0)
mx#=mousex()
my#=mousey()



if player.status=3
if timer()-rtime>wep(cw).reaload
rtime=timer()
player.status=1
num_bul=wep(cw).magazin
endif
endif



if leftmousebutton() and player.status<>3
if timer()-time>wep(cw).bullets_interval
shot(player.x#,player.y#,mx#,my#,wep(cw).v#)
time=timer()
endif
endif



if keystate(2) then cw=0
if keystate(3) then cw=1


if keystate(17) then player.y#-=player.speed#
if keystate(31) then player.y#+=player.speed#
if keystate(30) then player.x#-=player.speed#
if keystate(32) then player.x#+=player.speed#


update_bullets()
circlec mx#,my#,10,0,RGB(99, 150, 226)
circlec player.x#,player.y#,16,1,RGB(221, 47, 107)
text 10,10,wep(cw).name
text 10,20,num_bul



sync
loop


function shot(x#,y#,mx#,my#,spd#)
num_bul--
if player.status<>3
if num_bul=0
rtime=timer()
player.status=3
endif

bullets(num_bul,0)=x#
bullets(num_bul,1)=y#
bullets(num_bul,4)=1

dist#=getdistance2d(x#,y#,mx#,my#)
mvtime#=dist#/spd#
bullets(num_bul,2)=(mx#-x#)/float(mvtime#)+rndrange(wep(cw).accuracy_min#,wep(cw).accuracy_max#)
bullets(num_bul,3)=(my#-y#)/float(mvtime#)+rndrange(wep(cw).accuracy_min#,wep(cw).accuracy_max#)

endif

Login required to view complete source code

kevin

#1
 what does value 4 in the bullet array represent ?  

ie
bullets(num_bul,4)=1


Anyway, just assuming it's status of some type, the bullets code is probably better presented as type array, rather than a 2D integer array.  

PlayBASIC Code: [Select]
setfps 60
mouse off
loadfont "Times New Romans",2,17,1
setfont 2




dist#=0
mvtime#=0

type weapon
name as string
num as integer
v# as float
bullets_interval as integer
accuracy_min# as float
accuracy_max# as float
magazin as integer
reaload as integer
endtype

type player_st
x# as float
y# as float
hp as integer
speed# as float
status as integer // 1 - stay, 2 - shot, 3-reaload, 4 - damage, 5 - 0 hp
cur_bullets as integer
cur_weapon as integer
endtype

dim player as player_st
player.x# = 400
player.y# = 300
player.hp = 60
player.speed# = 7
player.status = 1
cur_weapon = 0
cur_bullets = 7

///////pistol
dim wep(7) as weapon
wep(0).name = "Pistol" //name
wep(0).num=0 // pistol
wep(0).v#=16.7 //bullets speed
wep(0).bullets_interval=200 //bullets_interval
wep(0).accuracy_min# = 0
wep(0).accuracy_max# = 0 // 0-1 - good, 2-3-normal, 3-4 bad, 5~n very bad
wep(0).magazin = 7 //shots in one magazin
wep(0).reaload = 300 //ms

/////storm rifle
wep(1).name = "AK-47" //name
wep(1).num=1 // pistol
wep(1).v#=21.7 //bullets speed
wep(1).bullets_interval=90 //bullets_interval
wep(1).accuracy_min# = -1.8
wep(1).accuracy_max# = 1.8 // 0-1 - good, 2-3-normal, 3~ very bad
wep(1).magazin = 30
wep(1).reaload = 700


bullets_max=50

global num_bul=7
global cw=0


Type TBullet
Status
x#
y#
speedx#
speedy#
EndType

dim bullets(bullets_max) as tBullet

time=timer()
global rtime=timer()


do

cls rgb(0,0,0)

mx#=mousex()
my#=mousey()



if player.status=3
if timer()-rtime>wep(cw).reaload
rtime=timer()
player.status=1
num_bul=wep(cw).magazin
endif
endif



if leftmousebutton() and player.status<>3
if timer()-time>wep(cw).bullets_interval
shot(player.x#,player.y#,mx#,my#,wep(cw).v#)
time=timer()
endif
endif



if keystate(2) then cw=0
if keystate(3) then cw=1


if keystate(17) then player.y#-=player.speed#
if keystate(31) then player.y#+=player.speed#
if keystate(30) then player.x#-=player.speed#
if keystate(32) then player.x#+=player.speed#


update_bullets()
circlec mx#,my#,10,0,RGB(99, 150, 226)
circlec player.x#,player.y#,16,1,RGB(221, 47, 107)
text 10,10,wep(cw).name
text 10,20,num_bul


sync
loop





function shot(x#,y#,mx#,my#,spd#)

num_bul--

if player.status<>3

if num_bul=0
rtime=timer()
player.status=3
endif

bullets(num_bul).x=x#
bullets(num_bul).y=y#
Login required to view complete source code


ATLUS

Thanks! Kevin, it's better


*it's was
bullets(num_bul,0) - start position x
bullets(num_bul,1) - start position y
bullets(num_bul,2) - speed x
bullets(num_bul,3) - speed y
bullets(num_bul,4) - status( is it flying or not)