Skip to content

Commit add9224

Browse files
committed
Add particle effects
1 parent a460f29 commit add9224

File tree

2 files changed

+60
-27
lines changed

2 files changed

+60
-27
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
# Moonman
1+
# ![Logo](https://github.com/synthic/moonman/raw/master/assets/logo.png)
22

3-
A game about an astronaut made with [LÖVE](https://love2d.org).<br><br>
3+
A game about an astronaut made with [LÖVE](https://love2d.org).
44

55
## Credits
66

main.lua

Lines changed: 58 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
1-
-- Version number
2-
version = 'V1.0'
3-
4-
-- Timer variable
5-
dtotal = 0
1+
function love.load()
2+
-- Version number
3+
version = 'V1.0'
64

7-
-- Enemies table
8-
enemies = {}
5+
-- Timer variable
6+
dtotal = 0
97

10-
function love.load()
118
-- Set screen attributes
129
screen = {
1310
width = 320,
@@ -37,9 +34,6 @@ function love.load()
3734
-- Set game state
3835
gamestate = 'title'
3936

40-
-- Initialize score
41-
score = 0
42-
4337
-- Set window attributes
4438
love.window.setTitle('Moonman')
4539
love.window.setMode(screen.width * 2, screen.height * 2)
@@ -141,6 +135,11 @@ function love.update(dt)
141135
bullet.x = 1000
142136
score = score + 100
143137
playSound(enemyDestroySfx)
138+
139+
local explosion = getExplosion(getBlast(50))
140+
explosion:setPosition(enemy.x + enemy.width/2, enemy.y + enemy.height/2)
141+
explosion:emit(5)
142+
table.insert(explosions, explosion)
144143
end
145144

146145
-- Collision checking (player)
@@ -150,6 +149,15 @@ function love.update(dt)
150149
else table.remove(enemies, i) end
151150
end
152151

152+
-- Update explosions
153+
for i=#explosions, 1, -1 do
154+
local explosion = explosions[i]
155+
explosion:update(dt)
156+
if explosion:getCount() == 0 then
157+
table.remove(explosions, i)
158+
end
159+
end
160+
153161
-- Timer (1s)
154162
dtotal = dtotal + dt
155163
if dtotal >= 1 then
@@ -216,9 +224,15 @@ function love.draw(dt)
216224
love.graphics.draw(animEnemy.spriteSheet, animEnemy.quads[spriteNum], enemy.x, enemy.y)
217225
end
218226

219-
-- Draw projectiles
227+
-- Draw projectile
220228
love.graphics.draw(bullet.img, bullet.x, bullet.y)
221229

230+
-- Draw explosions
231+
for i=#explosions, 1, -1 do
232+
local explosion = explosions[i]
233+
love.graphics.draw(explosion, 0, 0)
234+
end
235+
222236
-- Set drawing target to window
223237
love.graphics.setCanvas()
224238

@@ -238,17 +252,6 @@ function love.draw(dt)
238252
end
239253
end
240254

241-
function newEnemy(x,y)
242-
local enemy = {}
243-
enemy.x = x
244-
enemy.y = y
245-
enemy.width = 24
246-
enemy.height = 24
247-
enemy.speed = love.math.random(100, 300)
248-
enemy.removed = false
249-
table.insert(enemies, enemy)
250-
end
251-
252255
function startGame()
253256
-- Reset positions
254257
player.x = 40
@@ -258,8 +261,9 @@ function startGame()
258261
-- Reset score
259262
score = 0
260263

261-
-- Reset enemies
264+
-- Reset entities
262265
enemies = {}
266+
explosions = {}
263267

264268
-- Start music
265269
musicTrack:play()
@@ -284,19 +288,48 @@ end
284288
function shootGun()
285289
-- Shoot if no projectile on screen
286290
if bullet.x > screen.width then
287-
bullet.x = player.x + player.width
291+
bullet.x = player.x + player.width - 2
288292
bullet.y = player.y + ((player.height / 2) - (bullet.height / 2))
289293
playSound(shootSfx)
290294
end
291295
end
292296

297+
function newEnemy(x,y)
298+
local enemy = {}
299+
enemy.x = x
300+
enemy.y = y
301+
enemy.width = 24
302+
enemy.height = 24
303+
enemy.speed = love.math.random(100, 300)
304+
enemy.removed = false
305+
table.insert(enemies, enemy)
306+
end
307+
293308
function playSound(sound)
294309
sound:stop()
295310
pitchMod = 0.8 + love.math.random(0, 10) / 25
296311
sound:setPitch(pitchMod)
297312
sound:play()
298313
end
299314

315+
function getBlast(size)
316+
local blast = love.graphics.newCanvas(size, size)
317+
love.graphics.setCanvas(blast)
318+
love.graphics.setColor(255, 255, 255, 255)
319+
love.graphics.circle('fill', size/2, size/2, size/2)
320+
love.graphics.setCanvas()
321+
return blast
322+
end
323+
324+
function getExplosion(image)
325+
pSystem = love.graphics.newParticleSystem(image, 30)
326+
pSystem:setParticleLifetime(0.5, 0.5)
327+
pSystem:setLinearAcceleration(-100, -100, 100, 100)
328+
pSystem:setColors(255, 255, 0, 255, 255, 153, 51, 255, 64, 64, 64, 0)
329+
pSystem:setSizes(0.5, 0.5)
330+
return pSystem
331+
end
332+
300333
--[[
301334
Create animation from sprite sheet
302335
https://love2d.org/wiki/Tutorial:Animation

0 commit comments

Comments
 (0)