-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.lua
More file actions
177 lines (136 loc) · 3.95 KB
/
game.lua
File metadata and controls
177 lines (136 loc) · 3.95 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
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
require("background")
require("gameover")
require("obstacle")
require("player")
require("word")
Gamestate = require "lib/hump.gamestate"
game = {}
function game:init()
self.background = Background(loadImage("back1"), loadImage("back2"), loadImage("back3"),
loadImage("back4"))
self.hud_image = loadImage("hud")
self.pozo_image = loadImage("pozo")
self.corazon = loadImage("corazon")
self.font = love.graphics.newFont("assets/data/luximr.ttf", 48)
love.graphics.setFont(self.font)
-- Sonidos
self.jumpsound = love.audio.newSource("assets/audio/jump.ogg", "static")
self.diesound = love.audio.newSource("assets/audio/scream.ogg", "static")
self.music = love.audio.newSource("assets/audio/music.ogg", "stream")
self.obstacle_speed = 200
end
function game:enter()
self.lines = load_file(1)
self.word = get_word(self.lines)
self.player = Player(40, 380, loadImage("player"))
self.obstacle = spawn_obstacle()
self.puntos = 0
self.playedsound = false
end
function game:update(dt)
self.player:update(dt)
self.word:update(dt)
if self.obstacle ~= nil then
self.obstacle:update(dt)
if self.obstacle:isNear() and self.player.state ~= "reset" then
if self.word.status == 1 then
self.player.state = "jumping"
if soundactivated and not playedsound then
game.jumpsound:play()
playedsound = true
end
else
if (self.obstacle.x - 40) <= self.player.x then
if soundactivated and not playedsound then
game.diesound:play()
playedsound = true
end
self.player.state = "falling"
self.obstacle:stop()
end
end
end
if self.player.state == "reset" then
playedsound = false
game.obstacle:continue()
if self.obstacle.x < -190 then
self.obstacle = spawn_obstacle()
self.word = nil
self.word = get_word(self.lines)
self.player.state = "idle"
end
end
end
if self.player.state ~= "falling" then
self.background:update(dt)
self.puntos = self.puntos + dt
local pt = math.floor(self.puntos)
if pt == 20 then
self.lines = load_file(2)
elseif pt == 50 then
self.lines = load_file(3)
end
if pt == 80 then
self.obstacle_speed = 300
elseif pt == 110 then
self.obstacle_speed = 400
elseif pt == 150 then
self.obstacle_speed = 550
elseif pt == 180 then
self.obstacle_speed = 650
end
end
if self.player.vidas <= 0 then
Gamestate.switch(gameover)
end
if soundactivated then
self.music:play()
end
end
function game:draw()
self.background:draw()
self.player:draw()
love.graphics.draw(self.hud_image, 0, 0)
if self.obstacle ~= nil then
self.obstacle:draw()
end
if self.player.vidas == 3 then
love.graphics.draw(self.corazon, 940-140, 33)
love.graphics.draw(self.corazon, 940-70, 33)
love.graphics.draw(self.corazon, 940, 33)
elseif self.player.vidas == 2 then
love.graphics.draw(self.corazon, 940-70, 33)
love.graphics.draw(self.corazon, 940, 33)
else
love.graphics.draw(self.corazon, 940, 33)
end
love.graphics.print(math.floor(self.puntos), 78, 20)
self.word:draw()
end
function game:keypressed(key)
self.word:akey(key)
end
function spawn_obstacle()
return Obstacle(love.graphics.getWidth(), 486, game.obstacle_speed, game.pozo_image)
end
function load_file(level)
local fname = ""
if level == 1 then
fname = "assets/data/facil.txt"
elseif level == 2 then
fname = "assets/data/medio.txt"
elseif level == 3 then
fname = "assets/data/dificil.txt"
end
local file = io.open(fname, "rb")
if file == nil then print("File was nil") end
local ln = {}
for line in io.lines(fname) do
ln[#ln + 1] = line
end
return ln
end
function get_word(list)
local n = math.random(1, table.maxn(game.lines))
return Word(list[n])
end