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
- --[[
- define a class and specify objects of that class
- we will tie our paddles together with classes and objects
- ]]
- Class = require 'class' -- from Paddle.lua... require the line Paddle = Class{}
- -- in order to get it to work, you need to import the library in main.lua with class = require 'class'
- push = require 'push'
- 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')
- --[[
- player1Y = 30
- player2Y = VIRTUAL_HEIGHT - 50
- get replaced with paddle objects called paddle1 and paddle2
- ]]
- 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!
- --[[
- these lines are no longer needed
- ballX = VIRTUAL_WIDTH/2-2
- ballY = VIRTUAL_HEIGHT/2-2
- ballDX = math.random(2) == 1 and -100 or 100
- ballDY = math.random(-50,50)
- ]]
- 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()
- --love.graphics.rectangle('fill', 10, player1Y, 5, 20) -- left side
- --love.graphics.rectangle('line', VIRTUAL_WIDTH - 10, player2Y, 5, 20) -- right side
- 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
- --paddle1: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
- --paddle2:update(dt)
- --ball:render()
- -- 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()
- elseif key == 'enter' or key == 'return' then
- if gameState == 'start' then
- gameState = 'play'
- --elseif gameState == 'play' then
- else
- gameState = 'start'
- --[[
- will be copied into ball:reset()
- ballX = VIRTUAL_WIDTH/2-2
- ballY = VIRTUAL_HEIGHT/2-2
- ballDX = math.random(2) == 1 and -100 or 100
- ballDY = math.random(-50,50)
- ]]
- 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
- --[[
- object oriented programming
- Classes
- structs are similar to arguments in that they compartmentalize data in sematically meaningful way
- with classes you get data and also functions that are tied to a group of data
- you might have a class called car
- it will have attributes such as
- fuel
- brand
- color
- miles
- and anything else descriptive known as "fields"
- or maxspeed
- and also methods such as
- refuel() getFuel, setSpeed() getSpeed() drive accellerate(), turn() honk() which will tkae the form of functions
- objects are instatiated from class blueprints
- you can use the class as a blueprint to make objects of different class types.
- class is a blueprint, the object is what you use in the program
- the paddles and ball are good uses for objects
- love.window.setTitle(title)
- Simply sets the title of the application window, adding a slight level of polish
- love.timer.getFPS()
- returns the current FPS of our application, making it easy to monitor when printed
- ]]
- --collision detection
- --[[
- AABB Colision detection
- relies on all colliding etitites to have "axis-aligned bounding boxes", which simply means
- their collision boxes contain no rotation in our world space, which allows us
- to use a simple math formula to test for collision:
- if rect1.x is not > rect2.x + rect2.wdth and rect1.x + rect1.width is not < rect2.x and rect1.y is not > rect2.y + rec2.height and rect1.y + rect1.height is not < rect2.y:
- collision is true
- else
- collision is false
- checks if the top and sides of the bounding boxes are greater than their opposite side
- if they are... there is no collision
- ]]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement