-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayer.lua
127 lines (98 loc) · 3.59 KB
/
player.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
-- игрок
Player = {}
Player.__index = Player
function Player:New(speed)
self = setmetatable({}, self)
self:Init()
self.speed = speed
self.delta = 0
return self
end
function Player:Init()
self.x = math.floor(MAX_X/2)
self.y = 1
self.dir = DIR_NO
end
function Player:keypressed(key)
if key == 'down' then
self.dir = DIR_DN
elseif key == 'up' then
self.dir = DIR_UP
elseif key == 'left' then
self.dir = DIR_LT
elseif key == 'right' then
self.dir = DIR_RT
end
end
function Player:draw()
love.graphics.setColor(1, 1, 1)
love.graphics.circle("fill", OFFSET_X + player.x*CELL_SIZE_X - CELL_SIZE_X/2,
OFFSET_Y + player.y*CELL_SIZE_Y - CELL_SIZE_Y/2, CELL_SIZE_X/2)
love.graphics.setColor(.5, 1, .5)
love.graphics.circle("fill", OFFSET_X + player.x*CELL_SIZE_X - CELL_SIZE_X/2,
OFFSET_Y + player.y*CELL_SIZE_Y - CELL_SIZE_Y/2, CELL_SIZE_X/4)
love.graphics.setFont(fontS)
love.graphics.printf({{.5,1,.5,.5},
'Level: '..level..' Lives: '..lives..' Score: '..score..' Filled: '..filledp..'%'},
0, screen.height-fontS:getHeight(), screen.width, 'center')
end
function Player:update(dt)
if self.dir == DIR_NO then return end
self.delta = self.delta + self.speed*dt
if self.delta >= 1 then
field_prev = field[self.x][self.y]
if field[self.x][self.y] == F_NONE then
field[self.x][self.y] = F_TRACE
end
if self.dir == DIR_DN and self.y < MAX_Y then
self.y = self.y + 1
elseif self.dir == DIR_UP and self.y > 1 then
self.y = self.y - 1
elseif self.dir == DIR_RT and self.x < MAX_X then
self.x = self.x + 1
elseif self.dir == DIR_LT and self.x > 1 then
self.x = self.x - 1
end
self.delta = self.delta - 1
if field[self.x][self.y] == F_TRACE then
game:Over()
elseif field[self.x][self.y] == F_FILL and field_prev == F_NONE then
-- завершили линию, остановка
self.dx, self.dy, self.delta = 0, 0, 0
self.dir = DIR_NO
-- заливка и подсчёт очков
for _, dot in pairs(outdots) do
fill(dot.x, dot.y, F_NONE, F_REST)
end
for i = 1, MAX_X do
for j = 1, MAX_Y do
if field[i][j] == F_REST then
field[i][j] = F_NONE
elseif field[i][j] == F_NONE or field[i][j] == F_TRACE then
field[i][j] = F_FILL
score = score + 1
filled = filled + 1
end
end
end
-- percent filled
filledp = math.floor( .5 + filled * 100 / ((MAX_X - 4) * (MAX_Y - 4)) )
if filledp >= 75 then
sound_levelup:play()
Dialog:New('LEVEL', ' UP!!!')
level = level + 1
lives = lives + 1
game:make_field(level)
end
end
end
end
-- функция заливки, начинает с координат x,y и заменяет в области цвет bg на fil
function fill(x, y, bg, fil)
if field[x][y] ~= bg then return end
field[x][y] = fil
if x < MAX_X then fill(x+1, y, bg, fil) end
if x > 1 then fill(x-1, y, bg, fil) end
if y < MAX_Y then fill(x, y+1, bg, fil) end
if y > 1 then fill(x, y-1, bg, fil) end
end