Advertisement
Guest User

Game

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