Advertisement
joseleeph

Untitled

Dec 30th, 2020
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.17 KB | None | 0 0
  1. WINDOW_WIDTH = 1280
  2. WINDOW_HEIGHT = 720
  3.  
  4. VIRTUAL_WIDTH = 432
  5. VIRTUAL_HEIGHT = 243
  6.  
  7. PADDLE_SPEED = 200
  8. --[[
  9. define a class and specify objects of that class
  10. we will tie our paddles together with classes and objects
  11. ]]
  12. Class = require 'class' -- from Paddle.lua... require the line Paddle = Class{}
  13. -- in order to get it to work, you need to import the library in main.lua with class = require 'class'
  14. push = require 'push'
  15.  
  16. require 'Ball' -- require ball and paddle will have access to the class variable because they are declared first
  17. require 'Paddle'
  18.  
  19.  
  20.  
  21. function love.load()
  22. math.randomseed(os.time())
  23. love.graphics.setDefaultFilter('nearest','nearest')
  24.  
  25. --[[
  26. player1Y = 30
  27. player2Y = VIRTUAL_HEIGHT - 50
  28. get replaced with paddle objects called paddle1 and paddle2
  29. ]]
  30.  
  31. player1 = Paddle(5, 20, 5, 20) -- instatiate a paddle object. when you use the parentheses in what is called the constructor, init gets called through the class library implicitly
  32. player2 = Paddle(VIRTUAL_WIDTH - 10, VIRTUAL_HEIGHT - 30, 5, 20)
  33. ball = Ball(VIRTUAL_WIDTH/2 - 2, VIRTUAL_HEIGHT/2 - 2, 5, 5) -- instatiate the ball .... not balle!
  34.  
  35. --[[
  36. these lines are no longer needed
  37.  
  38. ballX = VIRTUAL_WIDTH/2-2
  39. ballY = VIRTUAL_HEIGHT/2-2
  40.  
  41. ballDX = math.random(2) == 1 and -100 or 100
  42. ballDY = math.random(-50,50)
  43.  
  44. ]]
  45.  
  46.  
  47. gameState = 'start'
  48. smallFont = love.graphics.newFont('04B_30__.ttf',8)
  49. scoreFont = love.graphics.newFont('TitilliumText22L005-webfont.ttf',14)
  50.  
  51. love.graphics.setFont(smallFont)
  52.  
  53. push:setupScreen(VIRTUAL_WIDTH,VIRTUAL_HEIGHT, WINDOW_WIDTH, WINDOW_HEIGHT, {
  54. fullscreen = false,
  55. vsync = true,
  56. resizeable = false
  57. })
  58.  
  59. player1Score = 0
  60. player2Score = 0
  61. end
  62. function love.draw()
  63. push:apply('start')
  64. love.graphics.clear(40/255,45/255,52/255,255/255) -- map values to 0-1
  65. scoreFont = love.graphics.newFont('TitilliumText22L005-webfont.ttf',20)
  66. love.graphics.setFont(smallFont)
  67. if gameState == 'start' then
  68. love.graphics.printf('Hello Start State!', 0,20, VIRTUAL_WIDTH, 'center')
  69. elseif gameState == 'play' then
  70. love.graphics.printf('Hello Play State!', 0, 20, VIRTUAL_WIDTH, 'center')
  71. end
  72. love.graphics.setFont(scoreFont)
  73. love.graphics.print(player1Score, VIRTUAL_WIDTH/2 - 50, VIRTUAL_HEIGHT /3)
  74. love.graphics.print(player2Score, VIRTUAL_WIDTH/2+30, VIRTUAL_HEIGHT/3)
  75. ball:render()--love.graphics.rectangle('fill', ballX, ballY, 4, 4)
  76. player1:render() -- paddle1 render() is now a function stored in Paddle.lua
  77. player2:render()
  78. --love.graphics.rectangle('fill', 10, player1Y, 5, 20) -- left side
  79. --love.graphics.rectangle('line', VIRTUAL_WIDTH - 10, player2Y, 5, 20) -- right side
  80.  
  81. displayFPS() -- show framerate
  82.  
  83. push:apply('end')
  84. end
  85.  
  86. function love.update(dt)
  87.  
  88. if ball:collides(player1) then -- collides() is stored in Ball.lua
  89. -- deflect the ball to the right
  90. -- change velocity from -100, to 100
  91. ball.dx = -ball.dx
  92. end
  93.  
  94. if ball:collides(player2) then
  95. -- deflect ball to the left
  96. ball.dx = -ball.dx
  97. end
  98.  
  99. if ball.y <= 0 then -- the top of the screen
  100. --deflect the ball down
  101. ball.dy = -ball.dy
  102. -- reset the ball in the case of it hitting the top of the screen
  103. ball.y = 0
  104. end
  105.  
  106. if ball.y >= VIRTUAL_HEIGHT - 4 then -- the ball is 4 pixels tall so offset collision by 4 pixels
  107. ball.dy = -ball.dy
  108. ball.y = VIRTUAL_HEIGHT - 4
  109. end
  110. -- player 1 movement
  111. if love.keyboard.isDown('w') then
  112. player1.dy = -PADDLE_SPEED
  113. elseif love.keyboard.isDown('s') then
  114. player1.dy = PADDLE_SPEED
  115. else
  116. player1.dy = 0 -- if neither is true, the paddle speed is 0
  117. end
  118. -- player 2 movement
  119. if love.keyboard.isDown('up') then
  120. player2.dy = -PADDLE_SPEED
  121. elseif love.keyboard.isDown('down') then
  122. player2.dy = PADDLE_SPEED
  123. else
  124. player2.dy = 0
  125. end
  126. if gameState == 'play' then
  127. ball:update(dt)
  128. end
  129.  
  130. player1:update(dt) -- replaces player1Y = math.min(VIRTUAL_HEIGHT - 20, player1Y + PADDLE_SPEED * dt)... now stored in the paddle class and replace player1Y with self.y
  131. player2:update(dt)
  132. ball:render()
  133. end
  134. function love.keypressed(key)
  135. if key == 'escape' then
  136. love.event.quit()
  137. elseif key == 'enter' or key == 'return' then
  138. if gameState == 'start' then
  139. gameState = 'play'
  140. else
  141. gameState = 'start'
  142. --[[
  143. will be copied into ball:reset()
  144. ballX = VIRTUAL_WIDTH/2-2
  145. ballY = VIRTUAL_HEIGHT/2-2
  146. ballDX = math.random(2) == 1 and -100 or 100
  147. ballDY = math.random(-50,50)
  148. ]]
  149. ball:reset()
  150. end
  151. end
  152. end
  153.  
  154. function displayFPS()
  155. love.graphics.setColor(0,1,0,1) -- toggles color for drawing text... default is white.... RGBT values... could also be 255
  156. love.graphics.setFont(smallFont)
  157. love.graphics.print('FPS: ' .. tostring(love.timer.getFPS()), 40, 20) -- .. is the string concatination operator
  158. -- tostring() will give an integer values which will be incompatible with string
  159. love.graphics.setColor(1, 1, 1, 1)
  160. end
  161.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement