-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.lua
143 lines (109 loc) · 3.77 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
-- Whack the mole in love2D
-- Author : DK Lee
-- 2016. 3. 17.
require "menu"
-- Setting Moles
Moles = {}
mole1 = {x = 70, y = 50, image = love.graphics.newImage("whack-a-mole.png"), isAlive = false, await = math.random(100, 200), life = math.random(100, 200) }
mole2 = {x = 320, y = 50, image = love.graphics.newImage("whack-a-mole.png"), isAlive = false, await = math.random(300, 500), life = math.random(100, 200) }
mole3 = {x = 570, y = 50, image = love.graphics.newImage("whack-a-mole.png"), isAlive = false, await = math.random(200, 500), life = math.random(100, 200) }
mole4 = {x = 70, y = 300, image = love.graphics.newImage("whack-a-mole.png"), isAlive = false, await = math.random(100, 600), life = math.random(100, 200) }
mole5 = {x = 320, y = 300, image = love.graphics.newImage("whack-a-mole.png"), isAlive = false, await = math.random(200, 300), life = math.random(100, 200) }
mole6 = {x = 570, y = 300, image = love.graphics.newImage("whack-a-mole.png"), isAlive = false, await = math.random(150, 600), life = math.random(100, 200) }
score = 0
-- Hit Sound
snd_Hit = {}
for i=1, 8 do
local snd_source = i..".mp3"
snd_Hit[i] = love.audio.newSource(snd_source)
end
-- Setting mole waiting time
await_min = 350
await_max = 600
function love.load()
cursor = love.mouse.newCursor("mole_hammer.png", 0, 0)
love.mouse.setCursor(cursor)
bgImage = love.graphics.newImage("background.png")
hole = love.graphics.newImage("hole.png")
bgAudio = love.audio.newSource("BubbleBubble.mp3")
winW = lg.getWidth()
winH = lg.getHeight()
table.insert(Moles, mole1)
table.insert(Moles, mole2)
table.insert(Moles, mole3)
table.insert(Moles, mole4)
table.insert(Moles, mole5)
table.insert(Moles, mole6)
love.audio.play(bgAudio)
-- Set Game state to game
state = "game"
end
function love.update(dt)
if state == "game" then
for i, m in ipairs(Moles) do
if m.isAlive == false then
m.await = m.await - 1
elseif m.isAlive == true then
m.life = m.life - 1
end
if m.await <= 0 then
m.isAlive = true
end
if m.life <= 0 then
score = score - 1
m.isAlive = false
m.life = math.random(150, 200)
m.await = math.random(await_min, await_max)
end
end
if await_min > 50 then
await_min = await_min - score
end
if await_min > 100 then
await_max = await_max - score
end
local down = love.keyboard.isDown
if down("escape") then
state = "menu"
end
elseif state == "menu" then
menu:update()
end
end
function love.mousepressed(x, y, button)
for i, m in ipairs(Moles) do
if m.isAlive == true then
if (x >= m.x) and x < (m.x + m.image:getWidth()) and (y >= m.y) and y < (m.y + m.image:getHeight()) then
m.isAlive = false
m.await = math.random(await_min, await_max)
m.life = math.random(150, 200)
score = score + 1
local iSound = math.random(1, 5)
love.audio.play(snd_Hit[iSound])
end
end
end
end
function love.draw()
if state == "game" then
-- background drawing
love.graphics.draw(bgImage, 0, 0)
-- hole drawing
love.graphics.draw(hole, 50, 100)
love.graphics.draw(hole, 300, 100)
love.graphics.draw(hole, 550, 100)
love.graphics.draw(hole, 50, 350)
love.graphics.draw(hole, 300, 350)
love.graphics.draw(hole, 550, 350)
-- popuping the moles
for i, m in ipairs(Moles) do
if m.isAlive == true then
love.graphics.draw(m.image, m.x, m.y)
end
end
love.graphics.print("Score : "..score, 600, 30)
end
if state == "menu" then
menu:draw()
end
end