Quote: "The bullet sprite size is far too small and for some reason it won't register collision at 7x7px.I scaled it up to 16x16 and it works."
The problem is he is moving it too far each loop. He has this piece of code:
BulletPosY(N)=BulletPosY(N)-40
It moves 40 pixels each time literally jumping over the enemy on occasion. He will have to make this number smaller, or the bullet bigger.
@ Ramon: I made a few changes and I suggest manipulating the x,y values yourself without using the sprite x(), sprite y() commands. I've found that this gives more control. If you have an questions, just ask.
Rem Declare Variables
StarAmount = 1000
xres = desktop width()
yres = desktop height()
Rem Screen Setup
Set display Mode xres, yres, 32, 1
Sync On
Sync Rate 60
Rem Types
type StarField
x
y
speed
endtype
Rem Arrays
Rem Star Array
dim stars(StarAmount) as StarField
For x = 1 to StarAmount
stars(x).x = rnd(xres)
stars(x).y = rnd(yres)
stars(x).speed = rnd(5)+1
Next x
Rem Bullet Array
MaxBullets = 1
Dim FiredBullet(MaxBullets)
Dim BulletPosX(MaxBullets)
Dim BulletPosY(MaxBullets)
// Hodgey's variables
playerx as integer
playery as integer
playery = yres - 50
enemyx as integer
enemyy as integer
enemyx = xres/2
enemyy = yres/2
`*******************************************************************************************************************
`***************************************************Main loop*******************************************************
`*******************************************************************************************************************
Do
cls
Gosub STARS
Rem print buttons
ink rgb(255,255,255),0
set text size 30
center text 320,300,"New Game"
center text 320,330,"Exit Game"
Rem click buttons
if mousex() > 256 and mousex() < 380 and mousey() > 308 and mousey() < 325 then ink rgb(0,120,0),0 : center text 320,300,"New Game"
if mousex() > 256 and mousex() < 380 and mousey() > 308 and mousey() < 325 and mouseclick()=1
gosub NEW_GAME
endif
if mousex() > 256 and mousex() < 380 and mousey() > 338 and mousey() < 355 then ink rgb(0,120,0),0 : center text 320,330,"Exit Game"
if mousex() > 256 and mousex() < 380 and mousey() > 338 and mousey() < 355 and mouseclick()=1
gosub EXIT_GAME
endif
Sync
Loop
`**************************************************************************************************************
`****************************************************NEW GAME**************************************************
`**************************************************************************************************************
NEW_GAME:
hide mouse
Rem Load Images
Load Image "Textures/player.bmp",1
Load Image "Textures/bullet.bmp",2
Load Image "Textures/enemyangrycube.bmp",3
player = 1
bullet = 2
enemy = 3
Rem Make Sprites
sprite player,xres/2,yres-40,player
sprite bullet,-10,0,bullet
sprite enemy,100,10,enemy
enemylife = 10
`****************************************************************************************************************
`***************************************MAIN LOOP NEW GAME*******************************************************
`****************************************************************************************************************
do
cls
Gosub STARS
If BulletCount > 0 Then Gosub MoveBullet
Rem Bullet Position X and Y
bulletPosX = sprite x (bullet) - image width(bullet)/2
bulletPosX = sprite y (bullet) - image height(bullet)/2
Print "Position BulletX: ";bulletPosX
Print "Position BulletY: ";bulletPosY
Print "Bullets On Screen: "+Str$(BulletCount)+" "
Rem Enemy Position X and Y
enemyPosX = sprite x (enemy) - image width(enemy)/2
enemyPosY = sprite y (enemy) - image height(enemy)/2
Print "Position EnemyX: ";enemyPosX
Print "Position EnemyY: ";enemyPosY
Rem Functions
playerController()
Rem Player Position X and Y
playerPosX = sprite x (player) - image width(player)/2
playerPosY = sprite y (player) - image height(player)/2
Print "Position player X: ";playerPosX
Print "Position player Y: ";playerPosY
Print "FPS :";screen fps()
Rem keep player on screen
if playerx > xres-20 then playerx = xres-20 // Hodgey's edit
if playery < 0 then playerx = 0 // Hodgey's edit
if sprite hit(bullet,enemy)
dec enemylife, 1
hide sprite bullet
endIf
if enemylife = 0
hide sprite enemy
inc enemylife, 10
// Hodgey's edit
enemyx = rnd(xres - sprite width(enemy)) + sprite width(enemy)
enemyy = rnd(yres - 100) + sprite height(enemy)
show sprite enemy
endIf
Print "Enemy Life: ";enemylife
// Hodgey's edit
// add movement
// change numbers for speed changes
if leftkey()=1 then dec playerx, 8
if rightkey()=1 then inc playerx, 8
// Hodgey's edit
// draw the player
sprite 1, playerx, playery, 1
// draw the enemy
sprite enemy, enemyx, enemyy, enemy
sync
loop
STARS:
for x = 1 to StarAmount
Inc stars(x).x, stars(x).speed
If stars(x).x > xres
stars(x).x = 0
stars(x).y = rnd(yres)
endIf
ink rgb(50*stars(x).speed,50*stars(x).speed,50*stars(x).speed),0
dot stars(x).x, stars(x).y
Next x
return
MoveBullet:
For N = 1 To MaxBullets
If FiredBullet(N)=1
BulletPosY(N)=BulletPosY(N)-20
sprite bullet, BulletPosX(N)+5,BulletPosY(N),bullet
Rem Check For Bullet Going Off Top Of Screen Here And Deal With It
If BulletPosY(N) <= -5
FiredBullet(N) = 0
Dec BulletCount
Endif
Rem Check For Hit Enemy Here And Deal With It
Endif
Next N
Return
AddBullet:
For N = 1 To MaxBullets
If FiredBullet(N)=0
FiredBullet(N)=1
BulletPosX(N) = PlayerPosX
BulletPosY(N) = PlayerPosY +40
show sprite bullet
Inc BulletCount
Exit
Endif
Next N
Return
`**************************************************************************************************************
`********************************************EXIT_GAME LOOP****************************************************
`**************************************************************************************************************
EXIT_GAME:
do
cls
center text xres/2,yres/2,"Thank you for playing"
Gosub STARS
if spacekey() = 1 then end
sync
loop
Rem Fun
function playerController()
if rightkey()= 1 then sprite 1,sprite x(1)+10,sprite y(1),1
if leftkey() = 1 then sprite 1,sprite x(1)-10,sprite y(1),1
If Spacekey()= 1 Then Gosub AddBullet
endFunction