primer commit
This commit is contained in:
228
space/space.lua
Normal file
228
space/space.lua
Normal file
@@ -0,0 +1,228 @@
|
||||
function init_player()
|
||||
setchar(244, 0x00, 0x00, 0x00, 0x00, 0x18, 0x7E, 0xFF, 0xFF)
|
||||
player = {
|
||||
x = 10,
|
||||
y = 14
|
||||
}
|
||||
end
|
||||
|
||||
function init_bullet()
|
||||
setchar(145, 0x08, 0x10, 0x08, 0x10, 0x08, 0x10, 0x08, 0x10)
|
||||
bullet = {
|
||||
x = 0,
|
||||
y = 0,
|
||||
enabled = false
|
||||
}
|
||||
end
|
||||
|
||||
function init_explosion()
|
||||
setchar(146, 0x10, 0x54, 0x28, 0xC6, 0x28, 0x54, 0x10, 0x00)
|
||||
explosion = {
|
||||
x = 0,
|
||||
y = 0,
|
||||
enabled = false,
|
||||
counter = 0
|
||||
}
|
||||
end
|
||||
|
||||
function init_enemies()
|
||||
setchar(16, 0x7E, 0xDB, 0xFF, 0x66, 0x3C, 0x66, 0x81, 0x00)
|
||||
-- setchar(16, 0x7E, 0xDB, 0x66, 0x3C, 0x66, 0x81, 0x66, 0x00)
|
||||
enemies = {}
|
||||
local index = 1
|
||||
for i = 1, 8 do
|
||||
for j = 1, 5 do
|
||||
local e = {
|
||||
x = (i * 2),
|
||||
y = j + 1,
|
||||
enabled = true
|
||||
}
|
||||
enemies[index] = e
|
||||
index = index + 1
|
||||
end
|
||||
end
|
||||
enemy_desp = 1
|
||||
enemy_speed = 80
|
||||
end
|
||||
|
||||
function create_bullet(x, y)
|
||||
bullet.x = x
|
||||
bullet.y = y
|
||||
bullet.enabled = true
|
||||
end
|
||||
|
||||
function delete_bullet()
|
||||
bullet.enabled = false
|
||||
end
|
||||
|
||||
function create_explosion(x, y)
|
||||
explosion.x = x
|
||||
explosion.y = y
|
||||
explosion.enabled = true
|
||||
explosion.counter = 50
|
||||
end
|
||||
|
||||
function delete_explosion()
|
||||
explosion.enabled = false
|
||||
end
|
||||
|
||||
function update_player()
|
||||
if clock % 5 == 0 then
|
||||
-- dreta
|
||||
if btn(KEY_RIGHT) and player.x < 19 then
|
||||
player.x = player.x + 1
|
||||
end
|
||||
|
||||
-- esquerra
|
||||
if btn(KEY_LEFT) and player.x > 0 then
|
||||
player.x = player.x - 1
|
||||
end
|
||||
end
|
||||
|
||||
-- dispara
|
||||
if btn(KEY_SPACE) and bullet.enabled == false then
|
||||
create_bullet(player.x, player.y - 1)
|
||||
end
|
||||
|
||||
-- reset
|
||||
if btn(KEY_R) then
|
||||
init()
|
||||
end
|
||||
end
|
||||
|
||||
function update_enemies()
|
||||
if clock % enemy_speed == 0 then
|
||||
sound(80, 50)
|
||||
local border_reached = false
|
||||
if enemy_desp ~= 0 then -- moviment horitzontal
|
||||
for i = 1, #enemies do
|
||||
if enemies[i].enabled == true then
|
||||
enemies[i].x = enemies[i].x + enemy_desp
|
||||
if enemies[i].x == 0 or enemies[i].x == 19 then
|
||||
border_reached = true
|
||||
end
|
||||
end
|
||||
end
|
||||
if border_reached == true then
|
||||
last_enemy_desp = enemy_desp
|
||||
enemy_desp = 0
|
||||
end
|
||||
elseif enemy_desp == 0 then -- moviment vertical
|
||||
for i = 1, #enemies do
|
||||
if enemies[i].enabled == true then
|
||||
enemies[i].y = enemies[i].y + 1
|
||||
end
|
||||
end
|
||||
enemy_desp = -last_enemy_desp
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function update_bullet()
|
||||
if clock % 10 == 0 and bullet.enabled == true then
|
||||
bullet.y = bullet.y - 1
|
||||
if bullet.y == 0 then
|
||||
delete_bullet()
|
||||
end
|
||||
for i = 1, #enemies do
|
||||
if enemies[i].enabled == true then
|
||||
if bullet.y == enemies[i].y and bullet.x == enemies[i].x then
|
||||
create_explosion(bullet.x, bullet.y)
|
||||
delete_bullet()
|
||||
enemies[i].enabled = false
|
||||
score = score + 10
|
||||
sound(400, 40)
|
||||
enemy_speed = enemy_speed - 2
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function update_explosion()
|
||||
if explosion.enabled == true then
|
||||
if explosion.counter > 0 then
|
||||
explosion.counter = explosion.counter - 1
|
||||
else
|
||||
delete_explosion()
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function draw_player()
|
||||
ink(COLOR_GREEN)
|
||||
paper(COLOR_BLACK)
|
||||
print("\244", player.x, player.y)
|
||||
end
|
||||
|
||||
function draw_enemies()
|
||||
ink(COLOR_LIGHT_GREEN)
|
||||
paper(COLOR_BLACK)
|
||||
for i = 1, #enemies do
|
||||
if enemies[i].enabled == true then
|
||||
print("\16", enemies[i].x, enemies[i].y)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function draw_bullet()
|
||||
if bullet.enabled == true then
|
||||
ink(COLOR_CYAN)
|
||||
paper(COLOR_BLACK)
|
||||
print("\145", bullet.x, bullet.y)
|
||||
end
|
||||
end
|
||||
|
||||
function draw_explosion()
|
||||
if explosion.enabled == true then
|
||||
ink(COLOR_RED)
|
||||
paper(COLOR_BLACK)
|
||||
print("\146", explosion.x, explosion.y)
|
||||
end
|
||||
end
|
||||
|
||||
function draw_hud()
|
||||
ink(COLOR_LIGHT_GRAY)
|
||||
paper(COLOR_BLACK)
|
||||
print(" ", 0, 0)
|
||||
print("SCORE: ", 0, 0)
|
||||
print(score, 6, 0)
|
||||
end
|
||||
|
||||
function draw_debug()
|
||||
ink(COLOR_BLACK)
|
||||
paper(COLOR_WHITE)
|
||||
locate(0, 0)
|
||||
print(player.x)
|
||||
end
|
||||
|
||||
function init()
|
||||
setmode(2)
|
||||
clock = 0
|
||||
score = 0
|
||||
paper(COLOR_BLACK)
|
||||
ink(COLOR_RED)
|
||||
init_player()
|
||||
init_enemies()
|
||||
init_bullet()
|
||||
init_explosion()
|
||||
end
|
||||
|
||||
function update()
|
||||
clock = clock + 1
|
||||
|
||||
update_player()
|
||||
update_enemies()
|
||||
update_bullet()
|
||||
update_explosion()
|
||||
|
||||
paper(COLOR_BLACK)
|
||||
cls()
|
||||
|
||||
draw_player()
|
||||
draw_enemies()
|
||||
draw_bullet()
|
||||
draw_explosion()
|
||||
draw_hud()
|
||||
-- draw_debug()
|
||||
end
|
||||
Reference in New Issue
Block a user