Advertisement
joseleeph

Untitled

Dec 25th, 2020
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.62 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'
  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
  17. require 'Paddle'
  18.  
  19. function love.load()
  20. math.randomseed(os.time())
  21. love.graphics.setDefaultFilter('nearest','nearest')
  22.  
  23. --[[
  24. player1Y = 30
  25. player2Y = 40 get
  26. get replaced with paddle objects called paddle1 and paddle2
  27. ]]
  28.  
  29. paddle1 = 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
  30. paddle2 = Paddle(VIRTUAL_WIDTH - 10, VIRTUAL_HEIGHT - 30, 5, 20)
  31. balle = Ball(VIRTUAL_WIDTH/2 - 2, VIRTUAL_HEIGHT/2 - 2, 5, 5) -- instatiate the ball
  32. --[[
  33. these lines are no longer needed
  34.  
  35. ballX = VIRTUAL_WIDTH/2-2
  36. ballY = VIRTUAL_HEIGHT/2-2
  37.  
  38. ballDX = math.random(2) == 1 and -100 or 100
  39. ballDY = math.random(-50,50)
  40.  
  41. ]]
  42.  
  43.  
  44. gameState = 'start'
  45. smallFont = love.graphics.newFont('04B_30__.ttf',8)
  46. scoreFont = love.graphics.newFont('TitilliumText22L005-webfont.ttf',14)
  47.  
  48. love.graphics.setFont(smallFont)
  49.  
  50. push:setupScreen(VIRTUAL_WIDTH,VIRTUAL_HEIGHT, WINDOW_WIDTH, WINDOW_HEIGHT, {
  51. fullscreen = false,
  52. vsync = true,
  53. resizeable = false
  54. })
  55.  
  56. player1Score = 0
  57. player2Score = 0
  58. end
  59. function love.draw()
  60. push:apply('start')
  61. love.graphics.clear(40/255,45/255,52/255,255/255) -- map values to 0-1
  62. scoreFont = love.graphics.newFont('TitilliumText22L005-webfont.ttf',20)
  63. love.graphics.setFont(smallFont)
  64. if gameState == 'start' then
  65. love.graphics.printf('Hello Start State!', 0,20, VIRTUAL_WIDTH, 'center')
  66. elseif gameState == 'play' then
  67. love.graphics.printf('Hello Play State!', 0, 20, VIRTUAL_WIDTH, 'center')
  68. end
  69. love.graphics.setFont(scoreFont)
  70. love.graphics.print(player1Score, VIRTUAL_WIDTH/2 - 50, VIRTUAL_HEIGHT /3)
  71. love.graphics.print(player2Score, VIRTUAL_WIDTH/2+30, VIRTUAL_HEIGHT/3)
  72. ball:render()--love.graphics.rectangle('fill', ballX, ballY, 4, 4)
  73. paddle1:render() -- paddle1 render() is now a function stored in Paddle.lua
  74. paddle2:render()
  75. --love.graphics.rectangle('fill', 10, player1Y, 5, 20) -- left side
  76. --love.graphics.rectangle('line', VIRTUAL_WIDTH - 10, player2Y, 5, 20) -- right side
  77. push:apply('end')
  78. end
  79.  
  80. function love.update(dt)
  81.  
  82. paddle1:update(dt)
  83. paddle2:update(dt)
  84.  
  85. -- player 1 movement
  86. if love.keyboard.isDown('w') then
  87. paddle1.dy = -PADDLE_SPEED -- is up
  88. elseif love.keyboard.isDown('s') then
  89. paddle1.dy = PADDLE_SPEED
  90. else
  91. paddle1.dy = 0 -- if neither is true, the paddle speed is 0
  92. end
  93. -- player 2 movement
  94. if love.keyboard.isDown('up') then
  95. paddle2.dy = -PADDLE_SPEED
  96. elseif love.keyboard.isDown('down') then
  97. paddle2.dy = PADDLE_SPEED
  98. else
  99. paddle2.dy = 0
  100. end
  101. if gameState == 'play' then
  102. ballX = ballX + ballDX * dt
  103. ballY = ballY + ballDY * dt
  104. end
  105. end
  106. function love.keypressed(key)
  107. if key == 'escape' then
  108. love.event.quit()
  109. elseif key == 'enter' or key == 'return' then
  110. if gameState == 'start' then
  111. gameState = 'play'
  112. --elseif gameState == 'play' then
  113. else
  114. gameState = 'start'
  115. --[[
  116. will be copied into ball:reset()
  117. ballX = VIRTUAL_WIDTH/2-2
  118. ballY = VIRTUAL_HEIGHT/2-2
  119. ballDX = math.random(2) == 1 and -100 or 100
  120. ballDY = math.random(-50,50)
  121. ]]
  122. ball:reset()
  123. end
  124. end
  125. end
  126.  
  127.  
  128.  
  129.  
  130. --[[
  131.  
  132. object oriented programming
  133.  
  134. Classes
  135. structs are similar to arguments in that they compartmentalize data in sematically meaningful way
  136. with classes you get data and also functions that are tied to a group of data
  137. you might have a class called car
  138. it will have attributes such as
  139. fuel
  140. brand
  141. color
  142. miles
  143. and anything else descriptive known as "fields"
  144. or maxspeed
  145. and also methods such as
  146. refuel() getFuel, setSpeed() getSpeed() drive accellerate(), turn() honk() which will tkae the form of functions
  147. objects are instatiated from class blueprints
  148.  
  149.  
  150. you can use the class as a blueprint to make objects of different class types.
  151. class is a blueprint, the object is what you use in the program
  152.  
  153. the paddles and ball are good uses for objects
  154. ]]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement