BonelessHotline

pong updated

Jun 7th, 2021
823
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function love.load()
  2.   love.window.setFullscreen(true)
  3.   player1 = {}
  4.   player1.posX = (love.graphics.getWidth() * .02)
  5.   player1.posY = love.graphics.getHeight() * .5 - 25
  6.   player1.speedU = 500
  7.   player1.speedD = 500
  8.   player1.score = 0
  9.   projectile = {}
  10.   projectile.posX = (love.graphics.getWidth() * .1)
  11.   projectile.posY = 0
  12.   projectile.speed = 500
  13.   ai = {}
  14.   ai.posX = ((love.graphics.getWidth() * .98) - 10)
  15.   ai.posY = love.graphics.getHeight() * .5 - 25
  16.   ai.speedU = 500
  17.   ai.speedD = 500
  18.   ai.score = 0
  19.   direction = 1
  20.   gamestart = false
  21.   debugmode = false
  22.   menu = {"START","DEBUG MODE", "EXIT"}
  23.   navigation = 1
  24. end
  25.  
  26. function bounds(player) -- checks if player is out of bounds
  27.   if player.posY <= 0 then -- if player is at the top
  28.     player.speedU = 0 -- sets speed upwards to 0
  29.   elseif player.posY > 0 and player.posY < (love.graphics.getHeight() - 51) then -- elseif player is in bounds: reset speed
  30.     player.speedU = 500
  31.     player.speedD = 500
  32.   elseif player.posY >= (love.graphics.getHeight() - 51) then -- elseif player is at the bottom
  33.     player.speedD = 0 -- sets speed downwards to 0
  34.   end
  35. end
  36.  
  37. function negator(v) -- returns 1 or -1 based on v (random int 1,2)
  38.   if v == 1 then
  39.     return -1
  40.   elseif v == 2 then
  41.     return 1
  42.   end
  43. end
  44.  
  45. function spawn() -- spawns projectile
  46.   projectile.posX = (love.graphics.getWidth()*.5 - 5 ) -- sets x pos to middle
  47.   projectile.posY = (love.graphics.getHeight()*.5 - 5) -- sets y pos to middle
  48.   love.math.setRandomSeed(os.time()) -- sets randomseed based on os.time()
  49.   angle = math.random(2, 4) -- generates angle in rad
  50.   local x = negator(math.random(1,2)) -- sets x to 1 or -1
  51.   local y = negator(math.random(1,2)) -- sets y to 1 or -1
  52.  
  53.   projectile.speedX = (projectile.speed * math.cos(angle) * x) -- factors in angle and direction into projectile x speed
  54.   projectile.speedY = (projectile.speed * math.sin(angle) * y) -- factors in angle and direction into projectile y speed
  55.  
  56. end
  57.  
  58. function collision() -- collision checker
  59.   if projectile.posX >= ((love.graphics.getWidth() * .02) - 5 ) and projectile.posX <= ((love.graphics.getWidth() * .02) + 10 ) then  -- if projectile is witin certain pos x interval behind player1 then
  60.     direction = direction * -1 -- only used for debugging / does nothing ingame
  61.     if projectile.posY >= (player1.posY-25) and projectile.posY <= (player1.posY + 50) then                                             -- if projectile is within certain interval (projectile y pos collides with player1 y pos) then
  62.       projectile.speedX = projectile.speedX * -1.2                                                                                        -- reverse x speed and increases x speed 20%
  63.     end
  64.   elseif projectile.posX > love.graphics.getWidth()-5 then                                                                            -- elseif projectile goes off the side (goal is scored)
  65.     player1.score = player1.score + 1                                                                                                   -- increases player1 score by 1
  66.     spawn()                                                                                                                             -- spawns new projectile
  67.   end
  68.   if projectile.posX >= ((love.graphics.getWidth() * .98) - 20) and projectile.posX <= ((love.graphics.getWidth() * .98) - 5) then
  69.     direction = direction* -1
  70.     if projectile.posY >= (ai.posY-25) and projectile.posY <= (ai.posY + 50) then
  71.       projectile.speedX = projectile.speedX * -1.2
  72.     end
  73.   elseif projectile.posX < -5 then
  74.     ai.score = ai.score + 1
  75.     spawn()
  76.   end
  77. end
  78.  
  79. function nav() -- menu and keyboard navigation
  80.   if gamestart == false then -- if game hasnt started: navigates in the main menu
  81.     function love.keypressed(key)
  82.       if key == "down" and navigation <= #menu - 1 then
  83.         navigation = navigation + 1
  84.       elseif key == "up" and navigation >= 2 then
  85.         navigation = navigation - 1
  86.       elseif key == "space" then
  87.         if menu[navigation] == "START" then
  88.           gamestart = true
  89.           spawn()
  90.         elseif menu[navigation] == "DEBUG MODE" then
  91.           debugmode = true
  92.           gamestart = true
  93.           spawn()
  94.         elseif menu[navigation] == "EXIT" then
  95.           love.event.quit()
  96.         end
  97.       end
  98.     end
  99.   else -- if game has started
  100.     function love.keypressed(key)
  101.       if key == "escape" then -- if escape is pressed:
  102.         love.load() -- calls love.load() : which sets gamestart = false and thereby exits the current game and resets variables
  103.       end
  104.     end
  105.   end
  106. end
  107.  
  108. function love.update(dt) -- called every frame
  109.  
  110.   nav() -- calls navigation function
  111.  
  112.   if gamestart == true then -- if game has started
  113.    
  114.     if love.keyboard.isDown("s") then                         -- if holding s
  115.       player1.posY = player1.posY + (player1.speedD * dt)       -- player1 move down
  116.     elseif love.keyboard.isDown("w") then                     -- elseif holding w
  117.       player1.posY = player1.posY - (player1.speedU * dt)       -- player1 move up
  118.     end
  119.    
  120.     if love.keyboard.isDown("down") then      -- if holding down arrow
  121.       ai.posY = ai.posY + (ai.speedD * dt)      -- ai/player2 move down
  122.     elseif love.keyboard.isDown("up") then    -- elseif holding up arrow
  123.       ai.posY = ai.posY - (ai.speedU * dt)      -- ai/player2 move up
  124.     end
  125.    
  126.     bounds(player1) -- calls bounds function, checks if player1 is out of bounds
  127.     bounds(ai)      -- calls bounds function, checks if ai/player2 is out of bounds
  128.    
  129.     collision() -- calls collision function, checks if projectile has collided
  130.      
  131.     projectile.posX = projectile.posX + (projectile.speedX * dt)  -- updates projectile x position
  132.     projectile.posY = projectile.posY + (projectile.speedY * dt)  -- updates projectile y position
  133.    
  134.     if projectile.posY <= -10 or projectile.posY >= (love.graphics.getHeight()-20) then       -- if projectile is out of bounds
  135.       projectile.speedY = projectile.speedY * -1                                                -- inverse y speed
  136.     end
  137.   end
  138. end
  139.  
  140.  
  141. function love.draw() -- called every frame
  142.   if gamestart == true then -- draws game
  143.     love.graphics.rectangle("fill", player1.posX, player1.posY, 10, 50)                         -- player1 body
  144.     love.graphics.rectangle("line", 0, 0, love.graphics.getWidth(), love.graphics.getHeight())  -- border
  145.     love.graphics.rectangle("fill", ai.posX, ai.posY, 10, 50)                                   -- ai/player2 body
  146.     love.graphics.rectangle("line", projectile.posX + 5, projectile.posY + 13, 10, 10)          -- projectile body
  147.     love.graphics.print(tostring(player1.score), ((love.graphics.getWidth()*.5) - 80), love.graphics.getHeight()*.9, 0, 3,3)  -- prints player1 score
  148.     love.graphics.print(tostring(ai.score), ((love.graphics.getWidth()*.5) + 60), love.graphics.getHeight()*.9, 0, 3,3)       -- prints ai/player2 score
  149.   else -- draws main menu
  150.     for i=1,#menu do -- prints every item in the menu table in a list
  151.       love.graphics.print(menu[i], 30, love.graphics.getHeight()*.65 + 25 * i,0,2,2)
  152.     end
  153.     love.graphics.print(">",15,love.graphics.getHeight()*.647 + (25) * navigation,0,1,2) -- prints arrow
  154.   end
  155.   -- debugmode check
  156.   if debugmode == true then
  157.     love.graphics.print("projectile pos x : "..tostring(projectile.posX), 50, 40)
  158.     love.graphics.print("projectile pos y : "..tostring(projectile.posY), 50, 55)
  159.     love.graphics.print("ai pos x : "..tostring(ai.posX), 50, 80)
  160.     love.graphics.print("direction : "..tostring(direction), 50, 95)
  161.     love.graphics.print("speed : "..tostring(math.sqrt((projectile.speedX)^2 + (projectile.speedY)^2)), 50, 150)
  162.     love.graphics.print("angle : "..tostring(angle), 50, 165)
  163.   end
  164. end
RAW Paste Data