Last active
August 4, 2018 14:41
-
-
Save emehrkay/eb29f10bba3af59d9004beed3c11ec23 to your computer and use it in GitHub Desktop.
simple love2d box with arrow controls
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
-- | |
-- classic | |
-- | |
-- Copyright (c) 2014, rxi | |
-- | |
-- This module is free software; you can redistribute it and/or modify it under | |
-- the terms of the MIT license. See LICENSE for details. | |
-- | |
local Object = {} | |
Object.__index = Object | |
function Object:new() | |
end | |
function Object:extend() | |
local cls = {} | |
for k, v in pairs(self) do | |
if k:find("__") == 1 then | |
cls[k] = v | |
end | |
end | |
cls.__index = cls | |
cls.super = self | |
setmetatable(cls, self) | |
return cls | |
end | |
function Object:implement(...) | |
for _, cls in pairs({...}) do | |
for k, v in pairs(cls) do | |
if self[k] == nil and type(v) == "function" then | |
self[k] = v | |
end | |
end | |
end | |
end | |
function Object:is(T) | |
local mt = getmetatable(self) | |
while mt do | |
if mt == T then | |
return true | |
end | |
mt = getmetatable(mt) | |
end | |
return false | |
end | |
function Object:__tostring() | |
return "Object" | |
end | |
function Object:__call(...) | |
local obj = setmetatable({}, self) | |
obj:new(...) | |
return obj | |
end | |
return Object |
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
Object = require "classic" | |
Dot = Object:extend() | |
function Dot:new() | |
self.width = 20 | |
self.height = 20 | |
self.x = math.random(0, love.graphics.getWidth()) | |
self.y = math.random(0, love.graphics.getHeight()) | |
self._position = 0 | |
end | |
function Dot:draw() | |
love.graphics.setColor(1, 0, 0) | |
love.graphics.rectangle("fill", self.x, self.y, self.width, self.height) | |
love.graphics.setColor(1, 1, 1) | |
end | |
function Dot:update(dt) | |
end | |
function Dot:coordinates() | |
local pos = {} | |
pos['top'] = self.y | |
pos['right'] = self.x + self.width | |
pos['bottom'] = self.y + self.height | |
pos['left'] = self.x | |
return pos | |
end |
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
Object = require "classic" | |
Score = Object:extend() | |
function Score:new(value) | |
self.value = value | |
end | |
function Score:draw() | |
love.graphics.print(self:score(), 50, 50) | |
end | |
function Score:update(value) | |
self.value = self.value + value | |
end | |
function Score:score() | |
return string.format('SCORE: %d', self.value) | |
end | |
Points = Object:extend() | |
function Points:new(value) | |
self.value = value | |
self.timer = 1 | |
end | |
function Points:draw() | |
love.graphics.print(self:score(), love.graphics.getWidth() - 300, | |
love.graphics.getHeight() - 100) | |
end | |
function Points:update(dt) | |
self.timer = self.timer - dt | |
if self.timer <= 0 then | |
self.value = self.value - 1 | |
local leftover = math.abs(self.timer) | |
self.timer = 1 - leftover | |
end | |
end | |
function Points:score() | |
return string.format('DOT VALUE: %d', self.value) | |
end | |
GameTime = Object:extend() | |
function GameTime:new(value) | |
self.value = value | |
self.timer = 1 | |
self.game_over = false | |
self.message = '' | |
end | |
function GameTime:draw() | |
love.graphics.print(self:time(), love.graphics.getWidth() - 300, 50) | |
x = (love.graphics.getWidth() - 150) / 2 | |
y = love.graphics.getHeight() / 2 | |
love.graphics.print(self.message, x, y) | |
end | |
function GameTime:update(dt) | |
self.timer = self.timer - dt | |
if self.timer <= 0 then | |
self.value = self.value - 1 | |
local leftover = math.abs(self.timer) | |
self.timer = 1 - leftover | |
end | |
if self.value <= 0 then | |
self.message = string.format('GAME OVER.\nPRESS s TO RESTART') | |
self.game_over = true | |
end | |
end | |
function GameTime:time() | |
return string.format('TIME: %d', self.value) | |
end | |
Position = Object:extend() | |
function Position:new(text) | |
self.text = text | |
end | |
function Position:draw() | |
x = 50 | |
y = love.graphics.getHeight() - 100 | |
love.graphics.print(self.text, x, y) | |
end | |
function Position:update(text) | |
self.text = text | |
end |
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
function love.load() | |
require 'player' | |
require 'dot' | |
require 'hud' | |
startGame() | |
end | |
function startGame() | |
player = Player() | |
position = string.format('x: %d y: %d', player.x, player.y) | |
pos = Position(position) | |
score = Score(0) | |
points = Points(10) | |
time = GameTime(10) | |
dots = {} | |
dot_count = 0 | |
dots_on_screen = 1 | |
end | |
function love.draw() | |
pos:draw() | |
player:draw() | |
score:draw() | |
points:draw() | |
time:draw() | |
for i, dot in pairs(dots) do | |
dot:draw() | |
end | |
end | |
function love.update(dt) | |
if time.game_over then | |
if love.keyboard.isDown("s") then | |
startGame() | |
end | |
return | |
end | |
player:update(dt) | |
points:update(dt) | |
time:update(dt) | |
pos:update(string.format('x: %d y: %d', player.x, player.y)) | |
checkCollisions() | |
shouldAddDot() | |
end | |
function shouldAddDot() | |
if dot_count < dots_on_screen then | |
addDot() | |
end | |
end | |
function addDot() | |
local dot = Dot() | |
table.insert(dots, dot) | |
local dot_pos = table.getn(dots) | |
dot._position = dot_pos | |
dot_count = dot_count + 1 | |
end | |
function removeDot(dot) | |
table.remove(dots, dot._position) | |
dot_count = dot_count - 1 | |
end | |
function checkCollisions() | |
if dot_count == 0 then | |
return | |
end | |
for k, dot in pairs(dots) do | |
if checkCollision(player, dot) then | |
removeDot(dot) | |
score:update(points.value) | |
points.value = 10 | |
end | |
end | |
end | |
function checkCollision(a, b) | |
local a_pos = a:coordinates() | |
local b_pos = b:coordinates() | |
return a_pos['right'] > b_pos['left'] | |
and a_pos['left'] < b_pos['right'] | |
and a_pos['bottom'] > b_pos['top'] | |
and a_pos['top'] < b_pos['bottom'] | |
end | |
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
Object = require "classic" | |
Player = Object:extend() | |
function Player:new() | |
self.width = 20 | |
self.height = 20 | |
self.x = (love.graphics.getWidth() - self.width) / 2 | |
self.y = (love.graphics.getHeight() - self.height) / 2 | |
self.speed = 250 | |
end | |
function Player:draw() | |
love.graphics.rectangle("fill", self.x, self.y, self.width, self.height) | |
end | |
function Player:update(dt) | |
if love.keyboard.isDown("left") then | |
self.x = self.x - self.speed * dt | |
end | |
if love.keyboard.isDown("right") then | |
self.x = self.x + self.speed * dt | |
end | |
if love.keyboard.isDown("up") then | |
self.y = self.y - self.speed * dt | |
end | |
if love.keyboard.isDown("down") then | |
self.y = self.y + self.speed * dt | |
end | |
end | |
function Player:coordinates() | |
local pos = {} | |
pos['top'] = self.y | |
pos['right'] = self.x + self.width | |
pos['bottom'] = self.y + self.height | |
pos['left'] = self.x | |
return pos | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How to run:
love .