Created
August 22, 2016 21:10
-
-
Save luizcarlos1405/4e00f8a91ae3d31481cfaba66adf3fc5 to your computer and use it in GitHub Desktop.
LÖVE 06 - VIdeoaula no link: https://youtu.be/pPiAtW-iDJ4
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
Bullet = {} | |
Bullet.img = love.graphics.newImage("bullet.png") | |
function Bullet:create() | |
local bullet = {} | |
bullet.w = Bullet.img:getWidth() | |
bullet.h = Bullet.img:getHeight() | |
bullet.x = Player.x | |
bullet.y = Player.y | |
bullet.ox = bullet.w / 2 | |
bullet.oy = bullet.h / 2 | |
bullet.sx = 1 | |
bullet.sy = 1 | |
bullet.r = 0 | |
bullet.speed = 1000 | |
bullet.life = 3 | |
-- Calcula direção da bala | |
bullet.dir = {x = love.mouse.getX() - bullet.x, y = love.mouse.getY() - bullet.y} | |
local norma = (bullet.dir.x ^ 2 + bullet.dir.y ^ 2) ^ 0.5 | |
bullet.dir.x = bullet.dir.x / norma | |
bullet.dir.y = bullet.dir.y / norma | |
return bullet | |
end | |
function Bullet:update(dt) | |
for i, b in ipairs(Player.bullets) do | |
-- Atualiza posição da bala | |
b.x = b.x + (b.speed * b.dir.x * dt) | |
b.y = b.y + (b.speed * b.dir.y * dt) | |
-- Destroi balas que existem há tempo de mais | |
b.life = b.life - dt | |
if b.life <= 0 then | |
table.remove(Player.bullets, i) | |
end | |
end | |
end | |
function Bullet:draw() | |
-- Desenha todas as balas | |
for i, b in ipairs(Player.bullets) do | |
love.graphics.draw(Bullet.img, b.x, b.y, b.r, b.sx, b.sy, b.ox, b.oy) | |
end | |
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.conf(t) | |
t.version = "0.10.1" | |
t.window.title = "Palco:dev()" | |
t.window.width = 1000 | |
t.window.height = 600 | |
t.window.borderless = false | |
t.window.fullscreen = false | |
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
HC = require("HC") | |
require("player") | |
require("bullet") | |
Width = love.graphics.getWidth() | |
Height = love.graphics.getHeight() | |
function love.load() | |
-- Define cor de fundo | |
love.graphics.setBackgroundColor(5, 70, 110) | |
-- Carrega Player | |
Player:load() | |
-- Para exibir variáveis de controles | |
debugMode = true | |
-- retangulos para teste | |
rects = {HC.rectangle(200, 200, 36, 36), HC.rectangle(400, 400, 36, 36), HC.rectangle(200, 400, 36, 36), HC.rectangle(400, 200, 36, 36)} | |
end | |
function love.update(dt) | |
-- Atualiza Player | |
Player:update(dt) | |
-- Atualiza bullets | |
Bullet:update(dt) | |
end | |
function love.draw() | |
-- Desenha bullets | |
Bullet:draw() | |
-- Desenha Player | |
Player:draw() | |
-- Variáveis de controles | |
if debugMode then | |
love.graphics.print("Bullets: "..#Player.bullets.."\nColliding: "..iscoll) | |
Player.body:draw() | |
end | |
-- desenha retangulos para teste | |
for i, r in ipairs(rects) do | |
r:draw() | |
end | |
end | |
function love.keypressed(key, scancode, isrepeat) | |
if key == "escape" then | |
love.event.quit("Apertou esc") | |
elseif key == "f2" then | |
if debugMode then | |
debugMode = false | |
else | |
debugMode = true | |
end | |
end | |
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
Player = {} | |
function Player:load() | |
-- Define "objeto" Player | |
self.img = love.graphics.newImage("player.png") | |
self.w = self.img:getWidth() | |
self.h = self.img:getHeight() | |
self.x = Width / 2 | |
self.y = Height / 2 | |
self.ox = self.w / 2 | |
self.oy = self.h / 2 | |
self.sx = 1 | |
self.sy = 1 | |
self.r = 0 | |
self.speed = 500 | |
self.xvel = self.speed * math.cos(math.pi / 4) | |
self.yvel = self.speed * math.sin(math.pi / 4) | |
self.bullets = {} | |
self.cd = 0.2 | |
self.canfire = 0 | |
self.body = HC.circle(self.x, self.y, self.w / 2) | |
self.iscoll = "NO" | |
end | |
function Player:update(dt) | |
-- Recebe e computa controles | |
if love.keyboard.isDown("w") and love.keyboard.isDown("a") then | |
self.y = self.y - self.yvel * dt | |
self.x = self.x - self.xvel * dt | |
elseif love.keyboard.isDown("a") and love.keyboard.isDown("s") then | |
self.y = self.y + self.yvel * dt | |
self.x = self.x - self.xvel * dt | |
elseif love.keyboard.isDown("s") and love.keyboard.isDown("d") then | |
self.y = self.y + self.yvel * dt | |
self.x = self.x + self.xvel * dt | |
elseif love.keyboard.isDown("d") and love.keyboard.isDown("w") then | |
self.y = self.y - self.yvel * dt | |
self.x = self.x + self.xvel * dt | |
elseif love.keyboard.isDown("w") then | |
self.y = self.y - self.speed * dt | |
elseif love.keyboard.isDown("a") then | |
self.x = self.x - self.speed * dt | |
elseif love.keyboard.isDown("s") then | |
self.y = self.y + self.speed * dt | |
elseif love.keyboard.isDown("d") then | |
self.x = self.x + self.speed * dt | |
end | |
-- Impede que o Player saia da tela | |
if self.y < self.h / 2 then | |
self.y = self.h / 2 | |
end | |
if self.x < self.w / 2 then | |
self.x = self.w / 2 | |
end | |
if self.y > Height - self.h / 2 then | |
self.y = Height - self.h / 2 | |
end | |
if self.x > Width - self.w / 2 then | |
self.x = Width - self.w / 2 | |
end | |
-- Atira caso clique com o botão esquerdo do mouse e esteja no cooldown | |
self.canfire = self.canfire - dt | |
if love.mouse.isDown(1) and self.canfire <= 0 then | |
self.canfire = self.cd | |
self:fire() | |
end | |
-- Move corpo para o Player | |
self.body:moveTo(self.x, self.y) | |
-- Detecta colisões do Player | |
iscoll = "NO" | |
for shape, delta in pairs(HC.collisions(self.body)) do | |
iscoll = "YES" | |
self.x = self.x + delta.x | |
self.y = self.y + delta.y | |
end | |
end | |
function Player:draw() | |
love.graphics.draw(self.img, self.x, self.y, self.r, self.sx, self.sy, self.ox, self.oy) | |
end | |
function Player:fire() | |
table.insert(self.bullets, Bullet:create()) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment