Thc03
I'm not sure that's really what I need for my, game. The atanfull function is really what I'm looking for, and I managed to figure out somewhat of what was wrong with my code. In the followpoint.x
and followpoint.y variables, I forgot to put in the Scroll.x and Scroll.y Values. Now they follow I think, but they keep straying off after a while...I've also added code so that when the bots start getting closer, they slow down until they are too close to move...Here's the updated code in the code snippet...
Rem Project: Tactical Space Combat
Rem Created: 8/8/06 4:13:45 PM
Rem ***** Main Source File *****
sync on:sync rate 0
set display mode 1024,768,32
backdrop off:randomize timer()
set image colorkey 0,0,0
Intro:
cls
load = timer() + 5000
l = (1024 / 2) - 200
repeat
cls
Title(1024 / 2,768 / 2,"Tactical Space Combat")
box (1024 / 2) - 200,600,(1024 / 2) + 200,632,rgb(0,255,0),rgb(0,100,0),rgb(0,255,0),rgb(0,100,0)
l = l + 9
if l > (1024 / 2) + (200 - 32)
l = (1024 / 2) - 200
endif
box l,600,l + 32,632,rgb(255,0,255),rgb(100,0,100),rgb(255,0,255),rgb(100,0,100)
sync
until timer() > load
MainMenu:
cls
do
cls
Title(1024 / 2,100,"Tactical Space Combat")
if Button("Arcade",(1024 / 2) - (text width("Arcade") / 2),200) = 1 then wait 500:goto Arcade
if Button("Exit",800,600) = 1 then end
sync
loop
Arcade:
cls
img_Ship1 = 1
img_Ship2 = 2
img_Bullet1 = 3
img_Bullet2 = 4
load image "Images\Ship_01.bmp",img_Ship1
load image "Images\Ship_03.bmp",img_Ship2
load image "Images\Bullet1.bmp",img_Bullet1
load image "Images\Bullet2.bmp",img_Bullet2
img_Star = 5
img_Smoke = 6
load image "Images\Bullet4.bmp",img_Star
load image "Images\Smoke.bmp",img_Smoke
type vector2
x as integer
y as integer
endtype
type rect
top as integer
left as integer
right as integer
bottom as integer
endtype
type Ship
num as integer
pos as vector2
pos2 as vector2
size as vector2
angle as integer
ammo as integer
maxammo as integer
sheilds as integer
armor as integer
speed as integer
maxspeed as integer
botsowned as integer
lastshot as integer
score as integer
endtype
dim Ship(1) as Ship
type Bots
num as integer
pos as vector2
pos2 as vector2
size as vector2
angle as integer
ammo as integer
maxammo as integer
sheilds as integer
armor as integer
speed as integer
maxspeed as integer
following as integer
followpoint as vector2
fleeing as integer
fleeingpoint as vector2
swarming as integer
swarmingpoint as vector2
swarmingarea as rect
lastshot as integer
owned as integer
endtype
dim Bot(199) as Bots
type BulletsU1
num as vector2
pos as vector2
pos2 as vector2
size as vector2
angle as integer
life as integer
mindammage as integer
maxdammage as integer
endtype
dim Bullet1(1,39) as BulletsU1
type TExplode
pos as vector2
pos2 as vector2
size as vector2
size2 as rect
life as integer
endtype
dim Explode(999,19) as TExplode
type TTrail
pos as vector2
pos2 as vector2
size as vector2
size2 as rect
life as integer
endtype
dim Trail(999,19) as TTrail
Scroll as vector2
Scroll.x = 100
Scroll.y = 9900
Ship(0).pos.x = 100
Ship(0).pos.y = 9900
Ship(0).Size.x = 32
Ship(0).Size.y = 32
Ship(0).Angle = 0
Ship(0).Ammo = 1500
Ship(0).maxammo = 2000
Ship(0).Sheilds = 800
Ship(0).Armor = 80
Ship(0).speed = 0
Ship(0).maxspeed = 24
Ship(0).botsowned = 5
Ship(0).lastshot = 0
Ship(1).pos.x = 9900
Ship(1).pos.y = 100
Ship(1).Size.x = 32
Ship(1).Size.y = 32
Ship(1).Angle = 180
Ship(1).Ammo = 1500
Ship(1).maxammo = 2000
Ship(1).Sheilds = 800
Ship(1).Armor = 80
Ship(1).speed = 0
Ship(1).maxspeed = 24
Ship(1).botsowned = 5
Ship(1).lastshot = 0
ii = 100
for i = 0 to 99
inc ii,1
if i < 5
Bot(i).num = ii
Bot(i).owned = 0
endif
if i > 4
Bot(i).num = ii
Bot(i).owned = -1
endif
next i
for i = 100 to 199
inc ii,1
if i < 105
Bot(i).num = ii
Bot(i).owned = 1
endif
if i > 104
Bot(i).num = ii
Bot(i).owned = -1
endif
next i
for i = 0 to 4
Bot(i).pos.x = 100 + rnd(100)
Bot(i).pos.y = 9900 - rnd(100)
Bot(i).Size.x = 4
Bot(i).Size.y = 4
Bot(i).Ammo = 750
Bot(i).maxammo = 800
Bot(i).Sheilds = 200
Bot(i).Armor = 60
Bot(i).speed = 0
Bot(i).maxspeed = 32
Bot(i).lastshot = 0
Bot(i).owned = 0
Bot(i).following = 1
next i
for i = 100 to 104
Bot(i).pos.x = 9900 - rnd(100)
Bot(i).pos.y = 100 + rnd(100)
Bot(i).Size.x = 4
Bot(i).Size.y = 4
Bot(i).Angle = 180
Bot(i).Ammo = 750
Bot(i).maxammo = 800
Bot(i).Sheilds = 200
Bot(i).Armor = 60
Bot(i).speed = 0
Bot(i).maxspeed = 32
Bot(i).lastshot = 0
Bot(i).owned = 1
Bot(i).following = 2
next i
Ship(0).num = 1
Ship(1).num = 2
type stars
pos as vector2
size as vector2
angle as integer
endtype
dim star(999) as stars
cls
for i = 0 to 999
star(i).pos.x = rnd(12000)
star(i).pos.y = rnd(12000)
star(i).size.x = rnd(31) + 1
star(i).size.y = rnd(31) + 1
star(i).angle = rnd(359)
next i
for i = 0 to 999
if sprite exist(i + 3000) = 1 then delete sprite i + 3000
next i
do
cls
gosub Draw_Stars
gosub Ship1_Movement
gosub Ship1_Bullets
gosub Ship1_Bots_Movement
sync
loop
Draw_Stars:
for i = 0 to 499
sprite i + 1000,star(i).pos.x - Scroll.x,star(i).pos.y - Scroll.y,img_Bullet1
set sprite i + 1000,1,1
size sprite i + 1000,star(i).size.x,star(i).size.y
offset sprite i + 1000,sprite width(i + 1000) / 2,sprite height(i + 1000) / 2
rotate sprite i + 1000,star(i).angle
next i
for i = 500 to 999
sprite i + 1000,star(i).pos.x - Scroll.x,star(i).pos.y - Scroll.y,img_Bullet2
set sprite i + 1000,1,1
size sprite i + 1000,star(i).size.x,star(i).size.y
offset sprite i + 1000,sprite width(i + 1000) / 2,sprite height(i + 1000) / 2
rotate sprite i + 1000,star(i).angle
next i
return
Ship1_Movement:
if keystate(17) = 1 and Ship(0).speed <= Ship(0).maxspeed
inc Ship(0).speed,1
endif
if keystate(31) = 1 and Ship(0).speed => (Ship(0).maxspeed * -1)
dec Ship(0).speed,1
endif
if keystate(48) = 1 and Ship(0).speed <> 0
if Ship(0).speed > 0 then dec Ship(0).speed,8
if Ship(0).speed < 0 then inc Ship(0).speed,8
endif
Ship(0).pos.x = NXV(Ship(0).pos.x,Ship(0).angle,Ship(0).speed)
Ship(0).pos.y = NYV(Ship(0).pos.y,Ship(0).angle,Ship(0).speed)
if Ship(0).pos.x - Scroll.x < 250 then dec Scroll.x,Ship(0).maxspeed
if Ship(0).pos.x - Scroll.x > 1024 - 250 then inc Scroll.x,Ship(0).maxspeed
if Ship(0).pos.y - Scroll.y < 250 then dec Scroll.y,Ship(0).maxspeed
if Ship(0).pos.y - Scroll.y > 768 - 250 then inc Scroll.y,Ship(0).maxspeed
if keystate(30) = 1 then dec Ship(0).angle,8
if keystate(32) = 1 then inc Ship(0).angle,8
sprite Ship(0).num,Ship(0).pos.x - Scroll.x,Ship(0).pos.y - Scroll.y,img_Ship1
set sprite 1,1,1
size sprite Ship(0).num,Ship(0).size.x,Ship(0).size.y
offset sprite 1,sprite width(1) / 2,sprite height(1) / 2
rotate sprite 1,Ship(0).angle
return
Ship1_Bullets:
for i = 0 to 39
Bullet1(0,i).Size.x = Ship(0).Size.x / 2
Bullet1(0,i).Size.y = Ship(0).Size.y / 2
if keystate(42) = 0 and Ship(0).ammo <= Ship(0).maxammo
inc Ship(0).ammo,1
endif
`left_shift
if keystate(42) = 1 and timer() > ls and timer() => Bullet1(0,i).life and Ship(0).ammo > 0
ls = timer() + 80
Bullet1(0,i).pos.x = Ship(0).pos.x
Bullet1(0,i).pos.y = Ship(0).pos.y
Bullet1(0,i).angle = Ship(0).angle
Bullet1(0,i).life = timer() + 80 * 40
dec Ship(0).ammo,1
endif
if timer() < Bullet1(0,i).life
Bullet1(0,i).pos.x = NXV(Bullet1(0,i).pos.x,Bullet1(0,i).angle,64)
Bullet1(0,i).pos.y = NYV(Bullet1(0,i).pos.y,Bullet1(0,i).angle,64)
sprite i + 3000,Bullet1(0,i).pos.x - Scroll.x,Bullet1(0,i).pos.y - Scroll.y,img_Bullet1
set sprite i + 3000,1,1
size sprite i + 3000,Bullet1(0,i).Size.x,Bullet1(0,i).Size.y
offset sprite i + 3000,sprite width(i + 3000) / 2,sprite height(i + 3000) / 2
endif
if timer() => Bullet1(0,i).life
if sprite exist(i + 3000) = 1 then delete sprite i + 3000
endif
next i
return
Ship1_Bots_Movement:
for i = 0 to Ship(0).Botsowned - 1
if Bot(i).following = 1
Bot(i).followpoint.x = Ship(0).pos.x - Scroll.x
Bot(i).followpoint.y = Ship(0).pos.y - Scroll.y
if Get_Dist(Bot(0).pos.x - Scroll.x,Bot(0).pos.y - Scroll.y,Bot(i).followpoint.x,Bot(i).followpoint.y) > 100
temp_angle# = Find_Angle(Bot(0).pos.x - Scroll.x,Bot(0).pos.y - Scroll.y,Bot(i).followpoint.x,Bot(i).followpoint.y,Bot(i).angle)
if Get_Dist(Bot(0).pos.x - Scroll.x,Bot(0).pos.y - Scroll.y,Bot(i).followpoint.x,Bot(i).followpoint.y) > 400
if Get_Dist(Bot(0).pos.x - Scroll.x,Bot(0).pos.y - Scroll.y,Bot(i).followpoint.x,Bot(i).followpoint.y) <= 500
Bot(i).speed = 32
endif
endif
if Get_Dist(Bot(0).pos.x - Scroll.x,Bot(0).pos.y - Scroll.y,Bot(i).followpoint.x,Bot(i).followpoint.y) > 300
if Get_Dist(Bot(0).pos.x - Scroll.x,Bot(0).pos.y - Scroll.y,Bot(i).followpoint.x,Bot(i).followpoint.y) <= 400
Bot(i).speed = 16
endif
endif
if Get_Dist(Bot(0).pos.x - Scroll.x,Bot(0).pos.y - Scroll.y,Bot(i).followpoint.x,Bot(i).followpoint.y) > 200
if Get_Dist(Bot(0).pos.x - Scroll.x,Bot(0).pos.y - Scroll.y,Bot(i).followpoint.x,Bot(i).followpoint.y) <= 300
Bot(i).speed = 8
endif
endif
if Get_Dist(Bot(0).pos.x - Scroll.x,Bot(0).pos.y - Scroll.y,Bot(i).followpoint.x,Bot(i).followpoint.y) > 100
if Get_Dist(Bot(0).pos.x - Scroll.x,Bot(0).pos.y - Scroll.y,Bot(i).followpoint.x,Bot(i).followpoint.y) <= 200
Bot(i).speed = 1
endif
endif
if temp_angle# < 0 then dec Bot(i).angle,8
if temp_angle# > 0 then inc Bot(i).angle,8
endif
if Get_Dist(Bot(0).pos.x - Scroll.x,Bot(0).pos.y - Scroll.y,Bot(i).followpoint.x,Bot(i).followpoint.y) < 16
temp_angle# = Find_Angle(Bot(0).pos.x - Scroll.x,Bot(0).pos.y - Scroll.y,Bot(i).followpoint.x,Bot(i).followpoint.y,Bot(i).angle)
Bot(i).speed = 0
if temp_angle# < 0 then dec Bot(i).angle,8
if temp_angle# > 0 then inc Bot(i).angle,8
endif
if Get_Dist(Bot(0).pos.x - Scroll.x,Bot(0).pos.y - Scroll.y,Bot(i).followpoint.x,Bot(i).followpoint.y) > 500 then Bot(i).speed = 0
endif
if Bot(i).speed > Bot(i).maxspeed then Bot(i).speed = Bot(i).maxspeed
if Bot(i).speed < Bot(i).maxspeed * -1 then Bot(i).speed = Bot(i).maxspeed * -1
Bot(i).pos.x = NXV(Bot(i).pos.x,Bot(i).angle,Bot(i).speed)
Bot(i).pos.y = NYV(Bot(i).pos.y,Bot(i).angle,Bot(i).speed)
sprite Bot(i).num,Bot(i).pos.x - Scroll.x,Bot(i).pos.y - Scroll.y,img_Ship1
set sprite Bot(i).num,1,1
size sprite Bot(i).num,Bot(i).size.x,Bot(i).size.y
offset sprite Bot(i).num,sprite width(Bot(i).num) / 2,sprite height(Bot(i).num) / 2
rotate sprite Bot(i).num,Bot(i).angle
next i
return
function Find_Angle(X1,Y1,X2,Y2,angle1)
x# = (X1 - X2)
y# = (Y2 - Y1)
angle# = wrapvalue(atanfull(x#,y#))
d# = int(abs(angle# - wrapvalue(angle1)))
if d# > 180 then dec d#,360
endfunction d#
function Get_Dist(X1,X2,Y1,Y2)
dist = int(sqrt(((X2 - X1) ^ 2) + ((Y2 - Y1) ^ 2)))
endfunction dist
function NXV(cur,angle,dest)
value# = cur + sin(angle) * dest
endfunction value#
function NYV(cur,angle,dest)
value# = cur + cos(angle + 180) * dest
endfunction value#
function Title(x,y,title$)
box x - (text width(title$) / 2),y,x + (text width(title$) / 2),y + text height(title$),rgb(0,0,100),rgb(0,0,255),rgb(100,0,0),rgb(255,0,0)
ink rgb(255,255,255),rgb(0,0,0)
set text size 72
text x - (text width(title$) / 2),y,title$
endfunction
function Button(txt$,x,y)
ink rgb(0,255,0),rgb(0,0,0)
set text size 48
text x,y,txt$
if mousex() > x and mousex() < x + text width(txt$)
if mousey() > y and mousey() < y + text height(txt$)
ink rgb(255,255,255),rgb(0,0,0)
set text size 25
text x,y,txt$
Pressed = mouseclick()
endif
endif
endfunction Pressed
function GameTime(BaseTime as integer)
local TimeValue as integer
local TextTime as string
TimeValue=abs( timer() - BaseTime )
` Centi-Seconds
TimeValue = TimeValue / 10
TextTime=PadNumber( TimeValue mod 100 , 2)
` Seconds
TimeValue = TimeValue / 100
TextTime=PadNumber( TimeValue mod 60, 2) + ":" + TextTime
` Minutes
TimeValue = TimeValue / 60
TextTime=PadNumber( TimeValue mod 60, 2) + ":" + TextTime
` Hours
TimeValue = TimeValue / 60
TextTime=PadNumber( TimeValue mod 60, 2) + ":" + TextTime
endfunction TextTime
function PadNumber(n as integer, Size as integer)
local PaddedNumber as string
PaddedNumber=str$(n)
while len(PaddedNumber) < Size
PaddedNumber = "0" + PaddedNumber
endwhile
endfunction PaddedNumber
Thanks
MickeyIII