Skip to content

Commit 5e335de

Browse files
author
Hasan Y Ahmed
committedDec 22, 2017
did a bunch of refactoring
1 parent d94ebb1 commit 5e335de

File tree

7 files changed

+635
-427
lines changed

7 files changed

+635
-427
lines changed
 

‎.idea/uiDesigner.xml

+124
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎.idea/workspace.xml

+184-83
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎out/META-INF/MANIFEST.MF

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Manifest-Version: 1.0
2+
Main-Class: com.hasahmed.simplesnake.Snake
3+

‎src/com/hasahmed/simplesnake/Circle.java

+92-244
Original file line numberDiff line numberDiff line change
@@ -20,80 +20,32 @@ class Circle {
2020
this.color = color;
2121
this.lastPos.setLocation(x - this.speed.x, y - this.speed.y);
2222
}
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-
}
3023
void draw(Graphics g) {
3124
g.setColor(this.color);
3225
g.fillOval(this.pos.x - this.radius, this.pos.y - this.radius, this.radius * 2, this.radius * 2);
33-
//g.setColor(this.color);
3426
g.setColor(Color.BLACK);
3527
g.drawOval(this.pos.x - this.radius, this.pos.y - this.radius, this.radius * 2, this.radius * 2);
3628
}
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-
4429
boolean isOutOfBounds(){
4530
int x = this.pos.x;
4631
int y = this.pos.y;
4732

4833
if (x > Snake.PLAY_AREA_X - 5 || x < 0 + 10 || y > Snake.PLAY_AREA_Y - 5 || y <= 0 + 5) return true;
4934
return false;
5035
}
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-
}
6336
int getX(){
6437
return this.pos.x;
6538
}
6639
int getY() {
6740
return this.pos.y;
6841
}
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+
9243
} // end circle
9344
///////////////////////////////////////////////////////////////////////////////////////////
94-
//BODY//
9545

9646

47+
48+
//BODY//
9749
class Body extends ArrayList<Circle>{
9850
//instance variables
9951
Circle head;
@@ -102,7 +54,6 @@ class Body extends ArrayList<Circle>{
10254

10355
//constructors
10456
public Body(int radius, Color color, int length){
105-
this.bodyColor = bodyColor;
10657
this.head = new Circle((Snake.PLAY_AREA_X/2) + 15, (Snake.PLAY_AREA_Y /2) + 15, radius, color);
10758
this.add(head);
10859
fillWithCircles(length);
@@ -112,224 +63,121 @@ public Body(int radius, Color color, int length){
11263

11364
//methods
11465

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+
}
12175
}
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-
}
12976

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(){
13381
this.add(new Circle(this.get(this.size() - 1).pos.x, this.get(this.size() - 1).pos.y, head.radius, bodyColor));
13482
}
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+
}
13693

13794

13895
//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+
}
140107

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+
}
146121

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))));
157122

158-
this.remove(this.get(this.size() -1));
159-
}
160-
161-
162-
//headOverlappingBody
123+
//headOverlappingBody
163124
//
164125
//checks to see if the 0th element in the list is in the same location as the ith element
165126
// in the list. If it is it removes the ith element, so that the redHead() method (turing the
166127
// 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
187128

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;
209139
}
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;
216142
}
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;
256145
}
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);
264148
}
149+
} // end circle array
265150

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();
269159
}
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);
274162
}
275163

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();
279173
}
280174

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;
328178
}
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;
332180
}
333-
}
334181

182+
}
335183

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.hasahmed.simplesnake;
2+
3+
/**
4+
* Created by Hasan Y Ahmed on 12/21/17.
5+
*/
6+
7+
import java.awt.*;
8+
9+
class Frills {
10+
static final int ARROW_X = 25;
11+
static final int ARROW_Y = 35;
12+
static final int BOARDER_WIDTH = 10;
13+
static void drawBorder(Graphics g){
14+
g.setColor(Color.BLACK);
15+
g.fillRect(0, 0, Snake.WIDTH, BOARDER_WIDTH); //top
16+
g.fillRect(0, Snake.PLAY_AREA_X, Snake.WIDTH, BOARDER_WIDTH); //bottom
17+
g.fillRect(0, 0, BOARDER_WIDTH, Snake.HEIGHT); //left
18+
g.fillRect(Snake.WIDTH - BOARDER_WIDTH, 0, BOARDER_WIDTH, Snake.HEIGHT); //right
19+
}
20+
static void drawStartScreen(Graphics g){
21+
//TITLE
22+
Graphics2D g2 = (Graphics2D)g;
23+
g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
24+
g.setFont(new Font("AndaleMono", Font.BOLD, 40));
25+
g.setColor(Color.BLACK);
26+
g2.drawString("SIMPLE SNAKE", 58, 60);
27+
//INSTRUCTIONS
28+
g.setFont(new Font("AndaleMono", Font.BOLD, 12));
29+
g.drawString("ARROW KEY to start", 145, 350);
30+
g.drawString("ESC to quit", 175, 365);
31+
g.drawString("P to pause", 175, 380);
32+
}
33+
static void drawPauseGraphics(Graphics g){
34+
String d = KeyboardHandler.saveSpeed;
35+
g.setFont(new Font("AndaleMono", Font.BOLD, 20));
36+
g.setColor(Color.GREEN);
37+
if (d.equals("UP")){
38+
g.drawString("\u2191", ARROW_X, ARROW_Y);
39+
}
40+
else if(d.equals("DOWN")) g.drawString("\u2193", ARROW_X, ARROW_Y);
41+
else if(d.equals("LEFT")) g.drawString("\u2190", ARROW_X, ARROW_Y);
42+
else g.drawString("\u2192", ARROW_X, ARROW_Y);
43+
}
44+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
package com.hasahmed.simplesnake;
2+
3+
/**
4+
* Created by Hasan Y Ahmed on 12/21/17.
5+
*/
6+
7+
/**
8+
* KeyboardHandler: Really not a good name for this class. It mostly handles the button pressing logic as well
9+
* as the array that remembers the button presses
10+
*/
11+
12+
import java.awt.*;
13+
import java.awt.event.KeyEvent;
14+
15+
public class KeyboardHandler {
16+
static Point speed = new Point(0, 0);
17+
static int keyOpp = 0;
18+
/**
19+
* dirls: A 2 element list the records array key presses. This is so that when 2 array keys are pressed
20+
* in the same 'tick' the second one isn't lost
21+
*/
22+
static Dir[] dirls = new Dir[2];
23+
static String saveSpeed;
24+
25+
26+
/**
27+
* Finds the first non-null position of the list and inserts there
28+
* @param d The direction to be added to the list
29+
*/
30+
static void add(Dir d){
31+
for (int i = 0; i < dirls.length; i++){
32+
if (dirls[i] == null){
33+
dirls[i] = d;
34+
break;
35+
}
36+
}
37+
}
38+
/**
39+
* removes the 0th element of the dirls Array, and moves the 1th element to its place, replacing the 1th
40+
* element with null
41+
*/
42+
static void dirlsRemoveFirst(){
43+
dirls[0] = dirls[1];
44+
dirls[1] = null;
45+
}
46+
enum Dir{
47+
UP("UP"), DOWN("DOWN"), LEFT("LEFT"), RIGHT("RIGHT"), STOP("STOP");
48+
String value;
49+
Dir(String v){
50+
value = v;
51+
}
52+
}
53+
54+
static void setDir(Dir d){
55+
keyOpp++;
56+
if (d == Dir.UP){
57+
if(KeyboardHandler.getDir() != Dir.DOWN)
58+
speed.setLocation(0, -10);
59+
}
60+
61+
else if (d == Dir.DOWN){
62+
if(KeyboardHandler.getDir() != Dir.UP)
63+
speed.setLocation(0, 10);
64+
}
65+
66+
else if (d == Dir.LEFT) {
67+
if(KeyboardHandler.getDir() != Dir.RIGHT)
68+
speed.setLocation(-10, 0);
69+
}
70+
71+
else if (d == Dir.RIGHT) {
72+
if(KeyboardHandler.getDir() != Dir.LEFT)
73+
speed.setLocation(10, 0);
74+
}
75+
76+
else if (d == Dir.STOP) speed.setLocation(0, 0);
77+
}
78+
static Dir getDir(){
79+
if (speed.equals(new Point(0, -10))) return Dir.UP;
80+
else if (speed.equals(new Point(0, 10))) return Dir.DOWN;
81+
else if (speed.equals(new Point(-10, 0))) return Dir.LEFT;
82+
else if (speed.equals(new Point(10, 0))) return Dir.RIGHT;
83+
return Dir.STOP;
84+
}
85+
static Point getSpeed(){
86+
if (speed.equals(new Point(0, -10))) return new Point(0, -10);
87+
else if (speed.equals(new Point(0, 10))) return new Point(0, 10);
88+
else if (speed.equals(new Point(-10, 0))) return new Point(-10, 0);
89+
else if (speed.equals(new Point(10, 0))) return new Point(10, 0);
90+
return new Point(0, 0);
91+
}
92+
static void right(){
93+
add(Dir.RIGHT);
94+
if(getDir() == Dir.LEFT)
95+
if(dirls[0] == Dir.RIGHT)
96+
dirlsRemoveFirst();
97+
}
98+
static void left(){
99+
add(Dir.LEFT);
100+
if(getDir() == Dir.RIGHT)
101+
if(dirls[0] == Dir.LEFT)
102+
dirlsRemoveFirst();
103+
}
104+
static void up(){
105+
add(Dir.UP);
106+
if( getDir() == Dir.DOWN
107+
&& dirls[0] == Dir.UP)
108+
dirlsRemoveFirst(); //execute
109+
}
110+
static void down(){
111+
if (dirls[keyOpp] != Dir.UP
112+
&& getDir() != Dir.STOP)
113+
add(Dir.DOWN); //execute
114+
115+
if(getDir() == Dir.UP
116+
&& dirls[0] == Dir.DOWN)
117+
dirlsRemoveFirst(); //execute
118+
}
119+
static void handleKeyPressed(KeyEvent e, Snake game) {
120+
if (!game.gameOver && KeyboardHandler.keyOpp < 2){
121+
122+
if (e.getKeyCode() == KeyEvent.VK_RIGHT){
123+
right();
124+
}
125+
else if (e.getKeyCode() == KeyEvent.VK_LEFT) {
126+
left();
127+
}
128+
129+
else if (e.getKeyCode() == KeyEvent.VK_UP){
130+
up();
131+
}
132+
else if (e.getKeyCode() == KeyEvent.VK_DOWN){
133+
down();
134+
}
135+
else if (e.getKeyCode() == KeyEvent.VK_P && !game.displayStartScreen){
136+
game.pause();
137+
}
138+
else if (e.getKeyCode() == KeyEvent.VK_Q){
139+
game.closeWindow();
140+
}
141+
else if (e.getKeyCode() == KeyEvent.VK_ESCAPE){
142+
game.closeWindow();
143+
}
144+
}
145+
146+
else if (e.getKeyCode() == KeyEvent.VK_R){
147+
game.reset();
148+
}
149+
150+
else if (e.getKeyCode() == KeyEvent.VK_ESCAPE || e.getKeyCode() == KeyEvent.VK_Q){
151+
game.closeWindow();
152+
}
153+
154+
if ( e.getKeyCode() == KeyEvent.VK_UP
155+
|| e.getKeyCode() == KeyEvent.VK_DOWN
156+
|| e.getKeyCode() == KeyEvent.VK_LEFT
157+
|| e.getKeyCode() == KeyEvent.VK_RIGHT){
158+
game.displayStartScreen = false;
159+
}
160+
} // end keyPressed
161+
}

‎src/com/hasahmed/simplesnake/Snake.java

+27-100
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,21 @@
77

88

99
public class Snake implements World{
10-
private static String version = "2.0.0";
11-
private boolean displayStartScreen = true;
10+
private static String version = "2.0.1";
11+
boolean displayStartScreen = true;
1212
static int PLAY_AREA_X = 400;
1313
static int PLAY_AREA_Y = 400;
1414
private static JFrame frame;
15-
private static BigBang game;
16-
private static int preLoop = 10010;
15+
// private static int preLoop = 10010; //loop the game a lot so that it will be faster?
16+
private static int preLoop = 1; //loop the game a lot so that it will be faster?
1717
private int STARTING_LENGTH = 5;
18-
boolean paused = false;
18+
private boolean paused = false;
1919
boolean endCalled = false; // keep end() from being called every tic
20-
Point speed = new Point(0, 0);
2120
int RADIUS = 5;
2221
int FOOD_GROWTH = 3;
2322
static Integer resetSpeed = 75;
24-
int INCVAL = RADIUS * 2;
25-
static int HEIGHT = 432;
26-
static int WIDTH = 410;
23+
static int HEIGHT = 432; //needs to be this because of the size of the size of OSX top of window
24+
static int WIDTH = PLAY_AREA_X + Frills.BOARDER_WIDTH;
2725
Color BODY_COLOR = Color.BLACK;
2826
Color FOOD_COLOR = Color.ORANGE;
2927
boolean drawMode = false;
@@ -33,14 +31,13 @@ public class Snake implements World{
3331
Food food = new Food(FOOD_COLOR, body);
3432
Circle head;
3533
Point saveSpeed;
36-
int updateFuncCallCount = 0;
3734

3835
int scoreWrittenToFile = WriteScore.getScoreFromFile();
3936

4037
void reset(){
4138
endCalled = false;
4239
displayStartScreen = true;
43-
Globals.setDir(Globals.Dir.STOP);
40+
KeyboardHandler.setDir(KeyboardHandler.Dir.STOP);
4441
drawMode = false;
4542
gameOver = false;
4643
score = new Score();
@@ -51,19 +48,19 @@ void reset(){
5148

5249
//update
5350
public void update() {
54-
Globals.setDir(Globals.dirls[0]);
55-
Globals.chopHead();
51+
KeyboardHandler.setDir(KeyboardHandler.dirls[0]);
52+
KeyboardHandler.dirlsRemoveFirst();
5653

5754
//variables
5855
head = body.get(0);
59-
Globals.keyOpp = 0;
56+
KeyboardHandler.keyOpp = 0;
6057
if (head.isOutOfBounds() || body.headOverlappingBody()){
6158
body.redHead();
6259
gameOver = true;
6360
if (!endCalled) end();
6461
}
6562
//updatePos
66-
if (!paused && !gameOver && Globals.getDir() != Globals.Dir.STOP) body.updatePos();
63+
if (!paused && !gameOver && KeyboardHandler.getDir() != KeyboardHandler.Dir.STOP) body.updatePos();
6764

6865
//drawMode
6966
if(drawMode) this.body.addCircle();
@@ -101,104 +98,31 @@ public void draw(Graphics g){
10198

10299
public void mousePressed(MouseEvent e) {}
103100

104-
public void right(){
105-
Globals.add(Globals.Dir.RIGHT);
106-
if(Globals.getDir() == Globals.Dir.LEFT)
107-
if(Globals.dirls[0] == Globals.Dir.RIGHT){
108-
Globals.chopHead();
109-
}
110-
}
111-
112-
public void left(){
113-
Globals.add(Globals.Dir.LEFT);
114-
if(Globals.getDir() == Globals.Dir.RIGHT)
115-
if(Globals.dirls[0] == Globals.Dir.LEFT){
116-
Globals.chopHead();
117-
}
118-
}
119-
120-
public void up(){
121-
Globals.add(Globals.Dir.UP);
122-
if( Globals.getDir() == Globals.Dir.DOWN
123-
&& Globals.dirls[0] == Globals.Dir.UP)
124-
Globals.chopHead(); //execute
125-
}
126101

127-
public void down(){
128-
if (Globals.dirls[Globals.keyOpp] != Globals.Dir.UP
129-
&& Globals.getDir() != Globals.Dir.STOP)
130-
Globals.add(Globals.Dir.DOWN); //execute
131102

132-
if(Globals.getDir() == Globals.Dir.UP
133-
&& Globals.dirls[0] == Globals.Dir.DOWN)
134-
Globals.chopHead(); //execute
135-
}
136-
137-
138-
public void pause(){
103+
void pause(){
139104
if (!paused){
140105
body.redHead();
141-
saveSpeed = Globals.getSpeed();
142-
Globals.saveSpeed = Globals.getDir().value;
106+
saveSpeed = KeyboardHandler.getSpeed();
107+
KeyboardHandler.saveSpeed = KeyboardHandler.getDir().value;
143108
paused = true;
144-
Globals.setDir(Globals.Dir.STOP);
109+
KeyboardHandler.setDir(KeyboardHandler.Dir.STOP);
145110
}
146111
else{
147112
body.blackHead();
148113
paused = false;
149-
Globals.speed.setLocation(saveSpeed);
114+
KeyboardHandler.speed.setLocation(saveSpeed);
150115
}
151116
}
152117

153118

154-
private void closeWindow(){
119+
void closeWindow(){
155120
frame.dispatchEvent(new WindowEvent(frame, WindowEvent.WINDOW_CLOSING));
156121
}
157122

158-
public void keyPressed(KeyEvent e) {
159-
if (!gameOver && Globals.keyOpp < 2){
160-
161-
if (e.getKeyCode() == KeyEvent.VK_RIGHT){
162-
right();
163-
}
164-
else if (e.getKeyCode() == KeyEvent.VK_LEFT) {
165-
left();
166-
}
167-
168-
else if (e.getKeyCode() == KeyEvent.VK_UP){
169-
up();
170-
}
171-
else if (e.getKeyCode() == KeyEvent.VK_DOWN){
172-
down();
173-
}
174-
else if (e.getKeyCode() == KeyEvent.VK_P && !displayStartScreen){
175-
pause();
176-
}
177-
else if (e.getKeyCode() == KeyEvent.VK_Q){
178-
closeWindow();
179-
}
180-
else if (e.getKeyCode() == KeyEvent.VK_D){
181-
}
182-
else if (e.getKeyCode() == KeyEvent.VK_ESCAPE){
183-
closeWindow();
184-
}
185-
} // if (!gameOver && Globals.keyOpp < 1) end
186-
187-
else if (e.getKeyCode() == KeyEvent.VK_R){
188-
reset();
189-
}
190-
191-
else if (e.getKeyCode() == KeyEvent.VK_ESCAPE || e.getKeyCode() == KeyEvent.VK_Q){
192-
closeWindow();
193-
}
194-
195-
if ( e.getKeyCode() == KeyEvent.VK_UP
196-
|| e.getKeyCode() == KeyEvent.VK_DOWN
197-
|| e.getKeyCode() == KeyEvent.VK_LEFT
198-
|| e.getKeyCode() == KeyEvent.VK_RIGHT){
199-
displayStartScreen = false;
200-
}
201-
} // end keyPressed
123+
public void keyPressed(KeyEvent e){
124+
KeyboardHandler.handleKeyPressed(e, this);
125+
}
202126

203127
void end(){
204128
endCalled = true;
@@ -223,9 +147,11 @@ public static void main(String[] args) throws IOException {
223147
WriteScore.init();
224148
System.setProperty("sun.java2d.opengl", "True");
225149
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
150+
226151
//BigBang
227152
Snake s = new Snake();
228-
game = new BigBang(resetSpeed, s);
153+
BigBang game = new BigBang(resetSpeed, s);
154+
229155
//JFrame
230156
frame = new JFrame("Simple Snake");
231157
frame.getContentPane().add(game);
@@ -238,12 +164,13 @@ public static void main(String[] args) throws IOException {
238164
frame.setLocation(dim.width/2 - frame.getSize().width /2, dim.height/2 - frame.getSize().height/2);
239165
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
240166
frame.setResizable(false);
241-
242-
for (; preLoop >= 0; preLoop--){ //this is here so that java will make the code in init native... need tests to see if actually faster
167+
for (; preLoop >= 0; preLoop--){ //this is here so that java will make the code in init native...
168+
// Is it actually faster? Who knows?
243169
s.init();
244170
}
245171

246172
game.begin();
173+
System.out.println(frame.getInsets().top);
247174
}
248175

249176
}

0 commit comments

Comments
 (0)
Please sign in to comment.