Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Project: Meteors
- // Created: 2018-11-22
- //James Sinclair
- //*** Include required library files ***
- #include "CoreLibrary.agc"
- #include "TopScoresLibrary.agc"
- #include "GraphicsLibrary.agc"
- //****** Data Types ******
- type VelocityType
- x as float //x-offset per move
- y as float //y-offset per move
- endtype
- type SpaceshipType
- // imgID as integer //Image ID
- sprID as integer //Sprite ID
- turning as float //Turning angle/frame
- thrust as float //Engine’s thrust
- velocity as VelocityType //Current velocity
- dragfactor as float //Drag factor applied to movement
- missile as MissileType //ship’s missile
- endtype
- type MissileType
- // imgID as integer //Image ID
- sprID as integer //Sprite ID
- thrust as float //Missile’s thrust
- velocity as VelocityType //velocity
- moves as integer //Moves made
- explodeimg as integer //Contains ID of explode image
- explodespr as integer //Contains ID of explode sprite
- endtype
- type MeteorType
- // imgID as integer //Image ID
- sprID as integer //Sprite ID
- velocity as VelocityType //velocity
- endtype
- type NumericTextType
- txtID as integer
- value as integer
- endtype
- type BackgroundType
- sprID as integer
- endtype
- type YouLostType
- sprID as integer
- endtype
- type YouWinType
- sprID as integer
- endtype
- type GameType
- imgIDs as integer[TOTALIMAGES]
- meteor as MeteorType[TOTALMETEOR] //Meteors
- ship as SpaceshipType
- explode as integer[3] //Contains explosion
- lives as NumericTextType
- score as NumericTextType
- MeteorsLeft as NumericTextType
- Background as BackgroundType
- MeteorsShot as NumericTextType
- Youlost as YouLostType
- Youwin as YouWinType
- YouWinOrLost as NumericTextType
- endtype
- #constant RANGE = 100
- #constant TOTALMETEOR = 5
- #constant TOTALIMAGES = 6
- #constant TOTALLIVES = 3
- //****** Global Variables ******
- global game as GameType
- //***************************************
- //*** Main program ***
- //***************************************
- InitialiseScreen(1024,768,"Meteors",LoadImage("background.png"),%1111)
- ShowSplashScreen("Splash.png")
- LoadResources()
- HideSplashScreen(4)
- InitialiseGameVariables()
- CreateInitialLayout()
- repeat
- HandleUserInput(GetUserInput())
- HandleOther()
- Sync()
- if game.MeteorsShot.value = 5
- repeat
- InitialLevel2()
- until game.MeteorsLeft.value = 1
- endif
- //Winorloss()
- until game.YouWinOrLost.value = 1
- ShowEndMessage("EndScreen.png",game.score.value)
- end
- //***************************************
- //*** Functions ***
- //***************************************
- function LoadResources()
- //*** Load images ***
- global imagenames as string[TOTALIMAGES]=["","Asteroid","SpaceShip2","Missile","Spacescreen","YouLost","YouWin"]
- for c = 1 to TOTALIMAGES
- game.imgIDs[c] = LoadImage(imagenames[c]+".png")
- next c
- game.ship.missile.explodeimg = LoadImage("Explode.png")
- for c = 1 to 3
- game.explode[c] = LoadImage("Explode0"+str(c)+ ".png")
- next c
- endfunction
- function InitialiseGameVariables()
- //*** Set the velocity ***
- // game.meteor.velocity.x = 0.5
- // game.meteor.velocity.y = 0.7
- //*** Set ship’s turning angle per frame ***
- game.ship.turning = 3
- //*** Set ship’s thrust ***
- game.ship.thrust = 0.005
- //*** Set ship’s drag factor ***
- game.ship.dragfactor = 0.995
- //*** Set missile’s thrust ***
- game.ship.missile.thrust = 2
- //*** Set Lives value ***
- game.lives.value = TOTALLIVES
- //*** Set Score value ***
- game.score.value = 0
- game.MeteorsLeft.value = 0
- // Set Value of meteors Destroyed
- game.MeteorsShot.value = 0
- // Set the Value of the games win/lost state
- game.YouWinOrLost.value = 0
- endfunction
- function CreateInitialLayout()
- //*** Create, size and position meteor sprite ***
- for c = 1 to TOTALMETEOR
- game.meteor[c].sprID = CreateSprite(game.imgIDs[1])
- SetSpriteSize(game.meteor[c].sprID, Random2(4,10),-1)
- SetSpritePosition(game.meteor[c].sprID,Random2(1,90),Random2(1,90))
- game.meteor[c].velocity.x = 0 //Random2(-2,1)
- game.meteor[c].velocity.y = 0 //Random2(-2,2)
- next c
- //Background
- game.Background.sprID = CreateSprite(game.imgIDS[4])
- SetSpriteSize(game.background.sprID,100,100)
- SetSpriteDepth(game.background.sprID,11)
- //*** Create, size and position spaceship ***
- game.ship.sprID = CreateSprite(game.imgIDs[2])
- SetSpriteSize(game.ship.sprID, 5,-1)
- SetSpritePosition(game.ship.sprID,48,48)
- //*** Define as an animated sprite ***
- SetSpriteAnimation(game.ship.sprID, 256, 197, 3)
- SetSpriteFrame(game.ship.sprID, 1)
- // Setup Asteroids
- for c = 1 to TOTALMETEOR
- SetSpriteAnimation(game.meteor[c].sprID, 250, 250, 16)
- next c
- //*** Add explosion to meteor ***
- //for c = 1 to 3
- // AddSpriteAnimationFrame(game.meteor[c].sprID,game.explode[c])
- //next c
- //Play Animation
- for c = 1 to TOTALMETEOR
- PlaySprite(game.meteor[c].sprID, 20,1,1,16)
- //*** Define explosion as animated sprite ***
- game.ship.missile.explodespr = CreateSprite(game.ship.missile.explodeimg)
- SetSpriteAnimation(game.ship.missile.explodespr,250,250,3)
- SetSpriteSize(game.ship.missile.explodespr, 5,-1)
- SetSpriteVisible(game.ship.missile.explodespr,0)
- next c
- // lives text
- game.lives.txtID = CreateText("Lives: "+Str(game.lives.value))
- SetTextPosition(game.lives.txtID, 12, 3.8)
- SetTextColor(game.lives.txtID, 255,255,255,255)
- //Score Text
- game.score.txtID = CreateText("Score: "+Str(game.score.value))
- SetTextAlignment(game.score.txtID, 2)
- SetTextPosition(game.score.txtID, 92, 3.8)
- SetTextColor(game.score.txtID, 255,255,255,255)
- // Meteors shot text
- game.MeteorsShot.txtID = CreateText("Meteors destroyed: "+str(game.MeteorsShot.value))
- SetTextAlignment(game.MeteorsShot.txtID, 2)
- SetTextPosition(game.MeteorsShot.txtID, 68, 3.8)
- SetTextColor(game.MeteorsShot.txtID, 255,255,255,255)
- endfunction
- function HandleOther()
- //*** Move meteor ***
- for c = 1 to TOTALMETEOR
- MoveSpriteWithWrap(game.meteor[c].sprID, game.meteor[c].velocity.x, game.meteor[c].velocity.y)
- next c
- //*** Move spaceship ***
- MoveSpriteWithWrap(game.ship.sprID, game.ship.velocity.x, game.ship.velocity.y)
- //*** Adjust velocity for drag ***
- game.ship.velocity.x = game.ship.velocity.x * game.ship.dragfactor
- game.ship.velocity.y = game.ship.velocity.y * game.ship.dragfactor
- //*** If it exists, move missile ***
- if GetSpriteExists(game.ship.missile.sprID) = 1
- MoveMissile()
- endif
- //*** If missile explosion playing enlarge and fade current frame ***
- if GetSpritePlaying(game.ship.missile.explodespr) = 1
- SetSpriteSize(game.ship.missile.explodespr, GetSpriteCurrentFrame(game.ship.missile.explodespr)*5, -1)
- SetSpriteColorAlpha(game.ship.missile.explodespr, 255 - GetSpriteCurrentFrame(game.ship.missile.explodespr)*70)
- //*** If missile explosion animation done, make invisible ***
- elseif GetSpritePlaying(game.ship.missile.explodespr) = 0
- SetSpriteVisible(game.ship.missile.explodespr,0)
- endif
- //*** if meteor playing***
- for c = 1 to TOTALMETEOR
- if GetSpritePlaying(game.meteor[c].sprID) = 1
- if GetSpriteCurrentFrame(game.meteor[c].sprID) > 16
- //*** expand and fade***
- SetSpriteSize(game.meteor[c].sprID,(GetSpriteCurrentFrame(game.meteor[c].sprID)-16)*5, -1)
- SetSpriteColorAlpha(game.meteor[c].sprID, 255 - (GetSpriteCurrentFrame(game.meteor[c].sprID)-16)*70)
- else
- // *** move meteor ***
- MoveSpriteWithWrap(game.meteor[c].sprID, game.meteor[c].velocity.x, game.meteor[c].velocity.y)
- endif
- //If its not playing, explosion finished, delete sprite ***
- else
- DeleteSprite(game.meteor[c].sprID)
- endif
- next c
- //*** if thruster animation has stopped, show frame 1 ***
- if GetSpritePlaying(game.ship.sprID) = 0
- SetSpriteFrame(game.ship.sprID, 1)
- endif
- for c = 1 to TOTALMETEOR
- if GetSpriteCollision(game.ship.sprID, game.meteor[c].sprID) = 1
- DeleteSprite(game.meteor[c].sprID)
- livesLost(c)
- Meteorsdestroyed()
- endif
- next c
- endfunction
- function GetUserInput()
- //*** Read keyboard ***
- //** If its the Left Arrow key, result is 1 ***
- if GetRawKeyState(37) = 1
- result = 1
- //** If its the Right arrow key result is 2 **
- elseif GetRawKeyState(39) = 1
- result = 2
- //** If it’s Up arrow result is 3 **
- elseif GetRawKeyState(38) = 1
- result = 3
- //** If it’s Space bar, result is 4 **
- elseif GetRawKeyState(32) = 1
- result = 4
- //** Anything else, result is zero **
- else
- result = 0
- endif
- endfunction result
- function HandleUserInput(in as integer)
- //*** If A pressed, rotate anticlockwise ***
- if in = 1
- SetSpriteAngle(game.ship.sprID, GetSpriteAngle(game.ship.sprID) - game.ship.turning)
- //*** If S pressed, rotate clockwise ***
- elseif in = 2
- SetSpriteAngle(game.ship.sprID, GetSpriteAngle(game.ship.sprID) + game.ship.turning)
- //*** If space pressed, fire thruster ***
- elseif in = 3
- thrustvelocity as VelocityType
- thrustvelocity = CalculateVelocity(game.ship.thrust,GetSpriteAngle(game.ship.sprID))
- game.ship.velocity.x = game.ship.velocity.x + thrustvelocity.x
- game.ship.velocity.y = game.ship.velocity.y + thrustvelocity.y
- //*** If thruster animation not playing, play it ***
- if GetSpritePlaying(game.ship.sprID)= 0
- PlaySprite(game.ship.sprID, 30, 0, 2, 3)
- endif
- //*** If Enter pressed, fire missile ***
- elseif in = 4
- FireMissile()
- endif
- endfunction
- function CalculateVelocity(thrust as float, angle as float)
- result as VelocityType
- result.x = cos(angle)*thrust
- result.y = sin(angle)*thrust *(GetDeviceWidth()*1.0/GetDeviceHeight())
- endfunction result
- //*** Creates missile and gives it a velocity ***
- function FireMissile()
- //*** If missile exists, exit ***
- if GetSpriteExists(game.ship.missile.sprID) = 1
- exitfunction
- endif
- //*** Create missile at centre of ship ***
- game.ship.missile.sprID = CreateSprite(game.imgIDs[3])
- SetSpriteSize(game.ship.missile.sprID, 3,-1)
- //*** Position it under centre of ship ***
- SetSpritePositionByOffset(game.ship.missile.sprID,GetSpriteXByOffset(game.ship.sprID),GetSpriteYByOffset(game.ship.sprID))
- SetSpriteDepth(game.ship.missile.sprID,9)
- //*** Rotate it to the same angle as ship ***
- SetSpriteAngle(game.ship.missile.sprID,GetSpriteAngle(game.ship.sprID))
- //*** Calculate velocity ***
- game.ship.missile.velocity = CalculateVelocity(game.ship.missile.thrust,GetSpriteAngle(game.ship.missile.sprID))
- //*** Set moves to zero ***
- game.ship.missile.moves = 0
- endfunction
- //*** Moves missile one step along its trajectory. If ***
- //*** meteor hit, missile and meteor are destroyed. ***
- //*** If at end of range, missile is destroyed ***
- function MoveMissile()
- //*** If missile doesn’t exist, exit ***
- if GetSpriteExists(game.ship.missile.sprID) = 0
- exitfunction
- endif
- //*** Move the missile ***
- MoveSpriteWithWrap(game.ship.missile.sprID,game.ship.missile.velocity.x,game.ship.missile.velocity.y)
- //*** If missile has hit meteor ***
- for c = 1 to TOTALMETEOR
- if GetSpriteCollision(game.ship.missile.sprID,game.meteor[c].sprID) = 1
- //*** Destroy both ***
- inc game.score.value, 1*100 //game.meteor[c].sprID
- Meteorsdestroyed() //inc game.MeteorsShot.value, 1
- SetTextString(game.score.txtID,"Score: "+Str(game.score.value))
- // PlaySprite(game.meteor[c].sprID,20,0,17,19)
- DeleteSprite(game.ship.missile.sprID)
- DeleteSprite(game.meteor[c].sprID)
- //*** If no hit ***
- else
- //*** Add to count of times missile has moved ***
- inc game.ship.missile.moves
- //*** If reached range, destroy missile and play explosion ***
- if game.ship.missile.moves >= RANGE
- SetSpritePositionByOffset(game.ship.missile.explodespr,GetSpriteXByOffset(game.ship.missile.sprID),GetSpriteYByOffset(game.ship.missile.sprID))
- SetSpriteVisible(game.ship.missile.explodespr,1)
- DeleteSprite(game.ship.missile.sprID)
- PlaySprite(game.ship.missile.explodespr,20,0)
- endif
- endif
- next c
- endfunction
- function livesLost(c as integer)
- dec game.lives.value
- if game.lives.value > 0
- SetTextString(game.lives.txtID,"Lives: "+Str(game.lives.value))
- While GetSpriteCollision(game.ship.sprID, game.meteor[c].sprID) = 1
- SetSpritePosition(game.meteor[c].sprID, Random2(1,90), Random2(1,90))
- EndWhile
- // reset ship
- setspriteframe(game.ship.sprID,1)
- SetSpritePosition(game.ship.sprID, 48, 48)
- else
- exitfunction
- endif
- endfunction
- function Meteorsdestroyed()
- inc game.MeteorsShot.value
- if game.MeteorsShot.value > 0
- SetTextString(game.MeteorsShot.txtID,"Meteors destroyed: "+str(game.MeteorsShot.value))
- endif
- endfunction
- function InitialLevel2()
- if game.MeteorsShot.value = 5 and game.MeteorsLeft.value = 0
- Sleep(200)
- //Delete Sprites
- for c = 1 to TOTALMETEOR
- DeleteSprite(game.meteor[c].sprID)
- sync()
- next c
- DeleteSprite(game.ship.sprID)
- DeleteSprite(game.ship.missile.sprID)
- DeleteSprite(game.background.sprID)
- DeleteText(game.lives.txtID)
- DeleteText(game.score.txtID)
- DeleteText(game.MeteorsShot.txtID)
- //Create Sprites
- //*** Create, size and position meteor sprite ***
- for c = 1 to TOTALMETEOR
- game.meteor[c].sprID = CreateSprite(game.imgIDs[1])
- SetSpriteSize(game.meteor[c].sprID, Random2(4,10),-1)
- SetSpritePosition(game.meteor[c].sprID,Random2(1,90),Random2(1,90))
- game.meteor[c].velocity.x = 0 //Random2(-2,1)
- game.meteor[c].velocity.y = 0 //Random2(-2,2)
- next c
- //Background
- game.Background.sprID = CreateSprite(game.imgIDS[4])
- SetSpriteSize(game.background.sprID,100,100)
- SetSpriteDepth(game.background.sprID,11)
- //*** Create, size and position spaceship ***
- game.ship.sprID = CreateSprite(game.imgIDs[2])
- SetSpriteSize(game.ship.sprID, 5,-1)
- SetSpritePosition(game.ship.sprID,48,48)
- SetSpriteAnimation(game.ship.sprID, 256, 197, 3)
- SetSpriteFrame(game.ship.sprID, 1)
- // Setup Asteroids
- for c = 1 to TOTALMETEOR
- SetSpriteAnimation(game.meteor[c].sprID, 250, 250, 16)
- next c
- //*** Add explosion to meteor ***
- //for c = 1 to 3
- // AddSpriteAnimationFrame(game.meteor[c].sprID,game.explode[c])
- //next c
- //Play Animation
- for c = 1 to TOTALMETEOR
- PlaySprite(game.meteor[c].sprID, 20,1,1,16)
- //*** Define explosion as animated sprite ***
- game.ship.missile.explodespr = CreateSprite(game.ship.missile.explodeimg)
- SetSpriteAnimation(game.ship.missile.explodespr,250,250,3)
- SetSpriteSize(game.ship.missile.explodespr, 5,-1)
- SetSpriteVisible(game.ship.missile.explodespr,0)
- next c
- // lives text
- game.lives.txtID = CreateText("Lives: "+Str(game.lives.value))
- SetTextPosition(game.lives.txtID, 12, 3.8)
- SetTextColor(game.lives.txtID, 255,255,255,255)
- //Score Text
- game.score.txtID = CreateText("Score: "+Str(game.score.value))
- SetTextAlignment(game.score.txtID, 2)
- SetTextPosition(game.score.txtID, 92, 3.8)
- SetTextColor(game.score.txtID, 255,255,255,255)
- // Meteors shot text
- game.MeteorsShot.txtID = CreateText("Meteors destroyed: "+str(game.MeteorsShot.value))
- SetTextAlignment(game.MeteorsShot.txtID, 2)
- SetTextPosition(game.MeteorsShot.txtID, 68, 3.8)
- SetTextColor(game.MeteorsShot.txtID, 255,255,255,255)
- sync()
- inc game.MeteorsLeft.value, 1
- endif
- endfunction
- function Winorloss()
- if game.lives.value <= 0
- // Delete sprites
- for c = 1 to TOTALMETEOR
- DeleteSprite(game.meteor[c].sprID)
- sync()
- next c
- DeleteSprite(game.ship.sprID)
- DeleteSprite(game.ship.missile.sprID)
- DeleteSprite(game.background.sprID)
- DeleteText(game.lives.txtID)
- DeleteText(game.score.txtID)
- DeleteText(game.MeteorsShot.txtID)
- //Show "You Lost" screen
- game.YouLost.sprID = CreateSprite(game.imgIDS[5])
- SetSpriteSize(game.YouLost.sprID,100,100)
- SetSpriteDepth(game.YouLost.sprID,11)
- sync()
- sleep(200)
- inc game.YouWinOrLost.value, 1
- elseif game.lives.value => 0 and game.MeteorsShot.value = 5
- // Delete sprites
- for c = 1 to TOTALMETEOR
- DeleteSprite(game.meteor[c].sprID)
- sync()
- next c
- DeleteSprite(game.ship.sprID)
- DeleteSprite(game.ship.missile.sprID)
- DeleteSprite(game.background.sprID)
- DeleteText(game.lives.txtID)
- DeleteText(game.score.txtID)
- DeleteText(game.MeteorsShot.txtID)
- sync()
- //Show "You Win" screen
- game.Youwin.sprID = CreateSprite(game.imgIDS[6])
- SetSpriteSize(game.Youwin.sprID,100,100)
- SetSpriteDepth(game.Youwin.sprID,11)
- sync()
- Sleep(200)
- inc game.YouWinOrLost.value, 1
- endif
- endfunction
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement