-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.lua
More file actions
149 lines (122 loc) · 3.2 KB
/
game.lua
File metadata and controls
149 lines (122 loc) · 3.2 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
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
require("objects")
require("timer")
require("coin")
require("bonus")
local tank = require("tank")
local Game = {}
local MAP_WIDTH = 15
local MAP_HEIGHT = 8
TILE_WIDTH = 128
TILE_HEIGHT = 135
Game.nbSpawn = 0
Game.Map = {}
Game.Map = {
{0, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 0},
{0, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 6, 0},
{0, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 6, 0},
{0, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 6, 0},
{0, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 6, 0},
{0, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 6, 0},
{0, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 6, 0},
{0, 7, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 9, 0}
}
Game.TileSheet = nil
Game.TileTextures = {}
function Game.spawn()
for i = 1, Game.nbSpawn do
spawnEnemy()
end
end
function Game.addSpawn()
Game.nbSpawn = Game.nbSpawn + 1
end
function Game.addCoin()
newCoin()
end
function Game.addBonus()
newBonus()
end
function Game.load()
initObjects()
tank.init()
Game.TileSheet = love.graphics.newImage("images/tiles/tiles.png")
local nCols = Game.TileSheet:getWidth() / TILE_WIDTH
local nLines = Game.TileSheet:getHeight() / TILE_HEIGHT
Game.Music = love.audio.newSource("Sons/Instrumental.mp3", "stream")
Game.Music:setVolume(0.7)
local l, c
local id = 1
Game.TileTextures[0] = nil
for l = 1, nLines do
for c = 1, nCols do
local texture =
love.graphics.newQuad(
(c - 1) * TILE_WIDTH,
(l - 1) * TILE_HEIGHT,
TILE_WIDTH,
TILE_HEIGHT,
Game.TileSheet:getWidth(),
Game.TileSheet:getHeight()
)
Game.TileTextures[id] = texture
id = id + 1
end
end
-- Si le tank colisionne avec un objet dès le début
-- On le déplace aléatoirement
while checkVehicleCollision(tank) do
tank.x = math.random(MARGIN, SCREEN_WIDTH - MARGIN)
tank.y = math.random(MARGIN, SCREEN_HEIGHT - MARGIN)
end
Game.nbSpawn = 1
newTimer(5, 20, Game.spawn)
newTimer(15, 30, Game.addSpawn)
newTimer(7, 15, Game.addCoin)
newTimer(15, 60, Game.addBonus)
Game.Music:stop()
Game.Music:play()
end
function Game.unload()
razBonus()
razTimers()
razCoins()
end
function Game.update(dt)
updateTimers(dt)
tank.update(dt)
updateEnemies(dt)
updateBullets(dt)
updateObjects(dt)
updateCoins(dt)
end
function Game.draw()
local c, l
for l = 1, MAP_HEIGHT do
for c = 1, MAP_WIDTH do
local id = Game.Map[l][c]
local texQuad = Game.TileTextures[id]
if texQuad ~= nil then
love.graphics.draw(Game.TileSheet, texQuad, ((c - 1) * TILE_WIDTH), ((l - 1) * TILE_HEIGHT))
end
end
end
drawObjects()
drawCoins()
drawBonus()
end
function GetTile(x, y)
local c = math.floor(x / TILE_WIDTH) + 1
local l = math.floor(y / TILE_HEIGHT) + 1
local retour = "Hors du tableau"
if c <= 0 then
c = 1
end
if l <= 0 then
l = 1
end
if c <= MAP_WIDTH and l <= MAP_HEIGHT then
retour = Game.Map[l][c]
end
return retour
end
return Game