-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.lua
137 lines (122 loc) · 3.59 KB
/
main.lua
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
function love.load()
game_over = false
game_won = false
invadersWhoCanFire = 3
-- Player vars
player = {}
player.x = 0
player.bullets = {}
player.speed = 5
bullets_generation_tick = 30
player.fire = function()
if bullets_generation_tick <= 0 then
love.audio.play(player_shoot_sound)
bullets_generation_tick = 30
bullet = {}
bullet.x = player.x + 25
bullet.y = 550
table.insert(player.bullets, bullet)
end
end
--Enemy & enemy controllers
enemy = {}
enemies_list = {}
enemies_list.enemies = {}
function enemies_list:spawn(x, y)
enemy = {}
enemy.x = x
enemy.y = y
enemy.height = 40
enemy.width = 85
enemy.speed = 0.2
table.insert(self.enemies, enemy)
end
--Enemy spawning patterns
for i=0, 3 do
for j=0,4 do
enemies_list:spawn(j*120+120,i*50+15)
end
end
function checkCollisiononEnemy(enemies, bullets)
for i,v in ipairs(enemies) do
for j,b in ipairs(bullets) do
if b.y <= v.y + v.height and b.x > v.x and b.x < v.x + v.width then
table.remove(player.bullets, j)
table.remove(enemies, i)
end
end
end
end
background_image = love.graphics.newImage('images/space_bg.png')
-- Player resources load
player.image = love.graphics.newImage('images/player.png')
player.explose_shoot = love.audio.newSource('sounds/sounds_shoot.mp3','static')
-- Enemy resources load
enemy.image = love.graphics.newImage('images/invader.png')
-- Sounds load
music = love.audio.newSource('sounds/music.mp3','static')
player_shoot_sound = love.audio.newSource('sounds/sounds_shoot.mp3','static')
music:setLooping(true)
love.audio.play(music)
end
function love.update(dt)
--Player controls
if love.keyboard.isDown("right") then
if player.x > 750 then
player.x = 750
end
player.x = player.x + player.speed
elseif love.keyboard.isDown("left") then
if player.x <0 then
player.x = 0
end
player.x = player.x - player.speed
end
if love.keyboard.isDown("space") then
player.fire()
enemy.fire()
end
--Game won check
if #enemies_list.enemies == 0 then
game_won = true
end
--Enemies controls
for _,v in pairs(enemies_list.enemies) do
-- Enemy pass through player, then game over check
if v.y >= 570 then
game_over = true
end
v.y = v.y + enemy.speed
end
--Player Bullets controller
for i,v in ipairs(player.bullets) do
if v.y < -1 then
table.remove(player.bullets, i)
end
v.y = v.y - 10
end
--Bullet cooldown controller
bullets_generation_tick = bullets_generation_tick - 1
checkCollisiononEnemy(enemies_list.enemies, player.bullets)
end
function love.draw()
love.graphics.draw(background_image)
if game_won == true then
love.graphics.print("Congratulations! You won!")
return
end
if game_over == true then
love.graphics.print("Game Over!")
return
end
love.graphics.draw(player.image, player.x, 560, 0, 0.5)
--Bullet spawner
love.graphics.setColor(255,255,255)
for _,v in pairs(player.bullets) do
love.graphics.rectangle("fill", v.x, v.y, 10,10)
end
--Enemy spawner
for _,v in pairs(enemies_list.enemies) do
love.graphics.draw(enemy.image, v.x, v.y, 0, 0.7)
end
end