Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Class = require 'class'
- push = require 'push'
- require 'Paddle'
- 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()) -- we want to decide on a velocity for the ball. math.random() will help. we must first seed the random number generator and we need to give it a value to actually seed. we give it os.time()
- -- is tied to use of math.random()
- love.graphics.setDefaultFilter('nearest', 'nearest')
- love.window.setTitle('Pong')
- smallFont= love.graphics.newFont('Retro Gaming.ttf', 8) -- creates a font object
- scoreFont = love.graphics.newFont('Retro Gaming.ttf', 12)
- victoryFont = love.graphics.newFont('Retro Gaming.ttf', 30)
- fpsFont = love.graphics.newFont()
- player1Score = 0
- player2Score = 0
- servingPlayer = math.random() == 1 and 1 or 2 -- a coin flip that will return 1 or 2
- winningPlayer = 0
- love.graphics.setFont(smallFont)
- sounds = { -- a table with 3 audio objects
- ['paddle_hit'] = love.audio.newSource('Paddle_Hit.wav', 'static'),
- ['point_scored'] = love.audio.newSource('Point_scored.wav', 'static'),
- ['wall_hit'] = love.audio.newSource('Wall_Hit.wav', 'static'),
- }
- push:setupScreen(VIRUTAL_WIDTH, VIRTUAL_HEIGHT, WINDOW_WIDTH, WINDOW_HEIGHT, {
- fullscreen = false,
- resizeable = true,
- vsync = true
- -- resizeable must now be true in order for the window to be able to resize
- --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)
- if servingPlayer == 1 then
- ball.dx = 100
- else
- ball.dx = -100
- end
- gameState = 'start' -- should gamestate be start or play? try both
- end
- function love.resize(w, h)
- push:resize(w, h) -- you can pass in arguments to resize with arguments w, and h
- end
- function love.update(dt)
- if gameState == 'play' then
- if ball.x < 0 then
- player2Score = player2Score + 1
- servingPlayer = 1
- sounds['point_scored']:play()
- if player2Score >= 3 then
- gameState = 'victory'
- winningPlayer = 2 -- winning player isn't displayed
- else
- gameState = 'serve'
- ball:reset()
- end
- end
- if ball.x >= VIRUTAL_WIDTH - 4 then
- player1Score = player1Score + 1
- servingPlayer = 2
- sounds['point_scored']:play()
- if player1Score >= 3 then
- gameState = 'victory'
- winningPlayer = 1 -- winning player message isn't displayed
- else
- gameState = 'serve'
- ball:reset()
- end
- end
- if ball:collides(player1) then
- ball.dx = -ball.dx * 1.03
- ball.x = player1.x + 5
- sounds['paddle_hit']:play() -- colon is important because it's an object
- if ball.dy < 0 then
- ball.dy = -math.random(10, 150)
- else
- ball.dy = math.random(10, 150)
- end
- end
- if ball:collides(player2) then
- ball.dx = -ball.dx
- sounds['paddle_hit']:play()
- end
- if ball.y <= 0 then
- ball.dy = -ball.dy -- deflect the ball down
- ball.y = 0 -- we want to reset the ball into the playfield in the case that it hit the top of the screen
- sounds['wall_hit']:play()
- end
- if ball.y >= VIRTUAL_HEIGHT - 4 then
- ball.dy = -ball.dy
- ball.y = VIRTUAL_HEIGHT - 4 -- in the case that it hits the bottom of the screen
- sounds['wall_hit']:play()
- end
- if love.keyboard.isDown('w') then
- player1.dy = -PADDLE_SPEED
- elseif love.keyboard.isDown('s') then
- player1.dy = PADDLE_SPEED
- else
- player1.dy = 0
- end
- player2.y = ball.y -10000000000*dt*player2.dy -- i'm trying to make my opponent less impossible to beat
- if gameState == 'play' then
- ball:update(dt)
- --ballX = ballX + ballDX * dt
- --ballY = ballY + ballDY * dt
- end
- player1:update(dt)
- player2:update(dt)
- end
- 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'
- gameState = 'serve'
- elseif gameState == 'victory' then
- gameState = 'start'
- player2Score = 0
- player1Score = 0
- elseif gameState == 'serve' then
- gameState = 'play'
- end
- end
- end
- function love.draw()
- push:apply('start')
- love.graphics.clear(40/255, 45/255, 52/255, 255/255)
- love.graphics.setFont(smallFont)
- love.graphics.setFont(scoreFont)
- if gameState == 'start' then
- love.graphics.printf("Welcome to Pong!", 0, 20, VIRUTAL_WIDTH, 'center')
- love.graphics.printf("press enter to play", 0, 32, VIRUTAL_WIDTH, 'center')
- elseif gameState == 'serve' then
- --love.graphics.setFont(smallFont)
- love.graphics.printf("Player " .. tostring(servingPlayer) .. "'s serve!", 0, 20, VIRUTAL_WIDTH, 'center') -- neither will happen during play state
- love.graphics.printf("Press enter to serve", 0, 32, VIRUTAL_WIDTH, 'center')
- elseif gamestate == 'victory' then
- -- draw a victory message
- love.graphics.setFont(victoryFont)
- love.graphics.printf("Player " .. tostring(winningPlayer) .. " Wins!", 0, 10, VIRUTAL_WIDTH, 'center')
- love.graphics.printf("Press enter to serve", 0, 42, VIRUTAL_WIDTH, 'center')
- love.graphics.setFont(smallFont)
- elseif gameState == 'play' then
- -- no UI messages to display in play
- end
- 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
- -- resize application
- --[[
- love.resize(width, height)
- LÖVE will pass width and height to push -- this doesn't work... but worry about it later
- your assignment is to create a basic ai to play against the first player
- make it as easy or difficult as you wish
- make sure the tracking movement is smooth and not instantaneous
- ]]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement