Load bubble images, create level and draw it

main
Tyler Scott 2022-02-03 13:01:35 +07:00
parent 3e06a6d970
commit 7d740b893e
9 changed files with 83 additions and 2 deletions

BIN
images/blue.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.3 KiB

BIN
images/green.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.3 KiB

BIN
images/orange.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.1 KiB

BIN
images/pink.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.3 KiB

BIN
images/purple.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.0 KiB

BIN
images/red.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.7 KiB

BIN
images/white.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.4 KiB

BIN
images/yellow.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.5 KiB

View File

@ -1,6 +1,28 @@
local tau = math.pi * 2
local game = {}
local function load_level(level, bubble_diameter, row_gap)
local slots = {}
local bubble_radius = bubble_diameter / 2
-- set prev_num_cols to size of 2nd row so x_offset is correct when row == 1
local prev_num_cols = #level[2]
for row = 1, #level do
local num_cols = #level[row]
local x_offset = num_cols < prev_num_cols and bubble_radius or 0
for col = 1, num_cols do
slots[#slots+1] = {
x = x_offset + bubble_radius + (col - 1) * bubble_diameter,
y = bubble_radius + (row - 1) * row_gap,
bubble_type = level[row][col]
}
end
prev_num_cols = num_cols
end
return slots
end
function love.load(arg)
game.window_width, game.window_height = love.graphics.getDimensions()
@ -16,8 +38,44 @@ function love.load(arg)
game.background_image = love.graphics.newImage('images/background.png')
game.background_width = game.background_image:getWidth()
local bubbles_wide = 8
local bubbles_high = 14
game.bubble_images = {
love.graphics.newImage('images/red.png'), -- 1
love.graphics.newImage('images/orange.png'), -- 2
love.graphics.newImage('images/yellow.png'), -- 3
love.graphics.newImage('images/green.png'), -- 4
love.graphics.newImage('images/blue.png'), -- 5
love.graphics.newImage('images/purple.png'), -- 6
love.graphics.newImage('images/pink.png'), -- 7
love.graphics.newImage('images/white.png') -- 8
}
game.levels = {}
game.levels[1] = {
{1,1,1,3,3,2,2,2},
{ 1,1,3,3,3,2,2 },
{4,4,7,8,8,6,5,5},
{ 4,7,7,8,6,6,5 },
{0,0,0,0,0,0,0,0},
{ 0,0,0,0,0,0,0 },
{0,0,0,0,0,0,0,0},
{ 0,0,0,0,0,0,0 },
{0,0,0,0,0,0,0,0},
{ 0,0,0,0,0,0,0 },
{0,0,0,0,0,0,0,0},
{ 0,0,0,0,0,0,0 },
{0,0,0,0,0,0,0,0},
{ 0,0,0,0,0,0,0 }
}
game.current_level = 1
game.bubble_slots = load_level(
game.levels[game.current_level], game.bubble_diameter, game.row_gap
)
local bubbles_wide = math.max(
#game.levels[game.current_level][1],
#game.levels[game.current_level][2]
)
local bubbles_high = #game.levels[game.current_level]
game.level_width = bubbles_wide * game.bubble_diameter
game.level_height = (bubbles_high - 1) * game.row_gap + game.bubble_diameter
game.level_x = (game.window_width - game.level_width) / 2
@ -39,6 +97,29 @@ function love.draw()
love.graphics.draw(game.background_image, 0, 0)
love.graphics.draw(game.background_image, game.background_width, 0)
for i = 1, #game.bubble_slots do
local slot = game.bubble_slots[i]
if slot.bubble_type >= 1 and slot.bubble_type <= #game.bubble_images then
love.graphics.draw(
game.bubble_images[slot.bubble_type], -- image
game.level_x + slot.x, -- x
game.level_y + slot.y, -- y
0, -- rotation
1, -- x scale
1, -- y scale
game.bubble_radius, -- x offset
game.bubble_radius -- y offset
)
else
love.graphics.circle(
'line',
game.level_x + slot.x,
game.level_y + slot.y,
game.bubble_radius
)
end
end
-- draw level border
love.graphics.setColor(0, 0, 0, 1)
love.graphics.rectangle(