Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Class = require 'class'
- 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
- 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
- require 'Ball'
- WINDOW_WIDTH = 1280
- WINDOW_HEIGHT = 720
- VIRUTAL_WIDTH = 432
- VIRTUAL_HEIGHT = 243
- PADDLE_SPEED = 200
- push = require 'push'
- function love.load()
- math.randomseed(os.time())
- love.graphics.setDefaultFilter('nearest', 'nearest')
- smallFont= love.graphics.newFont('Retro Gaming.ttf', 8) -- creates a font object
- scoreFont = love.graphics.newFont('Retro Gaming.ttf', 12)
- fpsFont = love.graphics.newFont()
- player1Score = 0
- player2Score = 0
- love.graphics.setFont(smallFont)
- push:setupScreen(VIRUTAL_WIDTH, VIRTUAL_HEIGHT, WINDOW_WIDTH, WINDOW_HEIGHT, {
- fullscreen = false,
- vsync = true,
- resizeable = false
- })
- player1 = Paddle(5, 20, 5, 20) -- construct a new paddle... it will call the init method implicitly
- player2 = Paddle(VIRUTAL_WIDTH - 10, VIRTUAL_HEIGHT - 30, 5, 20)
- ball = Ball(VIRUTAL_WIDTH/2-2, VIRTUAL_HEIGHT/2-2, 5, 5)
- gameState = 'play' -- should gamestate be start or play? try both
- end
- function love.update(dt)
- if ball:collides(player1) then
- --deflect to the right
- ball.dx = -ball.dx -- only dx needs to be flipped
- end
- if ball:collides(player2) then
- --deflect to the left
- ball.dx = -ball.dx
- end
- if ball.y <= 0 then
- ball.dy = -ball.dy
- ball.y = 0
- end
- if ball.y >= VIRTUAL_HEIGHT - 4 then
- ball.dy = -ball.dy
- ball.y = VIRTUAL_HEIGHT
- end
- if love.keyboard.isDown('w') then
- player1.dy = -PADDLE_SPEED
- elseif love.keyboard.isDown('s') then
- player2.dy = PADDLE_SPEED
- else
- player1.dy = 0
- end
- if love.keyboard.isDown('up') then
- player2.dy = -PADDLE_SPEED
- elseif love.keyboard.isDown('down') then
- player2.dy = PADDLE_SPEED
- else
- player2.dy = 0
- end
- if gameState == 'play' then
- ball:update(dt)
- end
- player1:update(dt)
- player2:update(dt)
- end
- function love.keypressed(key)
- if key == 'escape' then
- love.event.quit()
- elseif key == 'enter' or key == 'return' then
- if gameState == 'start' then
- gameState = 'play'
- else
- gameState = 'start'
- ball:reset()
- end
- end
- end
- function love.draw()
- push:apply('start')
- love.graphics.clear(40/255, 45/255, 52/255, 255/255)
- love.graphics.setFont(smallFont)
- if gameState == 'start' then
- love.graphics.printf("Hello Start State!", 0, 20, VIRUTAL_WIDTH, 'center')
- elseif gameState == 'play' then
- love.graphics.printf('Hello Play State!', 0, 20 , VIRUTAL_WIDTH, 'center')
- end
- love.graphics.setFont(scoreFont)
- love.graphics.print(player1Score, VIRUTAL_WIDTH/2-50, VIRTUAL_HEIGHT/3)
- love.graphics.print(player2Score, VIRUTAL_WIDTH/2 + 30, VIRTUAL_HEIGHT/3)
- --love.graphics.rectangle('fill', ballX, ballY, 5, 5) -- ball
- player1:render()
- player2:render()
- ball:render()
- displayFPS()
- push:apply('end')
- end
- function displayFPS()
- love.graphics.setColor(0,255,0)
- love.graphics.setFont(fpsFont, 10)
- 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
- 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
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement