Advertisement
Guest User

Untitled

a guest
Jan 17th, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.29 KB | None | 0 0
  1.  
  2. // Project: Meteors
  3. // Created: 2018-11-22
  4. //James Sinclair
  5.  
  6. //*** Include required library files ***
  7. #include "CoreLibrary.agc"
  8. #include "TopScoresLibrary.agc"
  9. #include "GraphicsLibrary.agc"
  10. //****** Data Types ******
  11. type VelocityType
  12. x as float //x-offset per move
  13. y as float //y-offset per move
  14. endtype
  15. type SpaceshipType
  16. // imgID as integer //Image ID
  17. sprID as integer //Sprite ID
  18. turning as float //Turning angle/frame
  19. thrust as float //Engine’s thrust
  20. velocity as VelocityType //Current velocity
  21. dragfactor as float //Drag factor applied to movement
  22. missile as MissileType //ship’s missile
  23. endtype
  24. type MissileType
  25. // imgID as integer //Image ID
  26. sprID as integer //Sprite ID
  27. thrust as float //Missile’s thrust
  28. velocity as VelocityType //velocity
  29. moves as integer //Moves made
  30. explodeimg as integer //Contains ID of explode image
  31. explodespr as integer //Contains ID of explode sprite
  32. endtype
  33. type MeteorType
  34. // imgID as integer //Image ID
  35. sprID as integer //Sprite ID
  36. velocity as VelocityType //velocity
  37. endtype
  38.  
  39. type NumericTextType
  40. txtID as integer
  41. value as integer
  42. endtype
  43.  
  44. type BackgroundType
  45. sprID as integer
  46. endtype
  47.  
  48. type YouLostType
  49. sprID as integer
  50. endtype
  51.  
  52. type YouWinType
  53. sprID as integer
  54. endtype
  55.  
  56. type GameType
  57. imgIDs as integer[TOTALIMAGES]
  58. meteor as MeteorType[TOTALMETEOR] //Meteors
  59. ship as SpaceshipType
  60. explode as integer[3] //Contains explosion
  61. lives as NumericTextType
  62. score as NumericTextType
  63. MeteorsLeft as NumericTextType
  64. Background as BackgroundType
  65. MeteorsShot as NumericTextType
  66. Youlost as YouLostType
  67. Youwin as YouWinType
  68. YouWinOrLost as NumericTextType
  69. endtype
  70.  
  71. #constant RANGE = 100
  72. #constant TOTALMETEOR = 5
  73. #constant TOTALIMAGES = 6
  74. #constant TOTALLIVES = 3
  75. //****** Global Variables ******
  76. global game as GameType
  77. //***************************************
  78. //*** Main program ***
  79. //***************************************
  80. InitialiseScreen(1024,768,"Meteors",LoadImage("background.png"),%1111)
  81. ShowSplashScreen("Splash.png")
  82. LoadResources()
  83. HideSplashScreen(4)
  84. InitialiseGameVariables()
  85. CreateInitialLayout()
  86.  
  87. repeat
  88.  
  89.  
  90.  
  91. HandleUserInput(GetUserInput())
  92. HandleOther()
  93. Sync()
  94.  
  95. if game.MeteorsShot.value = 5
  96. repeat
  97. InitialLevel2()
  98. until game.MeteorsLeft.value = 1
  99. endif
  100. //Winorloss()
  101.  
  102. until game.YouWinOrLost.value = 1
  103.  
  104. ShowEndMessage("EndScreen.png",game.score.value)
  105. end
  106.  
  107. //***************************************
  108. //*** Functions ***
  109. //***************************************
  110. function LoadResources()
  111. //*** Load images ***
  112. global imagenames as string[TOTALIMAGES]=["","Asteroid","SpaceShip2","Missile","Spacescreen","YouLost","YouWin"]
  113. for c = 1 to TOTALIMAGES
  114. game.imgIDs[c] = LoadImage(imagenames[c]+".png")
  115. next c
  116. game.ship.missile.explodeimg = LoadImage("Explode.png")
  117. for c = 1 to 3
  118. game.explode[c] = LoadImage("Explode0"+str(c)+ ".png")
  119. next c
  120. endfunction
  121.  
  122. function InitialiseGameVariables()
  123. //*** Set the velocity ***
  124. // game.meteor.velocity.x = 0.5
  125. // game.meteor.velocity.y = 0.7
  126. //*** Set ship’s turning angle per frame ***
  127. game.ship.turning = 3
  128. //*** Set ship’s thrust ***
  129. game.ship.thrust = 0.005
  130. //*** Set ship’s drag factor ***
  131. game.ship.dragfactor = 0.995
  132. //*** Set missile’s thrust ***
  133. game.ship.missile.thrust = 2
  134. //*** Set Lives value ***
  135. game.lives.value = TOTALLIVES
  136. //*** Set Score value ***
  137. game.score.value = 0
  138. game.MeteorsLeft.value = 0
  139. // Set Value of meteors Destroyed
  140. game.MeteorsShot.value = 0
  141. // Set the Value of the games win/lost state
  142. game.YouWinOrLost.value = 0
  143.  
  144. endfunction
  145. function CreateInitialLayout()
  146. //*** Create, size and position meteor sprite ***
  147. for c = 1 to TOTALMETEOR
  148. game.meteor[c].sprID = CreateSprite(game.imgIDs[1])
  149. SetSpriteSize(game.meteor[c].sprID, Random2(4,10),-1)
  150. SetSpritePosition(game.meteor[c].sprID,Random2(1,90),Random2(1,90))
  151. game.meteor[c].velocity.x = 0 //Random2(-2,1)
  152. game.meteor[c].velocity.y = 0 //Random2(-2,2)
  153. next c
  154. //Background
  155. game.Background.sprID = CreateSprite(game.imgIDS[4])
  156. SetSpriteSize(game.background.sprID,100,100)
  157. SetSpriteDepth(game.background.sprID,11)
  158.  
  159. //*** Create, size and position spaceship ***
  160. game.ship.sprID = CreateSprite(game.imgIDs[2])
  161. SetSpriteSize(game.ship.sprID, 5,-1)
  162. SetSpritePosition(game.ship.sprID,48,48)
  163. //*** Define as an animated sprite ***
  164. SetSpriteAnimation(game.ship.sprID, 256, 197, 3)
  165. SetSpriteFrame(game.ship.sprID, 1)
  166. // Setup Asteroids
  167. for c = 1 to TOTALMETEOR
  168. SetSpriteAnimation(game.meteor[c].sprID, 250, 250, 16)
  169. next c
  170. //*** Add explosion to meteor ***
  171. //for c = 1 to 3
  172. // AddSpriteAnimationFrame(game.meteor[c].sprID,game.explode[c])
  173. //next c
  174. //Play Animation
  175. for c = 1 to TOTALMETEOR
  176. PlaySprite(game.meteor[c].sprID, 20,1,1,16)
  177.  
  178. //*** Define explosion as animated sprite ***
  179. game.ship.missile.explodespr = CreateSprite(game.ship.missile.explodeimg)
  180. SetSpriteAnimation(game.ship.missile.explodespr,250,250,3)
  181. SetSpriteSize(game.ship.missile.explodespr, 5,-1)
  182. SetSpriteVisible(game.ship.missile.explodespr,0)
  183. next c
  184. // lives text
  185. game.lives.txtID = CreateText("Lives: "+Str(game.lives.value))
  186. SetTextPosition(game.lives.txtID, 12, 3.8)
  187. SetTextColor(game.lives.txtID, 255,255,255,255)
  188.  
  189. //Score Text
  190. game.score.txtID = CreateText("Score: "+Str(game.score.value))
  191. SetTextAlignment(game.score.txtID, 2)
  192. SetTextPosition(game.score.txtID, 92, 3.8)
  193. SetTextColor(game.score.txtID, 255,255,255,255)
  194.  
  195. // Meteors shot text
  196. game.MeteorsShot.txtID = CreateText("Meteors destroyed: "+str(game.MeteorsShot.value))
  197. SetTextAlignment(game.MeteorsShot.txtID, 2)
  198. SetTextPosition(game.MeteorsShot.txtID, 68, 3.8)
  199. SetTextColor(game.MeteorsShot.txtID, 255,255,255,255)
  200.  
  201. endfunction
  202.  
  203.  
  204. function HandleOther()
  205. //*** Move meteor ***
  206. for c = 1 to TOTALMETEOR
  207. MoveSpriteWithWrap(game.meteor[c].sprID, game.meteor[c].velocity.x, game.meteor[c].velocity.y)
  208. next c
  209. //*** Move spaceship ***
  210. MoveSpriteWithWrap(game.ship.sprID, game.ship.velocity.x, game.ship.velocity.y)
  211. //*** Adjust velocity for drag ***
  212. game.ship.velocity.x = game.ship.velocity.x * game.ship.dragfactor
  213. game.ship.velocity.y = game.ship.velocity.y * game.ship.dragfactor
  214.  
  215. //*** If it exists, move missile ***
  216. if GetSpriteExists(game.ship.missile.sprID) = 1
  217. MoveMissile()
  218. endif
  219. //*** If missile explosion playing enlarge and fade current frame ***
  220. if GetSpritePlaying(game.ship.missile.explodespr) = 1
  221. SetSpriteSize(game.ship.missile.explodespr, GetSpriteCurrentFrame(game.ship.missile.explodespr)*5, -1)
  222. SetSpriteColorAlpha(game.ship.missile.explodespr, 255 - GetSpriteCurrentFrame(game.ship.missile.explodespr)*70)
  223. //*** If missile explosion animation done, make invisible ***
  224. elseif GetSpritePlaying(game.ship.missile.explodespr) = 0
  225. SetSpriteVisible(game.ship.missile.explodespr,0)
  226. endif
  227. //*** if meteor playing***
  228. for c = 1 to TOTALMETEOR
  229. if GetSpritePlaying(game.meteor[c].sprID) = 1
  230. if GetSpriteCurrentFrame(game.meteor[c].sprID) > 16
  231. //*** expand and fade***
  232. SetSpriteSize(game.meteor[c].sprID,(GetSpriteCurrentFrame(game.meteor[c].sprID)-16)*5, -1)
  233. SetSpriteColorAlpha(game.meteor[c].sprID, 255 - (GetSpriteCurrentFrame(game.meteor[c].sprID)-16)*70)
  234. else
  235. // *** move meteor ***
  236. MoveSpriteWithWrap(game.meteor[c].sprID, game.meteor[c].velocity.x, game.meteor[c].velocity.y)
  237. endif
  238. //If its not playing, explosion finished, delete sprite ***
  239. else
  240. DeleteSprite(game.meteor[c].sprID)
  241. endif
  242. next c
  243.  
  244. //*** if thruster animation has stopped, show frame 1 ***
  245. if GetSpritePlaying(game.ship.sprID) = 0
  246. SetSpriteFrame(game.ship.sprID, 1)
  247. endif
  248.  
  249. for c = 1 to TOTALMETEOR
  250. if GetSpriteCollision(game.ship.sprID, game.meteor[c].sprID) = 1
  251. DeleteSprite(game.meteor[c].sprID)
  252. livesLost(c)
  253. Meteorsdestroyed()
  254. endif
  255. next c
  256. endfunction
  257.  
  258. function GetUserInput()
  259. //*** Read keyboard ***
  260. //** If its the Left Arrow key, result is 1 ***
  261. if GetRawKeyState(37) = 1
  262. result = 1
  263. //** If its the Right arrow key result is 2 **
  264. elseif GetRawKeyState(39) = 1
  265. result = 2
  266. //** If it’s Up arrow result is 3 **
  267. elseif GetRawKeyState(38) = 1
  268. result = 3
  269. //** If it’s Space bar, result is 4 **
  270. elseif GetRawKeyState(32) = 1
  271. result = 4
  272. //** Anything else, result is zero **
  273. else
  274. result = 0
  275. endif
  276. endfunction result
  277.  
  278. function HandleUserInput(in as integer)
  279. //*** If A pressed, rotate anticlockwise ***
  280. if in = 1
  281. SetSpriteAngle(game.ship.sprID, GetSpriteAngle(game.ship.sprID) - game.ship.turning)
  282. //*** If S pressed, rotate clockwise ***
  283. elseif in = 2
  284. SetSpriteAngle(game.ship.sprID, GetSpriteAngle(game.ship.sprID) + game.ship.turning)
  285. //*** If space pressed, fire thruster ***
  286. elseif in = 3
  287. thrustvelocity as VelocityType
  288. thrustvelocity = CalculateVelocity(game.ship.thrust,GetSpriteAngle(game.ship.sprID))
  289. game.ship.velocity.x = game.ship.velocity.x + thrustvelocity.x
  290. game.ship.velocity.y = game.ship.velocity.y + thrustvelocity.y
  291. //*** If thruster animation not playing, play it ***
  292. if GetSpritePlaying(game.ship.sprID)= 0
  293. PlaySprite(game.ship.sprID, 30, 0, 2, 3)
  294. endif
  295. //*** If Enter pressed, fire missile ***
  296. elseif in = 4
  297. FireMissile()
  298. endif
  299. endfunction
  300.  
  301. function CalculateVelocity(thrust as float, angle as float)
  302. result as VelocityType
  303. result.x = cos(angle)*thrust
  304. result.y = sin(angle)*thrust *(GetDeviceWidth()*1.0/GetDeviceHeight())
  305. endfunction result
  306.  
  307. //*** Creates missile and gives it a velocity ***
  308. function FireMissile()
  309. //*** If missile exists, exit ***
  310. if GetSpriteExists(game.ship.missile.sprID) = 1
  311. exitfunction
  312. endif
  313. //*** Create missile at centre of ship ***
  314. game.ship.missile.sprID = CreateSprite(game.imgIDs[3])
  315. SetSpriteSize(game.ship.missile.sprID, 3,-1)
  316. //*** Position it under centre of ship ***
  317. SetSpritePositionByOffset(game.ship.missile.sprID,GetSpriteXByOffset(game.ship.sprID),GetSpriteYByOffset(game.ship.sprID))
  318. SetSpriteDepth(game.ship.missile.sprID,9)
  319. //*** Rotate it to the same angle as ship ***
  320. SetSpriteAngle(game.ship.missile.sprID,GetSpriteAngle(game.ship.sprID))
  321. //*** Calculate velocity ***
  322. game.ship.missile.velocity = CalculateVelocity(game.ship.missile.thrust,GetSpriteAngle(game.ship.missile.sprID))
  323. //*** Set moves to zero ***
  324. game.ship.missile.moves = 0
  325. endfunction
  326.  
  327. //*** Moves missile one step along its trajectory. If ***
  328. //*** meteor hit, missile and meteor are destroyed. ***
  329. //*** If at end of range, missile is destroyed ***
  330. function MoveMissile()
  331. //*** If missile doesn’t exist, exit ***
  332. if GetSpriteExists(game.ship.missile.sprID) = 0
  333. exitfunction
  334. endif
  335. //*** Move the missile ***
  336. MoveSpriteWithWrap(game.ship.missile.sprID,game.ship.missile.velocity.x,game.ship.missile.velocity.y)
  337. //*** If missile has hit meteor ***
  338. for c = 1 to TOTALMETEOR
  339. if GetSpriteCollision(game.ship.missile.sprID,game.meteor[c].sprID) = 1
  340. //*** Destroy both ***
  341. inc game.score.value, 1*100 //game.meteor[c].sprID
  342. Meteorsdestroyed() //inc game.MeteorsShot.value, 1
  343. SetTextString(game.score.txtID,"Score: "+Str(game.score.value))
  344. // PlaySprite(game.meteor[c].sprID,20,0,17,19)
  345. DeleteSprite(game.ship.missile.sprID)
  346. DeleteSprite(game.meteor[c].sprID)
  347. //*** If no hit ***
  348.  
  349. else
  350. //*** Add to count of times missile has moved ***
  351. inc game.ship.missile.moves
  352. //*** If reached range, destroy missile and play explosion ***
  353. if game.ship.missile.moves >= RANGE
  354. SetSpritePositionByOffset(game.ship.missile.explodespr,GetSpriteXByOffset(game.ship.missile.sprID),GetSpriteYByOffset(game.ship.missile.sprID))
  355. SetSpriteVisible(game.ship.missile.explodespr,1)
  356. DeleteSprite(game.ship.missile.sprID)
  357. PlaySprite(game.ship.missile.explodespr,20,0)
  358. endif
  359.  
  360. endif
  361. next c
  362. endfunction
  363.  
  364. function livesLost(c as integer)
  365. dec game.lives.value
  366.  
  367. if game.lives.value > 0
  368.  
  369. SetTextString(game.lives.txtID,"Lives: "+Str(game.lives.value))
  370.  
  371. While GetSpriteCollision(game.ship.sprID, game.meteor[c].sprID) = 1
  372. SetSpritePosition(game.meteor[c].sprID, Random2(1,90), Random2(1,90))
  373. EndWhile
  374.  
  375. // reset ship
  376. setspriteframe(game.ship.sprID,1)
  377. SetSpritePosition(game.ship.sprID, 48, 48)
  378.  
  379. else
  380. exitfunction
  381. endif
  382. endfunction
  383.  
  384. function Meteorsdestroyed()
  385. inc game.MeteorsShot.value
  386.  
  387. if game.MeteorsShot.value > 0
  388. SetTextString(game.MeteorsShot.txtID,"Meteors destroyed: "+str(game.MeteorsShot.value))
  389. endif
  390. endfunction
  391.  
  392.  
  393. function InitialLevel2()
  394. if game.MeteorsShot.value = 5 and game.MeteorsLeft.value = 0
  395. Sleep(200)
  396. //Delete Sprites
  397. for c = 1 to TOTALMETEOR
  398. DeleteSprite(game.meteor[c].sprID)
  399. sync()
  400. next c
  401. DeleteSprite(game.ship.sprID)
  402. DeleteSprite(game.ship.missile.sprID)
  403. DeleteSprite(game.background.sprID)
  404. DeleteText(game.lives.txtID)
  405. DeleteText(game.score.txtID)
  406. DeleteText(game.MeteorsShot.txtID)
  407.  
  408. //Create Sprites
  409. //*** Create, size and position meteor sprite ***
  410. for c = 1 to TOTALMETEOR
  411. game.meteor[c].sprID = CreateSprite(game.imgIDs[1])
  412. SetSpriteSize(game.meteor[c].sprID, Random2(4,10),-1)
  413. SetSpritePosition(game.meteor[c].sprID,Random2(1,90),Random2(1,90))
  414. game.meteor[c].velocity.x = 0 //Random2(-2,1)
  415. game.meteor[c].velocity.y = 0 //Random2(-2,2)
  416. next c
  417. //Background
  418. game.Background.sprID = CreateSprite(game.imgIDS[4])
  419. SetSpriteSize(game.background.sprID,100,100)
  420. SetSpriteDepth(game.background.sprID,11)
  421.  
  422. //*** Create, size and position spaceship ***
  423. game.ship.sprID = CreateSprite(game.imgIDs[2])
  424. SetSpriteSize(game.ship.sprID, 5,-1)
  425. SetSpritePosition(game.ship.sprID,48,48)
  426. SetSpriteAnimation(game.ship.sprID, 256, 197, 3)
  427. SetSpriteFrame(game.ship.sprID, 1)
  428. // Setup Asteroids
  429. for c = 1 to TOTALMETEOR
  430. SetSpriteAnimation(game.meteor[c].sprID, 250, 250, 16)
  431. next c
  432. //*** Add explosion to meteor ***
  433. //for c = 1 to 3
  434. // AddSpriteAnimationFrame(game.meteor[c].sprID,game.explode[c])
  435. //next c
  436. //Play Animation
  437. for c = 1 to TOTALMETEOR
  438. PlaySprite(game.meteor[c].sprID, 20,1,1,16)
  439.  
  440. //*** Define explosion as animated sprite ***
  441. game.ship.missile.explodespr = CreateSprite(game.ship.missile.explodeimg)
  442. SetSpriteAnimation(game.ship.missile.explodespr,250,250,3)
  443. SetSpriteSize(game.ship.missile.explodespr, 5,-1)
  444. SetSpriteVisible(game.ship.missile.explodespr,0)
  445. next c
  446. // lives text
  447. game.lives.txtID = CreateText("Lives: "+Str(game.lives.value))
  448. SetTextPosition(game.lives.txtID, 12, 3.8)
  449. SetTextColor(game.lives.txtID, 255,255,255,255)
  450.  
  451. //Score Text
  452. game.score.txtID = CreateText("Score: "+Str(game.score.value))
  453. SetTextAlignment(game.score.txtID, 2)
  454. SetTextPosition(game.score.txtID, 92, 3.8)
  455. SetTextColor(game.score.txtID, 255,255,255,255)
  456.  
  457. // Meteors shot text
  458. game.MeteorsShot.txtID = CreateText("Meteors destroyed: "+str(game.MeteorsShot.value))
  459. SetTextAlignment(game.MeteorsShot.txtID, 2)
  460. SetTextPosition(game.MeteorsShot.txtID, 68, 3.8)
  461. SetTextColor(game.MeteorsShot.txtID, 255,255,255,255)
  462. sync()
  463. inc game.MeteorsLeft.value, 1
  464. endif
  465.  
  466. endfunction
  467.  
  468. function Winorloss()
  469.  
  470. if game.lives.value <= 0
  471. // Delete sprites
  472. for c = 1 to TOTALMETEOR
  473. DeleteSprite(game.meteor[c].sprID)
  474. sync()
  475. next c
  476. DeleteSprite(game.ship.sprID)
  477. DeleteSprite(game.ship.missile.sprID)
  478. DeleteSprite(game.background.sprID)
  479. DeleteText(game.lives.txtID)
  480. DeleteText(game.score.txtID)
  481. DeleteText(game.MeteorsShot.txtID)
  482.  
  483. //Show "You Lost" screen
  484. game.YouLost.sprID = CreateSprite(game.imgIDS[5])
  485. SetSpriteSize(game.YouLost.sprID,100,100)
  486. SetSpriteDepth(game.YouLost.sprID,11)
  487. sync()
  488. sleep(200)
  489. inc game.YouWinOrLost.value, 1
  490.  
  491. elseif game.lives.value => 0 and game.MeteorsShot.value = 5
  492. // Delete sprites
  493. for c = 1 to TOTALMETEOR
  494. DeleteSprite(game.meteor[c].sprID)
  495. sync()
  496. next c
  497. DeleteSprite(game.ship.sprID)
  498. DeleteSprite(game.ship.missile.sprID)
  499. DeleteSprite(game.background.sprID)
  500. DeleteText(game.lives.txtID)
  501. DeleteText(game.score.txtID)
  502. DeleteText(game.MeteorsShot.txtID)
  503. sync()
  504.  
  505. //Show "You Win" screen
  506. game.Youwin.sprID = CreateSprite(game.imgIDS[6])
  507. SetSpriteSize(game.Youwin.sprID,100,100)
  508. SetSpriteDepth(game.Youwin.sprID,11)
  509. sync()
  510. Sleep(200)
  511. inc game.YouWinOrLost.value, 1
  512. endif
  513.  
  514. endfunction
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement