Advertisement
Guest User

Untitled

a guest
Nov 21st, 2014
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.39 KB | None | 0 0
  1. game = {}
  2.  
  3. function game.load()
  4.     game.clock = 0
  5.  
  6.     --initialize player
  7.     game.player_size = img["player"]:getWidth()
  8.     game.playerX = 1280 / 2
  9.     game.playerY = 720 / 2 + 200
  10.     game.playerVel = 280
  11.  
  12.     --initialize bullets
  13.     game.bullet_size = img["bullet"]:getWidth()
  14.     game.bulletVel = 800
  15.     game.bullets = {}
  16.     game.bulletCooldown = 6
  17.  
  18.     cooldownRemaining = 0
  19.  
  20.     game.score = 0
  21.  
  22. end
  23.  
  24. function game.update(dt)
  25.  
  26.     --setting clock
  27.     game.clock = game.clock + dt
  28.  
  29.     --movement
  30.         if joystick:isGamepadDown("dpleft") then
  31.         game.playerX = game.playerX - game.playerVel * dt
  32.     elseif joystick:isGamepadDown("dpright") then
  33.         game.playerX = game.playerX + game.playerVel * dt
  34.     end
  35.  
  36.     if joystick:isGamepadDown("dpup") then
  37.         game.playerY = game.playerY - game.playerVel * dt
  38.     elseif joystick:isGamepadDown("dpdown") then
  39.         game.playerY = game.playerY + game.playerVel * dt
  40.     end
  41.  
  42.     --keep player onscreen
  43.     if game.playerY > 720 - game.player_size/2 then
  44.         game.playerY = 720 - game.player_size/2
  45.     end
  46.     if game.playerY < 0 + game.player_size/2 then
  47.         game.playerY = 0 + game.player_size/2
  48.     end
  49.     if game.playerX > 1000 - game.player_size/2 then
  50.         game.playerX = 1000 - game.player_size/2
  51.     end
  52.     if game.playerX < 280 + game.player_size/2 then
  53.         game.playerX = 280 + game.player_size/2
  54.     end
  55.  
  56.  
  57.     if cooldownRemaining > 0 then
  58.         cooldownRemaining = cooldownRemaining - 1
  59.     end
  60.  
  61.     --spawn new bullets when a is pressed
  62.     if joystick:isGamepadDown("a") and cooldownRemaining <=0 then
  63.         cooldownRemaining = game.bulletCooldown
  64.         NewBullet(10)
  65.         NewBullet(-10)
  66.         NewBullet(0)
  67.     end
  68.  
  69.  
  70.     --update bullet positions
  71.     for i, v in ipairs(game.bullets) do
  72.         v.X = v.X + game.bulletVel * dt * math.sin(math.rad(v.angle))
  73.         v.Y = v.Y - game.bulletVel * dt * math.cos(math.rad(v.angle))
  74.         if v.Y < 0 or v.X < 280 or v.X > 1000 then
  75.             table.remove(game.bullets, i)
  76.         end
  77.     end
  78.  
  79. end
  80.  
  81. function game.draw()
  82.  
  83.     love.graphics.draw(img["overlay"], 0, 0)
  84.  
  85.     love.graphics.draw(img["player"], game.playerX, game.playerY,
  86.                                          0, 1, 1, game.player_size/2, game.player_size/2)
  87.  
  88.     for i,v in ipairs(game.bullets) do
  89.         love.graphics.draw(img["bullet"], v.X, v.Y, 0, 1, 1, game.bullet_size/2, game.bullet_size/2)
  90.     end
  91.  
  92. end
  93.  
  94. function NewBullet(angle)
  95.  
  96.     --create new bullet
  97.     local bullet = {}
  98.     bullet.X = game.playerX
  99.     bullet.Y = game.playerY - 16
  100.     bullet.angle = angle
  101.     table.insert(game.bullets, bullet)
  102. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement