Advertisement
joseleeph

Untitled

Dec 27th, 2020
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.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. 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.  
  111. --paddle1: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
  112. --paddle2:update(dt)
  113.  
  114. --ball:render()
  115.  
  116. -- player 1 movement
  117. if love.keyboard.isDown('w') then
  118. player1.dy = -PADDLE_SPEED -- negative is up
  119. elseif love.keyboard.isDown('s') then
  120. player1.dy = PADDLE_SPEED -- replaces math.min(VIRTUAL_HEIGHT - 2-,PLAYER1Y + PADDLE_SPEED * dt)
  121. else
  122. player1.dy = 0 -- if neither is true, the paddle speed is 0
  123. end
  124. -- player 2 movement
  125. if love.keyboard.isDown('up') then
  126. player2.dy = -PADDLE_SPEED
  127. elseif love.keyboard.isDown('down') then
  128. player2.dy = PADDLE_SPEED
  129. else
  130. player2.dy = 0
  131. end
  132. if gameState == 'play' then
  133. ball:update(dt)
  134. --ballX = ballX + ballDX * dt these will instead be done with update
  135. --ballY = ballY + ballDY * dt
  136. end
  137.  
  138. 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
  139. player2:update(dt)
  140. ball:render()
  141. end
  142. function love.keypressed(key)
  143. if key == 'escape' then
  144. love.event.quit()
  145. elseif key == 'enter' or key == 'return' then
  146. if gameState == 'start' then
  147. gameState = 'play'
  148. --elseif gameState == 'play' then
  149. else
  150. gameState = 'start'
  151. --[[
  152. will be copied into ball:reset()
  153. ballX = VIRTUAL_WIDTH/2-2
  154. ballY = VIRTUAL_HEIGHT/2-2
  155. ballDX = math.random(2) == 1 and -100 or 100
  156. ballDY = math.random(-50,50)
  157. ]]
  158. ball:reset()
  159. end
  160. end
  161. end
  162.  
  163. function displayFPS()
  164. love.graphics.setColor(0,1,0,1) -- toggles color for drawing text... default is white.... RGBT values... could also be 255
  165. love.graphics.setFont(smallFont)
  166. love.graphics.print('FPS: ' .. tostring(love.timer.getFPS()), 40, 20) -- .. is the string concatination operator
  167. -- tostring() will give an integer values which will be incompatible with string
  168. love.graphics.setColor(1, 1, 1, 1)
  169. end
  170. --[[
  171.  
  172. object oriented programming
  173.  
  174. Classes
  175. structs are similar to arguments in that they compartmentalize data in sematically meaningful way
  176. with classes you get data and also functions that are tied to a group of data
  177. you might have a class called car
  178. it will have attributes such as
  179. fuel
  180. brand
  181. color
  182. miles
  183. and anything else descriptive known as "fields"
  184. or maxspeed
  185. and also methods such as
  186. refuel() getFuel, setSpeed() getSpeed() drive accellerate(), turn() honk() which will tkae the form of functions
  187. objects are instatiated from class blueprints
  188.  
  189.  
  190. you can use the class as a blueprint to make objects of different class types.
  191. class is a blueprint, the object is what you use in the program
  192.  
  193. the paddles and ball are good uses for objects
  194.  
  195. love.window.setTitle(title)
  196. Simply sets the title of the application window, adding a slight level of polish
  197.  
  198. love.timer.getFPS()
  199. returns the current FPS of our application, making it easy to monitor when printed
  200.  
  201. ]]
  202.  
  203. --collision detection
  204. --[[
  205. AABB Colision detection
  206.  
  207. relies on all colliding etitites to have "axis-aligned bounding boxes", which simply means
  208. their collision boxes contain no rotation in our world space, which allows us
  209. to use a simple math formula to test for collision:
  210.  
  211. if rect1.x is not > rect2.x + rect2.wdth and rect1.x + rect1.width is not < rect2.x and rect1.y is not > rect2.y + rec2.height and rect1.y + rect1.height is not < rect2.y:
  212. collision is true
  213. else
  214. collision is false
  215. checks if the top and sides of the bounding boxes are greater than their opposite side
  216. if they are... there is no collision
  217. ]]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement