Posted: 22nd Aug 2011 13:35
I have been playing around with AppGameKit and want to know if I am on the right track with tpes and Arrays.
I am having trouble getting sprite collision to work any help would be appreciated.

The following code is the start of a little invaders game.
+ Code Snippet
rem
rem AGK Application
rem
SetVirtualResolution ( width, height )
SetSyncRate( 30, 0 )
d=2
bom=0
speed=2
type alien
	x# as integer
	y# as integer
	id as integer
	d as integer
endtype
dim monster[20] as alien
alive = 0

type bomb
	id as integer
	x#
	y#
	endtype
dim bullit[30] as bomb
 count = 0

#constant width=320
#constant height=480

backdrop =  Createsprite (LoadImage ( "background.png" ))
SetSpriteSize ( backdrop, 320   , 480 )

player = loadimage ("hero1.png")
 createsprite (21,player)
 setspritescale (21,.5,.5)
 setspriteposition (21,width/2,440)
for j = 1 to 5
    for p = 1 to 4
        alive = alive + 1
if alive > 20 then alive = 0
    image = LoadImage ( "greensquare.png" )
      monster[alive].id = createsprite (image)

 setspritescale (monster[alive].id,.5,.5)
    ( monster[alive].x#) = j
   ( monster[alive].y#) = p
   SetSpritePosition ( monster[alive].id ,40 * monster[alive].x# +60  ,40* monster[alive].y#    +60 )

next j
next p

do

for k = 0 to alive
 alive = alive + 1
    if alive > 20 then alive = 0
        if (monster[alive].id) >0
        if GetSpriteExists (monster[alive].id) = 1
      x#=  getspritex (monster[alive].id)
    y#=  getspritey(monster[alive].id)
    w = getspritewidth (monster[alive].id)
    if x# >320-w
    bom = 5
    else bom = 0
    endif
    if x# > width-w then d = 1
    if x#< 0 then d = 2
    if d = 1 then speed = -2
    if d=2 then speed = 2
endif
endif
next k

for k = 0 to alive
alive = alive + 1
    if alive > 20 then alive = 0
    if (monster[alive].id) >0
    if GetSpriteExists (monster[alive].id) = 1
 SetSpriteGroup( monster[alive].id, 1 )
x#=  getspritex (monster[alive].id)
y#=  getspritey(monster[alive].id)

y# = y# + bom
   x# = x# + speed
SetSpritePosition ( (monster[alive].id), x#, y# )

endif
endif

next k

for c=1 to 1
x# = GetJoystickX ( )
    y# = GetJoystickY ( )
// add input to sprites position
    SetSpritePosition ( 21, GetSpriteX ( 21 ) + x#*3, GetSpriteY ( 21 ) + y#*3 )

    // ensure sprite cannot move past left of screen
    if ( GetSpriteX ( 21 ) < 10 )
        SetSpriteX ( 21, 10 )
    endif

    // ensure sprite cannot move past right of screen
    if ( GetSpriteX ( 21 ) > 310 )
        SetSpriteX ( 21, 310 )
    endif

    // ensure sprite cannot move past top of screen
    if ( GetSpriteY ( 21 ) < 10 )
        SetSpriteY ( 21, 10 )
    endif

    // ensure sprite cannot move past bottom of screen
    if ( GetSpriteY ( 21 ) > 470 )
        SetSpriteY ( 21, 470 )
    endif
      s = GetButtonPressed(1)
         if s then shoot()
next c
updatebulit()
bullithit()

	Print ( "Drawing set up time = " + str ( GetDrawingSetupTime ( ) ) )
	Print ( "Drawing time        = " + str ( GetDrawingTime ( ) ) )
	Print ( "Sprites drawn       = " + str ( GetManagedSpriteDrawnCount ( ) ) )
	Print ( "Particles drawn     = " + str ( GetParticleDrawnQuadCount ( ) ) )
	Print ( "Frame rate          = " + str ( screenFPS ( ) ) )

    // update the screen
    sync ( )
loop

function shoot()
count = count + 1
if count > 30 then count = 0
    bul = loadimage ("blueround.png")
    bullit[count].id = createsprite (bul)
    SetSpriteGroup( bullit[count].id, 2 )
    bullit[count].x#  = getspritex(21)
    bullit[count].y# = getspritey(21)
setspritescale (bullit[count].id,.5,.5)
setspriteposition (bullit[count].id, bullit[count].x# ,bullit[count].y# )
endfunction

function updatebulit()
for k = 0 to count
count = count + 1
if count > 30 then count = 0
          if (bullit[count].id) >0
    if GetSpriteExists(bullit[count].id) = 1
    x#=  getspritex (bullit[count].id)
    y#=  getspritey(bullit[count].id)
    y#=y#-4
    x#=x#
setspriteposition (bullit[count].id, x# ,y# )
if y# < 60
    deletesprite (bullit[count].id)
    endif
    endif
    endif
next k
endfunction

function bullithit()

endfunction


If the above is done right I'm sure someone will find the code useful.
Posted: 22nd Aug 2011 15:54
Josk,
Good start but I'd say you need to go through and do some code organization and more tab indentation to help with readability.
Also, you may want to go through and rename some of your variables to help with that.
Posted: 22nd Aug 2011 18:48
Yeah it is a bit of a mess, just get carried away sometimes. Which means its unreadable when I come back to it some time later.
Posted: 22nd Aug 2011 19:24
And that's fine, even in my code I can get a little messy as well since I'm basically working my way through a problem.