Advertisement
joseleeph

Untitled

Dec 26th, 2020
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.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. Class = require 'class' -- from Paddle.lua... require the line Paddle = Class{}
  10. push = require 'push'
  11.  
  12. require 'Ball'
  13. require 'Paddle'
  14.  
  15.  
  16.  
  17. function love.load()
  18. math.randomseed(os.time())
  19. love.graphics.setDefaultFilter('nearest','nearest')
  20.  
  21.  
  22. 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
  23. player2 = Paddle(VIRTUAL_WIDTH - 10, VIRTUAL_HEIGHT - 30, 5, 20)
  24. ball = Ball(VIRTUAL_WIDTH/2 - 2, VIRTUAL_HEIGHT/2 - 2, 5, 5) -- instatiate the ball .... not balle!
  25.  
  26.  
  27. gameState = 'start'
  28. smallFont = love.graphics.newFont('04B_30__.ttf',8)
  29. scoreFont = love.graphics.newFont('TitilliumText22L005-webfont.ttf',14)
  30.  
  31. love.graphics.setFont(smallFont)
  32.  
  33. push:setupScreen(VIRTUAL_WIDTH,VIRTUAL_HEIGHT, WINDOW_WIDTH, WINDOW_HEIGHT, {
  34. fullscreen = false,
  35. vsync = true,
  36. resizeable = false
  37. })
  38.  
  39. player1Score = 0
  40. player2Score = 0
  41. end
  42. function love.draw()
  43. push:apply('start')
  44. love.graphics.clear(40/255,45/255,52/255,255/255) -- map values to 0-1
  45. scoreFont = love.graphics.newFont('TitilliumText22L005-webfont.ttf',20)
  46. love.graphics.setFont(smallFont)
  47. if gameState == 'start' then
  48. love.graphics.printf('Hello Start State!', 0,20, VIRTUAL_WIDTH, 'center')
  49. elseif gameState == 'play' then
  50. love.graphics.printf('Hello Play State!', 0, 20, VIRTUAL_WIDTH, 'center')
  51. end
  52. love.graphics.setFont(scoreFont)
  53. love.graphics.print(player1Score, VIRTUAL_WIDTH/2 - 50, VIRTUAL_HEIGHT /3)
  54. love.graphics.print(player2Score, VIRTUAL_WIDTH/2+30, VIRTUAL_HEIGHT/3)
  55. ball:render()--love.graphics.rectangle('fill', ballX, ballY, 4, 4)
  56. player1:render() -- paddle1 render() is now a function stored in Paddle.lua
  57. player2:render()
  58.  
  59. displayFPS() -- show framerate
  60.  
  61. push:apply('end')
  62. end
  63.  
  64. function love.update(dt)
  65.  
  66. if ball:collides(player1) then -- collides() is stored in Ball.lua
  67. -- deflect the ball to the right
  68. -- change velocity from -100, to 100
  69. ball.dx = -ball.dx
  70. end
  71.  
  72. if ball:collides(player2) then
  73. -- deflect ball to the left
  74. ball.dx = -ball.dx
  75. end
  76.  
  77. if ball.y <= 0 then -- the top of the screen
  78. --deflect the ball down
  79. ball.dy = -ball.dy
  80. -- reset the ball in the case of it hitting the top of the screen
  81. ball.y = 0
  82. end
  83.  
  84. if ball.y >= VIRTUAL_HEIGHT - 4 then
  85. ball.dy = -ball.dy
  86. ball.y = VIRTUAL_HEIGHT - 4
  87. end
  88.  
  89.  
  90. -- player 1 movement
  91. if love.keyboard.isDown('w') then
  92. player1.dy = -PADDLE_SPEED -- negative is up
  93. elseif love.keyboard.isDown('s') then
  94. player1.dy = PADDLE_SPEED -- replaces math.min(VIRTUAL_HEIGHT - 2-,PLAYER1Y + PADDLE_SPEED * dt)
  95. else
  96. player1.dy = 0 -- if neither is true, the paddle speed is 0
  97. end
  98. -- player 2 movement
  99. if love.keyboard.isDown('up') then
  100. player2.dy = -PADDLE_SPEED
  101. elseif love.keyboard.isDown('down') then
  102. player2.dy = PADDLE_SPEED
  103. else
  104. player2.dy = 0
  105. end
  106. if gameState == 'play' then
  107. ball:update(dt)
  108. end
  109.  
  110. player1:update(dt)
  111. player2:update(dt)
  112. ball:render()
  113. end
  114. function love.keypressed(key)
  115. if key == 'escape' then
  116. love.event.quit()
  117. elseif key == 'enter' or key == 'return' then
  118. if gameState == 'start' then
  119. gameState = 'play'
  120. else
  121. gameState = 'start'
  122. ball:reset()
  123. end
  124. end
  125. end
  126.  
  127. function displayFPS()
  128. love.graphics.setColor(0,1,0,1)
  129. love.graphics.setFont(smallFont)
  130. love.graphics.print('FPS: ' .. tostring(love.timer.getFPS()), 40, 20)
  131. love.graphics.setColor(1, 1, 1, 1)
  132. end
  133.  
  134.  
  135. --collision detection
  136.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement