Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- pong3
- WINDOW_WIDTH = 1280
- WINDOW_HEIGHT = 720
- --[[ we want to use the push library to simulate that we are coding on a smaller raster size.
- nes was 240 p
- ]]
- VIRTUAL_WIDTH = 432
- VIRTUAL_HEIGHT = 243
- PADDLE_SPEED = 200 -- pixels per second
- -- not technically constants... the fact that they are capitalized is conventional notation to programmers that the values should not be changed
- --[[
- runs when the game first starts up, only once; used to initialize
- a similar resolution to the nes. still modern in appearance, but has the nes look to it
- ]]
- --import the push library
- push = require 'push'
- -- it will automatically know that you're looking for something.lua, or push.lua by default
- function love.load()
- math.randomseed(os.time()) -- seed random number generator... we need a number to actual seed it... a starting value for which to base the random number generation with os.time()
- -- looks blurry because it's applying filtering to a font. ... in order to fix this, use the setDefaultFilter() method
- love.graphics.setDefaultFilter('nearest','nearest')
- player1Y = 30
- player2Y = 40
- ballX = VIRTUAL_WIDTH/2-2
- ballY = VIRTUAL_HEIGHT/2-2
- ballDX = math.random(2) == 1 and -100 or 100 -- set the ball to -100, or 100, based effectively on a coin flip... a random value between 1 and 2
- ballDY = math.random(-50,50) -- give me a random value between -50, and 50...
- gameState = 'start'
- -- by combining a different random intial x and y change, we can set random velocities in both directions
- --[[
- if it equals 1 it's true and we will get -100. and if it's false we'll get 100
- and will take whatever the first false value it
- can find and if not the second value.
- if it finds a false value it will calculate the or
- or operates in such that it takes the first true value it finds.
- ]]
- smallFont = love.graphics.newFont('04B_30__.ttf',8) -- use the font named 04B_30__.ttf in 8 pixels, newfont function creates a font object
- scoreFont = love.graphics.newFont('TitilliumText22L005-webfont.ttf',14)
- love.graphics.setFont(smallFont) -- set the font object to use it
- push:setupScreen(VIRTUAL_WIDTH,VIRTUAL_HEIGHT, WINDOW_WIDTH, WINDOW_HEIGHT, {
- fullscreen = false,
- vsync = true,
- resizeable = false
- })
- player1Score = 0
- player2Score = 0
- --player1Y = 30
- --player2Y = 40
- --[[
- similar to love.window.setmode() but take 4 arguments instead
- vwidth and height for virtual raster to be, and windowwidth and window height in terms of how big we want our physical window dimensions to be
- ]]
- end
- -- end keyword is needed to tell lua that this is the end of the function definition
- --[[
- push is an object calling a method inside of itself.
- kind of like a function contained within that object that has data specific to that object as opposed to just a global object that belongs to a table
- push operates with the window in a different way than the default love.window.setmode()
- love.window.setMode(WINDOW_WIDTH, WINDOW_HEIGHT, {
- fullscreen = false,
- vsync = true,
- resizable = false
- })
- ]]
- -- how big do we want the screen to be
- -- end keyword is needed to tell lua that this is the end of the function definition
- --[[
- in order to get push to actually work, we've set it up, but it's not doing anything yet
- we must first call push:apply()
- ]]
- 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)
- love.graphics.rectangle('fill', ballX, ballY, 4, 4)
- --love.graphics.rectangle('fill', VIRTUAL_WIDTH/2 -2, VIRTUAL_WIDTH/2-2, 3.5, 3.5) -- center ball
- --[[
- game state is whether the game has beginning waiting for user input, playing the game and a
- boundary has passed, so change the score, maybe the ending score was reached and the game state is over
- math.randomseed(num)
- "seeds" the random number generator used by Lua(math.random) with some value such that
- its randomness is dependent on that supplied value, allowing us to pass in different numbers each playthrough
- to guarantee non-consistency across different program executions (or uniformity if we want consisten
- behavior for testing)
- os.time()
- Lua function that returns, in seconds, the time since 00:00:00 jan1, 1970, also
- known as Unix epoch time
- math.random(min, max)
- returns a random number dependent on the seeded random number generator,
- between min and max inclusive
- math.min(num1, num2)
- returns the lesser of the two numbers passed in
- math.max(num1, num2)
- returns the greater of the two numbers passed in
- ]]
- love.graphics.rectangle('fill', 10, player1Y, 5, 20) -- left side
- love.graphics.rectangle('line', VIRTUAL_WIDTH - 10, player2Y, 5, 20) -- right side
- -- ball
- --love.graphics.rectangle('fill', VIRTUAL_WIDTH/2 -2, VIRTUAL_HEIGHT/2-2, 4, 4) -- ball.. where it's centered, and it's size in pixels
- -- paddle left
- --love.graphics.rectangle('line', VIRTUAL_WIDTH - 10, VIRTUAL_HEIGHT - 50, 5, 20) -- paddle 2 on the left
- -- paddle right
- --love.graphics.rectangle('fill', 5, 20, 5, 20)-- 5 pixels from the left, 20 pixels from top, 5 pixels wide by 20 pixels tall
- --love.graphics.printf("Hello Pong!", 0, 20, VIRTUAL_WIDTH, 'center')-- draw the word 20 pixels down
- --love.graphics.printf("Hello Pong!", 0, VIRTUAL_HEIGHT/2-6, VIRTUAL_WIDTH, 'center')-- window is not that size anymore. will be drawn at 128-x720, but will be a raster of 432 x 243
- -- end rendering at virtual resolution
- push:apply('end')
- end
- --[[
- update function
- any time you want to change the position or configuration depending on user input
- you want to override love update(dt)
- ]]
- function love.update(dt)
- --[[
- dt allows you to move things independant of the frame rate. if you know hardware is going
- to run at a framerate, you can calculate everything on a per frame basis
- and not worry about time passed between frames. on different systems this can differ
- to normalize this multiply all the calculations that you want to have happen by the
- amount of time that has passed since the last frame dt
- ]]
- --player1 movement
- if love.keyboard.isDown('w') then
- player1Y = math.max(0, player1Y + -PADDLE_SPEED * dt) --math.max returns the greater of the two values
- --[[ clamp the y value to be no less than 0
- add negative paddle speed to current Y scaled by deltaTime
- player1Y = player1Y - PADDLE_SPEED * dt
- remember that higher numbers are down... dt is delta time
- if player1Y > 243 then
- player1Y = 243... does not work
- ]]
- elseif love.keyboard.isDown('s') then
- player1Y = math.min(VIRTUAL_HEIGHT - 20, player1Y + PADDLE_SPEED * dt) -- math.min returns the lesser of the two values
- -- y value should be no greaeter than the bottom of the screen minus 20 pixels... the size of the paddle
- --[[
- clamp the y value to no more than VIRTUAL_HEIGHT - 20... the height of the screen minus the height of the paddle
- remember that positive is down and negative is up... so set the minmum value to
- ]]
- end
- -- player 2 movement
- if love.keyboard.isDown('up') then
- player2Y = math.max(0, player2Y + -PADDLE_SPEED * dt) --down is positive ... add negative paddle speed to the current Y scaled by dletatime
- elseif love.keyboard.isDown('down') then
- player2Y = math.min(VIRTUAL_HEIGHT - 20, player2Y + PADDLE_SPEED * dt) -- up is negative... add positive padd
- end
- if gameState == 'play' then
- ballX = ballX + ballDX * dt
- ballY = ballY + ballDY * dt -- you may be adding a positive or negative number
- end
- end
- function love.keypressed(key)
- -- keyboard handling called by love each frame. passes in the key so we can access it
- if key == 'escape' then -- can be accessed by string name
- love.event.quit() --quit() is a function love gives us to terminate
- elseif key == 'enter' or key == 'return' then
- if gameState == 'start' then
- gameState = 'play'
- elseif gameState == 'play' then
- gameState = 'start'
- ballX = VIRTUAL_WIDTH/2-2
- ballY = VIRTUAL_HEIGHT/2-2
- ballDX = math.random(2) == 1 and -100 or 100 -- set the ball to -100, or 100, based effectively on a coin flip... a random value between 1 and 2
- ballDY = math.random(-50,50)
- -- repeating this code from above will ensure that the ball will be reset eachtime enter is pressed
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement