-
Notifications
You must be signed in to change notification settings - Fork 6
/
Board.kt
241 lines (174 loc) · 5.14 KB
/
Board.kt
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
230
231
232
233
234
235
236
237
238
239
240
241
package com.zetcode
import java.awt.*
import java.awt.event.ActionEvent
import java.awt.event.ActionListener
import java.awt.event.KeyAdapter
import java.awt.event.KeyEvent
import javax.swing.ImageIcon
import javax.swing.JPanel
import javax.swing.Timer
class Board : JPanel(), ActionListener {
private val boardWidth = 300
private val boardHeight = 300
private val dotSize = 10
private val allDots = 900
private val randPos = 29
private val delay = 140
private val x = IntArray(allDots)
private val y = IntArray(allDots)
private var nOfDots: Int = 0
private var appleX: Int = 0
private var appleY: Int = 0
private var leftDirection = false
private var rightDirection = true
private var upDirection = false
private var downDirection = false
private var inGame = true
private var timer: Timer? = null
private var ball: Image? = null
private var apple: Image? = null
private var head: Image? = null
init {
addKeyListener(TAdapter())
background = Color.black
isFocusable = true
preferredSize = Dimension(boardWidth, boardHeight)
loadImages()
initGame()
}
private fun loadImages() {
val iid = ImageIcon("src/main/resources/dot.png")
ball = iid.image
val iia = ImageIcon("src/main/resources/apple.png")
apple = iia.image
val iih = ImageIcon("src/main/resources/head.png")
head = iih.image
}
private fun initGame() {
nOfDots = 3
for (z in 0 until nOfDots) {
x[z] = 50 - z * 10
y[z] = 50
}
locateApple()
timer = Timer(delay, this)
timer!!.start()
}
public override fun paintComponent(g: Graphics) {
super.paintComponent(g)
doDrawing(g)
}
private fun doDrawing(g: Graphics) {
if (inGame) {
g.drawImage(apple, appleX, appleY, this)
for (z in 0 until nOfDots) {
if (z == 0) {
g.drawImage(head, x[z], y[z], this)
} else {
g.drawImage(ball, x[z], y[z], this)
}
}
Toolkit.getDefaultToolkit().sync()
} else {
gameOver(g)
}
}
private fun gameOver(g: Graphics) {
val msg = "Game Over"
val small = Font("Helvetica", Font.BOLD, 14)
val fontMetrics = getFontMetrics(small)
val rh = RenderingHints(
RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON)
rh[RenderingHints.KEY_RENDERING] = RenderingHints.VALUE_RENDER_QUALITY
(g as Graphics2D).setRenderingHints(rh)
g.color = Color.white
g.font = small
g.drawString(msg, (boardWidth - fontMetrics.stringWidth(msg)) / 2, boardHeight / 2)
}
private fun checkApple() {
if (x[0] == appleX && y[0] == appleY) {
nOfDots++
locateApple()
}
}
private fun move() {
for (z in nOfDots downTo 1) {
x[z] = x[z - 1]
y[z] = y[z - 1]
}
if (leftDirection) {
x[0] -= dotSize
}
if (rightDirection) {
x[0] += dotSize
}
if (upDirection) {
y[0] -= dotSize
}
if (downDirection) {
y[0] += dotSize
}
}
private fun checkCollision() {
for (z in nOfDots downTo 1) {
if (z > 4 && x[0] == x[z] && y[0] == y[z]) {
inGame = false
}
}
if (y[0] >= boardHeight) {
inGame = false
}
if (y[0] < 0) {
inGame = false
}
if (x[0] >= boardWidth) {
inGame = false
}
if (x[0] < 0) {
inGame = false
}
if (!inGame) {
timer!!.stop()
}
}
private fun locateApple() {
var r = (Math.random() * randPos).toInt()
appleX = r * dotSize
r = (Math.random() * randPos).toInt()
appleY = r * dotSize
}
override fun actionPerformed(e: ActionEvent) {
if (inGame) {
checkApple()
checkCollision()
move()
}
repaint()
}
private inner class TAdapter : KeyAdapter() {
override fun keyPressed(e: KeyEvent?) {
val key = e!!.keyCode
if (key == KeyEvent.VK_LEFT && !rightDirection) {
leftDirection = true
upDirection = false
downDirection = false
}
if (key == KeyEvent.VK_RIGHT && !leftDirection) {
rightDirection = true
upDirection = false
downDirection = false
}
if (key == KeyEvent.VK_UP && !downDirection) {
upDirection = true
rightDirection = false
leftDirection = false
}
if (key == KeyEvent.VK_DOWN && !upDirection) {
downDirection = true
rightDirection = false
leftDirection = false
}
}
}
}