try this one below.
i change the linux linebreaks to windows.
one mistake was to create a bullet sprite but not set the position.
means create bullet sprite was at position 0,0 until the set set position was called in your render functions.
the collision system used the real sprite position.
// Project: Starfighter
// Created: 2015-04-29
//SetPhysicsDebugOn() //see collision shape
// define booleans
#constant TRUE 1
#constant FALSE 0
// define keys
#constant KEY_ESC 27
#constant KEY_CTRL 17
#constant KEY_LEFT 37
#constant KEY_RIGHT 39
// define screen size
#constant SCREEN_W 320
#constant SCREEN_H 240
// define GameObject
type T_GameObject
id as Integer
alive as Integer
img as Integer
width as Integer
height as Integer
x as Float
y as Float
sx as Float
sy as Float
speed as Float
endtype
// set window properties
SetWindowTitle( "Starfighter" )
SetWindowSize(SCREEN_W, SCREEN_H, TRUE)
// set display properties
SetVirtualResolution(SCREEN_W, SCREEN_H)
// turn vertical sync on
SetVSync(TRUE)
SetClearColor(115, 58, 115)
SetBorderColor(8, 8, 8)
SetRawMouseVisible(FALSE)
// set player properties
global player as T_GameObject
player.img = LoadImage("starfighter.png")
SetImageMagFilter(player.img, 0)
player.width = GetImageWidth(player.img)
player.height = GetImageHeight(player.img)
player.sx = (SCREEN_W - player.width) * 0.5
player.sy = SCREEN_H - (player.height * 2)
player.x = player.sx
player.y = player.sy
player.speed = 2
// set weapon properties
global weapon as T_GameObject
weapon.img = LoadImage("doublebullet.png")
SetImageMagFilter(weapon.img, 0)
weapon.width = GetImageWidth(weapon.img)
weapon.height = GetImageHeight(weapon.img)
weapon.y = player.y + weapon.height * 2
weapon.speed = 3
// create weapon magazine
global mag as T_GameObject[]
// set enemy properties
global enemy as T_GameObject
enemy.img = LoadImage("enemy01.png")
SetImageMagFilter(enemy.img, 0)
enemy.width = GetImageWidth(enemy.img)
enemy.height = GetImageHeight(enemy.img)
enemy.speed = 0.75
// create enemy group
global enemies as T_GameObject[]
// initialize the game before playing
InitGame()
// main loop
while GetRawKeyPressed(KEY_ESC) <> 1
UpdateGame()
RenderGame()
Print(str(enemies.length))
Print(str(mag.length))
Sync()
endwhile
function InitGame()
// create sprites
player.id = CreateSprite(player.img)
player.alive = TRUE
SetSpritePosition(player.id, player.x, player.y)
CreateEnemies()
endfunction
function UpdateGame()
UpdatePlayer()
UpdateEnemies()
UpdateWeapon()
endfunction
function RenderGame()
RenderPlayer()
RenderWeapon()
RenderEnemies()
endfunction
function UpdatePlayer()
// shooting
if GetRawKeyPressed(KEY_CTRL) = 1
CreateBullet()
endif
// moving
if GetRawKeyState(KEY_LEFT) = 1
dec player.x, player.speed
elseif GetRawKeyState(KEY_RIGHT) = 1
inc player.x, player.speed
endif
// hold ship inside borders
if player.x < 0 then player.x = 0
if player.x > SCREEN_W - player.width then player.x = SCREEN_W - player.width
endfunction
function UpdateWeapon()
if mag.length <> -1
for i = mag.length to 0 step -1
if EnemyHit(i) = 1
DeleteSprite(mag[i].id)
mag.remove(i)
exit
endif
dec mag[i].y, mag[i].speed
if mag[i].y < 5
DeleteSprite(mag[i].id)
mag.remove(i)
endif
next i
endif
endfunction
function EnemyHit(bullet)
hit = 0
if enemies.length <> -1 and mag.length <> -1
for i = enemies.length to 0 step -1
if GetSpriteCollision(mag[bullet].id, enemies[i].id ) = 1
hit = 1
enemies[i].alive = FALSE
//SetSpriteColor( enemies[i].id,255,0,0,64)
exit
endif
next i
endif
endfunction hit
function UpdateEnemies()
// the distance an enemy can move left/right
// before changing the direction
distance = 50
if enemies.length <> -1
for i = enemies.length to 0 step -1
if enemies[i].alive = FALSE
DeleteSprite(enemies[i].id)
enemies.remove(i)
exit
endif
inc enemies[i].x, enemies[i].speed
inc enemies[i].y, Abs(enemies[i].speed) * 0.5
// change enemy direction after moving a distance
if enemies[i].x < enemies[i].sx - distance
enemies[i].speed = enemies[i].speed * -1
elseif enemies[i].x > enemies[i].sx + distance
enemies[i].speed = enemies[i].speed * -1
endif
if enemies[i].y > SCREEN_H
enemies[i].y = 0 - enemy.height
endif
next i
else
CreateEnemies()
endif
endfunction
function RenderPlayer()
SetSpritePosition(player.id, player.x, player.y)
endfunction
function RenderWeapon()
if mag.length <> -1
for i = 0 to mag.length
SetSpritePosition(mag[i].id, mag[i].x, mag[i].y)
next i
endif
endfunction
function RenderEnemies()
if enemies.length <> -1
for i = 0 to enemies.length
SetSpritePosition(enemies[i].id, enemies[i].x, enemies[i].y)
next i
endif
endfunction
function CreateBullet()
weapon.id = CreateSprite(weapon.img)
weapon.x = player.x + (player.width * 0.5) - (weapon.width * 0.5)
weapon.y = player.y
SetSpritePosition(weapon.id,weapon.x,weapon.y)
mag.insert(weapon)
endfunction
function CreateEnemies()
enemy.sy = 0 - enemy.height
enemy.y = enemy.sy
for j = 0 to 2
for i = 0 to 4
enemy.id = CreateSprite(enemy.img)
enemy.alive = TRUE
enemy.sx = i * (enemy.width + 40)
enemy.x = enemy.sx
enemies.insert(enemy)
next i
dec enemy.sy, 32
enemy.y = enemy.sy
next j
endfunction
AGK 108 (B)19 + AppGameKit V2 Beta .. : Windows 8.1 Pro 64 Bit : AMD Radeon R7 265 : Mac mini OS X 10.10 (Yosemite)