-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcoin.lua
More file actions
77 lines (65 loc) · 2.06 KB
/
coin.lua
File metadata and controls
77 lines (65 loc) · 2.06 KB
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
local coins = {}
local coinsImages = {}
local margin = 180
for i = 1, 2 do
coinsImages[i] = love.graphics.newImage("Images/Bonus/Coin" .. i .. ".png")
end
function createCoin()
local object = {}
object.x = math.random(margin, SCREEN_WIDTH - margin)
object.y = math.random(margin, SCREEN_HEIGHT - margin)
object.life = 10
local rand = math.random(1, #coinsImages)
object.image = coinsImages[rand]
object.imageWidth = object.image:getWidth()
object.imageHeight = object.image:getHeight()
object.radius = object.imageHeight
object.sound = love.audio.newSource("Sons/Coin.wav", "static")
object.value = 10
object.offsetX = object.x - object.imageWidth / 2
object.offsetY = object.y - object.imageHeight / 2
object.offsetX2 = object.offsetX + object.imageWidth
object.offsetY2 = object.offsetY + object.imageHeight
if rand == 2 then
object.value = 100
end
object.draw = function()
love.graphics.draw(object.image, object.x, object.y, 0, 1, 1, object.imageWidth / 2, object.imageHeight / 2)
--love.graphics.rectangle("line", object.offsetX, object.offsetY, object.imageWidth, object.imageHeight)
--love.graphics.print(tostring(object.value), object.x, object.y)
end
return object
end
function newCoin()
local object = createCoin()
while checkObjectCollision(object) do
object = createCoin()
end
table.insert(coins, object)
end
function updateCoins(dt)
for i = #coins, 1, -1 do
if coins[i].value == 0 then
table.remove(coins, i)
end
end
end
function drawCoins()
for _, object in ipairs(coins) do
object.draw()
end
end
function razCoins()
for i = #coins, 1, -1 do
table.remove(coins, i)
end
end
function checkCoinCollision(tank)
for _, object in ipairs(coins) do
if isIntersecting(object.x, object.y, object.radius, tank.x, tank.y, tank.radius) then
tank.points = tank.points + object.value
object.value = 0
object.sound:play()
end
end
end