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{}
- require 'Ball' -- require ball and paddle will have access to the class variable because they are declared first
- 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('TitilliumText22L005-webfont.ttf',10)
- scoreFont = love.graphics.newFont('04B_30__.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
- servingPlayer = math.random(2) == 1 and 1 or 2 -- a coin flip between 1 and 2 which 1 is true and which is false
- winningPlayer = 0
- if servingPlayer == 1 then
- ball.dx = 100
- else
- ball.dx = -100
- end
- 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('lsansd.ttf',20)
- love.graphics.setFont(smallFont)
- displayScore()
- if gamestate == 'start' then
- love.graphics.printf("Welcome to Pong!", 0, 20, VIRTUAL_WIDTH, 'center') -- it starts at the left edge, and the column size is virualwidth with center text
- love.graphics.printf("Press Enter to Play!", 0, 32, VIRTUAL_WIDTH, 'center')
- elseif gameState == 'serve' then
- love.graphics.printf("Player" .. tostring(servingPlayer) .. "'s turn!", 0, 30, VIRTUAL_WIDTH, 'center') -- always says serving player 2... why?
- love.graphics.printf("Press Enter to Serve", 0, 20, VIRTUAL_WIDTH, 'center')
- elseif gameState == 'victory' then
- love.graphics.setFont(victoryFont)
- love.graphics.printf("Player" .. tostring(winningPlayer) .. " wins!", 0, 30, VIRTUAL_WIDTH, 'center') -- always says serving player 2... why?
- love.graphics.printf('', 0, 42, VIRTUAL_WIDTH, 'center')
- elseif gameState == 'play' then
- end
- 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 -- the ball is 4 pixels tall so offset collision by 4 pixels
- ball.dy = -ball.dy
- ball.y = VIRTUAL_HEIGHT - 4
- end
- if ball.x < 0 then
- player2Score = player2Score + 1
- serveringPlayer = 1 -- serving player isn't displayed correctly ... ask reddit!
- ball:reset() -- randomizes the balls dx. we want to set that based on who scored
- if player2Score >= 10 then
- gameState = 'victory'
- winningPlayer = 2
- else
- gameState = 'serve'
- end
- -- if player2's score got incremented... player1 should serve
- -- ball.dx = 100 -- it will go towards player 2 put it back maybe...
- --gameState = 'serve'
- end
- if ball.x > VIRTUAL_WIDTH then
- player1Score = player1Score + 1 -- serving player not working... always says serving player 2
- servingPlayer = 2
- ball:reset()
- --ball.dx = -100
- --gameState = 'serve'
- if player1Score >= 3 then
- gameState = 'victory'
- winningPlayer = 1
- else
- gameState = 'serve'
- 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)
- --ballX = ballX + ballDX * dt these will instead be done with update
- --ballY = ballY + ballDY * dt
- end
- player1:update(dt) -- replaces player1Y = math.min(VIRTUAL_HEIGHT - 20, player1Y + PADDLE_SPEED * dt)... now stored in the paddle class and replace player1Y with self.y
- player2:update(dt)
- ball:render()
- end
- function love.keypressed(key)
- if key == 'escape' then
- love.event.quit()
- --end
- elseif key == 'enter' or key == 'return' then
- if gameState == 'start' then
- gameState = 'serve'
- elseif gameState == 'victory' then
- gameState == 'start'
- elseif gameState == 'serve' then
- gameState = 'play'
- --ball:reset()
- end
- end
- end
- function displayFPS()
- love.graphics.setColor(0,1,0,1) -- toggles color for drawing text... default is white.... RGBT values... could also be 255
- love.graphics.setFont(smallFont)
- love.graphics.print('FPS: ' .. tostring(love.timer.getFPS()), 40, 20) -- .. is the string concatination operator
- -- tostring() will give an integer values which will be incompatible with string
- love.graphics.setColor(1, 1, 1, 1)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement