I am creating a game that is very similar to geometry wars and currently working on simple enemy artificial intelligience.
It's not even really intelligience, but I am trying to create it so that my enemies (which are just circles so far) will follow the user and try to collide into him in order to make him lose a life.
So far I have tried to create a function that will gather the User's current location and then rotate the enemy toward, then it will move the enemy foward based on its rotation, but my enemy still does not move.
In addition to that I am trying to create a loop so that it will randomly generate these enemies into random locations on the map, but not on top of the User. So neither of my codes are working. I am working quite diligiently on this project, but I am always willing to get outside input.
Please have a look at my code snippet and see if you can find the problem with it. I will post what the solution is if I have found it by the time you have read this.
I have compiled my project into a .rar file so you can have a look at it.
Rem Project: SpriteMovement
Rem Created: Sunday, June 12, 2011
Type Objects
x as integer
y as integer
id as integer
r as integer
EndType
Global background as objects
Global player as objects
Global Dim bullet(19) as objects
Global enemy as objects
Global NumOfBullets As Integer = 0
Global MaxBullets as integer = 0
Global Enemies As Integer
`variables for the ship/bullet
Global shipx as float = 562.0
Global bulletx as float = 562.0
Global shipy as float = 384.0
Global bullety as float = 384.0
Global enemyxvel as float `x velocity for ship
Global enemyyvel as float `y velocity for ship
Global enemyx as float = 320.0 // holds x value
Global enemyy as float = 240.0 // holds y value
sync on : sync rate 60
Set Display Mode 1124, 768, 32
velocity as float = 2.0 `change this to increase/decrease speed
`Giving Identification to the Sprites
background.id = 1
player.id = 2
bullet(19).id = 3
enemy.id = 4
Music = 1
set image colorkey 255,255,255
load image "media\avatar.bmp", player.id
Load image "media\TheGrid.png", background.id
Load image "media\bullet.bmp", bullet(19).id
Load image "media\SimpleEnemy.png", enemy.id
Load Music "media\Music.mp3", Music
`Draw the sprite once so we can offset it
`This allows for rotation point to be in the middle
Sprite background.id, 0, 0, background.id
Sprite bullet(19).id, shipx, shipy, bullet(19).id
Sprite player.id, shipx, shipy, player.id
Sprite enemy.id, enemyx, enemyy, enemy.id
Offset Sprite player.id, Sprite Width(player.id) / 2, Sprite Height(player.id) / 2
Offset Sprite bullet(19).id, Sprite Width(bullet(19).id) / 2, Sprite Height(bullet(19).id) / 2
Create_Enemies()
Draw Sprites First
`~~~~~~~~~~~~~~~~~~~~~~~~~~~~Main Loop~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Repeat
`Plays the Music for the Game
Play Music Music
`Loop Function Move_Avatar(
Move_Avatar()
`Loop Function Follow_Mouse()
Point_Sprite(player.id,mousex(),mousey())
move sprite player.id, 0.1
`Loop Function Shoot_Bullet()
Shoot_Bullet()
Enemy_AI(player.r)
sprite player.id, shipx, shipy, player.id
sync
Until escapekey() = 1
`~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~END MAIN LOOP~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
`******************************************************
`Function Move_Avatar
`Post: Avatar will respond accordingly to WASD key inputs
`******************************************************
Function Move_Avatar()
`if the user hits the up arrowkey, move the ship fowards at it's chosen angle
if keystate(17)=1
dec shipy, 2
endif
`if the user hits the down arrowkey, move the ship backwards at it's chosen angle
if keystate(30) = 1
dec shipx, 2
endif
`use the left and right arrowkeys to increase, decrease the angle
if keystate(31)=1
inc shipy, 2
endif
`do the same for the leftkey
if keystate(32)=1
inc shipx, 2
endif
EndFunction
`@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
`Function for Point_Sprite
`Post: Avatar will be pointed at the mouse
`@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
Function Point_Sprite(s,x,y)
`Giving Sprite Location
dx = Sprite X(s) - x
dy = Sprite Y(s) - y
`Rotating Sprite to rotation of the mouse
ang# = Wrapvalue(Atanfull(dx,dy)*-1)
Rotate Sprite s,ang#
Endfunction
`######################################################
`Function for Shoot_Bullet
`Post: Bullets can be shot from all directions from avatar
`######################################################
Function Shoot_Bullet()
sprite bullet(19).id, shipx, shipy, bullet(19).id
rotate sprite bullet(19).id, sprite angle(player.id)
If Mouseclick()
For x = 0 to 19
If bullet(x).id > 0
If Sprite Exist(bullet(x).id) = 1
Move Sprite (bullet(19).id), velocity
Endif
Endif
Next x
Endif
`Move Sprite bullet(x+1).id, 50
EndFunction
`######################################################
`Function for Create_Enemies()
`Post: Enemies will be drawn to random positions on the screen
`######################################################
Function Create_Enemies()
Sprite enemy.id, RND(1124), RND(768), enemy.id
Enemies = Enemies + 1
While Enemies < 5
Enemies = Enemies + 1
If Enemies < 5
Paste Sprite enemy.id, RND(1124), RND(768)
Endif
EndWhile
EndFunction
`******************************************************
`Function for Enemy_AI()
`Post: Enemies will follow the User's Avatar
`******************************************************
Function Enemy_AI(shipangle as float)
shipangle = 0.0
`find the x velocity to move the ship at our chosen angle
`using the formula x velocity = v*cos(angle)
enemyxvel = velocity * cos(shipangle - 90)
`there is a "-90" in there because the sprite is facing up/forward, equivalent to 90 degrees
`on the unit circle and not 0 degrees
`now for the y velocity
enemyyvel = velocity * sin(shipangle - 90)
shipangle = wrapvalue(player.id)
If Sprite X(enemy.id) <> Sprite X(player.id) or Sprite Y(enemy.id) <> Sprite Y(player.id)
rotate sprite enemy.id, shipangle
inc enemyx, enemyxvel
inc enemyy, enemyyvel
Endif
EndFunction
Beginning Programmer
Professional Nerd