-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsnakeCals.java
More file actions
274 lines (214 loc) · 6.41 KB
/
snakeCals.java
File metadata and controls
274 lines (214 loc) · 6.41 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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
//package snake;
import java.util.*;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferStrategy;
import java.util.LinkedList;
import java.util.Random;
public class snakeCals implements Runnable,KeyListener{
private Thread thread;
private Display display;
private String title;
private int width,height;
private BufferStrategy buffer;
private Graphics g;
private LinkedList snake;
private int direction = Direction.no_direction ;
//rectangle set up
public static final int WIDTH = 10;
public static final int HEIGHT =10;
public static final int BOX_WIDTH =30;
public static final int BOX_HEIGHT =30;
//Fruit
private Point fruit;
//score
private int score;
private Level2 level2;
public snakeCals(String title,int width,int height){
this.title = title;
this.width = width;
this.height = height ;
}
//this is for window of game
public void init(){
display = new Display(title,width,height);
display.getFrame().addKeyListener(this);
snake = new LinkedList();
level2 = new Level2();
reStart();
randFruit();
}
public void Draw(Graphics g){
DrawRect(g);
DrawSnake(g);
DrawFruit(g);
DrawScore(g);
level2.DrawWall(g);
}
//this is for drawing rectangle
public void DrawRect(Graphics g){
Color color = new Color(179,250,130);
g.setColor(color);
g.fillRect(0, 0, WIDTH*BOX_WIDTH, HEIGHT*BOX_HEIGHT);
g.setColor(Color.red);
}
//This is for drawing snake
private void DrawSnake(Graphics g){
g.setColor(Color.blue);
Iterator itr=snake.iterator();
while(itr.hasNext())
{
Point p=(Point)itr.next();
g.fillRect(p.x*BOX_WIDTH, p.y*BOX_HEIGHT, BOX_WIDTH,BOX_HEIGHT );
}
g.setColor(Color.black);
}
//this is for restarting the snake game
public void reStart(){
direction = Direction.no_direction;
snake.clear();
level2.list.clear();
score = 0;
snake.add(new Point(9,7));
snake.add(new Point(8,7));
snake.add(new Point(7,7));
}
//this is for drawing fruit
public void DrawFruit(Graphics g){
g.setColor(Color.red);
g.fillOval(fruit.x*BOX_WIDTH, fruit.y*BOX_HEIGHT, BOX_WIDTH, BOX_HEIGHT);
g.setColor(Color.BLACK);
}
//this is for drawing Score
private void DrawScore(Graphics g){
g.setColor(Color.red);
g.setFont(new Font("arial",Font.PLAIN,37));
g.drawString("Score = "+score,22,HEIGHT*BOX_HEIGHT+40);
g.setColor(Color.black);
}
//this is creating fruits for snake
public void randFruit(){
Random rand = new Random();
int randX = rand.nextInt(WIDTH);
int randY = rand.nextInt(HEIGHT);
fruit = new Point(randX,randY);
while(snake.contains(fruit) || level2.list.contains(fruit)){
fruit = new Point(randX,randY);
}
}
//this is for direction of snake
public void move(){
Point head =(Point) snake.peekFirst();
Point newPoint = head;
switch(direction){
case Direction.up:
newPoint = new Point(head.x,head.y-1);
break;
case Direction.down:
newPoint = new Point(head.x,head.y+1);
break;
case Direction.right:
newPoint = new Point(head.x+1,head.y);
break;
case Direction.left:
newPoint = new Point(head.x-1,head.y);
break;
}
if(newPoint.x>= WIDTH){
newPoint = new Point(newPoint.x=0,newPoint.y);
}
else if (newPoint.x<0){
newPoint = new Point(newPoint.x=WIDTH-1,newPoint.y);
}
else if(newPoint.y>=HEIGHT){
newPoint = new Point(newPoint.x, newPoint.y=0);
}else if (newPoint.y<0){
newPoint = new Point(newPoint.x,newPoint.y=HEIGHT-1);
}
if(snake.contains(newPoint) || level2.list.contains(newPoint)){
reStart();
return;
}
snake.remove(snake.peekLast());
if(newPoint.equals(fruit) ){
Point add = (Point) newPoint.clone();
score = score + 5;
snake.push(add);
if(score >= 20){
level2.level2();
}
randFruit();
}
snake.push(newPoint);
}
public void render(){
buffer = display.getCanvas().getBufferStrategy();
if(buffer == null ){
display.getCanvas().createBufferStrategy(3);
return;
}
g = buffer.getDrawGraphics();
g.clearRect(0, 0, width, height);
//draw here
Draw(g);
//draw end
buffer.show();
g.dispose();
}
public synchronized void start(){
if(thread ==null){
thread = new Thread(this);
thread.start();
}
}
public synchronized void stop(){
try {
thread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
//this is for starting the game
public void run(){
init();
while(true){
render();
move();
Thread.currentThread();
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
//this is for laptop key
public void keyPressed(KeyEvent e) {
switch(e.getKeyCode()){
case KeyEvent.VK_UP:
if(direction != Direction.down)
direction = Direction.up;
break;
case KeyEvent.VK_DOWN:
if(direction != Direction.up)
direction =Direction.down;
break;
case KeyEvent.VK_LEFT:
if(direction != Direction.right)
direction =Direction.left;
break;
case KeyEvent.VK_RIGHT:
if(direction != Direction.left)
direction = Direction.right;
break;
}
}
public void keyReleased(KeyEvent e) {
}
public void keyTyped(KeyEvent e) {
}
}