function init()
sprites = loadimage('sprites.bmp')
star_scroll = 0
star_screens = 15
max_star_scroll = (star_screens - 1) * screen.w
stars = newimage(screen.w * star_screens, screen.h)
fill(stars, nil, black)
for i = 1, 100 * star_screens do -- 100 stars/screen
local coord = {math.random(0, stars.w), math.random(0, stars.h)}
stars[coord] = math.random(dgray, lgray)
end
-- copy the first "screen" to the last so that we can loop easily
copy(stars, {0,0,screen.w,screen.h}, stars, {screen.w * (star_screens - 1), 0})
player = {
loc = {3, 14},
draw = drawsprite,
sprite = {image = sprites, rect = {0, 0, 23, 10}},
update = function(self)
self.shot_time = self.shot_time + 1
simplemove(self)
end,
vel = {0, 0},
shot_time = 0,
}
bullets = {}
play_keys = {
down = {
[keys.up] = moveup,
[keys.down] = movedown,
[keys.z] = fire,
[keys.s] = screenshot,
[keys.p] = pause,
},
up = {
[keys.down] = stopmoving,
[keys.up] = stopmoving,
}
}
pause_keys = {
down = { [keys.p] = unpause }
}
unpause()
end
function moveup() player.vel[2] = -1 end
function movedown() player.vel[2] = 1 end
function stopmoving() player.vel[2] = 0 end
function fire()
if player.shot_time > 2 then
local b = {}
b.loc = {player.loc[1] + 19, player.loc[2] + 4, 4, 1}
b.draw = drawbullet
b.color = white
b.update = simplemove
b.vel = {3, 0}
table.insert(bullets, b)
player.shot_time = 0
end
end
function pause()
draw = draw_paused
keymap = pause_keys
paused_frame = 0
show_pause_text = true
end
function unpause()
draw = draw_play
keymap = play_keys
end
function do_stars()
if star_scroll < max_star_scroll then
star_scroll = star_scroll + 1
else
star_scroll = 1
end
copy(stars, {star_scroll,0, screen.w,screen.h}, screen, nil)
end
function do_bullets()
for i,bullet in pairs(bullets) do
bullet:update()
if bullet.loc[1] > screen.w then
bullets[i] = nil
else
bullet:draw()
end
end
end
function draw_play()
player:update()
do_stars()
player:draw()
do_bullets()
end
function draw_paused()
paused_frame = paused_frame + 1
screen[{23, 24, 50, 8}] = white
if paused_frame % 5 == 0 then
show_pause_text = not show_pause_text
end
if show_pause_text then imageprint(screen, {32,26}, "paused!!") end
end
function drawsprite(self)
copy(self.sprite.image, self.sprite.rect, screen, self.loc)
end
function simplemove(obj)
obj.loc[1] = obj.loc[1] + obj.vel[1]
obj.loc[2] = obj.loc[2] + obj.vel[2]
end
function drawbullet(self)
screen[self.loc] = white
end