-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGame.java
149 lines (128 loc) · 3.17 KB
/
Game.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
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
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.LinkedList;
import javax.swing.JFrame;
public class Game extends JFrame implements KeyListener {
public static int width;
public static int height;
public static int size;
public static int length;
public static int score;
private LinkedList<Snake> tail;
private Snake snake;
private Apple apple;
private boolean ready_to_turn;
private int speed;
public Game(int tile, Speed speed) {
size = 30;
width = size * tile; // 1500
height = tile > 25 ? size * 25 : size * tile; // 720
score = 0;
length = 5;
tail = new LinkedList<Snake>();
for (int i = 0; i < length; i++)
tail.add(new Snake());
snake = new Snake();
apple = new Apple();
setSpeed(speed);
setSize(width + 44, height + 37);
getContentPane().setBackground(Color.BLACK);
setTitle("Score: " + score);
setVisible(true);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
addKeyListener(this);
// addComponentListener(new ComponentAdapter() {
// public void componentResized(ComponentEvent componentEvent) {
// // do stuff
// }
// });
Thread t = new Thread(new Runnable() {
public void run() {
try {
while (true) {
snake.eat(apple, getThis());
tail = snake.updateTail(tail);
snake.move();
snake.teleport();
snake.death(tail);
ready_to_turn = true;
repaint();
Thread.sleep(getSpeed());
}
} catch (InterruptedException e) {
System.out.println(e.getMessage());
}
}
});
t.setPriority(10);
t.start();
}
@Override
public void paint(Graphics g) {
super.paint(g);
g.setColor(Color.RED);
g.fillRect(apple.getX() + 7, apple.getY(), size, size);
for (int i = 0; i < length; i++) {
Snake s = tail.get(i);
g.setColor(Color.GREEN);
g.fillRect(s.getX() + 7, s.getY(), size, size);
}
g.setColor(Color.GREEN);
g.fillRect(snake.getX() + 7, snake.getY(), size, size);
}
@Override
public void keyTyped(KeyEvent e) {}
@Override
public void keyPressed(KeyEvent e) {
int c = e.getKeyCode();
if (c == 37 && snake.getXv() != 1 && ready_to_turn) {
snake.setXv(-1);
snake.setYv(0);
ready_to_turn = false;
}
else if (c == 38 && snake.getYv() != 1 && ready_to_turn) {
snake.setXv(0);
snake.setYv(-1);
ready_to_turn = false;
}
else if (c == 39 && snake.getXv() != -1 && ready_to_turn) {
snake.setXv(1);
snake.setYv(0);
ready_to_turn = false;
}
else if (c == 40 && snake.getYv() != -1 && ready_to_turn) {
snake.setXv(0);
snake.setYv(1);
ready_to_turn = false;
}
}
@Override
public void keyReleased(KeyEvent e) {}
public Game getThis() {
return this;
}
public int getSpeed() {
return speed;
}
public void setSpeed(Speed speed) {
switch (speed) {
case SLOW:
this.speed = 60 / 2 * 3;
break;
case NORMAL:
this.speed = 60;
break;
case FAST:
this.speed = 60 / 2;
}
}
public static void main(String[] args) {
new Game(20, Speed.NORMAL);
}
}
enum Speed {
SLOW, NORMAL, FAST
}