Advertisement
joseleeph

Untitled

Jan 1st, 2021
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.46 KB | None | 0 0
  1. Class = require 'class'
  2. push = require 'push'
  3.  
  4. require 'Paddle'
  5. require 'Ball'
  6.  
  7. WINDOW_WIDTH = 1280
  8. WINDOW_HEIGHT = 720
  9.  
  10. VIRUTAL_WIDTH = 432
  11. VIRTUAL_HEIGHT = 243
  12.  
  13. PADDLE_SPEED = 200
  14.  
  15.  
  16. push = require 'push'
  17.  
  18. function love.load()
  19. math.randomseed(os.time()) -- we want to decide on a velocity for the ball. math.random() will help. we must first seed the random number generator and we need to give it a value to actually seed. we give it os.time()
  20. -- is tied to use of math.random()
  21. love.graphics.setDefaultFilter('nearest', 'nearest')
  22. love.window.setTitle('Pong')
  23. smallFont= love.graphics.newFont('Retro Gaming.ttf', 8) -- creates a font object
  24.  
  25. scoreFont = love.graphics.newFont('Retro Gaming.ttf', 12)
  26. victoryFont = love.graphics.newFont('Retro Gaming.ttf', 30)
  27.  
  28. fpsFont = love.graphics.newFont()
  29.  
  30. player1Score = 0
  31. player2Score = 0
  32.  
  33. servingPlayer = math.random() == 1 and 1 or 2 -- a coin flip that will return 1 or 2
  34.  
  35. winningPlayer = 0
  36.  
  37.  
  38. love.graphics.setFont(smallFont)
  39.  
  40. sounds = { -- a table with 3 audio objects
  41. ['paddle_hit'] = love.audio.newSource('Paddle_Hit.wav', 'static'),
  42. ['point_scored'] = love.audio.newSource('Point_scored.wav', 'static'),
  43. ['wall_hit'] = love.audio.newSource('Wall_Hit.wav', 'static'),
  44. }
  45.  
  46. push:setupScreen(VIRUTAL_WIDTH, VIRTUAL_HEIGHT, WINDOW_WIDTH, WINDOW_HEIGHT, {
  47. fullscreen = false,
  48. resizeable = true,
  49. vsync = true
  50. -- resizeable must now be true in order for the window to be able to resize
  51. --resizeable = false
  52. })
  53.  
  54. player1 = Paddle(5, 20, 5, 20) -- construct a new paddle... it will call the init method implicitly
  55. player2 = Paddle(VIRUTAL_WIDTH - 10, VIRTUAL_HEIGHT - 30, 5, 20)
  56. ball = Ball(VIRUTAL_WIDTH/2-2, VIRTUAL_HEIGHT/2-2, 5, 5)
  57.  
  58. if servingPlayer == 1 then
  59. ball.dx = 100
  60. else
  61. ball.dx = -100
  62. end
  63.  
  64.  
  65.  
  66. gameState = 'start' -- should gamestate be start or play? try both
  67.  
  68.  
  69.  
  70.  
  71. end
  72.  
  73. function love.resize(w, h)
  74. push:resize(w, h) -- you can pass in arguments to resize with arguments w, and h
  75. end
  76.  
  77. function love.update(dt)
  78. if gameState == 'play' then
  79.  
  80. if ball.x < 0 then
  81. player2Score = player2Score + 1
  82. servingPlayer = 1
  83. sounds['point_scored']:play()
  84.  
  85. if player2Score >= 3 then
  86. gameState = 'victory'
  87. winningPlayer = 2 -- winning player isn't displayed
  88. else
  89. gameState = 'serve'
  90. ball:reset()
  91. end
  92.  
  93. end
  94.  
  95. if ball.x >= VIRUTAL_WIDTH - 4 then
  96. player1Score = player1Score + 1
  97. servingPlayer = 2
  98.  
  99. sounds['point_scored']:play()
  100. if player1Score >= 3 then
  101. gameState = 'victory'
  102. winningPlayer = 1 -- winning player message isn't displayed
  103. else
  104. gameState = 'serve'
  105. ball:reset()
  106. end
  107.  
  108.  
  109. end
  110.  
  111. if ball:collides(player1) then
  112.  
  113. ball.dx = -ball.dx * 1.03
  114. ball.x = player1.x + 5
  115.  
  116. sounds['paddle_hit']:play() -- colon is important because it's an object
  117. if ball.dy < 0 then
  118. ball.dy = -math.random(10, 150)
  119. else
  120. ball.dy = math.random(10, 150)
  121. end
  122. end
  123.  
  124. if ball:collides(player2) then
  125.  
  126. ball.dx = -ball.dx
  127.  
  128. sounds['paddle_hit']:play()
  129. end
  130.  
  131.  
  132. if ball.y <= 0 then
  133. ball.dy = -ball.dy -- deflect the ball down
  134. ball.y = 0 -- we want to reset the ball into the playfield in the case that it hit the top of the screen
  135.  
  136. sounds['wall_hit']:play()
  137. end
  138.  
  139. if ball.y >= VIRTUAL_HEIGHT - 4 then
  140. ball.dy = -ball.dy
  141. ball.y = VIRTUAL_HEIGHT - 4 -- in the case that it hits the bottom of the screen
  142. sounds['wall_hit']:play()
  143. end
  144.  
  145.  
  146.  
  147. if love.keyboard.isDown('w') then
  148. player1.dy = -PADDLE_SPEED
  149. elseif love.keyboard.isDown('s') then
  150. player1.dy = PADDLE_SPEED
  151.  
  152.  
  153. else
  154. player1.dy = 0
  155. end
  156.  
  157.  
  158. player2.y = ball.y -10000000000*dt*player2.dy -- i'm trying to make my opponent less impossible to beat
  159.  
  160. if gameState == 'play' then
  161. ball:update(dt)
  162. --ballX = ballX + ballDX * dt
  163. --ballY = ballY + ballDY * dt
  164. end
  165. player1:update(dt)
  166. player2:update(dt)
  167. end
  168. end
  169.  
  170. function love.keypressed(key)
  171. if key == 'escape' then
  172. love.event.quit()
  173. elseif key == 'enter' or key == 'return' then
  174. if gameState == 'start' then
  175. --gameState = 'play'
  176. gameState = 'serve'
  177. elseif gameState == 'victory' then
  178. gameState = 'start'
  179. player2Score = 0
  180. player1Score = 0
  181. elseif gameState == 'serve' then
  182. gameState = 'play'
  183. end
  184. end
  185. end
  186.  
  187. function love.draw()
  188. push:apply('start')
  189. love.graphics.clear(40/255, 45/255, 52/255, 255/255)
  190. love.graphics.setFont(smallFont)
  191. love.graphics.setFont(scoreFont)
  192. if gameState == 'start' then
  193. love.graphics.printf("Welcome to Pong!", 0, 20, VIRUTAL_WIDTH, 'center')
  194. love.graphics.printf("press enter to play", 0, 32, VIRUTAL_WIDTH, 'center')
  195. elseif gameState == 'serve' then
  196. --love.graphics.setFont(smallFont)
  197. love.graphics.printf("Player " .. tostring(servingPlayer) .. "'s serve!", 0, 20, VIRUTAL_WIDTH, 'center') -- neither will happen during play state
  198. love.graphics.printf("Press enter to serve", 0, 32, VIRUTAL_WIDTH, 'center')
  199. elseif gamestate == 'victory' then
  200. -- draw a victory message
  201. love.graphics.setFont(victoryFont)
  202. love.graphics.printf("Player " .. tostring(winningPlayer) .. " Wins!", 0, 10, VIRUTAL_WIDTH, 'center')
  203. love.graphics.printf("Press enter to serve", 0, 42, VIRUTAL_WIDTH, 'center')
  204. love.graphics.setFont(smallFont)
  205. elseif gameState == 'play' then
  206. -- no UI messages to display in play
  207. end
  208.  
  209. love.graphics.print(player1Score, VIRUTAL_WIDTH/2-50, VIRTUAL_HEIGHT/3)
  210. love.graphics.print(player2Score, VIRUTAL_WIDTH/2 + 30, VIRTUAL_HEIGHT/3)
  211.  
  212.  
  213. --love.graphics.rectangle('fill', ballX, ballY, 5, 5) -- ball
  214. player1:render()
  215. player2:render()
  216.  
  217. ball:render()
  218.  
  219. displayFPS()
  220. push:apply('end')
  221.  
  222. end
  223.  
  224. function displayFPS()
  225. love.graphics.setColor(0,255,0)
  226. love.graphics.setFont(fpsFont, 10)
  227. love.graphics.print('fps:' .. tostring(love.timer.getFPS()), 40, 20) -- the tostring() method will give an ineger value. .. is the string concatination operator in lua
  228. love.graphics.setColor(1, 1, 1, 1) -- set everything back to white as the default color for whenever it draws anything else like the badle or ball
  229. end
  230.  
  231. -- resize application
  232. --[[
  233. love.resize(width, height)
  234. LÖVE will pass width and height to push -- this doesn't work... but worry about it later
  235.  
  236. your assignment is to create a basic ai to play against the first player
  237. make it as easy or difficult as you wish
  238. make sure the tracking movement is smooth and not instantaneous
  239. ]]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement