Last active
November 19, 2018 15:59
-
-
Save DoctorGester/df434062e0909d5fbaeb6e4734e27713 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local aspect_ratio = 4 / 3 | |
local game_width = 1000 | |
local game_height = game_width / aspect_ratio | |
local bat_height = 200 | |
local bat_width = 30 | |
local bats = {} | |
local bat_lerp = 8 | |
local ball_dimension = 25 | |
local ball_speed = 0 | |
local ball = {} | |
local score_font_size = 100 | |
local initial_ball_x_direction = 1 | |
local game_time = 0 | |
local round_ended_at = 0 | |
local screen_transform = love.math.newTransform() | |
local function make_bat(x) | |
return { | |
x = x, | |
y = (game_height - bat_height) / 2, | |
score = 0 | |
} | |
end | |
local function update_screen_transform() | |
local screen_width, screen_height = love.graphics.getDimensions() | |
local scale = screen_height / game_height | |
local new_width = screen_height * aspect_ratio | |
local offset_x = (screen_width - new_width) / 2 | |
screen_transform:reset() | |
screen_transform:translate(offset_x, 0) | |
screen_transform:scale(scale, scale) | |
end | |
local function restart_round() | |
ball_speed = 200 | |
ball = { | |
x = (game_width - ball_dimension) / 2, | |
y = (game_height - ball_dimension) / 2, | |
vx = initial_ball_x_direction, | |
vy = 1 | |
} | |
initial_ball_x_direction = initial_ball_x_direction * -1 | |
round_ended_at = game_time | |
end | |
local function lerp(a, b, t) | |
return a + (b - a) * t | |
end | |
local function clamp(n, low, high) | |
return math.min(math.max(n, low), high) | |
end | |
local function rectangles_intersect(x1, y1, w1, h1, x2, y2, w2, h2) | |
return x1 < x2 + w2 and | |
x2 < x1 + w1 and | |
y1 < y2 + h2 and | |
y2 < y1 + h1 | |
end | |
local function move_bat_towards_y(bat, target_y, dt) | |
target_y = clamp(target_y, 0, game_height - bat_height) | |
bat.y = lerp(bat.y, target_y, bat_lerp * dt) | |
end | |
local function update_player_bat(bat, dt) | |
local _, mouse_y = screen_transform:inverseTransformPoint(0, love.mouse.getY()) | |
move_bat_towards_y(bat, mouse_y, dt) | |
end | |
local function update_ai_bat(bat, dt) | |
local target_y = ball.y + (math.abs(ball.x - bat.x)) * ball.vy - bat_height / 2 | |
move_bat_towards_y(bat, target_y, dt) | |
end | |
local function update_ball(ball, dt) | |
ball.x = ball.x + ball.vx * ball_speed * dt | |
ball.y = ball.y + ball.vy * ball_speed * dt | |
for _, bat in ipairs(bats) do | |
if rectangles_intersect(ball.x, ball.y, ball_dimension, ball_dimension, bat.x, bat.y, bat_width, bat_height) then | |
ball_speed = ball_speed + 50 | |
ball.vx = -ball.vx | |
ball.x = ball.x + ball.vx * ball_speed * dt | |
break | |
end | |
end | |
if ball.x < 0 then | |
bats[2].score = bats[2].score + 1 | |
restart_round() | |
return | |
end | |
if ball.x + ball_dimension > game_width then | |
bats[1].score = bats[1].score + 1 | |
restart_round() | |
return | |
end | |
if ball.y < 0 or ball.y + ball_dimension > game_height then | |
ball.vy = -ball.vy | |
ball.y = ball.y + ball.vy * ball_speed * dt | |
end | |
end | |
function love.update(dt) | |
update_screen_transform() | |
update_player_bat(bats[1], dt) | |
update_ai_bat(bats[2], dt) | |
if game_time - round_ended_at > 2.0 then | |
update_ball(ball, dt) | |
end | |
game_time = game_time + dt | |
end | |
function love.draw() | |
local flash = 1 - clamp(game_time - round_ended_at, 0, 1) | |
love.graphics.clear(flash, flash, flash) | |
love.graphics.push() | |
love.graphics.applyTransform(screen_transform) | |
love.graphics.setColor(1, 1, 1) | |
for _, bat in ipairs(bats) do | |
love.graphics.rectangle("fill", bat.x, bat.y, bat_width, bat_height) | |
end | |
love.graphics.rectangle("fill", ball.x, ball.y, ball_dimension, ball_dimension) | |
love.graphics.pop() | |
local score_string = string.format("%d : %d", bats[1].score, bats[2].score) | |
local score_width = love.graphics.getFont():getWidth(score_string) | |
love.graphics.print(score_string, (love.graphics.getWidth() - score_width) / 2, 0) | |
end | |
function love.resize() | |
local screen_height = love.graphics.getHeight() | |
love.graphics.setNewFont(math.ceil(screen_height / game_height * score_font_size)) | |
end | |
function love.load() | |
love.window.setMode(800, 600, { | |
resizable = true, | |
highdpi = true | |
}) | |
love.resize() | |
bats = { make_bat(0), make_bat(game_width - bat_width) } | |
restart_round() | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment