Skip to content

Commit

Permalink
added examples for level 1.6
Browse files Browse the repository at this point in the history
  • Loading branch information
desk467 committed Oct 20, 2015
1 parent a5cbd72 commit 7b414be
Show file tree
Hide file tree
Showing 2 changed files with 166 additions and 0 deletions.
74 changes: 74 additions & 0 deletions world1/06_adding-some-action/creating bullets/main.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
player = {
x = 150,
y = 150,
xvel = 0,
yvel = 0,
rotation = 0
}

bullets = {}

-- Function to create bullets
function shot()

This comment has been minimized.

Copy link
@s-ol

s-ol Oct 21, 2015

Member

it's weird that we start introducing helper functions suddenly. At least we should have a solid reason to do so, here it's a single line; i think it should be inlined unless we start doing it this way from the beginning (which I wouldn't do for the prototype).

This comment has been minimized.

Copy link
@desk467

desk467 Oct 21, 2015

Author Contributor

i guess it is more easy to understand when we use helper functions, since we are separating the problem into easily identifiable pieces only by the function name. shot function is quite easily to inline. But we have another function, destroy_bullets, that i think it is more difficult to understand if we put this piece of code directly into love.update. IMHO

table.insert(bullets, {x = player.x, y = player.y, speed = 1000, rotation = player.rotation})
end

local ANGACCEL = 4
local ACCELERATION = 400

function love.update(dt)
if love.keyboard.isDown"right" then
-- rotate clockwise
player.rotation = player.rotation + ANGACCEL*dt
end
if love.keyboard.isDown"left" then
-- rotate counter-clockwise
player.rotation = player.rotation - ANGACCEL*dt
end
if love.keyboard.isDown"down" then
-- decelerate / accelerate backwards
player.xvel = player.xvel - ACCELERATION*dt * math.cos(player.rotation)
player.yvel = player.yvel - ACCELERATION*dt * math.sin(player.rotation)
end
if love.keyboard.isDown"up" then
-- accelerate
player.xvel = player.xvel + ACCELERATION*dt * math.cos(player.rotation)
player.yvel = player.yvel + ACCELERATION*dt * math.sin(player.rotation)
end

if love.keyboard.isDown" " then
shot()

This comment has been minimized.

Copy link
@s-ol

s-ol Oct 21, 2015

Member

I think we should be using the love.keypressed callback instead of the state query here. I am not sure whether you can hold down the fire button in the original asteroids? Even if you can we can't spawn a bullet every frame.

This comment has been minimized.

Copy link
@desk467

desk467 Oct 21, 2015

Author Contributor

i changed this here. Now i will rewrite Lesson 1.6 to teach about love.keypressed.

end

player.x = player.x + player.xvel*dt
player.y = player.y + player.yvel*dt

-- Update all bullets
for _, bullet in ipairs(bullets) do
bullet.x = bullet.x + math.cos(bullet.rotation) * bullet.speed * dt
bullet.y = bullet.y + math.sin(bullet.rotation) * bullet.speed * dt
end

-- for dragging
player.xvel = player.xvel * 0.99
player.yvel = player.yvel * 0.99

end

function love.draw()
-- Draw all bullets
for _, bullet in ipairs(bullets) do
love.graphics.setColor(255,255,255)
love.graphics.circle("fill", bullet.x, bullet.y, 2)
end

love.graphics.print("Qtde. de tiros: " .. #bullets, 10, 10)

This comment has been minimized.

Copy link
@s-ol

s-ol Oct 21, 2015

Member

this should be english of course, if present at all

This comment has been minimized.

Copy link
@desk467

desk467 Oct 21, 2015

Author Contributor

this line should not be there :'(


love.graphics.setColor(80, 80, 80)
love.graphics.translate(player.x, player.y)
love.graphics.rotate(player.rotation)
love.graphics.rectangle("fill", -50, -10, 100, 20)
love.graphics.setColor(200, 200, 200)
love.graphics.line(20, 0, 50, 0)

end
92 changes: 92 additions & 0 deletions world1/06_adding-some-action/destroying bullets/main.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
player = {
x = 150,
y = 150,
xvel = 0,
yvel = 0,
rotation = 0
}

bullets = {}
bullets.trash = {} -- auxiliary table to remove bullets from memory

-- Function to create bullets
function shot()
table.insert(bullets, {x = player.x, y = player.y, speed = 1000, rotation = player.rotation})
end

function destroy_bullets()
for i,v in ipairs(bullets.trash) do
table.remove(bullets, i)
end

bullets.trash = {}
end

local ANGACCEL = 4
local ACCELERATION = 400

function love.update(dt)
if love.keyboard.isDown"right" then
-- rotate clockwise
player.rotation = player.rotation + ANGACCEL*dt
end
if love.keyboard.isDown"left" then
-- rotate counter-clockwise
player.rotation = player.rotation - ANGACCEL*dt
end
if love.keyboard.isDown"down" then
-- decelerate / accelerate backwards
player.xvel = player.xvel - ACCELERATION*dt * math.cos(player.rotation)
player.yvel = player.yvel - ACCELERATION*dt * math.sin(player.rotation)
end
if love.keyboard.isDown"up" then
-- accelerate
player.xvel = player.xvel + ACCELERATION*dt * math.cos(player.rotation)
player.yvel = player.yvel + ACCELERATION*dt * math.sin(player.rotation)
end

if love.keyboard.isDown" " then
shot()
end

player.x = player.x + player.xvel*dt
player.y = player.y + player.yvel*dt

-- Update all bullets
for _, bullet in ipairs(bullets) do
bullet.x = bullet.x + math.cos(bullet.rotation) * bullet.speed * dt
bullet.y = bullet.y + math.sin(bullet.rotation) * bullet.speed * dt

-- if a bullet go outside from screen, then it will be destroyed
if bullet.x > love.graphics.getWidth() or bullet.x < 0 then
table.insert(bullets.trash, bullet)
end
if bullet.y > love.graphics.getHeight() or bullet.y < 0 then
table.insert(bullets.trash, bullet)
end
end

-- for dragging
player.xvel = player.xvel * 0.99
player.yvel = player.yvel * 0.99

-- verify if have some bullet on trash to destroy it
destroy_bullets()

end

function love.draw()
-- Draw all bullets
for _, bullet in ipairs(bullets) do
love.graphics.setColor(255,255,255)
love.graphics.circle("fill", bullet.x, bullet.y, 2)
end

love.graphics.setColor(80, 80, 80)
love.graphics.translate(player.x, player.y)
love.graphics.rotate(player.rotation)
love.graphics.rectangle("fill", -50, -10, 100, 20)
love.graphics.setColor(200, 200, 200)
love.graphics.line(20, 0, 50, 0)

end

0 comments on commit 7b414be

Please sign in to comment.