-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.lua
144 lines (120 loc) · 2.97 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
138
139
140
141
142
143
144
love.graphics.setDefaultFilter("nearest","nearest")
inimigo={}
inimigos_controller = {}
inimigos_controller.inimigos = {}
brutal = love.graphics.newImage("inimigo.png")
defensor = love.graphics.newImage("jogador.png")
tiro= love.audio.newSource("tiro.wav")
fundo= love.audio.newSource("fundo.mp3")
morte= love.audio.newSource("morte.wav")
gameover = love.audio.newSource("gameover.wav")
imagemfundo=love.graphics.newImage("space.jpg")
function love.load()
game_over = false
game_win =false
love.audio.play(fundo)
player = {}
player.x = 0
player.bullets = {}
player.cooldown =20
player.fire = function()
if player.cooldown <=0 then
love.audio.play(tiro)
player.cooldown =20
bullet = {}
bullet.x = player.x + 4
bullet.y = 106
table.insert(player.bullets,bullet)
end
end
for i=0,10 do
inimigos_controller:gerainimigo(i*15,0)
inimigos_controller:gerainimigo(i*15,10)
inimigos_controller:gerainimigo(i*15,20)
inimigos_controller:gerainimigo(i*15,30)
inimigos_controller:gerainimigo(i*15,50)
end
end
function check(inimigos,tiros)
for n,i in ipairs(inimigos) do
for m,t in ipairs(tiros) do
if t.y<=i.y+i.height and t.x>i.x and t.x<i.x+i.width then
table.remove(inimigos,n)
table.remove(tiros,m)
love.audio.play(morte)
end
end
end
end
function inimigos_controller:gerainimigo(x,y)
inimigo ={}
inimigo.x=x
inimigo.y=y
inimigo.height=10
inimigo.width=10
inimigo.bullets = {}
inimigo.cooldown =20
table.insert(self.inimigos,inimigo)
end
function inimigo:fire()
if player.cooldown <=0 then
self.cooldown =20
bullet = {}
bullet.x = self.x + 7
bullet.y = 106
table.insert(self.bullets,bullet)
end
end
function love.update(dt)
player.cooldown = player.cooldown -1
--Controles
if love.keyboard.isDown("right") then
player.x=player.x+1
elseif love.keyboard.isDown("left") then
player.x=player.x-1
end
if love.keyboard.isDown("space") then
player.fire()
end
--Inimigos
for _,i in pairs(inimigos_controller.inimigos) do
if i.y >= love.graphics.getHeight()/4 then
game_over=true
end
i.y= i.y+0.01
end
--Tiro player
for i,b in ipairs(player.bullets) do
if b.y < -10 then
table.remove(player.bullets, i)
end
b.y=b.y-0.2
end
check(inimigos_controller.inimigos,player.bullets)
if #inimigos_controller.inimigos == 0 then
game_win =true
end
end
function love.draw()
love.graphics.scale(5)
love.graphics.draw(imagemfundo)
if game_over then
love.graphics.print("GAME OVER !")
return
elseif game_win then
love.graphics.print("VOCÊ VENCEU!")
return
end
-- Player
love.graphics.setColor(255,255,255)
love.graphics.draw(defensor,player.x,106)
-- Inimigos
for _,i in pairs(inimigos_controller.inimigos) do
love.graphics.draw(brutal,i.x,i.y,0)
end
--Tiro
love.graphics.setColor(255,255,255)
for _,v in pairs(player.bullets) do
love.graphics.rectangle("fill",v.x,v.y,2,2)
end
end