Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- INSTALLATIONS
- -- https://cs50.harvard.edu/x/2021/seminars/#a-taste-of-game-development
- -- https://love2d.org/
- -- https://code.visualstudio.com/download
- -- https://github.com/Ulydev/push
- VIRTUAL_WIDTH = 384
- VIRTUAL_HEIGHT = 216
- WINDOW_WIDTH = 1280
- WINDOW_HEIGHT = 720
- PADDLE_WIDTH = 8
- PADDLE_HEIGHT = 32
- PADDLE_SPEED = 140
- BALL_SIZE = 4
- LARGE_FONT = love.graphics.newFont(32)
- SMALL_FONT = love.graphics.newFont(16)
- POINTS_WINNER = 2
- -- #include.....
- push = require 'push'
- gamestate = 'title'
- player1 = {
- x = 10, y = 10 , score = 0
- }
- player2 = {
- x = VIRTUAL_WIDTH - PADDLE_WIDTH - 10,
- y = VIRTUAL_HEIGHT - PADDLE_HEIGHT - 10,
- score = 0
- }
- ball = {
- x = (VIRTUAL_WIDTH / 2) - (BALL_SIZE/2),
- y = (VIRTUAL_HEIGHT / 2) - (BALL_SIZE/2),
- dx = 0, dy = 0
- }
- function love.load()
- math.randomseed(os.time())
- love.graphics.setDefaultFilter('nearest','nearest')
- push:setupScreen(VIRTUAL_WIDTH, VIRTUAL_HEIGHT, WINDOW_WIDTH, WINDOW_HEIGHT)
- resetBall()
- end
- function love.update(dt)
- if love.keyboard.isDown('w') then
- player1.y = player1.y - PADDLE_SPEED * dt
- elseif love.keyboard.isDown('s') then
- player1.y = player1.y + PADDLE_SPEED * dt
- end
- if love.keyboard.isDown('up') then
- player2.y = player2.y - PADDLE_SPEED * dt
- elseif love.keyboard.isDown('down') then
- player2.y = player2.y + PADDLE_SPEED * dt
- end
- if gamestate == 'play' then
- ball.x = ball.x + ball.dx * dt
- ball.y = ball.y + ball.dy * dt
- --1P mode
- if ball.dx > 0 then
- if (ball.y < player2.y) then
- player2.y = player2.y - PADDLE_SPEED * dt
- else
- player2.y = player2.y + PADDLE_SPEED * dt
- end
- end
- if ball.y <= 0 then
- ball.dy = -ball.dy
- elseif ball.y >= VIRTUAL_HEIGHT - BALL_SIZE then
- ball.dy = -ball.dy
- end
- if collides(ball, player1) then
- ball.x = player1.x + PADDLE_WIDTH
- ball.dx = -ball.dx
- elseif collides(ball, player2) then
- ball.x = player2.x - BALL_SIZE
- ball.dx = -ball.dx
- end
- if ball.x <= 0 then
- resetBall()
- gamestate = 'serve'
- player2.score = player2.score + 1
- if player2.score >= POINTS_WINNER then
- gamestate = 'win'
- end
- elseif ball.x >= VIRTUAL_WIDTH - BALL_SIZE then
- resetBall()
- gamestate = 'serve'
- player1.score = player1.score + 1
- if player1.score >= POINTS_WINNER then
- gamestate = 'win'
- end
- end
- end
- end
- function love.keypressed(key)
- if key == 'escape' then
- love.event.quit()
- end
- if key == 'enter' or key == 'return' then
- if gamestate == 'title' then
- gamestate = 'serve'
- elseif gamestate == 'serve' then
- gamestate = 'play'
- elseif gamestate == 'win' then
- player1.score = 0
- player2.score = 0
- gamestate = 'title'
- end
- end
- end
- function love.draw()
- push:start()
- --love.graphics.print('Hello, world!')
- --set RBG of background
- love.graphics.clear(40/255, 45/255, 52/255, 255/255)
- if gamestate == 'title' then
- love.graphics.setFont(LARGE_FONT)
- love.graphics.printf('Pre50 Pong', 0, 10, VIRTUAL_WIDTH, 'center')
- love.graphics.setFont(SMALL_FONT)
- love.graphics.printf("Press Enter to start the game!", 0, VIRTUAL_HEIGHT-32, VIRTUAL_WIDTH, 'center')
- elseif gamestate == 'serve' then
- love.graphics.setFont(SMALL_FONT)
- love.graphics.printf("Press Enter to Serve!", 0, 10, VIRTUAL_WIDTH, 'center')
- elseif gamestate == 'win' then
- love.graphics.setFont(LARGE_FONT)
- --local winner = player1.score >= 10 and '1' or '2'
- local winner
- if player1.score >= POINTS_WINNER then
- winner = '1'
- else
- winner = '2'
- end
- love.graphics.printf('Player ' .. winner .. ' wins!', 0, 10, VIRTUAL_WIDTH, 'center')
- love.graphics.setFont(SMALL_FONT)
- love.graphics.printf("Press Enter to restart the game!", 0, VIRTUAL_HEIGHT-32, VIRTUAL_WIDTH, 'center')
- end
- love.graphics.setFont(LARGE_FONT)
- love.graphics.print(player1.score, VIRTUAL_WIDTH / 2 - 32, VIRTUAL_HEIGHT / 2 - 16)
- love.graphics.print(player2.score, VIRTUAL_WIDTH / 2 + 16, VIRTUAL_HEIGHT / 2 - 16)
- love.graphics.setFont(SMALL_FONT)
- love.graphics.rectangle('fill',player1.x, player1.y, PADDLE_WIDTH, PADDLE_HEIGHT)
- love.graphics.rectangle('fill',player2.x, player2.y, PADDLE_WIDTH, PADDLE_HEIGHT)
- love.graphics.rectangle('fill',ball.x, ball.y, BALL_SIZE, BALL_SIZE)
- push:finish()
- end
- function collides(b, p)
- return not (b.y > p.y + PADDLE_HEIGHT or b.x > p.x + PADDLE_WIDTH or p.y > b.y + BALL_SIZE or p.x > b.x + BALL_SIZE)
- end
- function resetBall()
- ball.x = (VIRTUAL_WIDTH / 2) - (BALL_SIZE/2)
- ball.y = (VIRTUAL_HEIGHT / 2) - (BALL_SIZE/2)
- ball.dx = 60 + math.random(60)
- if math.random(2) == 1 then
- ball.dx = -ball.dx
- end
- ball.dy = 30 + math.random(60)
- if math.random(2) == 1 then
- ball.dy = -ball.dy
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement