I have revamped the code to my flight game, however, when you shoot the enemy airplane, and knock him down, the game tells you that you shot the plane, but then instead of the program restarting from the beginning, it winds up quitting on you. How would I go about letting the game reloop itself until you manually quit the game? This is quite weird, here's the code for that:
Something tells me that the Return command seems to be the culprit, but I could be wrong. I thought the Return was supposed to return to the beginning of the main program loop. What gives?
; 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 ; 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 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 ; 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 <ESC> to Quit Program: " Text 0,24, "Collisions:\> " + player\collision ; 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) If (ImagesOverlap(player\image,player\x,player\y,enemy\image,enemy\x,enemy\y)) PlaySound collisionsound 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) EndIf If KeyDown(57) bullets.bullet = New bullet bullets\x = player\x bullets\y = player\y bullets\image = LoadImage ("bullet.png") PlaySound bulletsound EndIf 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 260,300, "Looks like you shot down the enemy plane!" 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 ;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 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 ; 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
Something tells me that the Return command seems to be the culprit, but I could be wrong. I thought the Return was supposed to return to the beginning of the main program loop. What gives?