-
Notifications
You must be signed in to change notification settings - Fork 0
/
hellocoffee.coffee
177 lines (154 loc) · 5.74 KB
/
hellocoffee.coffee
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
#Игра змейка, массив змеи - массив координат блоков игрового поля
pointIndicator = null
points = 0
gameOverIndicator = null
newGame = null
fieldWidth = 80
fieldHeight = 30
gameField = null
startX = 40
startY = 15
caneat = false
#направления движения
LEFT = 1
UP = 2
RIGHT = 3
DOWN = 4
direction = RIGHT
snake = [[startX, startY], [startX - 1, startY], [startX - 2, startY]]
xeatcoord = null
yeatcoord = null
go = null
#создание игрового поля
createGameField = ->
table = document.createElement("table")
table.id = "snake"
thead = document.createElement("thead")
thead.appendChild row = document.createElement("tr")
for [0...3]
row.appendChild th = document.createElement("th")
if !_i
th.colSpan = 20
pointIndicator = document.createElement("span");
pointIndicator.innerHTML = "POINTS: " + 0
th.appendChild pointIndicator
else if _i == 1
th.colSpan = 40
newGame = document.createElement("span")
newGame.innerHTML = "START"
newGame.style.cursor = "pointer"
th.appendChild newGame
newGame.onclick = -> start()
else
gameOverIndicator = document.createElement("span")
gameOverIndicator.style.color = "red"
th.appendChild gameOverIndicator
th.colSpan = 20
gameField = tbody = document.createElement("tbody")
for [0...fieldHeight]
tbody.appendChild row = document.createElement("tr")
for [0...fieldWidth]
row.appendChild td = document.createElement("td")
table.appendChild thead
table.appendChild tbody
document.body.appendChild table
#обработчик смены неправления движения
document.body.onkeydown =(e)->
e = e || window.event
switch e.keyCode
when 37 then if direction != RIGHT then direction = LEFT
when 38 then if direction != DOWN then direction = UP
when 39 then if direction != LEFT then direction = RIGHT
when 40 then if direction != UP then direction = DOWN
move =->
canmove = false
switch direction
when LEFT then canmove = leftMovePossible()
when RIGHT then canmove = rightMovePossible()
when DOWN then canmove = downMovePossible()
when UP then canmove = upMovePossible()
if canmove
if caneat
grow()
caneat = false
placeEat()
else
moveSnake()
else
endGame()
leftMovePossible = ->
if snake[0][0] - 1 < 0 or gameField.rows[snake[0][1]].cells[snake[0][0] - 1].className == "snakeBody" then return false
if gameField.rows[snake[0][1]].cells[snake[0][0] - 1].className == "eat" then caneat = true
return true
rightMovePossible = ->
if snake[0][0] + 1 == fieldWidth or gameField.rows[snake[0][1]].cells[snake[0][0] + 1].className == "snakeBody" then return false
if gameField.rows[snake[0][1]].cells[snake[0][0] + 1].className == "eat" then caneat = true
return true
downMovePossible = ->
if snake[0][1] + 1 == fieldHeight or gameField.rows[snake[0][1] + 1].cells[snake[0][0]].className == "snakeBody" then return false
if gameField.rows[snake[0][1] + 1].cells[snake[0][0]].className == "eat" then caneat = true
return true
upMovePossible = ->
if snake[0][1] - 1 < 0 or gameField.rows[snake[0][1] - 1].cells[snake[0][0]].className == "snakeBody" then return false
if gameField.rows[snake[0][1] - 1].cells[snake[0][0]].className == "eat" then caneat = true
return true
moveSnake = ->
#стерли змею
for [0...snake.length]
gameField.rows[snake[_i][1]].cells[snake[_i][0]].className = ""
snake.pop()
snake.unshift([snake[0][0], snake[0][1]])
switch direction
when LEFT then snake[0][0] -= 1
when RIGHT then snake[0][0] += 1
when DOWN then snake[0][1] += 1
when UP then snake[0][1] -= 1
#нарисовали змею
for [0...snake.length]
gameField.rows[snake[_j][1]].cells[snake[_j][0]].className = "snakeBody"
grow = ->
snake.unshift([snake[0][0], snake[0][1]])
switch direction
when LEFT then snake[0][0] -= 1
when RIGHT then snake[0][0] += 1
when DOWN then snake[0][1] += 1
when UP then snake[0][1] -= 1
gameField.rows[snake[0][1]].cells[snake[0][0]].className = "snakeBody"
pointIndicator.innerHTML = "POINTS: " + ++points
placeEat = ->
xeatcoord = getRandNum(0, fieldWidth - 1)
yeatcoord = getRandNum(0, fieldHeight - 1)
while gameField.rows[yeatcoord].cells[xeatcoord].className == "snakeBody"
xeatcoord = getRandNum(0, fieldWidth - 1)
yeatcoord = getRandNum(0, fieldHeight - 1)
gameField.rows[yeatcoord].cells[xeatcoord].className = "eat"
endGame = ->
gameOverIndicator.innerHTML = "YOU LOOSE"
clearInterval(go)
getRandNum =(min, max)->
rand = min - 0.5 + Math.random()*(max-min+1);
return Math.round(rand);
#создали поле
createGameField()
start = ->
#если игра уже идет, или закончилась, очистим поле, убьем таймер
#очистим змею
for [0...snake.length]
gameField.rows[snake[_i][1]].cells[snake[_i][0]].className = ""
#начальное положение змея
snake = [[startX, startY], [startX - 1, startY], [startX - 2, startY]]
clearInterval(go)
if xeatcoord != null
gameField.rows[yeatcoord].cells[xeatcoord].className = ""
direction = RIGHT
pointIndicator.innerHTML = "POINTS: " + 0
points = 0;
gameOverIndicator.innerHTML = ""
#положили змею
for [0...snake.length]
gameField.rows[snake[_j][1]].cells[snake[_j][0]].className = "snakeBody"
#положили еду
placeEat()
#запустили змею
go = setInterval(move, 100)
start()