Advertisement
ssoni

pong.lua

Mar 29th, 2022
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.41 KB | None | 0 0
  1. -- INSTALLATIONS
  2. -- https://cs50.harvard.edu/x/2021/seminars/#a-taste-of-game-development
  3. -- https://love2d.org/
  4. -- https://code.visualstudio.com/download
  5. -- https://github.com/Ulydev/push
  6.  
  7. VIRTUAL_WIDTH = 384
  8. VIRTUAL_HEIGHT = 216
  9. WINDOW_WIDTH = 1280
  10. WINDOW_HEIGHT = 720
  11.  
  12. PADDLE_WIDTH = 8
  13. PADDLE_HEIGHT = 32
  14. PADDLE_SPEED = 140
  15.  
  16. BALL_SIZE = 4
  17.  
  18. LARGE_FONT = love.graphics.newFont(32)
  19. SMALL_FONT = love.graphics.newFont(16)
  20.  
  21. POINTS_WINNER = 2
  22.  
  23. -- #include.....
  24. push = require 'push'
  25.  
  26. gamestate = 'title'
  27.  
  28. player1 = {
  29. x = 10, y = 10 , score = 0
  30. }
  31.  
  32. player2 = {
  33. x = VIRTUAL_WIDTH - PADDLE_WIDTH - 10,
  34. y = VIRTUAL_HEIGHT - PADDLE_HEIGHT - 10,
  35. score = 0
  36. }
  37.  
  38. ball = {
  39. x = (VIRTUAL_WIDTH / 2) - (BALL_SIZE/2),
  40. y = (VIRTUAL_HEIGHT / 2) - (BALL_SIZE/2),
  41. dx = 0, dy = 0
  42. }
  43.  
  44. function love.load()
  45. math.randomseed(os.time())
  46. love.graphics.setDefaultFilter('nearest','nearest')
  47. push:setupScreen(VIRTUAL_WIDTH, VIRTUAL_HEIGHT, WINDOW_WIDTH, WINDOW_HEIGHT)
  48.  
  49. resetBall()
  50. end
  51.  
  52. function love.update(dt)
  53. if love.keyboard.isDown('w') then
  54. player1.y = player1.y - PADDLE_SPEED * dt
  55. elseif love.keyboard.isDown('s') then
  56. player1.y = player1.y + PADDLE_SPEED * dt
  57. end
  58.  
  59. if love.keyboard.isDown('up') then
  60. player2.y = player2.y - PADDLE_SPEED * dt
  61. elseif love.keyboard.isDown('down') then
  62. player2.y = player2.y + PADDLE_SPEED * dt
  63. end
  64.  
  65. if gamestate == 'play' then
  66. ball.x = ball.x + ball.dx * dt
  67. ball.y = ball.y + ball.dy * dt
  68.  
  69. --1P mode
  70. if ball.dx > 0 then
  71. if (ball.y < player2.y) then
  72. player2.y = player2.y - PADDLE_SPEED * dt
  73. else
  74. player2.y = player2.y + PADDLE_SPEED * dt
  75. end
  76. end
  77.  
  78. if ball.y <= 0 then
  79. ball.dy = -ball.dy
  80. elseif ball.y >= VIRTUAL_HEIGHT - BALL_SIZE then
  81. ball.dy = -ball.dy
  82. end
  83.  
  84. if collides(ball, player1) then
  85. ball.x = player1.x + PADDLE_WIDTH
  86. ball.dx = -ball.dx
  87. elseif collides(ball, player2) then
  88. ball.x = player2.x - BALL_SIZE
  89. ball.dx = -ball.dx
  90. end
  91.  
  92. if ball.x <= 0 then
  93. resetBall()
  94. gamestate = 'serve'
  95. player2.score = player2.score + 1
  96. if player2.score >= POINTS_WINNER then
  97. gamestate = 'win'
  98. end
  99. elseif ball.x >= VIRTUAL_WIDTH - BALL_SIZE then
  100. resetBall()
  101. gamestate = 'serve'
  102. player1.score = player1.score + 1
  103. if player1.score >= POINTS_WINNER then
  104. gamestate = 'win'
  105. end
  106. end
  107.  
  108. end
  109. end
  110.  
  111. function love.keypressed(key)
  112. if key == 'escape' then
  113. love.event.quit()
  114. end
  115.  
  116. if key == 'enter' or key == 'return' then
  117. if gamestate == 'title' then
  118. gamestate = 'serve'
  119. elseif gamestate == 'serve' then
  120. gamestate = 'play'
  121. elseif gamestate == 'win' then
  122. player1.score = 0
  123. player2.score = 0
  124. gamestate = 'title'
  125. end
  126. end
  127.  
  128. end
  129.  
  130. function love.draw()
  131. push:start()
  132. --love.graphics.print('Hello, world!')
  133.  
  134. --set RBG of background
  135. love.graphics.clear(40/255, 45/255, 52/255, 255/255)
  136. if gamestate == 'title' then
  137. love.graphics.setFont(LARGE_FONT)
  138. love.graphics.printf('Pre50 Pong', 0, 10, VIRTUAL_WIDTH, 'center')
  139. love.graphics.setFont(SMALL_FONT)
  140. love.graphics.printf("Press Enter to start the game!", 0, VIRTUAL_HEIGHT-32, VIRTUAL_WIDTH, 'center')
  141. elseif gamestate == 'serve' then
  142. love.graphics.setFont(SMALL_FONT)
  143. love.graphics.printf("Press Enter to Serve!", 0, 10, VIRTUAL_WIDTH, 'center')
  144. elseif gamestate == 'win' then
  145. love.graphics.setFont(LARGE_FONT)
  146. --local winner = player1.score >= 10 and '1' or '2'
  147.  
  148. local winner
  149. if player1.score >= POINTS_WINNER then
  150. winner = '1'
  151. else
  152. winner = '2'
  153. end
  154. love.graphics.printf('Player ' .. winner .. ' wins!', 0, 10, VIRTUAL_WIDTH, 'center')
  155. love.graphics.setFont(SMALL_FONT)
  156. love.graphics.printf("Press Enter to restart the game!", 0, VIRTUAL_HEIGHT-32, VIRTUAL_WIDTH, 'center')
  157. end
  158.  
  159. love.graphics.setFont(LARGE_FONT)
  160. love.graphics.print(player1.score, VIRTUAL_WIDTH / 2 - 32, VIRTUAL_HEIGHT / 2 - 16)
  161. love.graphics.print(player2.score, VIRTUAL_WIDTH / 2 + 16, VIRTUAL_HEIGHT / 2 - 16)
  162. love.graphics.setFont(SMALL_FONT)
  163.  
  164. love.graphics.rectangle('fill',player1.x, player1.y, PADDLE_WIDTH, PADDLE_HEIGHT)
  165. love.graphics.rectangle('fill',player2.x, player2.y, PADDLE_WIDTH, PADDLE_HEIGHT)
  166. love.graphics.rectangle('fill',ball.x, ball.y, BALL_SIZE, BALL_SIZE)
  167. push:finish()
  168. end
  169.  
  170.  
  171. function collides(b, p)
  172. return not (b.y > p.y + PADDLE_HEIGHT or b.x > p.x + PADDLE_WIDTH or p.y > b.y + BALL_SIZE or p.x > b.x + BALL_SIZE)
  173.  
  174. end
  175.  
  176. function resetBall()
  177.  
  178. ball.x = (VIRTUAL_WIDTH / 2) - (BALL_SIZE/2)
  179. ball.y = (VIRTUAL_HEIGHT / 2) - (BALL_SIZE/2)
  180.  
  181. ball.dx = 60 + math.random(60)
  182. if math.random(2) == 1 then
  183. ball.dx = -ball.dx
  184. end
  185.  
  186. ball.dy = 30 + math.random(60)
  187. if math.random(2) == 1 then
  188. ball.dy = -ball.dy
  189. end
  190.  
  191. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement