Now I updated the code. I also tried to make some code for the enemy to fire at you, however, it tells me in the function that you must define enemybullets as a type, which I did, but it didn't tell me that in the function for EnemyBullets(). I thought I did this right, here's the updated code, see if I did the enemybullet code right. It tells me that the enemybullets variable must be a type, but I did that, not too sure where the code should be corrected:
; EnemyAI.bb - This is the source code for the Enemy Plane's Artificial Intelligence for the game, "Avoid the Plane"
; Hence, this is the action that you have to avoid from the plane to crash into you.
Graphics 800,600
SeedRnd MilliSecs()
; Initialize the BackBuffer() and AutoMidHandle Default Settings
SetBuffer BackBuffer()
AutoMidHandle True
; Load the images for both your airplane, and the enemy airplane
; First, here are the types that are needed to make this program work properly
Type enemyplane
Field x,y ; These are the coordinate variables for the enemy airplane
Field xv,yv ; These define the velocity of the enemy plane
Field image ; This defines the actual image of the airplane
End Type
; Now, here's the type for your airplane
Type playerplane
Field x,y ; These are the coordinate variables for your airplane
Field collision ; This defines the amount of collisions that you have suffered
Field image ; This defines your plane's image
Field hits ; Number of hits on the enemy plane
End Type
; Load the type for the bullets that the planes fire at each other
Type bullet
Field x,y ;The x and y coordinates
Field image ;The bullet image
End Type
Type enemybullet
Field x,y ; These are the x and y coordinates for the enemy's bullet
Field image ; This is the enemy's bullet image
End Type
; Below defines the constants used in this game
Const PLAYERSPEED = 10
Const ENEMYSPEED = 5
; These define the enemy's actions
Global enemy.enemyplane = New enemyplane
enemy\x = 400
enemy\y = 200
enemy\xv = Rand(-5,5)
enemy\yv = Rand(-5,5)
enemy\image = LoadImage("enemyplane.png")
; The following defines the creation of your airplane
Global player.playerplane = New playerplane
player\x = 400
player\y = 400
player\collision = 0
player\image = LoadImage("playerplane.png")
; The following loads the sounds for the plane engine, as well as a collision crash sound
Global enginesound = LoadSound("engine.wav")
Global collisionsound = LoadSound("crash.wav")
Global bulletsound = LoadSound("bullet.wav")
Global explosionsound = LoadSound("kaboom.wav")
; The following is the main loop for this program
While Not KeyDown(1)
; Make sure that the screen is clear
Cls
; Verify that all the text for this program is in the top-left corner
Text 0,0, "Press <LEFT> to Move Left, Press <RIGHT> to Move Right"
Text 0,12, "Press <UP> to Move Up, Press <DOWN> to Move Down "
Text 0,24, "Press <SPACE BAR> to shoot bullets, Press <ESC> to Quit: "
Text 0,48, "Collisions:\> " + player\collision
Text 0,60, "Hits:\> " + player\hits
; Verify whether or not the enemy plane has hit the borders
TestEnemyCollisions()
; Now, test the keyboard for this program
TestKeys()
SoundVolume enginesound, .25
LoopSound enginesound
propsound = PlaySound(enginesound)
CollisionCrash()
If KeyDown(57)
bullets.bullet = New bullet
bullets\x = player\x
bullets\y = player\y
bullets\image = LoadImage ("bullet.png")
PlaySound bulletsound
EndIf
UpDateBullets()
EnemyBullets()
;Assign the enemy's image
enemy\image = LoadImage("enemyplane.png")
MaskImage enemy\image, 255 ,255,255
; Once the player is above the enemy plane, move the enemy northwards towards the plane
If player\y > enemy\y
enemy\y = enemy\y + ENEMYSPEED
EndIf
; Once the player is to the left of the enemy plane, move the enemy to the west
If player\x < enemy\x
enemy\x = enemy\x - ENEMYSPEED
ChannelPan propsound, 1
EndIf
; Once the player is to the right of the enemy plane, move the enemy to the east
If player\x > enemy\x
enemy\x = enemy\x + ENEMYSPEED
ChannelPan propsound, -1
EndIf
; Once the player is below the enemy plane, move the enemy to the south
If player\y < enemy\y
enemy\y = enemy\y - ENEMYSPEED
; Shoot bullets at the player's plane
enemybullets.enemybullet = New enemybullet
enemybullets\x = enemy\x
enemybullets\y = enemy\y
enemybullets\image = LoadImage("enemybullet.png")
PlaySound bulletsound
EndIf
; Now, keep the enemy in motion, as his objective is to collide with your airspace
enemy\x = enemy\x + enemy\xv
enemy\y = enemy\y + enemy\yv
; Once the enemy reaches the edges of the screen, move him into viewable range
If enemy\x <= 0
enemy\x = 0
ElseIf enemy\x >= 800
enemy\x = 800
EndIf
If enemy\y <= 0
enemy\y = 0
ElseIf enemy\y >= 600
enemy\y = 600
EndIf
; Then, draw the images of both the enemy's plane and yours
DrawImage enemy\image,enemy\x,enemy\y
DrawImage player\image,player\x,player\y
Flip
; Slow the program down
Delay 50
Wend
; This now ends the main loop of this part of the program
; Here is the function for the Enemy Plane's Collisions
Function TestEnemyCollisions()
;If enemy hits a wall, reverse his velocity
If enemy\x < 0
enemy\xv = -enemy\xv
EndIf
If enemy\x > 800
enemy\xv = - enemy\xv
EndIf
If enemy\y < 0
enemy\yv = -enemy\yv
EndIf
If enemy\y > 600
enemy\yv = -enemy\yv
EndIf
End Function
; On the same token, here is the function for you to move left to right to avoid the enemy
Function TestKeys()
If KeyDown(200)
player\y = player\y - PLAYERSPEED
EndIf
If KeyDown(208)
player\y = player\y + PLAYERSPEED
EndIf
If KeyDown(203)
player\x = player\x - PLAYERSPEED
ChannelPan propsound, -1
EndIf
If KeyDown(205)
player\x = player\x + PLAYERSPEED
ChannelPan propsound, 1
EndIf
End Function
Function UpDateBullets()
For bullets.bullet = Each bullet
bullets\y = bullets\y - 5
DrawImage bullets\image,bullets\x,bullets\y
If ImagesOverlap(enemy\image,enemy\x,enemy\y,bullets\image,bullets\x,bullets\y)
SoundVolume explosionsound,.30
PlaySound explosionsound
Cls
Text 400,300, "Enemy down!"
player\hits = player\hits + 1
Flip
Delay 4000
;Delete each of the bullets (if there are any)
For bullets.bullet = Each bullet
Delete bullets
Next
;Choose the player's x and y coords
player\x = 400
player\y = 400
;Assign the player's image
player\image = LoadImage("playerplane.png")
;Choose the enemy's beginning x and y coords
enemy\x = 400
enemy\y = 200
;Give the enemy a velocity that is between -7 and 7
enemy\xv = Rand (-5,5)
enemy\yv = Rand (-5,5)
Return
EndIf
If bullets\y < 0
Delete bullets
EndIf
Next
End Function
Function CollisionCrash()
If (ImagesOverlap(player\image,player\x,player\y,enemy\image,enemy\x,enemy\y))
SoundVolume collisionsound, .35
PlaySound collisionsound
Cls
Text 300,300, "Oops, you crashed into the enemy!"
Flip
Delay 4000
; Remove any bullets that may be out there
For bullets.bullet = Each bullet
Delete bullets
Next
player\collision = player\collision + 1
player\x = 400
player\y = 400
enemy\x = 400
enemy\y = 200
enemy\xv = Rand(-5,5)
enemy\yv = Rand(-5,5)
player\image = LoadImage("playerplane.png")
EndIf
End Function
Function EnemyBullets()
If (ImagesOverlap(player\image,player\x,player\y,enemybullets\image,enemybullets\x,enemybullets\y))
SoundVolume explosionsound, .35
PlaySound explosionsound
Cls
Text 400,300, "You've been hit!"
Flip
Delay 4000
; Remove any of the bullets
For bullets.bullet = Each bullet
Delete bullets
Next
For enemybullet.enemybullets = Each enemybullets
player\misses = player\misses + 1
player\x = 400
player\y = 400
enemy\x = 400
enemy\y = 200
enemy\xv = Rand(-5,5)
enemy\yv = Rand(-5,5)
player\image = LoadImage("playerplane.png")
enemy\image = LoadImage("enemyplane.png")
Next
EndIf
End Function
|