-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain-with-images.lua
More file actions
441 lines (396 loc) · 13.6 KB
/
main-with-images.lua
File metadata and controls
441 lines (396 loc) · 13.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
-- Space Shooter Game - WITH IMAGE SUPPORT
-- This is an enhanced version of main.lua that supports loading sprite images
-- If images are not found, it falls back to procedural graphics
-- 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 = {}
-- Image storage
local images = {
player = nil,
bullet = nil,
enemy = nil,
background = nil
}
-- Timing
local bulletCooldown = 0
local enemySpawnTimer = 0
local gameState = "menu" -- menu, playing, gameover
-- 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 .. " (using procedural graphics)")
return nil
end
end
-- 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},
{width = 60, height = 60, speed = 75, color = {1, 0.6, 0.2}, health = 2, points = 200},
{width = 30, height = 30, speed = 150, color = {0.8, 0.2, 0.8}, health = 1, points = 150}
}
local enemyType = enemyTypes[math.random(1, #enemyTypes)]
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
})
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 (fallback for when no images are available)
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 (with image support)
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, -- rotation
scale_x, scale_y,
img_w/2, img_h/2 -- origin (center)
)
else
-- Fallback to procedural graphics
-- 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
function love.load()
love.window.setTitle("Space Shooter - Example")
love.graphics.setBackgroundColor(0.02, 0.02, 0.08)
math.randomseed(os.time())
initStars()
-- Try to load images (will use procedural graphics as fallback)
-- Using organized folder structure
images.player = loadImage("assets/images/player/spaceship.png")
images.bullet = loadImage("assets/images/projectiles/bullet-player.png")
images.enemy = loadImage("assets/images/enemies/enemy-basic.png")
images.background = loadImage("assets/images/backgrounds/space-bg.png")
-- Alternative: Try flat structure first, then organized structure
if not images.player then
images.player = loadImage("assets/spaceship.png")
end
if not images.bullet then
images.bullet = loadImage("assets/bullet.png")
end
if not images.enemy then
images.enemy = loadImage("assets/enemy.png")
end
if not images.background then
images.background = loadImage("assets/background.png")
end
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.background then
love.graphics.setColor(1, 1, 1, 0.5) -- Semi-transparent
love.graphics.draw(images.background, 0, 0)
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
if images.bullet then
-- Draw bullet image
love.graphics.setColor(1, 1, 1, 1)
local img_w = images.bullet:getWidth()
local img_h = images.bullet:getHeight()
local scale_x = bullet.width / img_w
local scale_y = bullet.height / img_h
love.graphics.draw(
images.bullet,
bullet.x, bullet.y,
0,
scale_x, scale_y,
img_w/2, img_h/2
)
else
-- Fallback to procedural bullet
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
end
-- Draw enemies
for _, enemy in ipairs(enemies) do
if images.enemy then
-- Draw enemy image
love.graphics.setColor(1, 1, 1, 1)
local img_w = images.enemy:getWidth()
local img_h = images.enemy:getHeight()
local scale_x = enemy.width / img_w
local scale_y = enemy.height / img_h
love.graphics.draw(
images.enemy,
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
-- Draw UI
love.graphics.setColor(1, 1, 1, 1)
love.graphics.print("Score: " .. player.score, 10, 10)
love.graphics.print("Lives: " .. player.lives, 10, 30)
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