@@ -20,80 +20,32 @@ class Circle {
20
20
this .color = color ;
21
21
this .lastPos .setLocation (x - this .speed .x , y - this .speed .y );
22
22
}
23
-
24
- Circle (Circle c ){
25
- this .pos .setLocation (c .pos .x , c .pos .y );
26
- this .radius = c .radius ;
27
- this .color = c .color ;
28
- this .lastPos .setLocation (c .lastPos .x , c .lastPos .y );
29
- }
30
23
void draw (Graphics g ) {
31
24
g .setColor (this .color );
32
25
g .fillOval (this .pos .x - this .radius , this .pos .y - this .radius , this .radius * 2 , this .radius * 2 );
33
- //g.setColor(this.color);
34
26
g .setColor (Color .BLACK );
35
27
g .drawOval (this .pos .x - this .radius , this .pos .y - this .radius , this .radius * 2 , this .radius * 2 );
36
28
}
37
- void setPos (int x , int y ){
38
- this .lastPos .x = this .pos .x ;
39
- this .lastPos .y = this .pos .y ;
40
- this .pos .x = x ;
41
- this .pos .y = y ;
42
- }
43
-
44
29
boolean isOutOfBounds (){
45
30
int x = this .pos .x ;
46
31
int y = this .pos .y ;
47
32
48
33
if (x > Snake .PLAY_AREA_X - 5 || x < 0 + 10 || y > Snake .PLAY_AREA_Y - 5 || y <= 0 + 5 ) return true ;
49
34
return false ;
50
35
}
51
-
52
- void outOfBoundsRePos (){
53
- if (this .pos .x > 400 - (this .radius * 2 ))
54
- this .pos .x = 0 + (this .radius * 2 );
55
- else if (this .pos .y > 400 - (this .radius * 2 )){
56
- this .pos .y = 0 + (this .radius * 2 );
57
- }
58
- else if (this .pos .y < 0 + (this .radius * 2 ))
59
- this .pos .y = 400 - (this .radius * 2 );
60
- else if (this .pos .x < 0 + (this .radius * 2 ))
61
- this .pos .x = 400 - (this .radius * 2 );
62
- }
63
36
int getX (){
64
37
return this .pos .x ;
65
38
}
66
39
int getY () {
67
40
return this .pos .y ;
68
41
}
69
- Point getPos (){
70
- return this .pos ;
71
- }
72
- Point getLastPos (){
73
- return this .lastPos ;
74
- }
75
- // 1. load direction into setNextDir;
76
- // 2. setNextDir loads into currentDir
77
- // 3. On UPdate, move head in Dir direction, reset currentDir;
78
- //
79
- String getDir (){
80
- if (this .speed .y == -10 ) return "UP" ;
81
- else if (this .speed .y == 10 ) return "DOWN" ;
82
- else if (this .speed .x == -10 ) return "LEFT" ;
83
- else if (this .speed .x == 10 ) return "RIGHT" ;
84
- return "NA" ;
85
- }
86
- void stop (){
87
- this .speed .setLocation (0 , 0 );
88
- }
89
-
90
-
91
-
42
+
92
43
} // end circle
93
44
///////////////////////////////////////////////////////////////////////////////////////////
94
- //BODY//
95
45
96
46
47
+
48
+ //BODY//
97
49
class Body extends ArrayList <Circle >{
98
50
//instance variables
99
51
Circle head ;
@@ -102,7 +54,6 @@ class Body extends ArrayList<Circle>{
102
54
103
55
//constructors
104
56
public Body (int radius , Color color , int length ){
105
- this .bodyColor = bodyColor ;
106
57
this .head = new Circle ((Snake .PLAY_AREA_X /2 ) + 15 , (Snake .PLAY_AREA_Y /2 ) + 15 , radius , color );
107
58
this .add (head );
108
59
fillWithCircles (length );
@@ -112,224 +63,121 @@ public Body(int radius, Color color, int length){
112
63
113
64
//methods
114
65
115
- //fillWithCircles(int numOfCircles)
116
- //fills this (the Body) with a number of circles, and sets their locations such that they correctly stack vertically
117
- //Used at the begining of the game and that is all
118
- void fillWithCircles (int numOfCircles ) {
119
- for (int i = 1 ; i < numOfCircles + 1 ; i ++) {
120
- this .add (new Circle (this .get (i - 1 ).pos .x , this .get (i - 1 ).pos .y + this .get (i - 1 ).radius * 2 , head .radius , bodyColor ));
66
+ /**
67
+ * fills this (the Body) with a number of circles, and sets their locations such that they correctly stack vertically
68
+ * Used at the begining of the game and that is all
69
+ * @param numOfCircles the number of circles to fill the body with
70
+ */
71
+ void fillWithCircles (int numOfCircles ) {
72
+ for (int i = 1 ; i < numOfCircles + 1 ; i ++) {
73
+ this .add (new Circle (this .get (i - 1 ).pos .x , this .get (i - 1 ).pos .y + this .get (i - 1 ).radius * 2 , head .radius , bodyColor ));
74
+ }
121
75
}
122
- }//end fillWithCircles
123
-
124
-
125
- //addCircle
126
- void addCircle (){
127
- this .add (new Circle (this .get (this .size () - 1 ).pos .x , this .get (this .size () - 1 ).pos .y , head .radius , bodyColor ));
128
- }
129
76
130
- //addCircle
131
- void addCircle (int num ){
132
- for (int i = 0 ; i < num ; i ++){
77
+ /**
78
+ * add circle to the end of the body
79
+ */
80
+ void addCircle (){
133
81
this .add (new Circle (this .get (this .size () - 1 ).pos .x , this .get (this .size () - 1 ).pos .y , head .radius , bodyColor ));
134
82
}
135
- } //end addCircle
83
+
84
+ /**
85
+ * Add multiple circles to the snake
86
+ * @param num the number of circles to add
87
+ */
88
+ void addCircle (int num ){
89
+ for (int i = 0 ; i < num ; i ++){
90
+ this .add (new Circle (this .get (this .size () - 1 ).pos .x , this .get (this .size () - 1 ).pos .y , head .radius , bodyColor ));
91
+ }
92
+ }
136
93
137
94
138
95
//updatePos(): updates the position of the snake, by adding a new circle to the front of the
139
- //body, and removing one from the back.
96
+ //body, and removing one from the back.
97
+
98
+ /**
99
+ * Updates the position of the snake.
100
+ * This is done by removing moving the tail of the snake, and appending a new head
101
+ */
102
+ void updatePos (){
103
+ Circle ref = this .get (0 );
104
+ this .add (0 , new Circle (ref .getX () + KeyboardHandler .speed .x , ref .getY () + KeyboardHandler .speed .y , ref .radius , ref .color ));
105
+ this .remove (this .get (this .size () -1 ));
106
+ }
140
107
141
- void updatePos (){
142
- Circle ref = this .get (0 );
143
- this .add (0 , new Circle (ref .getX () + Globals .speed .x , ref .getY () + Globals .speed .y , ref .radius , ref .color ));
144
- this .remove (this .get (this .size () -1 ));
145
- }//end updatePos()
108
+ /**
109
+ * Exact same as updatePos except with a little more flavor
110
+ */
111
+ void rainbowUpdatePos (){
112
+ Random randy = new Random ();
113
+ Circle ref = this .get (0 );
114
+ this .add (0 , new Circle (ref .getX () + KeyboardHandler .speed .x ,
115
+ ref .getY () + KeyboardHandler .speed .y ,
116
+ ref .radius ,
117
+ new Color (randy .nextInt (255 ),
118
+ randy .nextInt (255 ), randy .nextInt (255 ))));
119
+ this .remove (this .get (this .size () -1 ));
120
+ }
146
121
147
- //rainbowUpdatePos();
148
- //this updates the position of the body, the same as updatePos(), but, each new circle it creates has a random
149
- //color
150
- void rainbowUpdatePos (){
151
- Random randy = new Random ();
152
- Circle ref = this .get (0 );
153
- //this.add(0, new Circle(ref.getX() + Globals.speed.x, ref.getY() + Globals.speed.y, ref.radius, ref.color));
154
- //random rainbow colors
155
- this .add (0 , new Circle (ref .getX () + Globals .speed .x , ref .getY () + Globals .speed .y , ref .radius , new Color (randy .nextInt (255 ),
156
- randy .nextInt (255 ), randy .nextInt (255 ))));
157
122
158
- this .remove (this .get (this .size () -1 ));
159
- }
160
-
161
-
162
- //headOverlappingBody
123
+ //headOverlappingBody
163
124
//
164
125
//checks to see if the 0th element in the list is in the same location as the ith element
165
126
// in the list. If it is it removes the ith element, so that the redHead() method (turing the
166
127
// head of the body red, is visible
167
- boolean headOverlappingBody (){
168
- for (int i = 1 ; i < this .size (); i ++)
169
- if (this .get (0 ).pos .equals (this .get (i ).pos )){
170
- //this.remove(i);
171
- this .get (i ).pos = new Point (Integer .MAX_VALUE , Integer .MAX_VALUE );
172
- return true ;
173
- }
174
- return false ;
175
- }//end headOverlappingBody
176
-
177
- void redHead (){
178
- this .get (0 ).color = Color .RED ;
179
- }
180
- void blackHead (){
181
- this .get (0 ).color = Color .BLACK ;
182
- }
183
- void draw (Graphics g ){
184
- for (int i = 0 ; i < this .size (); i ++) this .get (i ).draw (g );
185
- }
186
- } // end circle array
187
128
188
- class Food extends Circle {
189
- Body body ;
190
- Food (Color color , Body b ){
191
- super (0 , 0 , b .head .radius , color );
192
- this .moveToRandomPos ();
193
- this .body = b ;
194
- //test trying to work out a bug where the food spawns in the lower left hand corner
195
- this .pos = new Point (0 , 400 );
196
- while (this .isOutOfBounds ()) moveToRandomPos ();
197
- }
198
- boolean isOverlappingHead (Circle h ){
199
- if (this .pos .equals (h .pos )) return true ;
200
- return false ;
201
- }
202
- void moveToRandomPos (){
203
- Random randy = new Random ();
204
- int x = randy .nextInt (Snake .PLAY_AREA_X / 10 );
205
- int y = randy .nextInt (Snake .PLAY_AREA_Y / 10 );
206
- this .pos .setLocation ((x * 10 ) + 15 , (y * 10 ) + 15 );
207
- while (this .isOutOfBounds ()) moveToRandomPos ();
208
- //if(this.hittingBody())
129
+ /**
130
+ * @return returns true if the head is colliding with the body
131
+ */
132
+ boolean headOverlappingBody (){
133
+ for (int i = 1 ; i < this .size (); i ++)
134
+ if (this .get (0 ).pos .equals (this .get (i ).pos )){
135
+ this .get (i ).pos = new Point (Integer .MAX_VALUE , Integer .MAX_VALUE );
136
+ return true ;
137
+ }
138
+ return false ;
209
139
}
210
-
211
- boolean hittingBody (){
212
- for (int i = 0 ; i < this .body .size (); i ++){
213
- if (body .get (i ).pos .equals (this .pos )) return true ;
214
- }
215
- return false ;
140
+ void redHead (){
141
+ this .get (0 ).color = Color .RED ;
216
142
}
217
-
218
- }
219
-
220
- class Globals {
221
- public static Point speed = new Point (0 , 0 );
222
- public static int keyOpp = 0 ;
223
- static Dir [] dirls = new Dir [2 ];
224
- public static String saveSpeed ;
225
-
226
-
227
- //add(Dir d)
228
- //adds a Dir d to the first non-null position in dirls
229
- static void add (Dir d ){
230
- for (int i = 0 ; i < dirls .length ; i ++){
231
- if (dirls [i ] == null ){
232
- dirls [i ] = d ;
233
- break ;
234
- }
235
- }
236
- }
237
-
238
- // remove(int i)
239
- // removes the ith element from dirls
240
- static void remove (int i ){
241
- dirls [i ] = null ;
242
- }
243
-
244
- //chopHead()
245
- // removes the 0th element of the dirls Array, and moves the 1th element to its place, replacing the 1th
246
- // element with null
247
- static void chopHead (){
248
- dirls [0 ] = dirls [1 ];
249
- dirls [1 ] = null ;
250
- }
251
- public static enum Dir {
252
- UP ("UP" ), DOWN ("DOWN" ), LEFT ("LEFT" ), RIGHT ("RIGHT" ), STOP ("STOP" );
253
- String value ;
254
- private Dir (String v ){
255
- value = v ;
143
+ void blackHead (){
144
+ this .get (0 ).color = Color .BLACK ;
256
145
}
257
- }
258
-
259
- public static void setDir (Dir d ){
260
- keyOpp ++;
261
- if (d == Dir .UP ){
262
- if (Globals .getDir () != Dir .DOWN )
263
- speed .setLocation (0 , -10 );
146
+ void draw (Graphics g ){
147
+ for (int i = 0 ; i < this .size (); i ++) this .get (i ).draw (g );
264
148
}
149
+ } // end circle array
265
150
266
- else if (d == Dir .DOWN ){
267
- if (Globals .getDir () != Dir .UP )
268
- speed .setLocation (0 , 10 );
151
+ class Food extends Circle {
152
+ Body body ;
153
+ Food (Color color , Body b ){
154
+ super (0 , 0 , b .head .radius , color );
155
+ this .moveToRandomPos ();
156
+ this .body = b ;
157
+ this .pos = new Point (0 , 400 );
158
+ while (this .isOutOfBounds ()) moveToRandomPos ();
269
159
}
270
-
271
- else if (d == Dir .LEFT ) {
272
- if (Globals .getDir () != Dir .RIGHT )
273
- speed .setLocation (-10 , 0 );
160
+ boolean isOverlappingHead (Circle h ){
161
+ return this .pos .equals (h .pos );
274
162
}
275
163
276
- else if (d == Dir .RIGHT ) {
277
- if (Globals .getDir () != Dir .LEFT )
278
- speed .setLocation (10 , 0 );
164
+ /**
165
+ * Move the food to a random position
166
+ */
167
+ void moveToRandomPos (){
168
+ Random randy = new Random ();
169
+ int x = randy .nextInt (Snake .PLAY_AREA_X / 10 );
170
+ int y = randy .nextInt (Snake .PLAY_AREA_Y / 10 );
171
+ this .pos .setLocation ((x * 10 ) + 15 , (y * 10 ) + 15 );
172
+ while (this .isOutOfBounds ()) moveToRandomPos ();
279
173
}
280
174
281
- else if (d == Dir .STOP ) speed .setLocation (0 , 0 );
282
- }
283
- public static Dir getDir (){
284
- if (speed .equals (new Point (0 , -10 ))) return Dir .UP ;
285
- else if (speed .equals (new Point (0 , 10 ))) return Dir .DOWN ;
286
- else if (speed .equals (new Point (-10 , 0 ))) return Dir .LEFT ;
287
- else if (speed .equals (new Point (10 , 0 ))) return Dir .RIGHT ;
288
- return Dir .STOP ;
289
- }
290
- public static Point getSpeed (){
291
- if (speed .equals (new Point (0 , -10 ))) return new Point (0 , -10 );
292
- else if (speed .equals (new Point (0 , 10 ))) return new Point (0 , 10 );
293
- else if (speed .equals (new Point (-10 , 0 ))) return new Point (-10 , 0 );
294
- else if (speed .equals (new Point (10 , 0 ))) return new Point (10 , 0 );
295
- return new Point (0 , 0 );
296
- }
297
- }
298
-
299
- class Frills {
300
-
301
- static void drawBorder (Graphics g ){
302
- int BOARDER_WIDTH = 10 ;
303
- g .setColor (Color .BLACK );
304
- g .fillRect (0 , 0 , Snake .WIDTH , BOARDER_WIDTH ); //top
305
- g .fillRect (0 , Snake .PLAY_AREA_X , Snake .WIDTH , BOARDER_WIDTH ); //bottom?
306
- g .fillRect (0 , 0 , BOARDER_WIDTH , Snake .HEIGHT ); //left
307
- g .fillRect (Snake .WIDTH - BOARDER_WIDTH , 0 , BOARDER_WIDTH , Snake .HEIGHT ); //right
308
- }
309
- static void drawStartScreen (Graphics g ){
310
- //TITLE
311
- Graphics2D g2 = (Graphics2D )g ;
312
- g2 .setRenderingHint (RenderingHints .KEY_TEXT_ANTIALIASING ,RenderingHints .VALUE_TEXT_ANTIALIAS_ON );
313
- g .setFont (new Font ("AndaleMono" , Font .BOLD , 40 ));
314
- g .setColor (Color .BLACK );
315
- g2 .drawString ("SIMPLE SNAKE" , 58 , 60 );
316
- //INSTRUCTIONS
317
- g .setFont (new Font ("AndaleMono" , Font .BOLD , 12 ));
318
- g .drawString ("ARROW KEY to start" , 145 , 350 );
319
- g .drawString ("ESC to quit" , 175 , 365 );
320
- g .drawString ("P to pause" , 175 , 380 );
321
- }
322
- static void drawPauseGraphics (Graphics g ){
323
- String d = Globals .saveSpeed ;
324
- g .setFont (new Font ("AndaleMono" , Font .BOLD , 20 ));
325
- g .setColor (Color .GREEN );
326
- if (d .equals ("UP" )){
327
- g .drawString ("\u2191 " , 25 , 35 );
175
+ boolean hittingBody (){
176
+ for (int i = 0 ; i < this .body .size (); i ++){
177
+ if (body .get (i ).pos .equals (this .pos )) return true ;
328
178
}
329
- else if (d .equals ("DOWN" )) g .drawString ("\u2193 " , 25 , 35 );
330
- else if (d .equals ("LEFT" )) g .drawString ("\u2190 " , 25 , 35 );
331
- else g .drawString ("\u2192 " , 25 , 35 );
179
+ return false ;
332
180
}
333
- }
334
181
182
+ }
335
183
0 commit comments