-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.lua
More file actions
589 lines (522 loc) · 20.6 KB
/
main.lua
File metadata and controls
589 lines (522 loc) · 20.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
-- Space Shooter Game
-- A simple but visually appealing game using LOVE2D framework
-- Game state
local player = {
x = 400,
y = 500,
width = 50,
height = 50,
speed = 300,
color = {0.2, 0.6, 1.0},
lives = 3,
score = 0
}
local bullets = {}
local enemies = {}
local particles = {}
local stars = {}
-- Images
local images = {
player = nil,
enemies = {},
backgrounds = {},
ui = {},
currentBgIndex = 1
}
-- Timing
local bulletCooldown = 0
local enemySpawnTimer = 0
local gameState = "menu" -- menu, playing, gameover
-- Initialize stars for background
local function initStars()
for i = 1, 100 do
table.insert(stars, {
x = math.random(0, 800),
y = math.random(0, 600),
size = math.random(1, 3),
speed = math.random(50, 150),
brightness = math.random(50, 100) / 100
})
end
end
-- Create explosion particles
local function createExplosion(x, y, color)
for i = 1, 20 do
local angle = math.random() * math.pi * 2
local speed = math.random(100, 300)
table.insert(particles, {
x = x,
y = y,
vx = math.cos(angle) * speed,
vy = math.sin(angle) * speed,
life = 1.0,
color = color or {1, 0.5, 0.2},
size = math.random(3, 8)
})
end
end
-- Spawn a new enemy
local function spawnEnemy()
local enemyTypes = {
{width = 40, height = 40, speed = 100, color = {1, 0.3, 0.3}, health = 1, points = 100, size = "mini"},
{width = 60, height = 60, speed = 75, color = {1, 0.6, 0.2}, health = 2, points = 200, size = "small"},
{width = 30, height = 30, speed = 150, color = {0.8, 0.2, 0.8}, health = 1, points = 150, size = "tiny"}
}
local enemyType = enemyTypes[math.random(1, #enemyTypes)]
-- Select a random sprite from the appropriate size category
local spriteList = images.enemies[enemyType.size]
local sprite = nil
if spriteList and #spriteList > 0 then
-- Filter out nil values
local validSprites = {}
for _, s in ipairs(spriteList) do
if s then table.insert(validSprites, s) end
end
if #validSprites > 0 then
sprite = validSprites[math.random(1, #validSprites)]
end
end
table.insert(enemies, {
x = math.random(50, 750),
y = -50,
width = enemyType.width,
height = enemyType.height,
speed = enemyType.speed,
color = enemyType.color,
health = enemyType.health,
points = enemyType.points,
wobble = math.random() * math.pi * 2,
sprite = sprite
})
end
-- Fire a bullet
local function fireBullet()
table.insert(bullets, {
x = player.x,
y = player.y - player.height / 2,
width = 6,
height = 15,
speed = 500,
color = {0.5, 1, 0.5}
})
end
-- Check collision between two rectangles
local function checkCollision(a, b)
return a.x - a.width/2 < b.x + b.width/2 and
a.x + a.width/2 > b.x - b.width/2 and
a.y - a.height/2 < b.y + b.height/2 and
a.y + a.height/2 > b.y - b.height/2
end
-- Draw a glowing rectangle (ship/enemy)
local function drawGlowingRect(x, y, width, height, color, glowSize)
glowSize = glowSize or 10
-- Draw glow layers
for i = glowSize, 1, -2 do
local alpha = 0.1 * (1 - i / glowSize)
love.graphics.setColor(color[1], color[2], color[3], alpha)
love.graphics.rectangle("fill",
x - width/2 - i,
y - height/2 - i,
width + i*2,
height + i*2)
end
-- Draw main shape
love.graphics.setColor(color[1], color[2], color[3], 1)
love.graphics.rectangle("fill", x - width/2, y - height/2, width, height)
-- Draw highlight
love.graphics.setColor(1, 1, 1, 0.3)
love.graphics.rectangle("fill", x - width/2 + 2, y - height/2 + 2, width - 4, height/3)
end
-- Draw player ship (triangle shape)
local function drawPlayer()
local x, y = player.x, player.y
local w, h = player.width, player.height
if images.player then
-- Draw the spaceship image
love.graphics.setColor(1, 1, 1, 1)
local img_w = images.player:getWidth()
local img_h = images.player:getHeight()
local scale_x = w / img_w
local scale_y = h / img_h
love.graphics.draw(
images.player,
x, y,
0,
scale_x, scale_y,
img_w/2, img_h/2
)
else
-- Fallback: Glow effect
for i = 15, 1, -3 do
local alpha = 0.1 * (1 - i / 15)
love.graphics.setColor(player.color[1], player.color[2], player.color[3], alpha)
love.graphics.polygon("fill",
x, y - h/2 - i,
x - w/2 - i, y + h/2 + i,
x + w/2 + i, y + h/2 + i)
end
-- Main ship body
love.graphics.setColor(player.color[1], player.color[2], player.color[3], 1)
love.graphics.polygon("fill",
x, y - h/2,
x - w/2, y + h/2,
x + w/2, y + h/2)
-- Cockpit
love.graphics.setColor(0.1, 0.2, 0.3, 1)
love.graphics.polygon("fill",
x, y - h/4,
x - w/4, y + h/4,
x + w/4, y + h/4)
-- Engine glow
love.graphics.setColor(1, 0.5, 0.2, 0.8)
love.graphics.polygon("fill",
x - w/4, y + h/2,
x, y + h/2 + 15 + math.random(0, 5),
x + w/4, y + h/2)
end
end
-- Helper function to safely load images
local function loadImage(path)
local success, img = pcall(love.graphics.newImage, path)
if success then
print("Loaded: " .. path)
return img
else
print("Could not load: " .. path)
return nil
end
end
function love.load()
love.window.setTitle("Space Shooter - Example")
love.graphics.setBackgroundColor(0.02, 0.02, 0.08)
math.randomseed(os.time())
initStars()
-- Load player ship (try all 4 variations, use first one found)
local playerShips = {
"assets/images/player/ship-1-vertical-up.png",
"assets/images/player/ship2-vertical-up.png",
"assets/images/player/ship3-vertical-up.png",
"assets/images/player/ship4-vertical-up.png"
}
for _, path in ipairs(playerShips) do
images.player = loadImage(path)
if images.player then break end
end
-- Load enemy sprites (categorized by size)
images.enemies.large = {}
images.enemies.medium = {}
images.enemies.small = {}
images.enemies.tiny = {}
images.enemies.mini = {}
-- Large enemies
table.insert(images.enemies.large, loadImage("assets/images/enemies/mother-ship-1-large-vertical-down.png"))
table.insert(images.enemies.large, loadImage("assets/images/enemies/big-brother-ship-large-vertical-down.png"))
table.insert(images.enemies.large, loadImage("assets/images/enemies/big-sis-ship-large-vertical-down.png"))
-- Medium enemies
table.insert(images.enemies.medium, loadImage("assets/images/enemies/bronzy-medium-vertical-down.png"))
table.insert(images.enemies.medium, loadImage("assets/images/enemies/fast-orange-medium-vertical-down.png"))
table.insert(images.enemies.medium, loadImage("assets/images/enemies/purpish-medium-vertical-down.png"))
-- Small enemies
table.insert(images.enemies.small, loadImage("assets/images/enemies/jackfrosty-small-vertical-down.png"))
table.insert(images.enemies.small, loadImage("assets/images/enemies/jilly-small-vertical-down.png"))
table.insert(images.enemies.small, loadImage("assets/images/enemies/magic-moe-small-vertical-down.png"))
-- Tiny enemies
for i = 1, 6 do
table.insert(images.enemies.tiny, loadImage("assets/images/enemies/tiny" .. i .. ".png"))
end
-- Mini enemies
for i = 1, 8 do
table.insert(images.enemies.mini, loadImage("assets/images/enemies/mini" .. i .. ".png"))
end
-- Load backgrounds
for i = 1, 11 do
local bg = loadImage("assets/images/backgrounds/level" .. i .. ".png")
if bg then
table.insert(images.backgrounds, bg)
end
end
-- Set initial background
if #images.backgrounds > 0 then
images.currentBgIndex = math.random(1, #images.backgrounds)
end
-- Load UI elements
images.ui.healthBarFrame = loadImage("assets/images/ui/health-bar-frame.png")
end
function love.update(dt)
-- Update stars (parallax background)
for _, star in ipairs(stars) do
star.y = star.y + star.speed * dt
if star.y > 600 then
star.y = 0
star.x = math.random(0, 800)
end
end
if gameState == "menu" then
if love.keyboard.isDown("space") then
gameState = "playing"
player.lives = 3
player.score = 0
enemies = {}
bullets = {}
end
return
end
if gameState == "gameover" then
if love.keyboard.isDown("space") then
gameState = "menu"
end
return
end
-- Player movement
if love.keyboard.isDown("left") or love.keyboard.isDown("a") then
player.x = math.max(player.width/2, player.x - player.speed * dt)
end
if love.keyboard.isDown("right") or love.keyboard.isDown("d") then
player.x = math.min(800 - player.width/2, player.x + player.speed * dt)
end
if love.keyboard.isDown("up") or love.keyboard.isDown("w") then
player.y = math.max(player.height/2, player.y - player.speed * dt)
end
if love.keyboard.isDown("down") or love.keyboard.isDown("s") then
player.y = math.min(600 - player.height/2, player.y + player.speed * dt)
end
-- Shooting
bulletCooldown = bulletCooldown - dt
if love.keyboard.isDown("space") and bulletCooldown <= 0 then
fireBullet()
bulletCooldown = 0.15
end
-- Update bullets
for i = #bullets, 1, -1 do
bullets[i].y = bullets[i].y - bullets[i].speed * dt
if bullets[i].y < -20 then
table.remove(bullets, i)
end
end
-- Spawn enemies
enemySpawnTimer = enemySpawnTimer - dt
if enemySpawnTimer <= 0 then
spawnEnemy()
enemySpawnTimer = math.max(0.5, 2 - player.score / 5000)
end
-- Update enemies
for i = #enemies, 1, -1 do
local enemy = enemies[i]
enemy.y = enemy.y + enemy.speed * dt
enemy.wobble = enemy.wobble + dt * 3
enemy.x = enemy.x + math.sin(enemy.wobble) * 0.5
-- Check if enemy reached bottom
if enemy.y > 650 then
table.remove(enemies, i)
-- Check collision with player
elseif checkCollision(enemy, player) then
createExplosion(enemy.x, enemy.y, enemy.color)
table.remove(enemies, i)
player.lives = player.lives - 1
if player.lives <= 0 then
gameState = "gameover"
createExplosion(player.x, player.y, player.color)
end
else
-- Check collision with bullets
for j = #bullets, 1, -1 do
if checkCollision(enemy, bullets[j]) then
enemy.health = enemy.health - 1
table.remove(bullets, j)
if enemy.health <= 0 then
createExplosion(enemy.x, enemy.y, enemy.color)
player.score = player.score + enemy.points
table.remove(enemies, i)
end
break
end
end
end
end
-- Update particles
for i = #particles, 1, -1 do
local p = particles[i]
p.x = p.x + p.vx * dt
p.y = p.y + p.vy * dt
p.life = p.life - dt * 2
p.vx = p.vx * 0.98
p.vy = p.vy * 0.98
if p.life <= 0 then
table.remove(particles, i)
end
end
end
function love.draw()
-- Draw background image if available
if #images.backgrounds > 0 and images.currentBgIndex <= #images.backgrounds then
local bg = images.backgrounds[images.currentBgIndex]
if bg then
love.graphics.setColor(1, 1, 1, 1)
-- Scale background to fit window
local scaleX = 800 / bg:getWidth()
local scaleY = 600 / bg:getHeight()
love.graphics.draw(bg, 0, 0, 0, scaleX, scaleY)
end
end
-- Draw stars
for _, star in ipairs(stars) do
love.graphics.setColor(1, 1, 1, star.brightness)
love.graphics.circle("fill", star.x, star.y, star.size)
end
if gameState == "menu" then
love.graphics.setColor(1, 1, 1, 1)
love.graphics.printf("SPACE SHOOTER", 0, 200, 800, "center")
love.graphics.printf("Press SPACE to Start", 0, 300, 800, "center")
love.graphics.printf("Controls: WASD or Arrow Keys to Move", 0, 350, 800, "center")
love.graphics.printf("SPACE to Shoot", 0, 380, 800, "center")
return
end
-- Draw bullets
for _, bullet in ipairs(bullets) do
-- Bullet glow
for i = 8, 1, -2 do
local alpha = 0.2 * (1 - i / 8)
love.graphics.setColor(bullet.color[1], bullet.color[2], bullet.color[3], alpha)
love.graphics.rectangle("fill",
bullet.x - bullet.width/2 - i,
bullet.y - bullet.height/2 - i,
bullet.width + i*2,
bullet.height + i*2)
end
love.graphics.setColor(bullet.color[1], bullet.color[2], bullet.color[3], 1)
love.graphics.rectangle("fill", bullet.x - bullet.width/2, bullet.y - bullet.height/2, bullet.width, bullet.height)
end
-- Draw enemies
for _, enemy in ipairs(enemies) do
if enemy.sprite then
-- Draw enemy sprite
love.graphics.setColor(1, 1, 1, 1)
local img_w = enemy.sprite:getWidth()
local img_h = enemy.sprite:getHeight()
local scale_x = enemy.width / img_w
local scale_y = enemy.height / img_h
love.graphics.draw(
enemy.sprite,
enemy.x, enemy.y,
0,
scale_x, scale_y,
img_w/2, img_h/2
)
else
-- Fallback to procedural
drawGlowingRect(enemy.x, enemy.y, enemy.width, enemy.height, enemy.color)
end
end
-- Draw particles
for _, p in ipairs(particles) do
love.graphics.setColor(p.color[1], p.color[2], p.color[3], p.life)
love.graphics.circle("fill", p.x, p.y, p.size * p.life)
end
-- Draw player
if gameState == "playing" then
drawPlayer()
end
-- =======================================================================
-- UI LAYER - HEALTH BAR AND HUD
-- =======================================================================
-- This section draws the UI elements on top of everything else
-- The health bar frame stretches across the entire top of the screen
if images.ui.healthBarFrame then
-- ===================================================================
-- HEALTH BAR FRAME CONFIGURATION
-- ===================================================================
-- Adjust these values to position and scale the health bar frame
local frameImg = images.ui.healthBarFrame
local frameOriginalWidth = frameImg:getWidth()
local frameOriginalHeight = frameImg:getHeight()
-- Position settings (adjust these to move the frame)
local frameY = 0 -- Y position (0 = top of screen)
local frameX = 0 -- X position (0 = left edge)
-- Scaling settings (adjust these to resize the frame)
local frameScaleX = 800 / frameOriginalWidth -- Scale to screen width (800)
local frameScaleY = 0.7 -- Taller frame (1.8x original height)
-- Draw the health bar frame stretched across the top
love.graphics.setColor(1, 1, 1, 1)
love.graphics.draw(
frameImg,
frameX,
frameY,
0, -- rotation
frameScaleX, -- scale X (stretches to screen width)
frameScaleY, -- scale Y (maintains aspect or custom)
0, -- origin X
0 -- origin Y
)
-- ===================================================================
-- HEALTH METER CONFIGURATION
-- ===================================================================
-- The health meter fills inside the frame based on player health
-- Health meter position (adjust to align with frame design)
local healthMeterX = 250 -- X position of health meter start
local healthMeterY = 16 -- Y position of health meter (from top)
local healthMeterWidth = 300 -- Maximum width of health meter
local healthMeterHeight = 25 -- Height of health meter bar
-- Calculate health percentage
local healthPercent = player.lives / 3 -- 3 = max lives
local currentHealthWidth = healthMeterWidth * healthPercent
-- Draw health meter background (dark/empty portion)
love.graphics.setColor(0.2, 0.1, 0.1, 0.8)
love.graphics.rectangle("fill", healthMeterX, healthMeterY, healthMeterWidth, healthMeterHeight)
-- Draw health meter fill (colored portion based on health)
-- Color changes based on health: green > yellow > red
if healthPercent > 0.6 then
love.graphics.setColor(0.2, 0.8, 0.2, 1) -- Green (healthy)
elseif healthPercent > 0.3 then
love.graphics.setColor(0.9, 0.9, 0.2, 1) -- Yellow (warning)
else
love.graphics.setColor(0.9, 0.2, 0.2, 1) -- Red (critical)
end
love.graphics.rectangle("fill", healthMeterX, healthMeterY, currentHealthWidth, healthMeterHeight)
-- Draw health meter border
love.graphics.setColor(0.8, 0.8, 0.8, 1)
love.graphics.rectangle("line", healthMeterX, healthMeterY, healthMeterWidth, healthMeterHeight)
-- ===================================================================
-- ADJUSTMENT GUIDE
-- ===================================================================
-- frameY: Move frame up (negative) or down (positive)
-- frameX: Move frame left (negative) or right (positive)
-- frameScaleX: Stretch width (> 1 wider, < 1 narrower)
-- frameScaleY: Stretch height (> 1 taller, < 1 shorter)
--
-- healthMeterX: Move meter left/right to align with frame
-- healthMeterY: Move meter up/down to align with frame
-- healthMeterWidth: Change total width of health bar
-- healthMeterHeight: Change height/thickness of health bar
-- ===================================================================
end
-- Draw UI text with glowing green effect
-- Lives on the left side (aligned with health bar)
local livesX = 150
local livesY = 20
love.graphics.setColor(0, 1, 0, 0.3) -- Glow layer (transparent green)
love.graphics.print("Lives: " .. player.lives, livesX - 1, livesY)
love.graphics.print("Lives: " .. player.lives, livesX + 1, livesY)
love.graphics.print("Lives: " .. player.lives, livesX, livesY - 1)
love.graphics.print("Lives: " .. player.lives, livesX, livesY + 1)
love.graphics.setColor(0, 1, 0, 1) -- Main text (bright green)
love.graphics.print("Lives: " .. player.lives, livesX, livesY)
-- Score on the right side (aligned with health bar)
local scoreText = "Score: " .. player.score
local scoreWidth = love.graphics.getFont():getWidth(scoreText)
local scoreX = 800 - scoreWidth - 150 -- 20 pixels from right edge
local scoreY = 20
love.graphics.setColor(0, 1, 0, 0.3) -- Glow layer (transparent green)
love.graphics.print(scoreText, scoreX - 1, scoreY)
love.graphics.print(scoreText, scoreX + 1, scoreY)
love.graphics.print(scoreText, scoreX, scoreY - 1)
love.graphics.print(scoreText, scoreX, scoreY + 1)
love.graphics.setColor(0, 1, 0, 1) -- Main text (bright green)
love.graphics.print(scoreText, scoreX, scoreY)
if gameState == "gameover" then
love.graphics.setColor(1, 0.3, 0.3, 1)
love.graphics.printf("GAME OVER", 0, 250, 800, "center")
love.graphics.setColor(1, 1, 1, 1)
love.graphics.printf("Final Score: " .. player.score, 0, 300, 800, "center")
love.graphics.printf("Press SPACE to Return to Menu", 0, 350, 800, "center")
end
end