Advertisement
joseleeph

Untitled

Dec 30th, 2020
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.90 KB | None | 0 0
  1. Class = require 'class'
  2. push = require 'push' -- push is a library that will allow us to draw our game at a virtual resolution. in order to get it to work you need to import the library
  3.  
  4. require 'Paddle' -- because of the order that we are declaring the variables, require ball and paddle will have access to the class variable. if you don't specify local it will be accessible anywhere in the app
  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())
  20. love.graphics.setDefaultFilter('nearest', 'nearest')
  21. smallFont= love.graphics.newFont('Retro Gaming.ttf', 8) -- creates a font object
  22.  
  23. scoreFont = love.graphics.newFont('Retro Gaming.ttf', 12)
  24.  
  25. fpsFont = love.graphics.newFont()
  26.  
  27. player1Score = 0
  28. player2Score = 0
  29.  
  30. love.graphics.setFont(smallFont)
  31. push:setupScreen(VIRUTAL_WIDTH, VIRTUAL_HEIGHT, WINDOW_WIDTH, WINDOW_HEIGHT, {
  32. fullscreen = false,
  33. vsync = true,
  34. resizeable = false
  35. })
  36.  
  37. player1 = Paddle(5, 20, 5, 20) -- construct a new paddle... it will call the init method implicitly
  38. player2 = Paddle(VIRUTAL_WIDTH - 10, VIRTUAL_HEIGHT - 30, 5, 20)
  39. ball = Ball(VIRUTAL_WIDTH/2-2, VIRTUAL_HEIGHT/2-2, 5, 5)
  40.  
  41. gameState = 'play' -- should gamestate be start or play? try both
  42.  
  43.  
  44.  
  45.  
  46. end
  47.  
  48. function love.update(dt)
  49. if ball:collides(player1) then
  50. --deflect to the right
  51. ball.dx = -ball.dx -- only dx needs to be flipped
  52. end
  53.  
  54. if ball:collides(player2) then
  55. --deflect to the left
  56. ball.dx = -ball.dx
  57. end
  58.  
  59.  
  60. if ball.y <= 0 then
  61. ball.dy = -ball.dy
  62. ball.y = 0
  63. end
  64.  
  65. if ball.y >= VIRTUAL_HEIGHT - 4 then
  66. ball.dy = -ball.dy
  67. ball.y = VIRTUAL_HEIGHT
  68. end
  69.  
  70.  
  71.  
  72. if love.keyboard.isDown('w') then
  73. player1.dy = -PADDLE_SPEED
  74. elseif love.keyboard.isDown('s') then
  75. player2.dy = PADDLE_SPEED
  76.  
  77.  
  78. else
  79. player1.dy = 0
  80. end
  81. if love.keyboard.isDown('up') then
  82. player2.dy = -PADDLE_SPEED
  83.  
  84. elseif love.keyboard.isDown('down') then
  85. player2.dy = PADDLE_SPEED
  86. else
  87. player2.dy = 0
  88. end
  89.  
  90. if gameState == 'play' then
  91. ball:update(dt)
  92.  
  93. end
  94. player1:update(dt)
  95. player2:update(dt)
  96. end
  97.  
  98. function love.keypressed(key)
  99. if key == 'escape' then
  100. love.event.quit()
  101. elseif key == 'enter' or key == 'return' then
  102. if gameState == 'start' then
  103. gameState = 'play'
  104. else
  105. gameState = 'start'
  106. ball:reset()
  107. end
  108. end
  109. end
  110.  
  111. function love.draw()
  112. push:apply('start')
  113. love.graphics.clear(40/255, 45/255, 52/255, 255/255)
  114. love.graphics.setFont(smallFont)
  115.  
  116. if gameState == 'start' then
  117. love.graphics.printf("Hello Start State!", 0, 20, VIRUTAL_WIDTH, 'center')
  118. elseif gameState == 'play' then
  119. love.graphics.printf('Hello Play State!', 0, 20 , VIRUTAL_WIDTH, 'center')
  120. end
  121. love.graphics.setFont(scoreFont)
  122. love.graphics.print(player1Score, VIRUTAL_WIDTH/2-50, VIRTUAL_HEIGHT/3)
  123. love.graphics.print(player2Score, VIRUTAL_WIDTH/2 + 30, VIRTUAL_HEIGHT/3)
  124.  
  125.  
  126. --love.graphics.rectangle('fill', ballX, ballY, 5, 5) -- ball
  127. player1:render()
  128. player2:render()
  129.  
  130. ball:render()
  131.  
  132. displayFPS()
  133. push:apply('end')
  134.  
  135. end
  136.  
  137. function displayFPS()
  138. love.graphics.setColor(0,255,0)
  139. love.graphics.setFont(fpsFont, 10)
  140. 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
  141. 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
  142. end
  143.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement