Advertisement
Guest User

OLC Challenge #18

a guest
Jun 11th, 2018
252
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. SuperStrict
  2. SetGraphicsDriver(GLMax2DDriver())
  3. AppTitle = "Fly Tesko Home"
  4. Graphics(640, 480, 0, 144)
  5. SetVirtualResolution(160, 120)
  6. SetMaskColor(255, 0, 255)
  7.  
  8. 'Variables
  9. Global PlayerX:Float = 80
  10. Global PlayerY:Float = 140
  11. Global PlayerVX: Float = 0
  12. Global PlayerScore:Int = 0
  13.  
  14. Global BackgroundScrollY:Float = 0
  15.  
  16. Global SpawnTime:Int
  17. Global SpawnOldTime:Int
  18.  
  19. Global GameTime:Int
  20. Global GameOldTime:Int
  21.  
  22. Global GameState:Int = 0 '0 = Menu, 1 = Main Game, 2 = Game Over
  23.  
  24. 'GFX
  25. Global GFXBackground:TImage = LoadImage("gfx\background_stars.bmp")
  26. Global GFXPlayer:TImage = LoadImage("gfx\ship.bmp", MASKEDIMAGE | FILTEREDIMAGE)
  27. Global GFXAsteroid:TImage = LoadImage("gfx\asteroid.bmp", MASKEDIMAGE | FILTEREDIMAGE); MidHandleImage(GFXAsteroid)
  28. Global GFXExplosion:TImage = LoadAnimImage("gfx\explosion.bmp", 24, 24, 0, 7, MASKEDIMAGE | FILTEREDIMAGE); MidHandleImage(GFXExplosion)
  29.  
  30. 'Types
  31. Type Entity
  32.     Field X:Float
  33.     Field Y:Float
  34.     Field A:Float
  35.        
  36.     Method Update() Abstract
  37. EndType
  38.  
  39. Type Asteroid Extends Entity
  40.     Function Spawn:Asteroid()
  41.         Local A:Asteroid = New Asteroid
  42.         A.X = Rand(0, 15) * 10 + 5
  43.         A.Y = -10
  44.         A.A = Rand(0, 360)
  45.        
  46.         Return A
  47.     EndFunction
  48.    
  49.     Method Update()
  50.         Y = Y + 1
  51.         A = A + 0.5; If A > 360 Then A = 0
  52.        
  53.         SetRotation(A); DrawImage GFXAsteroid, X, Y; SetRotation(0)
  54.     EndMethod
  55. EndType
  56.  
  57. Type Explosion Extends Entity
  58.     Field Frame:Int
  59.     Field Time:Int
  60.     Field OldTime:Int = 0
  61.    
  62.     Function Spawn:Explosion(x:Int, y:Int)
  63.         Local E:Explosion = New Explosion
  64.         E.X = x
  65.         E.Y = y
  66.        
  67.         Return E
  68.     EndFunction
  69.    
  70.     Method Update()
  71.         If OldTime = 0 Then OldTime = MilliSecs()
  72.         Time = MilliSecs() - OldTime
  73.        
  74.         If Time > 71 Then
  75.             Frame = Frame + 1
  76.             OldTime = 0
  77.         EndIf
  78.        
  79.         SetScale(0.5, 0.5); DrawImage(GFXExplosion, X, Y, Frame); SetScale(1, 1)
  80.     EndMethod
  81. EndType
  82.  
  83. Global AstList:TList = New TList
  84. Global Ast:Asteroid
  85. Global ExpList:TList = New TList
  86. Global Exp1:Explosion
  87.  
  88. While Not AppTerminate()
  89.     Select GameState
  90.     Case 0 'Main menu
  91.         UpdateMainMenu()
  92.     Case 1 'Game
  93.         UpdateGame()
  94.     Case 2 'Game Over Screen
  95.         UpdateGameOverScreen()
  96.     EndSelect
  97.    
  98.     Flip(True)
  99.    
  100.     Cls
  101. Wend
  102.  
  103. End
  104.  
  105. 'Functions
  106.  
  107. Function UpdateMainMenu()
  108.     If KeyDown(KEY_ENTER) Then
  109.         GameState = 1
  110.         GameTime = 0
  111.         GameOldTime = 0
  112.     EndIf
  113.  
  114.     If GameOldTime = 0 Then GameOldTime = MilliSecs()  
  115.     GameTime = MilliSecs() - GameOldTime
  116.            
  117.     BackgroundScrollY = BackgroundScrollY + 1
  118.     TileImage(GFXBackground, 0, BackgroundScrollY)
  119.    
  120.     SetColor(255, 200, 0); DrawText2("-FLY TESKO HOME-", 80, 15, 1, 1); SetColor(255, 255, 255)
  121.     If GameTime > 500 Then DrawText2("Press [Enter]", 80, 60, 1, 1)
  122.     If GameTime > 1000 Then GameOldTime = 0
  123. EndFunction
  124.  
  125. Function UpdateGameOverScreen()
  126.     If GameOldTime = 0 Then GameOldTime = MilliSecs()  
  127.     GameTime = MilliSecs() - GameOldTime
  128.  
  129.     BackgroundScrollY = BackgroundScrollY + 1
  130.     TileImage(GFXBackground, 0, BackgroundScrollY)
  131.  
  132.     For Ast = EachIn AstList
  133.         If Ast.Y > 130 Then AstList.Remove(Ast)
  134.                
  135.         Ast.Update()
  136.     Next
  137.    
  138.     For Exp1 = EachIn ExpList
  139.         If Exp1.Frame = 6 Then ExpList.Remove(Exp1)
  140.         Exp1.Update()
  141.     Next
  142.    
  143.     If GameTime > 1000
  144.         SetColor(255, 0, 0); DrawText2("GAME OVER!", 80, 60, 1, 1); SetColor(255, 255, 255)
  145.         DrawText2("Score: " + PlayerScore, 80, 10, 1, 1)
  146.    
  147.         If KeyDown(KEY_ENTER) Then 'If Enter is pressed Then Play again!
  148.             GameTime = 0
  149.             GameOldTime = 0
  150.             PlayerScore = 0
  151.             PlayerX = 80
  152.             PlayerY = 130
  153.             PlayerVX = 0
  154.             GameState = 1
  155.         ElseIf GameTime > 10000 Or KeyDown(KEY_ESCAPE) 'If Escape or no key are pressed then return to main menu
  156.             GameTime = 0
  157.             GameOldTime = 0
  158.             PlayerScore = 0
  159.             PlayerX = 80
  160.             PlayerY = 130
  161.             PlayerVX = 0
  162.             GameState = 0
  163.         EndIf
  164.     EndIf
  165. EndFunction
  166.  
  167. Function UpdateGame()
  168.     If GameOldTime = 0 Then GameOldTime = MilliSecs()
  169.     GameTime = MilliSecs() - GameOldTime
  170.    
  171.     If GameTime > 2000 Then
  172.         If KeyDown(KEY_A) And PlayerX > 0 Then
  173.             PlayerVX = PlayerVX - 0.25
  174.         ElseIf KeyDown(KEY_D) And PlayerX < 150  Then
  175.             PlayerVX = PlayerVX + 0.25
  176.         Else
  177.             PlayerVX = 0
  178.         EndIf
  179.        
  180.         'Clamp Player Vel                  
  181.         If PlayerVX >  1 Then PlayerVX = 1
  182.         If PlayerVX < -1 Then PlayerVX =-1
  183.            
  184.         PlayerX = PlayerX + PlayerVX
  185.        
  186.         If SpawnOldTime = 0 Then SpawnOldTime = MilliSecs()
  187.         SpawnTime = MilliSecs() - SpawnOldTime
  188.        
  189.         'Spawn Asteroids every 100 millisecs
  190.         If SpawnTime > 100 Then
  191.             AstList.AddLast(Asteroid.Spawn())
  192.             SpawnOldTime = 0
  193.         EndIf
  194.        
  195.         PlayerScore = (GameTime - 2000) / 10
  196.     Else
  197.         'Ship entry animation
  198.         PlayerY = PlayerY - 0.5
  199.         If PlayerY < 100 Then PlayerY = 100
  200.     EndIf
  201.    
  202.     'Draw GFX
  203.     BackgroundScrollY = BackgroundScrollY + 1
  204.     TileImage(GFXBackground, 0, BackgroundScrollY)
  205.    
  206.     'Draw Player   
  207.     DrawImage(GFXPlayer, PlayerX, PlayerY)
  208.    
  209.     'Update Asteroids
  210.     For Ast = EachIn AstList
  211.         If Ast.Y > 130 Then AstList.Remove(Ast)
  212.        
  213.         'Check collision between player and asteroids
  214.         If RectsOverlap(PlayerX + 2, PlayerY, 6, 10, Ast.X - 5, Ast.Y - 5, 10, 10) Then
  215.             ExpList.AddLast(Explosion.Spawn(PlayerX + 5, PlayerY + 5))
  216.             GameTime = 0
  217.             GameOldTime = 0
  218.             GameState = 2
  219.         EndIf
  220.        
  221.         Ast.Update()
  222.     Next
  223.    
  224.     'Update Explosions
  225.     For Exp1 = EachIn ExpList
  226.         If Exp1.Frame = 6 Then ExpList.Remove(Exp1)
  227.         Exp1.Update()
  228.     Next
  229.    
  230.     DrawText2(PlayerScore, 80, 10, 1, 1)
  231. EndFunction
  232.  
  233. Function DrawText2(text:String, x:Float, y:Float, cx:Int = 0, cy:Int = 0)
  234.     If cx Then x = x - (TextWidth(text) * 0.5)
  235.     If cy Then y = y - (TextHeight(text) * 0.5)
  236.    
  237.     DrawText(text, x, y)
  238. EndFunction
  239.  
  240. Rem
  241. Function DrawWireRect(x:Float,y:Float,w:Float,h:Float)
  242.     DrawLine(x, y, x + w, y)
  243.     DrawLine(x + w, y, x + w, y + w)
  244.     DrawLine(x + w, y + h, x, y + h)
  245.     DrawLine(x, y, x, y + h)
  246. EndFunction
  247. EndRem
  248.  
  249. Function RectsOverlap:Int(x1:Float, y1:Float, w1:Float, h1:Float, x2:Float, y2:Float, w2:Float, h2:Float)
  250.     If x1 > (x2 + w2) Or (x1 + w1) < x2 Then Return False
  251.     If y1 > (y2 + h2) Or (y1 + h1) < y2 Then Return False
  252.     Return True
  253. End Function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement