Created
August 14, 2016 18:50
-
-
Save luizcarlos1405/00ac8b1c2cadf229fe051c28c0e11eb6 to your computer and use it in GitHub Desktop.
Código finaln da aula 05. link: https://youtu.be/YTI9hrMboU0
This file contains hidden or 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.img = Bullet.img | |
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 | |
return bullet | |
end | |
function Bullet:update(dt) | |
for i, b in ipairs(Player.bullets) do | |
b.x = b.x + b.speed * dt | |
b.life = b.life - dt | |
if b.life <= 0 then | |
table.remove(Player.bullets, i) | |
end | |
end | |
end | |
function Bullet:draw() | |
for i, b in ipairs(Player.bullets) do | |
love.graphics.draw(b.img, b.x, b.y, b.r, b.sx, b.sy, b.ox, b.oy) | |
end | |
end |
This file contains hidden or 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 hidden or 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
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() | |
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 | |
love.graphics.print("Bullets: "..#Player.bullets) | |
end |
This file contains hidden or 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 = 300 | |
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 | |
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 | |
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