Created
April 13, 2010 03:40
-
-
Save andrewle/364287 to your computer and use it in GitHub Desktop.
Simple spaceship scroller game demo in Lua with the Love game engine
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() | |
config = { | |
initial_x = 200, | |
initial_y = 400, | |
step = 500, | |
numStars = 100, | |
starSpeedFactor = 1 | |
} | |
spaceship = { | |
image = love.graphics.newImage("images/love-ball.png"), | |
x = config.initial_x, | |
y = config.initial_y, | |
isBoosted = false, | |
boostFactor = 4, | |
boostDuration = 2, | |
timeBoosted = 0 | |
} | |
star = love.graphics.newImage("images/star.png") | |
stars = {} | |
love.graphics.setColorMode("modulate") | |
scrollerHeight = 30 | |
letterSize = 21 | |
angle = {} | |
x = {} | |
initStars() | |
end | |
function love.update(dt) | |
if spaceship.isBoosted then | |
spaceship.timeBoosted = spaceship.timeBoosted + dt | |
if spaceship.timeBoosted >= spaceship.boostDuration then | |
spaceship.isBoosted = false | |
config.starSpeedFactor = 1 | |
end | |
end | |
if love.keyboard.isDown("left") and (spaceship.x - config.step * dt) >= 0 then | |
spaceship.x = spaceship.x - config.step * dt | |
end | |
if love.keyboard.isDown("right") and (spaceship.x + config.step * dt) < 740 then | |
spaceship.x = spaceship.x + config.step * dt | |
end | |
if love.keyboard.isDown("up") and (spaceship.y - config.step * dt) >= 0 then | |
spaceship.y = spaceship.y - config.step * dt | |
end | |
if love.keyboard.isDown("down") and (spaceship.y + config.step * dt) < 540 then | |
spaceship.y = spaceship.y + config.step * dt | |
end | |
for i = 1,config.numStars do | |
stars[i].x = stars[i].x - stars[i].speed * config.starSpeedFactor * dt | |
if stars[i].x < -20 then | |
stars[i].x = math.random(830, 900) | |
stars[i].y = math.random(1,600) | |
end | |
angle[i] = angle[i] + math.pi/1.5 * dt; | |
x[i] = x[i] - letterSize*3*dt | |
if x[config.numStars-1] < -letterSize*2 then | |
initStars() | |
end | |
end | |
end | |
function love.keypressed(k) | |
if k == ' ' and not spaceship.isBoosted then | |
config.starSpeedFactor = spaceship.boostFactor | |
spaceship.isBoosted = true | |
spaceship.timeBoosted = 0 | |
end | |
end | |
function love.draw() | |
for i = 1,config.numStars do | |
love.graphics.setColor(255 - stars[i].speed,255 - stars[i].speed/2,150,stars[i].speed*0.9); | |
love.graphics.draw(star, stars[i].x, stars[i].y, 0, stars[i].speed/255 + 0.55) | |
end | |
love.graphics.setColor(255, 255, 255) | |
love.graphics.draw(spaceship.image, spaceship.x, spaceship.y) | |
love.graphics.print("spaceship.timeBoosted: " .. spaceship.timeBoosted, 10, 20) | |
end | |
function initStars() | |
local w = 800 | |
for i = 1,config.numStars do | |
table.insert(stars, {x = math.random(1,800), y = math.random(1,600), speed = math.random(2,255)}) | |
angle[i] = i*6 | |
x[i] = w+i*letterSize | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment