-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPreSnakeGame.java
122 lines (107 loc) · 3.61 KB
/
PreSnakeGame.java
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
package algorithms;
import javax.swing.JFrame;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.Timer;
/**
*
* @author Gökhan DAĞTEKİN
*
* 2016
*/
public class PreSnakeGame extends JFrame implements ActionListener, KeyListener {
private int[] x = {10, 10, 10, 10, 10, 10, 10, 10, 10, 10};
private int[] y = {30, 40, 50, 60, 70, 80, 90, 100, 110, 120};
private static final int UP = 0, DOWN = 1, LEFT = 2, RIGHT = 3;
private int direction = DOWN;
private Timer t;
private int xHead = x[x.length - 1], yHead = y[y.length - 1];
public PreSnakeGame() {
setSize(600, 400);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
addKeyListener(this);
t = new Timer(100, this);
t.start();
}
public void actionPerformed(ActionEvent e) {
if (direction == UP) { // W/UP
for (int i = 0; i < y.length - 1; i++) {
y[i] = y[i + 1];
x[i] = x[i + 1];
}
y[y.length - 1] -= 10;
xHead = x[x.length - 1];
yHead = y[y.length - 1];
} else if (direction == DOWN) { // S/DOWN
for (int i = 0; i < y.length - 1; i++) {
y[i] = y[i + 1];
x[i] = x[i + 1];
}
y[y.length - 1] += 10;
xHead = x[x.length - 1];
yHead = y[y.length - 1];
} else if (direction == RIGHT) { // D/RIGHT
for (int i = 0; i < y.length - 1; i++) {
y[i] = y[i + 1];
x[i] = x[i + 1];
}
x[x.length - 1] += 10;
xHead = x[x.length - 1];
yHead = y[y.length - 1];
} else if (direction == LEFT) { // A/LEFT
for (int i = 0; i < y.length - 1; i++) {
y[i] = y[i + 1];
x[i] = x[i + 1];
}
x[x.length - 1] -= 10;
xHead = x[x.length - 1];
yHead = y[y.length - 1];
}
}
public void paint(Graphics g) {
super.paint(g);
repaint();
g.setColor(Color.red);
for (int i = 0; i < x.length; i++) {
g.fillRect(x[i], y[i], 20, 20);
}
try {
Thread.sleep(10);
} catch (InterruptedException ex) {
Logger.getLogger(PreSnakeGame.class.getName()).log(Level.SEVERE, null, ex);
}
}
public static void main(String[] args) {
PreSnakeGame d = new PreSnakeGame();
}
@Override
public void keyTyped(KeyEvent e) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public void keyPressed(KeyEvent e) {
int tus = e.getKeyCode();
if ((tus == KeyEvent.VK_A || tus == KeyEvent.VK_LEFT) && direction != RIGHT) {
direction = LEFT;
}
if ((tus == KeyEvent.VK_D || tus == KeyEvent.VK_RIGHT) && direction != LEFT) {
direction = RIGHT;
}
if ((tus == KeyEvent.VK_S || tus == KeyEvent.VK_DOWN) && direction != UP) {
direction = DOWN;
}
if ((tus == KeyEvent.VK_W || tus == KeyEvent.VK_UP) && direction != DOWN) {
direction = UP;
}
}
@Override
public void keyReleased(KeyEvent e) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
}