Take a look at this code:
sync on
sync rate 60
`Set up our global variables
global ScreenWidth
global ScreenHeight
global CannonX
global CannonY
global Speed# = 5.0
ScreenWidth = Screen Width()
ScreenHeight = Screen Height()
CannonX = ScreenWidth / 2
CannonY = ScreenHeight / 2
`Create a new User Defined Type and Arraylist
Type Bullet
X# as float
Y# as float
XSpeed# as float
YSpeed# as float
EndType
Dim BulletList() as Bullet
do
cls
mX = mouseX()
mY = mouseY()
if mouseclick() then New_Bullet(mX, mY)
Bullet_Update()
Render_Cannon(mX, mY)
Render_HUD()
`Control cannon's position with WASD keys
CannonX = CannonX + keystate(32) - keystate(30)
CannonY = CannonY + keystate(31) - keystate(17)
sync
loop
end
`Add a bullet to our array and store the information needed to travel to and beyond mouse position
function New_Bullet(mX as integer, mY as integer)
array insert at bottom BulletList()
Angle# = atanfull(mX - CannonX, mY - CannonY)
BulletList().X# = CannonX
BulletList().Y# = CannonY
BulletList().XSpeed# = (sin(Angle#) * Speed#)
BulletList().YSpeed# = (cos(Angle#) * Speed#)
endfunction
`Update our bullet's position, draw it to the screen, and remove it from the list if it exceeds the screen's bounds
function Bullet_Update()
array index to top BulletList()
ink rgb(255,0,0), 0
while array index valid(BulletList())
inc BulletList().X#, BulletList().XSpeed#
inc BulletList().Y#, BulletList().YSpeed#
circle int(BulletList().X#), int(BulletList().Y#), 1
if BulletList().X# > ScreenWidth || BulletList().X# < 0 || BulletList().Y# > ScreenHeight || BulletList().Y# < 0 then array delete element BulletList()
next array index BulletList()
endwhile
endfunction
`Draw our cannon
function Render_Cannon(mX as integer, mY as integer)
ink rgb(255,255,255), 0
CannonAngle# = atanfull(mX - CannonX, mY - CannonY)
CannonPointX = (sin(CannonAngle#) * 10) + CannonX
CannonPointY = (cos(CannonAngle#) * 10) + CannonY
line CannonX, CannonY, CannonPointX, CannonPointY
circle CannonX, CannonY, 3
endfunction
`Place text on the screen
function Render_HUD()
set cursor 0,0
Print "Screen FPS: " + str$(screen fps())
Print "Total bullets on screen: " + str$(Array Count(BulletList()) + 1)
Print "Use WASD keys to move the cannon. Click in the screen to shoot the cannon"
endfunction
It's a little movable turret that shoot towards the mouse cursor. If the bullet's velocity does not change, you would only need to use sin/cos/atan when the bullet is created which would save some resources. The math you'll use and the data you'll want to store is:
Angle# = atanfull(mX - CannonX, mY - CannonY)
BulletList().X# = CannonX
BulletList().Y# = CannonY
BulletList().XSpeed# = (sin(Angle#) * Speed#)
BulletList().YSpeed# = (cos(Angle#) * Speed#)
Then you can move the bullet by adding its 'speed' to it's current position:
inc BulletList().X#, BulletList().XSpeed#
inc BulletList().Y#, BulletList().YSpeed#
Then to draw it:
circle int(BulletList().X#), int(BulletList().Y#), 1
Hope this helps.
Everybody is a genius. But if you judge a fish by its ability to climb a tree, it will live its whole life believing that it is stupid.