-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.lua
190 lines (156 loc) · 4.68 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
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
function love.load()
sprites = {}
sprites.player = love.graphics.newImage('sprites/player.png')
sprites.bullet = love.graphics.newImage('sprites/bullet.png')
sprites.zombie = love.graphics.newImage('sprites/zombie.png')
sprites.background = love.graphics.newImage('sprites/background.png')
player = {}
player.x = love.graphics.getWidth()/2
player.y = love.graphics.getHeight()/2
player.speed = 180
zombies = {}
bullets = {}
gameState = 1
maxTime = 2
timer = maxTime
score = 0
myFont = love.graphics.newFont(40)
end
function love.update(dt)
if gameState == 2 then
if love.keyboard.isDown("down") and player.y < love.graphics.getHeight() then
player.y = player.y + player.speed * dt
end
if love.keyboard.isDown("up") and player.y > 0 then
player.y = player.y - player.speed * dt
end
if love.keyboard.isDown("left") and player.x > 0 then
player.x = player.x - player.speed * dt
end
if love.keyboard.isDown("right") and player.x < love.graphics.getWidth() then
player.x = player.x + player.speed * dt
end
end
for i,z in ipairs(zombies) do
z.x = z.x + math.cos(zombie_player_angle(z)) * z.speed * dt
z.y = z.y + math.sin(zombie_player_angle(z)) * z.speed * dt
if distanceBetween(z.x, z.y, player.x, player.y) < 30 then
for i,z in ipairs(zombies) do
zombies[i] = nil
gameState = 1
player.x = love.graphics.getWidth()/2
player.y = love.graphics.getHeight()/2
end
end
end
for i,b in ipairs(bullets) do
b.x = b.x + math.cos(b.direction) * b.speed * dt
b.y = b.y + math.sin(b.direction) * b.speed * dt
end
for i=#bullets,1,-1 do
local b = bullets[i]
if b.x < 0 or b.y < 0 or b.x > love.graphics.getWidth() or b.y > love.graphics.getHeight() then
table.remove(bullets, i)
end
end
for i,z in ipairs(zombies) do
for j,b in ipairs(bullets) do
if distanceBetween(z.x, z.y, b.x, b.y) < 20 then
z.dead = true
b.dead = true
score = score + 1
end
end
end
for i=#zombies,1,-1 do
local z = zombies[i]
if z.dead == true then
table.remove(zombies, i)
end
end
for i=#bullets, 1, -1 do
local b = bullets[i]
if b.dead == true then
table.remove(bullets, i)
end
end
if gameState == 2 then
timer = timer - dt
if timer <= 0 then
spawnZombie()
maxTime = maxTime * 0.95
timer = maxTime
end
end
end
function love.draw()
love.graphics.draw(sprites.background, 0, 0)
if gameState == 1 then
love.graphics.setFont(myFont)
love.graphics.printf("Click anywhere to begin!", 0, 50, love.graphics.getWidth(), "center")
end
love.graphics.printf("Score: " .. score, 0, love.graphics.getHeight() - 100, love.graphics.getWidth(), "center")
love.graphics.draw(sprites.player, player.x, player.y, player_mouse_angle(), nil, nil, sprites.player:getWidth()/2, sprites.player:getHeight()/2)
for i,z in ipairs(zombies) do
love.graphics.draw(sprites.zombie, z.x, z.y, zombie_player_angle(z), nil, nil, sprites.zombie:getWidth()/2, sprites.zombie:getHeight()/2)
end
for i,b in ipairs(bullets) do
love.graphics.draw(sprites.bullet, b.x, b.y, nil, 0.5, 0.5, sprites.bullet:getWidth()/2, sprites.bullet:getHeight()/2)
end
end
function player_mouse_angle()
return math.atan2(player.y - love.mouse.getY(), player.x - love.mouse.getX()) + math.pi
end
function zombie_player_angle(enemy)
return math.atan2(player.y - enemy.y, player.x - enemy.x)
end
function spawnZombie()
zombie = {}
zombie.x = 0
zombie.y = 0
zombie.speed = 140
zombie.dead = false
local side = math.random(1, 4)
if side == 1 then
zombie.x = -30
zombie.y = math.random(0, love.graphics.getHeight())
elseif side == 2 then
zombie.x = math.random(0, love.graphics.getWidth())
zombie.y = -30
elseif side == 3 then
zombie.x = love.graphics.getWidth() + 30
zombie.y = math.random(0, love.graphics.getHeight())
else
zombie.x = math.random(0, love.graphics.getWidth())
zombie.y = love.graphics.getHeight() + 30
end
table.insert(zombies, zombie)
end
function spawnBullet()
bullet = {}
bullet.x = player.x
bullet.y = player.y
bullet.speed = 500
bullet.direction = player_mouse_angle()
bullet.dead = false
table.insert(bullets, bullet)
end
function love.keypressed(key, scancode, isrepeat)
if key == "space" then
spawnZombie()
end
end
function love.mousepressed( x, y, b, istouch)
if b == 1 and gameState == 2 then
spawnBullet()
end
if gameState == 1 then
gameState = 2
maxTime = 2
timer = maxTime
score = 0
end
end
function distanceBetween(x1, y1, x2, y2)
return math.sqrt((y2 - y1)^2 + (x2 - x1)^2)
end