This repository was archived by the owner on Apr 30, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSnake.lua
More file actions
229 lines (211 loc) · 6.09 KB
/
Copy pathSnake.lua
File metadata and controls
229 lines (211 loc) · 6.09 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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
g_ScriptTitle = "Snake"
g_ScriptInfo = "Snake from the K-Client.\nUsage: Arrowkeys"
-- global vars
Size = 15
-- arena vars
MainQuad = {}
Quads = {}
QuadPos = {}
QuadSize = 50
LineSize = 5
-- snake vars
SnakeSize = 4
SnakePos = {}
Posx = 128
Posy = 128
Snake = {}
Mode = 1 -- 1 = right, 2 = left, 3 = up and 4 = down
SnakeTime = Game.Client.LocalTime+0.2
Apple = {}
ApplePos = {}
IsFullscreen = false -- Actually dont use this var ;)
function OnScriptInit()
EnterFullscreen()
CreateArena()
CreateSnake()
CreateApple()
-- if not IsFullscreen then error("Snake is only playable in the fullscreen mode!") end
return true
end
function OnEnterFullscreen()
IsFullscreen = true
print("enter fullscreen")
end
function OnExitFullscreen()
IsFullscreen = false
print("exit fullscreen")
end
function CreateArena()
local x = 0-Size/2
local y = 1-Size/2
while (true) do
if x < Size/2 then
table.insert(Quads,QuadItem(Engine.Graphics.ScreenWidth/2+(x*QuadSize)+LineSize*x,Engine.Graphics.ScreenHeight/2+(y*QuadSize)+LineSize*y,QuadSize,QuadSize))
table.insert(QuadPos,vec2f(Engine.Graphics.ScreenWidth/2+(x*QuadSize)+LineSize*x,Engine.Graphics.ScreenHeight/2+(y*QuadSize)+LineSize*y))
x = x+1
else
if y < Size/2 then
y=y+1
x = 0-Size/2
else
table.insert(MainQuad,QuadItem(Engine.Graphics.ScreenWidth/2-QuadSize/2-LineSize/2,Engine.Graphics.ScreenHeight/2+QuadSize/2+LineSize/2,QuadSize*(Size+1)-Size/2+LineSize*Size/2,QuadSize*(Size+1)+Size/2+LineSize/2*Size/2))
break
end
end
end
end
function CreateSnake()
for i = 1, SnakeSize do
Posx = Posx+1
table.insert(SnakePos,vec2f(QuadPos[Posx].x,QuadPos[Posy].y))
table.insert(Snake,QuadItem(SnakePos[#SnakePos].x,SnakePos[#SnakePos].y,QuadSize,QuadSize))
end
end
function SnakeMove()
if Mode == 1 then
Posx = Posx+1
if Posx == 15 or Posx == 30 or Posx == 45 or Posx == 60 or Posx == 75 or Posx == 90 or Posx == 105 or Posx == 120 or Posx == 135 or Posx == 150 or Posx == 165 or Posx == 180 or Posx == 210 or Posx == 225 then
Posx = Posx-15
end
table.remove(SnakePos,1)
table.insert(SnakePos,vec2f(QuadPos[Posx].x,QuadPos[Posy].y))
table.remove(Snake,1)
table.insert(Snake,QuadItem(SnakePos[#SnakePos].x,SnakePos[#SnakePos].y,QuadSize,QuadSize))
elseif Mode == 2 then
Posx = Posx-1
if Posx == 1 or Posx == 16 or Posx == 31 or Posx == 46 or Posx == 61 or Posx == 76 or Posx == 91 or Posx == 106 or Posx == 121 or Posx == 136 or Posx == 151 or Posx == 166 or Posx == 181 or Posx == 211 or Posx == 226 then
Posx = Posx+15
end
table.remove(SnakePos,1)
table.insert(SnakePos,vec2f(QuadPos[Posx].x,QuadPos[Posy].y))
table.remove(Snake,1)
table.insert(Snake,QuadItem(SnakePos[#SnakePos].x,SnakePos[#SnakePos].y,QuadSize,QuadSize))
elseif Mode == 3 then
Posy = Posy-15
if Posy <= 1 then
Posy = Posy+15*15
end
table.remove(SnakePos,1)
table.insert(SnakePos,vec2f(QuadPos[Posx].x,QuadPos[Posy].y))
table.remove(Snake,1)
table.insert(Snake,QuadItem(SnakePos[#SnakePos].x,SnakePos[#SnakePos].y,QuadSize,QuadSize))
else
Posy = Posy+15
if Posy >= 15*15-1 then
Posy = Posy-15*15
end
table.remove(SnakePos,1)
table.insert(SnakePos,vec2f(QuadPos[Posx].x,QuadPos[Posy].y))
table.remove(Snake,1)
table.insert(Snake,QuadItem(SnakePos[#SnakePos].x,SnakePos[#SnakePos].y,QuadSize,QuadSize))
end
for i=1,#SnakePos-1 do
if SnakePos[#SnakePos] == SnakePos[i] then
RestartSnake()
end
end
if SnakePos[#SnakePos] == ApplePos[1] then
table.remove(Apple,1)
table.remove(ApplePos,1)
CreateApple()
AddScore(1)
end
end
function DrawArena()
if not IsFullscreen then return end
if Game.Client.LocalTime > SnakeTime then
SnakeMove()
SnakeTime = Game.Client.LocalTime+0.2
end
Engine.Graphics:TextureSet(-1)
Engine.Graphics:MapScreen(0,0,Engine.Graphics.ScreenWidth,Engine.Graphics.ScreenHeight)
Engine.Graphics:QuadsBegin()
Engine.Graphics:SetColor(0.9,0.4,0,1)
Engine.Graphics:QuadsDraw(MainQuad)
Engine.Graphics:QuadsEnd()
Engine.Graphics:QuadsBegin()
Engine.Graphics:SetColor(0.8,0.3,0,1)
Engine.Graphics:QuadsDraw(Quads)
Engine.Graphics:QuadsEnd()
Engine.Graphics:QuadsBegin()
Engine.Graphics:SetColor(1,0.9,0.8,1)
Engine.Graphics:QuadsDraw(Snake)
Engine.Graphics:QuadsEnd()
Engine.Graphics:QuadsBegin()
Engine.Graphics:SetColor(0.7,0.9,0.4,1)
Engine.Graphics:QuadsDraw(Apple)
Engine.Graphics:QuadsEnd()
Engine.Graphics:QuadsBegin()
Engine.Graphics:SetColor(0.9,0.2,0.2,0.4)
Engine.Graphics:QuadsDraw({Snake[#Snake]})
Engine.Graphics:QuadsEnd()
local d = ""
if Mode == 1 then -- right
d = "→"
elseif Mode == 2 then -- left
d = "←"
elseif Mode == 3 then -- up
d = "↑"
elseif Mode == 4 then -- down
d = "↓"
end
Game.Ui:DoLabelScaled(UIRect(SnakePos[#SnakePos].x-QuadSize/2,SnakePos[#SnakePos].y-12,QuadSize,QuadSize), d, 20, 0, -1, nil, 0)
end
function CreateApple()
local CanApple = true
if #Apple < 1 then
local rand = math.random(1,#QuadPos)
for i=1, #SnakePos do
if SnakePos[i].x == QuadPos[rand].x and SnakePos[i].y == QuadPos[rand].y then
CanApple = false
end
end
if CanApple then
table.insert(Apple,QuadItem(QuadPos[rand].x,QuadPos[rand].y,QuadSize,QuadSize))
table.insert(ApplePos,vec2f(QuadPos[rand].x,QuadPos[rand].y))
else
CreateApple()
end
end
end
function OnKeyPress(Key)
if Key == "right" or Key == "d" then
if Mode ~= 2 then
Mode = 1
end
elseif Key == "left" or Key == "a" then
if Mode ~= 1 then
Mode = 2
end
elseif Key == "up" or Key == "w" and Mode ~= 4 then
if Mode ~= 4 then
Mode = 3
end
elseif Key == "down" or Key == "s" and Mode ~= 3 then
if Mode ~= 3 then
Mode = 4
end
end
end
function AddScore(num)
SnakeSize = SnakeSize+1
AddSnake()
end
function RestartSnake()
SnakeSize = 4
SnakePos = {}
Posx = 128
Posy = 128
Snake = {}
Mode = 1 -- 1 = right, 2 = left, 3 = up and 4 = down
SnakeTime = Game.Client.LocalTime+0.2
Apple = {}
ApplePos = {}
CreateSnake()
CreateApple()
end
function AddSnake()
table.insert(SnakePos, 1, vec2f(QuadPos[Posx].x,QuadPos[Posy].y))
table.insert(Snake, 1, QuadItem(SnakePos[#SnakePos].x,SnakePos[#SnakePos].y,QuadSize,QuadSize))
end
RegisterEvent("OnRenderLevel22","DrawArena")