Advertisement
Guest User

Untitled

a guest
Mar 1st, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ; Moving a Graphic
  2. ; Aiden Kerr
  3.  
  4. ; Set up graphics stuff
  5.  
  6. Const SCREENWIDTH = 1280, SCREENHEIGHT = 720
  7.  
  8. Graphics SCREENWIDTH, SCREENHEIGHT
  9.  
  10. SetBuffer BackBuffer()
  11.  
  12. ; Constants
  13.  
  14. Const ESCKEY = 1, SPACEBAR = 57, LEFTKEY = 203, RIGHTKEY = 205, UPKEY = 200, DOWNKEY = 208
  15.  
  16. ; Types
  17.  
  18. Type PlayerType
  19.     Field x, y
  20.     Field Speed
  21.     Field Graphic
  22. End Type
  23.  
  24. Type BulletType
  25.     Field x, y
  26.     Field Speed
  27.     Field Graphic
  28.     Field Alive
  29. End Type
  30.  
  31. Type EnemyType
  32.     Field x,y
  33.     Field Graphic
  34.     Field XSpeed
  35.     Field YSpeed
  36.     Field Alive
  37.     Field Frame
  38. End Type
  39.  
  40. Type ExplosionType
  41.     Field x, y
  42.     Field Alive
  43.     Field Graphic
  44.     Field Frame
  45. End Type
  46.  
  47. ; Setup
  48. SeedRnd MilliSecs()
  49.  
  50. Global LaserSound = LoadSound("laser.wav")
  51. Global clock = 0
  52.  
  53. Global Player.PlayerType = New PlayerType
  54. SetupPlayer()
  55. SetupEnemies(4)
  56.  
  57. ; Main Loop
  58.  
  59. While Not KeyDown(ESCKEY)
  60.     Cls
  61.         PlayerControls()
  62.         MoveBullet()
  63.         MoveEnemies()
  64.         CheckCollisions()
  65.         EraseDead()
  66.         DrawAll()
  67.         DrawExplosion()
  68.         Time()
  69.     Flip
  70. Wend
  71.  
  72. ; Functions
  73.  
  74. Function SetupPlayer()
  75.     Player\Graphic = LoadImage("player.bmp")
  76.     Player\x = SCREENWIDTH/2
  77.     Player\y = SCREENHEIGHT/2
  78.     Player\Speed = 1.5
  79. End Function
  80.  
  81. Function PlayerControls()
  82.     ; player movement
  83.     If KeyDown(UPKEY) Then Player\y = Player\y - Player\Speed
  84.     If KeyDown(DOWNKEY) Then Player\y = Player\y + Player\Speed
  85.     If KeyDown(LEFTKEY) Then Player\x = Player\x - Player\Speed
  86.     If KeyDown(RIGHTKEY) Then Player\x = Player\x + Player\Speed
  87.    
  88.     ; Restraining the player locations to the screen.
  89.     If Player\y < 0 Then Player\y = 0
  90.     If Player\y > SCREENHEIGHT-33 Then Player\y = SCREENHEIGHT-33
  91.     If Player\x < -1 Then Player\x = -1
  92.     If Player\x > SCREENWIDTH-33 Then Player\x = SCREENWIDTH-33
  93.    
  94.     ; Other controls
  95.     ; Shooting
  96.     If KeyHit(SPACEBAR)
  97.         NewBullet.BulletType = New BulletType
  98.         NewBullet\x = Player\x
  99.         NewBullet\y = player\y
  100.         NewBullet\Speed = 5
  101.         NewBullet\Graphic = LoadImage("PlayerShot.bmp")
  102.         NewBullet\Alive = True
  103.         PlaySound(LaserSound)
  104.     EndIf
  105. End Function
  106.  
  107. Function MoveBullet()
  108.     For Bcnt.BulletType = Each BulletType
  109.         Bcnt\y = Bcnt\y - Bcnt\Speed
  110.         ; Delete when off-screen
  111.         If Bcnt\y < -7 Then Delete Bcnt
  112.     Next
  113. End Function
  114.  
  115. ; Creates enemies and sets values
  116. Function SetupEnemies(num)
  117.     For i = 1 To num
  118.         Enemy.EnemyType = New EnemyType
  119.         Enemy\x = Rand(0, SCREENWIDTH-80)
  120.         Enemy\y = Rand(0, SCREENHEIGHT-80)
  121.         Enemy\Graphic = LoadAnimImage("Enemy.bmp",64,64,0,2)
  122.         Enemy\XSpeed = Rand(-1, 1)
  123.         Enemy\YSpeed = Rand(-1, 1)
  124.         Enemy\Alive = True
  125.         Enemy\Frame = 2 ; so it starts with frame 0
  126.     Next
  127. End Function
  128.  
  129. ; Handles enemy movement and edge detection
  130. Function MoveEnemies()
  131.     For Ecnt.EnemyType = Each EnemyType
  132.         ; Flip their directions if they're near the edge
  133.         If Ecnt\y < -10 Or Ecnt\y > SCREENHEIGHT-55 Then Ecnt\YSpeed = Ecnt\YSpeed * -1
  134.         If Ecnt\x < -10 Or Ecnt\x > SCREENWIDTH-55 Then Ecnt\XSpeed = Ecnt\XSpeed * -1
  135.        
  136.         ; Move them by adding their speed to their location
  137.         Ecnt\x = Ecnt\x + Ecnt\XSPeed
  138.         Ecnt\y = Ecnt\y + Ecnt\YSpeed
  139.        
  140.         ; Animation
  141.         If Clock Mod 100 = 0 Then
  142.             Ecnt\Frame = Ecnt\Frame + 1
  143.             If Ecnt\Frame > 1 Then Ecnt\Frame = 0
  144.         EndIf
  145.     Next
  146. End Function
  147.  
  148.  
  149. Function CheckCollisions()
  150.     ; Loop through bullets and enemies to
  151.     For Bcnt.BulletType = Each BulletType
  152.         For Ecnt.EnemyType = Each EnemyType
  153.             If ImagesCollide(Bcnt\Graphic, Bcnt\x, Bcnt\y, 0, Ecnt\Graphic, Ecnt\x, Ecnt\y, 0) Then
  154.                 Ecnt\Alive = False
  155.                 Bcnt\Alive = False
  156.                 CreateExplosion(Ecnt\x, Ecnt\y)
  157.             EndIf
  158.         Next
  159.     Next
  160. End Function
  161.  
  162. Function EraseDead()
  163.     ; Check if each enemy should be dead. Also, explosion animations
  164.    
  165.     For Bcnt.BulletType = Each BulletType
  166.         If Bcnt\Alive = False Then Delete Bcnt
  167.     Next
  168.    
  169.     For Ecnt.EnemyType = Each EnemyType
  170.         If Ecnt\Alive = False Then Delete Ecnt
  171.     Next
  172.    
  173.     For ExpCnt.ExplosionType = Each ExplosionType
  174.         If ExpCnt\Alive = False Then Delete ExpCnt
  175.     Next
  176.  
  177. End Function
  178.  
  179. Function DrawAll()
  180.     ; Draw player
  181.     DrawImage Player\Graphic, Player\x, Player\y
  182.    
  183.     ; Draw Bullets
  184.     For Bcnt.BulletType = Each BulletType
  185.         DrawImage Bcnt\Graphic, Bcnt\x, Bcnt\y
  186.     Next
  187.    
  188.     ;Draw Enemies
  189.     For Ecnt.EnemyType = Each EnemyType
  190.         DrawImage Ecnt\Graphic, Ecnt\x, Ecnt\y, Ecnt\Frame
  191.     Next
  192. End Function
  193.  
  194. ; Creates the explosion type when required
  195. Function CreateExplosion(x, y)
  196.     Explosion.ExplosionType = New ExplosionType
  197.     Explosion\x = x
  198.     Explosion\y = y
  199.     Explosion\Alive = True
  200.     Explosion\Graphic = LoadAnimImage("EnemyKaboom.bmp",64,64,0,5)
  201. End Function
  202.  
  203. ; Animates the explosions
  204. Function DrawExplosion()
  205.     For ExpCnt.ExplosionType = Each ExplosionType
  206.         If Clock Mod 40 = 0 Then
  207.             ExpCnt\Frame = ExpCnt\Frame + 1
  208.             If ExpCnt\Frame > 4 Then
  209.                 ExpCnt\alive = False
  210.                 ExpCnt\Frame = 0
  211.             EndIf
  212.         EndIf
  213.         DrawImage ExpCnt\Graphic, ExpCnt\x, ExpCnt\y, ExpCnt\Frame
  214.        
  215.     Next
  216. End Function
  217.  
  218. ;Increments the clock every frame
  219. Function Time()
  220.     Clock = Clock + 1
  221. End Function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement