Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- WINDOW_WIDTH = 1280
- WINDOW_HEIGHT = 720
- VIRTUAL_WIDTH = 432
- VIRTUAL_HEIGHT = 243
- PADDLE_SPEED = 200
- Class = require 'class' -- from Paddle.lua... require the line Paddle = Class{}
- push = require 'push'
- require 'Ball'
- require 'Paddle'
- function love.load()
- math.randomseed(os.time())
- love.graphics.setDefaultFilter('nearest','nearest')
- 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
- player2 = Paddle(VIRTUAL_WIDTH - 10, VIRTUAL_HEIGHT - 30, 5, 20)
- ball = Ball(VIRTUAL_WIDTH/2 - 2, VIRTUAL_HEIGHT/2 - 2, 5, 5) -- instatiate the ball .... not balle!
- gameState = 'start'
- smallFont = love.graphics.newFont('04B_30__.ttf',8)
- scoreFont = love.graphics.newFont('TitilliumText22L005-webfont.ttf',14)
- love.graphics.setFont(smallFont)
- push:setupScreen(VIRTUAL_WIDTH,VIRTUAL_HEIGHT, WINDOW_WIDTH, WINDOW_HEIGHT, {
- fullscreen = false,
- vsync = true,
- resizeable = false
- })
- player1Score = 0
- player2Score = 0
- end
- function love.draw()
- push:apply('start')
- love.graphics.clear(40/255,45/255,52/255,255/255) -- map values to 0-1
- scoreFont = love.graphics.newFont('TitilliumText22L005-webfont.ttf',20)
- love.graphics.setFont(smallFont)
- if gameState == 'start' then
- love.graphics.printf('Hello Start State!', 0,20, VIRTUAL_WIDTH, 'center')
- elseif gameState == 'play' then
- love.graphics.printf('Hello Play State!', 0, 20, VIRTUAL_WIDTH, 'center')
- end
- love.graphics.setFont(scoreFont)
- love.graphics.print(player1Score, VIRTUAL_WIDTH/2 - 50, VIRTUAL_HEIGHT /3)
- love.graphics.print(player2Score, VIRTUAL_WIDTH/2+30, VIRTUAL_HEIGHT/3)
- ball:render()--love.graphics.rectangle('fill', ballX, ballY, 4, 4)
- player1:render() -- paddle1 render() is now a function stored in Paddle.lua
- player2:render()
- displayFPS() -- show framerate
- push:apply('end')
- end
- function love.update(dt)
- if ball:collides(player1) then -- collides() is stored in Ball.lua
- -- deflect the ball to the right
- -- change velocity from -100, to 100
- ball.dx = -ball.dx
- end
- if ball:collides(player2) then
- -- deflect ball to the left
- ball.dx = -ball.dx
- end
- if ball.y <= 0 then -- the top of the screen
- --deflect the ball down
- ball.dy = -ball.dy
- -- reset the ball in the case of it hitting the top of the screen
- ball.y = 0
- end
- if ball.y >= VIRTUAL_HEIGHT - 4 then
- ball.dy = -ball.dy
- ball.y = VIRTUAL_HEIGHT - 4
- end
- -- player 1 movement
- if love.keyboard.isDown('w') then
- player1.dy = -PADDLE_SPEED -- negative is up
- elseif love.keyboard.isDown('s') then
- player1.dy = PADDLE_SPEED -- replaces math.min(VIRTUAL_HEIGHT - 2-,PLAYER1Y + PADDLE_SPEED * dt)
- else
- player1.dy = 0 -- if neither is true, the paddle speed is 0
- end
- -- player 2 movement
- 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)
- ball:render()
- 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 displayFPS()
- love.graphics.setColor(0,1,0,1)
- love.graphics.setFont(smallFont)
- love.graphics.print('FPS: ' .. tostring(love.timer.getFPS()), 40, 20)
- love.graphics.setColor(1, 1, 1, 1)
- end
- --collision detection
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement