Advertisement
joseleeph

Untitled

Dec 29th, 2020
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.63 KB | None | 0 0
  1. Class = require 'class'
  2. push = require 'push'
  3. WINDOW_WIDTH = 1280
  4. WINDOW_HEIGHT = 720
  5.  
  6. VIRTUAL_WIDTH = 432
  7. VIRTUAL_HEIGHT = 243
  8.  
  9. PADDLE_SPEED = 200
  10.  
  11. Class = require 'class'
  12.  
  13. require 'Ball' -- require ball and paddle will have access to the class variable because they are declared first
  14. require 'Paddle'
  15.  
  16.  
  17.  
  18. function love.load()
  19. math.randomseed(os.time())
  20. love.graphics.setDefaultFilter('nearest','nearest')
  21.  
  22.  
  23. 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
  24. paddle2 = Paddle(VIRTUAL_WIDTH - 10, VIRTUAL_HEIGHT - 30, 5, 20)
  25. ball = Ball(VIRTUAL_WIDTH/2 - 2, VIRTUAL_HEIGHT/2 - 2, 5, 5)
  26. gameState = 'start'
  27. smallFont = love.graphics.newFont('04B_30__.ttf',8)
  28. scoreFont = love.graphics.newFont('TitilliumText22L005-webfont.ttf',14)
  29.  
  30. love.graphics.setFont(smallFont)
  31.  
  32. push:setupScreen(VIRTUAL_WIDTH,VIRTUAL_HEIGHT, WINDOW_WIDTH, WINDOW_HEIGHT, {
  33. fullscreen = false,
  34. vsync = true,
  35. resizeable = false
  36. })
  37.  
  38. player1Score = 0
  39. player2Score = 0
  40. end
  41. function love.draw()
  42. push:apply('start')
  43. love.graphics.clear(40/255,45/255,52/255,255/255) -- map values to 0-1
  44. scoreFont = love.graphics.newFont('TitilliumText22L005-webfont.ttf',20)
  45. smallFont = love.graphics.newFont('04B_30__.TTF',13)
  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. paddle1:render() -- paddle1 render() is now a function stored in Paddle.lua
  57. paddle2:render()
  58. --love.graphics.rectangle('fill', 10, player1Y, 5, 20) -- left side
  59. --love.graphics.rectangle('line', VIRTUAL_WIDTH - 10, player2Y, 5, 20) -- right side
  60.  
  61. displayFPS()
  62.  
  63. push:apply('end')
  64. end
  65.  
  66. function love.update(dt)
  67.  
  68. paddle1:update(dt)
  69. paddle2:update(dt)
  70.  
  71. ball:render()
  72.  
  73.  
  74. if love.keyboard.isDown('w') then
  75. paddle1.dy = -PADDLE_SPEED
  76. elseif love.keyboard.isDown('s') then
  77. paddle1.dy = PADDLE_SPEED
  78. else
  79. paddle1.dy = 0
  80. end
  81.  
  82. if love.keyboard.isDown('up') then
  83. paddle2.dy = -PADDLE_SPEED
  84. elseif love.keyboard.isDown('down') then
  85. paddle2.dy = PADDLE_SPEED
  86. else
  87. paddle2.dy = 0
  88. end
  89. if gameState == 'play' then
  90. ball:update(dt)
  91. --ballX = ballX + ballDX * dt these will instead be done with update
  92. --ballY = ballY + ballDY * dt
  93. end
  94. end
  95. function love.keypressed(key)
  96. if key == 'escape' then
  97. love.event.quit()
  98. elseif key == 'enter' or key == 'return' then
  99. if gameState == 'start' then
  100. gameState = 'play'
  101. --elseif gameState == 'play' then
  102. else
  103. gameState = 'start'
  104. --[[
  105. will be copied into ball:reset()
  106. ballX = VIRTUAL_WIDTH/2-2
  107. ballY = VIRTUAL_HEIGHT/2-2
  108. ballDX = math.random(2) == 1 and -100 or 100
  109. ballDY = math.random(-50,50)
  110. ]]
  111. ball:reset()
  112. end
  113. end
  114. end
  115.  
  116. function displayFPS()
  117. love.graphics.setColor(0,1,0,1) -- toggles color for drawing text... default is white.... RGBT values... could also be 255
  118. love.graphics.setFont(smallFont)
  119. love.graphics.print('FPS: ' .. tostring(love.timer.getFPS()), 40, 20) -- .. is the string concatination operator
  120. -- tostring() will give an integer values which will be incompatible with string
  121. love.graphics.setColor(1, 1, 1, 1)
  122. end
  123.  
  124.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement