Advertisement
joseleeph

Untitled

Dec 23rd, 2020
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.68 KB | None | 0 0
  1. pong3
  2. WINDOW_WIDTH = 1280
  3. WINDOW_HEIGHT = 720
  4.  
  5. --[[ we want to use the push library to simulate that we are coding on a smaller raster size.
  6. nes was 240 p
  7. ]]
  8. VIRTUAL_WIDTH = 432
  9. VIRTUAL_HEIGHT = 243
  10.  
  11. PADDLE_SPEED = 200 -- pixels per second
  12.  
  13.  
  14. -- not technically constants... the fact that they are capitalized is conventional notation to programmers that the values should not be changed
  15. --[[
  16. runs when the game first starts up, only once; used to initialize
  17. a similar resolution to the nes. still modern in appearance, but has the nes look to it
  18. ]]
  19.  
  20. --import the push library
  21.  
  22. push = require 'push'
  23. -- it will automatically know that you're looking for something.lua, or push.lua by default
  24.  
  25. function love.load()
  26. math.randomseed(os.time()) -- seed random number generator... we need a number to actual seed it... a starting value for which to base the random number generation with os.time()
  27. -- looks blurry because it's applying filtering to a font. ... in order to fix this, use the setDefaultFilter() method
  28. love.graphics.setDefaultFilter('nearest','nearest')
  29. player1Y = 30
  30. player2Y = 40
  31.  
  32. ballX = VIRTUAL_WIDTH/2-2
  33. ballY = VIRTUAL_HEIGHT/2-2
  34.  
  35. ballDX = math.random(2) == 1 and -100 or 100 -- set the ball to -100, or 100, based effectively on a coin flip... a random value between 1 and 2
  36. ballDY = math.random(-50,50) -- give me a random value between -50, and 50...
  37.  
  38. gameState = 'start'
  39. -- by combining a different random intial x and y change, we can set random velocities in both directions
  40. --[[
  41. if it equals 1 it's true and we will get -100. and if it's false we'll get 100
  42. and will take whatever the first false value it
  43. can find and if not the second value.
  44. if it finds a false value it will calculate the or
  45. or operates in such that it takes the first true value it finds.
  46. ]]
  47.  
  48.  
  49.  
  50. smallFont = love.graphics.newFont('04B_30__.ttf',8) -- use the font named 04B_30__.ttf in 8 pixels, newfont function creates a font object
  51. scoreFont = love.graphics.newFont('TitilliumText22L005-webfont.ttf',14)
  52.  
  53. love.graphics.setFont(smallFont) -- set the font object to use it
  54.  
  55. push:setupScreen(VIRTUAL_WIDTH,VIRTUAL_HEIGHT, WINDOW_WIDTH, WINDOW_HEIGHT, {
  56. fullscreen = false,
  57. vsync = true,
  58. resizeable = false
  59. })
  60.  
  61. player1Score = 0
  62. player2Score = 0
  63.  
  64. --player1Y = 30
  65. --player2Y = 40
  66. --[[
  67. similar to love.window.setmode() but take 4 arguments instead
  68. vwidth and height for virtual raster to be, and windowwidth and window height in terms of how big we want our physical window dimensions to be
  69. ]]
  70. end
  71. -- end keyword is needed to tell lua that this is the end of the function definition
  72.  
  73. --[[
  74. push is an object calling a method inside of itself.
  75. kind of like a function contained within that object that has data specific to that object as opposed to just a global object that belongs to a table
  76. push operates with the window in a different way than the default love.window.setmode()
  77. love.window.setMode(WINDOW_WIDTH, WINDOW_HEIGHT, {
  78. fullscreen = false,
  79. vsync = true,
  80. resizable = false
  81. })
  82. ]]
  83. -- how big do we want the screen to be
  84. -- end keyword is needed to tell lua that this is the end of the function definition
  85. --[[
  86. in order to get push to actually work, we've set it up, but it's not doing anything yet
  87. we must first call push:apply()
  88. ]]
  89. function love.draw()
  90. push:apply('start')
  91. love.graphics.clear(40/255,45/255,52/255,255/255) -- map values to 0-1
  92. scoreFont = love.graphics.newFont('TitilliumText22L005-webfont.ttf',20)
  93. love.graphics.setFont(smallFont)
  94. if gameState == 'start' then
  95. love.graphics.printf('Hello Start State!', 0,20, VIRTUAL_WIDTH, 'center')
  96. elseif gameState == 'play' then
  97. love.graphics.printf('Hello Play State!', 0, 20, VIRTUAL_WIDTH, 'center')
  98. end
  99. love.graphics.setFont(scoreFont)
  100. love.graphics.print(player1Score, VIRTUAL_WIDTH/2 - 50, VIRTUAL_HEIGHT /3)
  101. love.graphics.print(player2Score, VIRTUAL_WIDTH/2+30, VIRTUAL_HEIGHT/3)
  102.  
  103. love.graphics.rectangle('fill', ballX, ballY, 4, 4)
  104.  
  105. --love.graphics.rectangle('fill', VIRTUAL_WIDTH/2 -2, VIRTUAL_WIDTH/2-2, 3.5, 3.5) -- center ball
  106.  
  107. --[[
  108. game state is whether the game has beginning waiting for user input, playing the game and a
  109. boundary has passed, so change the score, maybe the ending score was reached and the game state is over
  110.  
  111. math.randomseed(num)
  112. "seeds" the random number generator used by Lua(math.random) with some value such that
  113. its randomness is dependent on that supplied value, allowing us to pass in different numbers each playthrough
  114. to guarantee non-consistency across different program executions (or uniformity if we want consisten
  115. behavior for testing)
  116.  
  117. os.time()
  118. Lua function that returns, in seconds, the time since 00:00:00 jan1, 1970, also
  119. known as Unix epoch time
  120.  
  121. math.random(min, max)
  122. returns a random number dependent on the seeded random number generator,
  123. between min and max inclusive
  124.  
  125. math.min(num1, num2)
  126. returns the lesser of the two numbers passed in
  127.  
  128. math.max(num1, num2)
  129. returns the greater of the two numbers passed in
  130. ]]
  131.  
  132. love.graphics.rectangle('fill', 10, player1Y, 5, 20) -- left side
  133. love.graphics.rectangle('line', VIRTUAL_WIDTH - 10, player2Y, 5, 20) -- right side
  134.  
  135.  
  136. -- ball
  137. --love.graphics.rectangle('fill', VIRTUAL_WIDTH/2 -2, VIRTUAL_HEIGHT/2-2, 4, 4) -- ball.. where it's centered, and it's size in pixels
  138. -- paddle left
  139. --love.graphics.rectangle('line', VIRTUAL_WIDTH - 10, VIRTUAL_HEIGHT - 50, 5, 20) -- paddle 2 on the left
  140. -- paddle right
  141. --love.graphics.rectangle('fill', 5, 20, 5, 20)-- 5 pixels from the left, 20 pixels from top, 5 pixels wide by 20 pixels tall
  142.  
  143.  
  144.  
  145. --love.graphics.printf("Hello Pong!", 0, 20, VIRTUAL_WIDTH, 'center')-- draw the word 20 pixels down
  146. --love.graphics.printf("Hello Pong!", 0, VIRTUAL_HEIGHT/2-6, VIRTUAL_WIDTH, 'center')-- window is not that size anymore. will be drawn at 128-x720, but will be a raster of 432 x 243
  147. -- end rendering at virtual resolution
  148. push:apply('end')
  149. end
  150.  
  151. --[[
  152. update function
  153. any time you want to change the position or configuration depending on user input
  154. you want to override love update(dt)
  155. ]]
  156.  
  157. function love.update(dt)
  158. --[[
  159. dt allows you to move things independant of the frame rate. if you know hardware is going
  160. to run at a framerate, you can calculate everything on a per frame basis
  161. and not worry about time passed between frames. on different systems this can differ
  162. to normalize this multiply all the calculations that you want to have happen by the
  163. amount of time that has passed since the last frame dt
  164. ]]
  165.  
  166. --player1 movement
  167. if love.keyboard.isDown('w') then
  168. player1Y = math.max(0, player1Y + -PADDLE_SPEED * dt) --math.max returns the greater of the two values
  169. --[[ clamp the y value to be no less than 0
  170. add negative paddle speed to current Y scaled by deltaTime
  171. player1Y = player1Y - PADDLE_SPEED * dt
  172. remember that higher numbers are down... dt is delta time
  173.  
  174. if player1Y > 243 then
  175. player1Y = 243... does not work
  176. ]]
  177. elseif love.keyboard.isDown('s') then
  178. player1Y = math.min(VIRTUAL_HEIGHT - 20, player1Y + PADDLE_SPEED * dt) -- math.min returns the lesser of the two values
  179. -- y value should be no greaeter than the bottom of the screen minus 20 pixels... the size of the paddle
  180. --[[
  181. clamp the y value to no more than VIRTUAL_HEIGHT - 20... the height of the screen minus the height of the paddle
  182. remember that positive is down and negative is up... so set the minmum value to
  183. ]]
  184. end
  185. -- player 2 movement
  186. if love.keyboard.isDown('up') then
  187. player2Y = math.max(0, player2Y + -PADDLE_SPEED * dt) --down is positive ... add negative paddle speed to the current Y scaled by dletatime
  188. elseif love.keyboard.isDown('down') then
  189. player2Y = math.min(VIRTUAL_HEIGHT - 20, player2Y + PADDLE_SPEED * dt) -- up is negative... add positive padd
  190. end
  191. if gameState == 'play' then
  192. ballX = ballX + ballDX * dt
  193. ballY = ballY + ballDY * dt -- you may be adding a positive or negative number
  194. end
  195. end
  196.  
  197. function love.keypressed(key)
  198. -- keyboard handling called by love each frame. passes in the key so we can access it
  199. if key == 'escape' then -- can be accessed by string name
  200. love.event.quit() --quit() is a function love gives us to terminate
  201. elseif key == 'enter' or key == 'return' then
  202. if gameState == 'start' then
  203. gameState = 'play'
  204. elseif gameState == 'play' then
  205. gameState = 'start'
  206. ballX = VIRTUAL_WIDTH/2-2
  207. ballY = VIRTUAL_HEIGHT/2-2
  208. ballDX = math.random(2) == 1 and -100 or 100 -- set the ball to -100, or 100, based effectively on a coin flip... a random value between 1 and 2
  209. ballDY = math.random(-50,50)
  210. -- repeating this code from above will ensure that the ball will be reset eachtime enter is pressed
  211. end
  212. end
  213. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement