Advertisement
joseleeph

Untitled

Dec 27th, 2020
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.58 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. Class = require 'class' -- from Paddle.lua... require the line Paddle = Class{}
  10.  
  11.  
  12. require 'Ball' -- require ball and paddle will have access to the class variable because they are declared first
  13. require 'Paddle'
  14.  
  15.  
  16.  
  17. function love.load()
  18. math.randomseed(os.time())
  19. love.graphics.setDefaultFilter('nearest','nearest')
  20.  
  21.  
  22. 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
  23. player2 = Paddle(VIRTUAL_WIDTH - 10, VIRTUAL_HEIGHT - 30, 5, 20)
  24. ball = Ball(VIRTUAL_WIDTH/2 - 2, VIRTUAL_HEIGHT/2 - 2, 5, 5) -- instatiate the ball .... not balle!
  25.  
  26.  
  27. gameState = 'start'
  28. smallFont = love.graphics.newFont('TitilliumText22L005-webfont.ttf',10)
  29. scoreFont = love.graphics.newFont('04B_30__.TTF',14)
  30.  
  31. love.graphics.setFont(smallFont)
  32.  
  33. push:setupScreen(VIRTUAL_WIDTH,VIRTUAL_HEIGHT, WINDOW_WIDTH, WINDOW_HEIGHT, {
  34. fullscreen = false,
  35. vsync = true,
  36. resizeable = false
  37. })
  38.  
  39. player1Score = 0
  40. player2Score = 0
  41.  
  42. servingPlayer = math.random(2) == 1 and 1 or 2 -- a coin flip between 1 and 2 which 1 is true and which is false
  43.  
  44. winningPlayer = 0
  45.  
  46. if servingPlayer == 1 then
  47. ball.dx = 100
  48. else
  49. ball.dx = -100
  50. end
  51. end
  52. function love.draw()
  53. push:apply('start')
  54. love.graphics.clear(40/255,45/255,52/255,255/255) -- map values to 0-1
  55. scoreFont = love.graphics.newFont('lsansd.ttf',20)
  56. love.graphics.setFont(smallFont)
  57.  
  58. displayScore()
  59.  
  60. if gamestate == 'start' then
  61. love.graphics.printf("Welcome to Pong!", 0, 20, VIRTUAL_WIDTH, 'center') -- it starts at the left edge, and the column size is virualwidth with center text
  62. love.graphics.printf("Press Enter to Play!", 0, 32, VIRTUAL_WIDTH, 'center')
  63. elseif gameState == 'serve' then
  64. love.graphics.printf("Player" .. tostring(servingPlayer) .. "'s turn!", 0, 30, VIRTUAL_WIDTH, 'center') -- always says serving player 2... why?
  65. love.graphics.printf("Press Enter to Serve", 0, 20, VIRTUAL_WIDTH, 'center')
  66. elseif gameState == 'victory' then
  67. love.graphics.setFont(victoryFont)
  68. love.graphics.printf("Player" .. tostring(winningPlayer) .. " wins!", 0, 30, VIRTUAL_WIDTH, 'center') -- always says serving player 2... why?
  69. love.graphics.printf('', 0, 42, VIRTUAL_WIDTH, 'center')
  70. elseif gameState == 'play' then
  71.  
  72. end
  73.  
  74.  
  75. end
  76. love.graphics.setFont(scoreFont)
  77. love.graphics.print(player1Score, VIRTUAL_WIDTH/2 - 50, VIRTUAL_HEIGHT /3)
  78. love.graphics.print(player2Score, VIRTUAL_WIDTH/2+30, VIRTUAL_HEIGHT/3)
  79. ball:render()--love.graphics.rectangle('fill', ballX, ballY, 4, 4)
  80. player1:render() -- paddle1 render() is now a function stored in Paddle.lua
  81. player2:render()
  82.  
  83. displayFPS() -- show framerate
  84.  
  85. push:apply('end')
  86. end
  87.  
  88. function love.update(dt)
  89.  
  90. if ball:collides(player1) then -- collides() is stored in Ball.lua
  91. -- deflect the ball to the right
  92. -- change velocity from -100, to 100
  93. ball.dx = -ball.dx
  94. end
  95.  
  96. if ball:collides(player2) then
  97. -- deflect ball to the left
  98. ball.dx = -ball.dx
  99. end
  100.  
  101. if ball.y <= 0 then -- the top of the screen
  102. --deflect the ball down
  103. ball.dy = -ball.dy
  104. -- reset the ball in the case of it hitting the top of the screen
  105. ball.y = 0
  106. end
  107.  
  108. if ball.y >= VIRTUAL_HEIGHT - 4 then -- the ball is 4 pixels tall so offset collision by 4 pixels
  109. ball.dy = -ball.dy
  110. ball.y = VIRTUAL_HEIGHT - 4
  111. end
  112.  
  113. if ball.x < 0 then
  114. player2Score = player2Score + 1
  115. serveringPlayer = 1 -- serving player isn't displayed correctly ... ask reddit!
  116. ball:reset() -- randomizes the balls dx. we want to set that based on who scored
  117. if player2Score >= 10 then
  118. gameState = 'victory'
  119. winningPlayer = 2
  120. else
  121. gameState = 'serve'
  122. end
  123. -- if player2's score got incremented... player1 should serve
  124. -- ball.dx = 100 -- it will go towards player 2 put it back maybe...
  125. --gameState = 'serve'
  126. end
  127.  
  128. if ball.x > VIRTUAL_WIDTH then
  129. player1Score = player1Score + 1 -- serving player not working... always says serving player 2
  130. servingPlayer = 2
  131. ball:reset()
  132. --ball.dx = -100
  133. --gameState = 'serve'
  134. if player1Score >= 3 then
  135. gameState = 'victory'
  136. winningPlayer = 1
  137. else
  138. gameState = 'serve'
  139. end
  140.  
  141. -- player 1 movement
  142. if love.keyboard.isDown('w') then
  143. player1.dy = -PADDLE_SPEED -- negative is up
  144. elseif love.keyboard.isDown('s') then
  145. player1.dy = PADDLE_SPEED -- replaces math.min(VIRTUAL_HEIGHT - 2-,PLAYER1Y + PADDLE_SPEED * dt)
  146. else
  147. player1.dy = 0 -- if neither is true, the paddle speed is 0
  148. end
  149. -- player 2 movement
  150. if love.keyboard.isDown('up') then
  151. player2.dy = -PADDLE_SPEED
  152. elseif love.keyboard.isDown('down') then
  153. player2.dy = PADDLE_SPEED
  154. else
  155. player2.dy = 0
  156. end
  157. if gameState == 'play' then
  158. ball:update(dt)
  159. --ballX = ballX + ballDX * dt these will instead be done with update
  160. --ballY = ballY + ballDY * dt
  161. end
  162.  
  163. 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
  164. player2:update(dt)
  165. ball:render()
  166. end
  167. function love.keypressed(key)
  168. if key == 'escape' then
  169. love.event.quit()
  170. --end
  171. elseif key == 'enter' or key == 'return' then
  172. if gameState == 'start' then
  173. gameState = 'serve'
  174. elseif gameState == 'victory' then
  175. gameState == 'start'
  176. elseif gameState == 'serve' then
  177. gameState = 'play'
  178. --ball:reset()
  179. end
  180. end
  181. end
  182.  
  183. function displayFPS()
  184. love.graphics.setColor(0,1,0,1) -- toggles color for drawing text... default is white.... RGBT values... could also be 255
  185. love.graphics.setFont(smallFont)
  186. love.graphics.print('FPS: ' .. tostring(love.timer.getFPS()), 40, 20) -- .. is the string concatination operator
  187. -- tostring() will give an integer values which will be incompatible with string
  188. love.graphics.setColor(1, 1, 1, 1)
  189. end
  190.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement