Advertisement
joseleeph

Untitled

Dec 23rd, 2020
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.85 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. push = require 'push'
  14.  
  15. require 'Ball'
  16. require 'Paddle'
  17.  
  18. function love.load()
  19. math.randomseed(os.time())
  20. love.graphics.setDefaultFilter('nearest','nearest')
  21.  
  22. paddle1 = Paddle(5, 20, 5, 20)
  23. paddle2 = Paddle(VIRTUAL_WIDTH - 10, VIRTUAL_HEIGHT - 30, 5, 20)
  24.  
  25. ballX = VIRTUAL_WIDTH/2-2
  26. ballY = VIRTUAL_HEIGHT/2-2
  27.  
  28. ballDX = math.random(2) == 1 and -100 or 100
  29. ballDY = math.random(-50,50)
  30.  
  31. gameState = 'start'
  32. smallFont = love.graphics.newFont('04B_30__.ttf',8)
  33. scoreFont = love.graphics.newFont('TitilliumText22L005-webfont.ttf',14)
  34.  
  35. love.graphics.setFont(smallFont)
  36.  
  37. push:setupScreen(VIRTUAL_WIDTH,VIRTUAL_HEIGHT, WINDOW_WIDTH, WINDOW_HEIGHT, {
  38. fullscreen = false,
  39. vsync = true,
  40. resizeable = false
  41. })
  42.  
  43. player1Score = 0
  44. player2Score = 0
  45. end
  46. function love.draw()
  47. push:apply('start')
  48. love.graphics.clear(40/255,45/255,52/255,255/255) -- map values to 0-1
  49. scoreFont = love.graphics.newFont('TitilliumText22L005-webfont.ttf',20)
  50. love.graphics.setFont(smallFont)
  51. if gameState == 'start' then
  52. love.graphics.printf('Hello Start State!', 0,20, VIRTUAL_WIDTH, 'center')
  53. elseif gameState == 'play' then
  54. love.graphics.printf('Hello Play State!', 0, 20, VIRTUAL_WIDTH, 'center')
  55. end
  56. love.graphics.setFont(scoreFont)
  57. love.graphics.print(player1Score, VIRTUAL_WIDTH/2 - 50, VIRTUAL_HEIGHT /3)
  58. love.graphics.print(player2Score, VIRTUAL_WIDTH/2+30, VIRTUAL_HEIGHT/3)
  59. love.graphics.rectangle('fill', ballX, ballY, 4, 4)
  60. paddle1:render()
  61. paddle2:render()
  62. --love.graphics.rectangle('fill', 10, player1Y, 5, 20) -- left side
  63. --love.graphics.rectangle('line', VIRTUAL_WIDTH - 10, player2Y, 5, 20) -- right side
  64. push:apply('end')
  65. end
  66.  
  67. function love.update(dt)
  68.  
  69. paddle1:update(dt)
  70. paddle2:update(dt)
  71.  
  72. -- player 1 movement
  73. if love.keyboard.isDown('w') then
  74. paddle1.dy = -PADDLE_SPEED
  75. elseif love.keyboard.isDown('s') then
  76. paddle1.dy = PADDLE_SPEED
  77. else
  78. paddle1.dy = 0
  79. end
  80. -- player 2 movement
  81. if love.keyboard.isDown('up') then
  82. paddle2.dy = -PADDLE_SPEED
  83. elseif love.keyboard.isDown('down') then
  84. paddle2.dy = PADDLE_SPEED
  85. else
  86. paddle2.dy = 0
  87. end
  88. if gameState == 'play' then
  89. ballX = ballX + ballDX * dt
  90. ballY = ballY + ballDY * dt
  91. end
  92. end
  93. function love.keypressed(key)
  94. if key == 'escape' then
  95. love.event.quit()
  96. elseif key == 'enter' or key == 'return' then
  97. if gameState == 'start' then
  98. gameState = 'play'
  99. elseif gameState == 'play' then
  100. gameState = 'start'
  101. ballX = VIRTUAL_WIDTH/2-2
  102. ballY = VIRTUAL_HEIGHT/2-2
  103. ballDX = math.random(2) == 1 and -100 or 100
  104. ballDY = math.random(-50,50)
  105. end
  106. end
  107. end
  108.  
  109. function Paddle:render()
  110. love.graphics.rectangle('fill', self.x, self.y, self.width, self.height)
  111. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement