I need help dividiving this code into classes, I got a feedback that It\'s is be
ID: 3845128 • Letter: I
Question
I need help dividiving this code into classes, I got a feedback that It's is better to have different files than having a single file with all the code. I guess it has to do with inheritance and encapsulation.
import javax.swing.*;
import java.awt.event.*;
import javax.swing.event.*;
import java.util.*;
import java.io.*;
import java.text.*;
import java.awt.*;
import javax.swing.Timer;
class SnakeGame extends JFrame implements ActionListener
{
private Container c;
private int snakeLength =1;
private LinkedList<Point> snakeBody = new LinkedList<Point>();
private Point pointSnakeHead;
private Random random = new Random();
private Snake snake;
private int is_one_player_game;
private int difficulty;
private Timer timer;
public SnakeGame(int is_one_player_game,int difficulty)
{
this.is_one_player_game=is_one_player_game;
this.difficulty=difficulty;
timer=new Timer(difficulty,this);
addKeyListener(new KeyInput(this));
Toolkit toolkit = Toolkit.getDefaultToolkit();
c=getContentPane();
setBackground(Color.WHITE);
snake = new Snake(is_one_player_game);
c.add(snake);
setVisible(true);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(((int)toolkit.getScreenSize().getWidth()-1000)/2,((int)toolkit.getScreenSize().getHeight()-1000)/2,1000,1000);
timer.start();
}
public void keyPressed(KeyEvent e)
{
int temp_key =e.getKeyCode();
if(is_one_player_game==1)
{
if(temp_key == KeyEvent.VK_UP)
{
snake.setUp_one();
}
else if(temp_key == KeyEvent.VK_DOWN)
{
snake.setDown_one();
}
else if(temp_key == KeyEvent.VK_LEFT)
{
snake.setLeft_one();
}
else if(temp_key == KeyEvent.VK_RIGHT)
{
snake.setRight_one();
}
else if(temp_key == KeyEvent.VK_W)
{
snake.setUp_one();
}
else if(temp_key == KeyEvent.VK_A)
{
snake.setLeft_one();
}
else if(temp_key == KeyEvent.VK_S)
{
snake.setDown_one();
}
else if(temp_key == KeyEvent.VK_D)
{
snake.setRight_one();
}
else if(temp_key == KeyEvent.VK_ENTER)
{
snake.reStart_the_game();
}
else if(temp_key == KeyEvent.VK_ESCAPE)
{
snake.setStop();
if (JOptionPane.showConfirmDialog(null, "Do You Really Want to exit?","Exit?", JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE)== JOptionPane.YES_OPTION)
{
JOptionPane.showConfirmDialog(null,"Written By LI_HING. "+"04/26/2017. "+"LI_HING@student.smc.edu ","See You!",JOptionPane.DEFAULT_OPTION);
System.exit(0);
}
else
{
snake.setStart();
}
}
}
else if(is_one_player_game==2)
{
if(temp_key == KeyEvent.VK_UP)
{
snake.setUp_two();
}
else if(temp_key == KeyEvent.VK_DOWN)
{
snake.setDown_two();
}
else if(temp_key == KeyEvent.VK_LEFT)
{
snake.setLeft_two();
}
else if(temp_key == KeyEvent.VK_RIGHT)
{
snake.setRight_two();
}
else if(temp_key == KeyEvent.VK_W)
{
snake.setUp_one();
}
else if(temp_key == KeyEvent.VK_A)
{
snake.setLeft_one();
}
else if(temp_key == KeyEvent.VK_S)
{
snake.setDown_one();
}
else if(temp_key == KeyEvent.VK_D)
{
snake.setRight_one();
}
else if(temp_key == KeyEvent.VK_ENTER)
{
snake.reStart_the_game();
}
else if(temp_key == KeyEvent.VK_ESCAPE)
{
snake.setStop();
if (JOptionPane.showConfirmDialog(null, "Do You Really Want to exit?","Exit?", JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE)== JOptionPane.YES_OPTION)
{
JOptionPane.showConfirmDialog(null,"Written By LI_HING. "+"04/26/2017. "+"LI_HING@student.smc.edu ","See You!",JOptionPane.DEFAULT_OPTION);
System.exit(0);
}
else
{
snake.setStart();
}
}
}
}
@Override
public void actionPerformed(ActionEvent args)
{
snake.repaint();
}
}
class Snake extends JPanel
{
private Point pointSnakeHead;
long t = System.currentTimeMillis();
private Random random = new Random(t);
private Point snakeHead;
private ImageIcon appleIcon;
private ImageIcon snakeIcon_one;
private ImageIcon snakeIcon_two;
private ImageIcon background;
private int size_one=2;
private int size_two=2;
private int max_size=16;
private ImageIcon [] snake_Body_one = new ImageIcon[max_size];
private ImageIcon [] snake_Body_two = new ImageIcon[max_size];
private int [] tempX_Snake_one =new int[16];
private int [] tempY_Snake_one =new int[16];
private int [] tempX_Snake_two =new int[16];
private int [] tempY_Snake_two =new int[16];
private int tempX_Apple;
private int tempY_Apple;
private boolean left_one=false;
private boolean background_picture_draw=true;
private boolean right_one=true;
private boolean up_one=false;
private boolean down_one=false;
private boolean left_two=false;
private boolean right_two=true;
private boolean up_two=false;
private boolean down_two=false;
private boolean stop=false;
private boolean game_over=false;
private int is_one_player_game;
public Snake()
{
background=new ImageIcon("background.png");
snakeIcon_one = new ImageIcon("rightmouth.png");
appleIcon = new ImageIcon("Apple.png");
for(int i=0;i<max_size;i++)
{
snake_Body_one[i]=new ImageIcon("snakebody.png");
}
tempX_Snake_one[0]=(random.nextInt(40))*25;//save Head x informantion..
tempY_Snake_one[0]=(random.nextInt(40))*25;//save Head y informantion..
tempX_Apple=(random.nextInt(15))*25;
tempY_Apple=(random.nextInt(15))*25;
}
public Snake(int is_one_player_game)
{
background=new ImageIcon("background.png");
this.is_one_player_game=is_one_player_game;
if(is_one_player_game==1)
{
snakeIcon_one = new ImageIcon("rightmouth.png");
appleIcon = new ImageIcon("Apple.png");
for(int i=0;i<max_size;i++)
{
snake_Body_one[i]=new ImageIcon("snakebody.png");
}
tempX_Snake_one[0]=(random.nextInt(40))*25;//save Head x informantion..
tempY_Snake_one[0]=(random.nextInt(40))*25;//save Head y informantion..
tempX_Apple=(random.nextInt(15))*25;
tempY_Apple=(random.nextInt(15))*25;
}
else if(is_one_player_game==2)
{
snakeIcon_one = new ImageIcon("rightmouth.png");
snakeIcon_two = new ImageIcon("leftmouth_snake_two.png");
appleIcon = new ImageIcon("Apple.png");
for(int i=0;i<max_size;i++)
{
snake_Body_one[i]=new ImageIcon("snakebody.png");
snake_Body_two[i]=new ImageIcon("snakebody_snake_two.png");
}
tempX_Snake_one[0]=(random.nextInt(40))*25;//save Head x informantion..
tempY_Snake_one[0]=(random.nextInt(40))*25;//save Head y informantion..
tempX_Snake_two[0]=(random.nextInt(40))*25;
tempY_Snake_two[0]=(random.nextInt(40))*25;
//Make Sure Sanke One and Snake two are not in the same place...
while(tempX_Snake_one[0]==tempX_Snake_two[0]&&tempY_Snake_one[0]==tempY_Snake_two[0])
{
tempX_Snake_two[0]=(random.nextInt(40))*25;
tempY_Snake_two[0]=(random.nextInt(40))*25;
}
tempX_Apple=(random.nextInt(15))*25;
tempY_Apple=(random.nextInt(15))*25;
}
}
protected void paintComponent(Graphics g)
{
if(!stop&&Check_If_Snake_Eat_Itself()==false&&!game_over&&Check_If_Snake_Touch_Each_Other()==false)
{
super.paintComponent(g);
g.setColor(Color.BLACK);
g.fillRect(0,0,1000,1000);
if(background_picture_draw)
{
background.paintIcon(this,g,0,0);
//background_picture_draw=false;
}
//background.paintIcon(this,g,0,0);
appleIcon.paintIcon(this,g,tempX_Apple,tempY_Apple);
if(Check_if_Snake_Eat_Food())
{
tempX_Apple=(random.nextInt(15))*25;
tempY_Apple=(random.nextInt(15))*25;
appleIcon.paintIcon(this,g,tempX_Apple,tempY_Apple);
}
//For Snake one..
if(right_one)
{
snakeIcon_one= new ImageIcon("rightmouth.png");
if(tempX_Snake_one[0]>=1000)
{
tempX_Snake_one[0]=0;
}
snakeIcon_one.paintIcon(this,g,tempX_Snake_one[0]+25,tempY_Snake_one[0]);
for(int i=size_one;i>=1;i--)//Body strat from 1..
{
tempX_Snake_one[i]=tempX_Snake_one[i-1];
tempY_Snake_one[i]=tempY_Snake_one[i-1];
snake_Body_one[i-1].paintIcon(this,g,tempX_Snake_one[i],tempY_Snake_one[i]);
}
tempX_Snake_one[0]+=25;
}
else if(left_one)
{
snakeIcon_one= new ImageIcon("leftmouth.png");
if(tempX_Snake_one[0]<=0)
{
tempX_Snake_one[0]=1000;
}
snakeIcon_one.paintIcon(this,g,tempX_Snake_one[0]-25,tempY_Snake_one[0]);
for(int i=size_one;i>=1;i--)//Body strat from 1..
{
tempX_Snake_one[i]=tempX_Snake_one[i-1];
tempY_Snake_one[i]=tempY_Snake_one[i-1];
snake_Body_one[i-1].paintIcon(this,g,tempX_Snake_one[i],tempY_Snake_one[i]);
}
tempX_Snake_one[0]-=25;
}
else if(up_one)
{
snakeIcon_one= new ImageIcon("upmouth.png");
if(tempY_Snake_one[0]<=0)
{
tempY_Snake_one[0]=1000;
}
snakeIcon_one.paintIcon(this,g,tempX_Snake_one[0],tempY_Snake_one[0]-25);
for(int i=size_one;i>=1;i--)//Body strat from 1..
{
tempX_Snake_one[i]=tempX_Snake_one[i-1];
tempY_Snake_one[i]=tempY_Snake_one[i-1];
snake_Body_one[i-1].paintIcon(this,g,tempX_Snake_one[i],tempY_Snake_one[i]);
}
tempY_Snake_one[0]-=25;
}
else if(down_one)
{
snakeIcon_one= new ImageIcon("downmouth.png");
if(tempY_Snake_one[0]>=1000)
{
tempY_Snake_one[0]=0;
}
snakeIcon_one.paintIcon(this,g,tempX_Snake_one[0],tempY_Snake_one[0]+25);
for(int i=size_one;i>=1;i--)//Body strat from 1..
{
tempX_Snake_one[i]=tempX_Snake_one[i-1];
tempY_Snake_one[i]=tempY_Snake_one[i-1];
snake_Body_one[i-1].paintIcon(this,g,tempX_Snake_one[i],tempY_Snake_one[i]);
}
tempY_Snake_one[0]+=25;
}
if(is_one_player_game==2)
{
if(right_two)
{
snakeIcon_two= new ImageIcon("rightmouth_snake_two.png");
if(tempX_Snake_two[0]>=1000)
{
tempX_Snake_two[0]=0;
}
snakeIcon_two.paintIcon(this,g,tempX_Snake_two[0]+25,tempY_Snake_two[0]);
for(int i=size_two;i>=1;i--)//Body strat from 1..
{
tempX_Snake_two[i]=tempX_Snake_two[i-1];
tempY_Snake_two[i]=tempY_Snake_two[i-1];
snake_Body_two[i-1].paintIcon(this,g,tempX_Snake_two[i],tempY_Snake_two[i]);
}
tempX_Snake_two[0]+=25;
}
else if(left_two)
{
snakeIcon_two= new ImageIcon("leftmouth_snake_two.png");
if(tempX_Snake_two[0]<=0)
{
tempX_Snake_two[0]=1000;
}
snakeIcon_two.paintIcon(this,g,tempX_Snake_two[0]-25,tempY_Snake_two[0]);
for(int i=size_two;i>=1;i--)//Body strat from 1..
{
tempX_Snake_two[i]=tempX_Snake_two[i-1];
tempY_Snake_two[i]=tempY_Snake_two[i-1];
snake_Body_two[i-1].paintIcon(this,g,tempX_Snake_two[i],tempY_Snake_two[i]);
}
tempX_Snake_two[0]-=25;
}
else if(up_two)
{
snakeIcon_two= new ImageIcon("upmouth_snake_two.png");
if(tempY_Snake_two[0]<=0)
{
tempY_Snake_two[0]=1000;
}
snakeIcon_two.paintIcon(this,g,tempX_Snake_two[0],tempY_Snake_two[0]-25);
for(int i=size_two;i>=1;i--)//Body strat from 1..
{
tempX_Snake_two[i]=tempX_Snake_two[i-1];
tempY_Snake_two[i]=tempY_Snake_two[i-1];
snake_Body_two[i-1].paintIcon(this,g,tempX_Snake_two[i],tempY_Snake_two[i]);
}
tempY_Snake_two[0]-=25;
}
else if(down_two)
{
snakeIcon_two= new ImageIcon("downmouth_snake_two.png");
if(tempY_Snake_two[0]>=1000)
{
tempY_Snake_two[0]=0;
}
snakeIcon_two.paintIcon(this,g,tempX_Snake_two[0],tempY_Snake_two[0]+25);
for(int i=size_two;i>=1;i--)//Body strat from 1..
{
tempX_Snake_two[i]=tempX_Snake_two[i-1];
tempY_Snake_two[i]=tempY_Snake_two[i-1];
snake_Body_two[i-1].paintIcon(this,g,tempX_Snake_two[i],tempY_Snake_two[i]);
}
tempY_Snake_two[0]+=25;
}
}
}
else
{
super.paintComponent(g);
g.setColor(Color.BLACK);
g.setFont(new Font("arial",Font.BOLD,20));
g.drawString("Game Over",400,500);
g.drawString("Press enter to restart the game",350,530);
}
}
public void setLeft_one()
{
if(!right_one)
{
left_one=true;
right_one=false;
up_one=false;
down_one=false;
stop=false;
}
}
public void setLeft_two()
{
if(!right_two)
{
left_two=true;
right_two=false;
up_two=false;
down_two=false;
stop=false;
}
}
public void setRight_one()
{
if(!left_one)
{
right_one=true;
left_one=false;
up_one=false;
down_one=false;
stop=false;
}
}
public void setRight_two()
{
if(!left_two)
{
right_two=true;
left_two=false;
up_two=false;
down_two=false;
stop=false;
}
}
public void setUp_one()
{
if(!down_one)
{
up_one=true;
left_one=false;
right_one=false;
down_one=false;
stop=false;
}
}
public void setUp_two()
{
if(!down_two)
{
up_two=true;
left_two=false;
right_two=false;
down_two=false;
stop=false;
}
}
public void setDown_one()
{
if(!up_one)
{
down_one=true;
up_one=false;
left_one=false;
right_one=false;
stop=false;
}
}
public void setDown_two()
{
if(!up_two)
{
down_two=true;
up_two=false;
left_two=false;
right_two=false;
stop=false;
}
}
public void setStart()
{
stop=false;
}
public void setStop()
{
stop=true;
}
public boolean Check_if_Snake_Eat_Food()
{
if((tempX_Snake_one[0]==tempX_Apple)&&(tempY_Apple==tempY_Snake_one[0]))
{
if(size_one<15)
{
size_one++;
}
return true;
}
if(is_one_player_game==2)
{
if((tempX_Snake_two[0]==tempX_Apple)&&(tempY_Apple==tempY_Snake_two[0]))
{
if(size_two<15)
{
size_two++;
}
return true;
}
else
{
return false;
}
}
return false;
}
public boolean Check_If_Snake_Eat_Itself()
{
for(int i=1;i<size_one;i++)
{
if(tempX_Snake_one[0]==tempX_Snake_one[i]&&tempY_Snake_one[0]==tempY_Snake_one[i])
{
System.out.printf("Snake 1 eat itself ");
game_over=true;
stop=true;
return true;
}
}
if(is_one_player_game==2)
{
for(int i=1;i<size_two;i++)
{
if(tempX_Snake_two[0]==tempX_Snake_two[i]&&tempY_Snake_two[0]==tempY_Snake_two[i])
{
System.out.printf("Snake 2 eat itself ");
game_over=true;
stop=true;
return true;
}
}
}
return false;
}
public boolean Check_If_Snake_Touch_Each_Other()
{
for(int i=0;i<size_two;i++)
{
if(tempX_Snake_one[0]==tempX_Snake_two[i]&&tempY_Snake_one[0]==tempY_Snake_two[i])
{
System.out.printf("Snake 1 touch snake 2 ");
System.out.printf("%d %d %d ",i,tempX_Snake_two[0],tempX_Snake_one[i]);
System.out.printf("%d %d %d ",i,tempY_Snake_two[0],tempY_Snake_one[i]);
game_over=true;
stop=true;
return true;
}
}
for(int i=0;i<size_one;i++)
{
if(tempX_Snake_two[0]==tempX_Snake_one[i]&&tempY_Snake_two[0]==tempY_Snake_one[i])
{
System.out.printf("Snake 2 touch snake 1 ");
System.out.printf("%d %d %d ",i,tempX_Snake_two[0],tempX_Snake_one[i]);
System.out.printf("%d %d %d ",i,tempY_Snake_two[0],tempY_Snake_one[i]);
game_over=true;
stop=true;
return true;
}
}
return false;
}
public boolean getGameOver()
{
return game_over;
}
public void reStart_the_game()
{
System.out.printf("haha ");
if(game_over)
{
System.out.printf("haha1 ");
for(int i=0;i<size_one;i++)
{
tempX_Snake_one[i]=0;
tempY_Snake_one[i]=0;
}
if(is_one_player_game==2)
{
for(int i=0;i<size_two;i++)
{
tempX_Snake_two[i]=0;
tempY_Snake_two[i]=0;
}
tempX_Snake_two[0]=(random.nextInt(40))*25;
tempY_Snake_two[0]=(random.nextInt(40))*25;
}
tempX_Snake_one[0]=(random.nextInt(40))*25;
tempY_Snake_one[0]=(random.nextInt(40))*25;
System.out.printf("%d %d ",tempX_Snake_one[0],tempX_Snake_two[0]);
System.out.printf("%d %d ",tempY_Snake_one[0],tempY_Snake_two[0]);
size_one=2;
size_two=2;
game_over=false;
stop=false;
}
}
public int get_X()
{
return tempX_Snake_one[0];
}
public int get_Y()
{
return tempY_Snake_one[0];
}
}
class KeyInput extends KeyAdapter
{
SnakeGame game;
public KeyInput(SnakeGame game)
{
this.game = game;
}
public void keyPressed(KeyEvent e)
{
game.keyPressed(e);
}
}
Explanation / Answer
I have divided the code into two different classes namely, Snake and SnakeGame respectively which will be stored in a package named snakegame.
1. Snake class code
package snakegame; // name of the package in which the class file of Snake will be stored
import javax.swing.*;
import java.util.*;
import java.awt.*;
public class Snake extends JPanel
{
private Point pointSnakeHead;
long t = System.currentTimeMillis();
private Random random = new Random(t);
private Point snakeHead;
private ImageIcon appleIcon;
private ImageIcon snakeIcon_one;
private ImageIcon snakeIcon_two;
private ImageIcon background;
private int size_one=2;
private int size_two=2;
private int max_size=16;
private ImageIcon [] snake_Body_one = new ImageIcon[max_size];
private ImageIcon [] snake_Body_two = new ImageIcon[max_size];
private int [] tempX_Snake_one =new int[16];
private int [] tempY_Snake_one =new int[16];
private int [] tempX_Snake_two =new int[16];
private int [] tempY_Snake_two =new int[16];
private int tempX_Apple;
private int tempY_Apple;
private boolean left_one=false;
private boolean background_picture_draw=true;
private boolean right_one=true;
private boolean up_one=false;
private boolean down_one=false;
private boolean left_two=false;
private boolean right_two=true;
private boolean up_two=false;
private boolean down_two=false;
private boolean stop=false;
private boolean game_over=false;
private int is_one_player_game;
public Snake()
{
background=new ImageIcon("background.png");
snakeIcon_one = new ImageIcon("rightmouth.png");
appleIcon = new ImageIcon("Apple.png");
for(int i=0;i<max_size;i++)
{
snake_Body_one[i]=new ImageIcon("snakebody.png");
}
tempX_Snake_one[0]=(random.nextInt(40))*25;//save Head x informantion..
tempY_Snake_one[0]=(random.nextInt(40))*25;//save Head y informantion..
tempX_Apple=(random.nextInt(15))*25;
tempY_Apple=(random.nextInt(15))*25;
}
public Snake(int is_one_player_game)
{
background=new ImageIcon("background.png");
this.is_one_player_game=is_one_player_game;
if(is_one_player_game==1)
{
snakeIcon_one = new ImageIcon("rightmouth.png");
appleIcon = new ImageIcon("Apple.png");
for(int i=0;i<max_size;i++)
{
snake_Body_one[i]=new ImageIcon("snakebody.png");
}
tempX_Snake_one[0]=(random.nextInt(40))*25;//save Head x informantion..
tempY_Snake_one[0]=(random.nextInt(40))*25;//save Head y informantion..
tempX_Apple=(random.nextInt(15))*25;
tempY_Apple=(random.nextInt(15))*25;
}
else if(is_one_player_game==2)
{
snakeIcon_one = new ImageIcon("rightmouth.png");
snakeIcon_two = new ImageIcon("leftmouth_snake_two.png");
appleIcon = new ImageIcon("Apple.png");
for(int i=0;i<max_size;i++)
{
snake_Body_one[i]=new ImageIcon("snakebody.png");
snake_Body_two[i]=new ImageIcon("snakebody_snake_two.png");
}
tempX_Snake_one[0]=(random.nextInt(40))*25;//save Head x informantion..
tempY_Snake_one[0]=(random.nextInt(40))*25;//save Head y informantion..
tempX_Snake_two[0]=(random.nextInt(40))*25;
tempY_Snake_two[0]=(random.nextInt(40))*25;
//Make Sure Sanke One and Snake two are not in the same place...
while(tempX_Snake_one[0]==tempX_Snake_two[0]&&tempY_Snake_one[0]==tempY_Snake_two[0])
{
tempX_Snake_two[0]=(random.nextInt(40))*25;
tempY_Snake_two[0]=(random.nextInt(40))*25;
}
tempX_Apple=(random.nextInt(15))*25;
tempY_Apple=(random.nextInt(15))*25;
}
}
protected void paintComponent(Graphics g)
{
if(!stop&&Check_If_Snake_Eat_Itself()==false&&!game_over&&Check_If_Snake_Touch_Each_Other()==false)
{
super.paintComponent(g);
g.setColor(Color.BLACK);
g.fillRect(0,0,1000,1000);
if(background_picture_draw)
{
background.paintIcon(this,g,0,0);
//background_picture_draw=false;
}
//background.paintIcon(this,g,0,0);
appleIcon.paintIcon(this,g,tempX_Apple,tempY_Apple);
if(Check_if_Snake_Eat_Food())
{
tempX_Apple=(random.nextInt(15))*25;
tempY_Apple=(random.nextInt(15))*25;
appleIcon.paintIcon(this,g,tempX_Apple,tempY_Apple);
}
//For Snake one..
if(right_one)
{
snakeIcon_one= new ImageIcon("rightmouth.png");
if(tempX_Snake_one[0]>=1000)
{
tempX_Snake_one[0]=0;
}
snakeIcon_one.paintIcon(this,g,tempX_Snake_one[0]+25,tempY_Snake_one[0]);
for(int i=size_one;i>=1;i--)//Body strat from 1..
{
tempX_Snake_one[i]=tempX_Snake_one[i-1];
tempY_Snake_one[i]=tempY_Snake_one[i-1];
snake_Body_one[i-1].paintIcon(this,g,tempX_Snake_one[i],tempY_Snake_one[i]);
}
tempX_Snake_one[0]+=25;
}
else if(left_one)
{
snakeIcon_one= new ImageIcon("leftmouth.png");
if(tempX_Snake_one[0]<=0)
{
tempX_Snake_one[0]=1000;
}
snakeIcon_one.paintIcon(this,g,tempX_Snake_one[0]-25,tempY_Snake_one[0]);
for(int i=size_one;i>=1;i--)//Body strat from 1..
{
tempX_Snake_one[i]=tempX_Snake_one[i-1];
tempY_Snake_one[i]=tempY_Snake_one[i-1];
snake_Body_one[i-1].paintIcon(this,g,tempX_Snake_one[i],tempY_Snake_one[i]);
}
tempX_Snake_one[0]-=25;
}
else if(up_one)
{
snakeIcon_one= new ImageIcon("upmouth.png");
if(tempY_Snake_one[0]<=0)
{
tempY_Snake_one[0]=1000;
}
snakeIcon_one.paintIcon(this,g,tempX_Snake_one[0],tempY_Snake_one[0]-25);
for(int i=size_one;i>=1;i--)//Body strat from 1..
{
tempX_Snake_one[i]=tempX_Snake_one[i-1];
tempY_Snake_one[i]=tempY_Snake_one[i-1];
snake_Body_one[i-1].paintIcon(this,g,tempX_Snake_one[i],tempY_Snake_one[i]);
}
tempY_Snake_one[0]-=25;
}
else if(down_one)
{
snakeIcon_one= new ImageIcon("downmouth.png");
if(tempY_Snake_one[0]>=1000)
{
tempY_Snake_one[0]=0;
}
snakeIcon_one.paintIcon(this,g,tempX_Snake_one[0],tempY_Snake_one[0]+25);
for(int i=size_one;i>=1;i--)//Body strat from 1..
{
tempX_Snake_one[i]=tempX_Snake_one[i-1];
tempY_Snake_one[i]=tempY_Snake_one[i-1];
snake_Body_one[i-1].paintIcon(this,g,tempX_Snake_one[i],tempY_Snake_one[i]);
}
tempY_Snake_one[0]+=25;
}
if(is_one_player_game==2)
{
if(right_two)
{
snakeIcon_two= new ImageIcon("rightmouth_snake_two.png");
if(tempX_Snake_two[0]>=1000)
{
tempX_Snake_two[0]=0;
}
snakeIcon_two.paintIcon(this,g,tempX_Snake_two[0]+25,tempY_Snake_two[0]);
for(int i=size_two;i>=1;i--)//Body strat from 1..
{
tempX_Snake_two[i]=tempX_Snake_two[i-1];
tempY_Snake_two[i]=tempY_Snake_two[i-1];
snake_Body_two[i-1].paintIcon(this,g,tempX_Snake_two[i],tempY_Snake_two[i]);
}
tempX_Snake_two[0]+=25;
}
else if(left_two)
{
snakeIcon_two= new ImageIcon("leftmouth_snake_two.png");
if(tempX_Snake_two[0]<=0)
{
tempX_Snake_two[0]=1000;
}
snakeIcon_two.paintIcon(this,g,tempX_Snake_two[0]-25,tempY_Snake_two[0]);
for(int i=size_two;i>=1;i--)//Body strat from 1..
{
tempX_Snake_two[i]=tempX_Snake_two[i-1];
tempY_Snake_two[i]=tempY_Snake_two[i-1];
snake_Body_two[i-1].paintIcon(this,g,tempX_Snake_two[i],tempY_Snake_two[i]);
}
tempX_Snake_two[0]-=25;
}
else if(up_two)
{
snakeIcon_two= new ImageIcon("upmouth_snake_two.png");
if(tempY_Snake_two[0]<=0)
{
tempY_Snake_two[0]=1000;
}
snakeIcon_two.paintIcon(this,g,tempX_Snake_two[0],tempY_Snake_two[0]-25);
for(int i=size_two;i>=1;i--)//Body strat from 1..
{
tempX_Snake_two[i]=tempX_Snake_two[i-1];
tempY_Snake_two[i]=tempY_Snake_two[i-1];
snake_Body_two[i-1].paintIcon(this,g,tempX_Snake_two[i],tempY_Snake_two[i]);
}
tempY_Snake_two[0]-=25;
}
else if(down_two)
{
snakeIcon_two= new ImageIcon("downmouth_snake_two.png");
if(tempY_Snake_two[0]>=1000)
{
tempY_Snake_two[0]=0;
}
snakeIcon_two.paintIcon(this,g,tempX_Snake_two[0],tempY_Snake_two[0]+25);
for(int i=size_two;i>=1;i--)//Body strat from 1..
{
tempX_Snake_two[i]=tempX_Snake_two[i-1];
tempY_Snake_two[i]=tempY_Snake_two[i-1];
snake_Body_two[i-1].paintIcon(this,g,tempX_Snake_two[i],tempY_Snake_two[i]);
}
tempY_Snake_two[0]+=25;
}
}
}
else
{
super.paintComponent(g);
g.setColor(Color.BLACK);
g.setFont(new Font("arial",Font.BOLD,20));
g.drawString("Game Over",400,500);
g.drawString("Press enter to restart the game",350,530);
}
}
public void setLeft_one()
{
if(!right_one)
{
left_one=true;
right_one=false;
up_one=false;
down_one=false;
stop=false;
}
}
public void setLeft_two()
{
if(!right_two)
{
left_two=true;
right_two=false;
up_two=false;
down_two=false;
stop=false;
}
}
public void setRight_one()
{
if(!left_one)
{
right_one=true;
left_one=false;
up_one=false;
down_one=false;
stop=false;
}
}
public void setRight_two()
{
if(!left_two)
{
right_two=true;
left_two=false;
up_two=false;
down_two=false;
stop=false;
}
}
public void setUp_one()
{
if(!down_one)
{
up_one=true;
left_one=false;
right_one=false;
down_one=false;
stop=false;
}
}
public void setUp_two()
{
if(!down_two)
{
up_two=true;
left_two=false;
right_two=false;
down_two=false;
stop=false;
}
}
public void setDown_one()
{
if(!up_one)
{
down_one=true;
up_one=false;
left_one=false;
right_one=false;
stop=false;
}
}
public void setDown_two()
{
if(!up_two)
{
down_two=true;
up_two=false;
left_two=false;
right_two=false;
stop=false;
}
}
public void setStart()
{
stop=false;
}
public void setStop()
{
stop=true;
}
public boolean Check_if_Snake_Eat_Food()
{
if((tempX_Snake_one[0]==tempX_Apple)&&(tempY_Apple==tempY_Snake_one[0]))
{
if(size_one<15)
{
size_one++;
}
return true;
}
if(is_one_player_game==2)
{
if((tempX_Snake_two[0]==tempX_Apple)&&(tempY_Apple==tempY_Snake_two[0]))
{
if(size_two<15)
{
size_two++;
}
return true;
}
else
{
return false;
}
}
return false;
}
public boolean Check_If_Snake_Eat_Itself()
{
for(int i=1;i<size_one;i++)
{
if(tempX_Snake_one[0]==tempX_Snake_one[i]&&tempY_Snake_one[0]==tempY_Snake_one[i])
{
System.out.printf("Snake 1 eat itself ");
game_over=true;
stop=true;
return true;
}
}
if(is_one_player_game==2)
{
for(int i=1;i<size_two;i++)
{
if(tempX_Snake_two[0]==tempX_Snake_two[i]&&tempY_Snake_two[0]==tempY_Snake_two[i])
{
System.out.printf("Snake 2 eat itself ");
game_over=true;
stop=true;
return true;
}
}
}
return false;
}
public boolean Check_If_Snake_Touch_Each_Other()
{
for(int i=0;i<size_two;i++)
{
if(tempX_Snake_one[0]==tempX_Snake_two[i]&&tempY_Snake_one[0]==tempY_Snake_two[i])
{
System.out.printf("Snake 1 touch snake 2 ");
System.out.printf("%d %d %d ",i,tempX_Snake_two[0],tempX_Snake_one[i]);
System.out.printf("%d %d %d ",i,tempY_Snake_two[0],tempY_Snake_one[i]);
game_over=true;
stop=true;
return true;
}
}
for(int i=0;i<size_one;i++)
{
if(tempX_Snake_two[0]==tempX_Snake_one[i]&&tempY_Snake_two[0]==tempY_Snake_one[i])
{
System.out.printf("Snake 2 touch snake 1 ");
System.out.printf("%d %d %d ",i,tempX_Snake_two[0],tempX_Snake_one[i]);
System.out.printf("%d %d %d ",i,tempY_Snake_two[0],tempY_Snake_one[i]);
game_over=true;
stop=true;
return true;
}
}
return false;
}
public boolean getGameOver()
{
return game_over;
}
public void reStart_the_game()
{
System.out.printf("haha ");
if(game_over)
{
System.out.printf("haha1 ");
for(int i=0;i<size_one;i++)
{
tempX_Snake_one[i]=0;
tempY_Snake_one[i]=0;
}
if(is_one_player_game==2)
{
for(int i=0;i<size_two;i++)
{
tempX_Snake_two[i]=0;
tempY_Snake_two[i]=0;
}
tempX_Snake_two[0]=(random.nextInt(40))*25;
tempY_Snake_two[0]=(random.nextInt(40))*25;
}
tempX_Snake_one[0]=(random.nextInt(40))*25;
tempY_Snake_one[0]=(random.nextInt(40))*25;
System.out.printf("%d %d ",tempX_Snake_one[0],tempX_Snake_two[0]);
System.out.printf("%d %d ",tempY_Snake_one[0],tempY_Snake_two[0]);
size_one=2;
size_two=2;
game_over=false;
stop=false;
}
}
public int get_X()
{
return tempX_Snake_one[0];
}
public int get_Y()
{
return tempY_Snake_one[0];
}
}
2. SnakeGame class code
package snakegame; // name of the package in which the class file of Snake will be stored
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
import java.util.*;
import javax.swing.Timer;
class KeyInput extends KeyAdapter
{
SnakeGame game;
public KeyInput(SnakeGame game)
{
this.game = game;
}
public void keyPressed(KeyEvent e)
{
game.keyPressed(e);
}
}
public class SnakeGame extends JFrame implements ActionListener
{
private Container c;
private int snakeLength =1;
private LinkedList<Point> snakeBody = new LinkedList<Point>();
private Point pointSnakeHead;
private Random random = new Random();
Snake snake;
private int is_one_player_game;
private int difficulty;
private Timer timer;
public SnakeGame(int is_one_player_game,int difficulty)
{
this.is_one_player_game=is_one_player_game;
this.difficulty=difficulty;
timer=new Timer(difficulty,this);
addKeyListener(new KeyInput(this));
Toolkit toolkit = Toolkit.getDefaultToolkit();
c=getContentPane();
setBackground(Color.WHITE);
snake = new Snake(is_one_player_game);
c.add(snake);
setVisible(true);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(((int)toolkit.getScreenSize().getWidth()-1000)/2,((int)toolkit.getScreenSize().getHeight()-1000)/2,1000,1000);
timer.start();
}
public void keyPressed(KeyEvent e)
{
int temp_key =e.getKeyCode();
if(is_one_player_game==1)
{
if(temp_key == KeyEvent.VK_UP)
{
snake.setUp_one();
}
else if(temp_key == KeyEvent.VK_DOWN)
{
snake.setDown_one();
}
else if(temp_key == KeyEvent.VK_LEFT)
{
snake.setLeft_one();
}
else if(temp_key == KeyEvent.VK_RIGHT)
{
snake.setRight_one();
}
else if(temp_key == KeyEvent.VK_W)
{
snake.setUp_one();
}
else if(temp_key == KeyEvent.VK_A)
{
snake.setLeft_one();
}
else if(temp_key == KeyEvent.VK_S)
{
snake.setDown_one();
}
else if(temp_key == KeyEvent.VK_D)
{
snake.setRight_one();
}
else if(temp_key == KeyEvent.VK_ENTER)
{
snake.reStart_the_game();
}
else if(temp_key == KeyEvent.VK_ESCAPE)
{
snake.setStop();
if (JOptionPane.showConfirmDialog(null, "Do You Really Want to exit?","Exit?", JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE)== JOptionPane.YES_OPTION)
{
JOptionPane.showConfirmDialog(null,"Written By LI_HING. "+"04/26/2017. "+"LI_HING@student.smc.edu ","See You!",JOptionPane.DEFAULT_OPTION);
System.exit(0);
}
else
{
snake.setStart();
}
}
}
else if(is_one_player_game==2)
{
if(temp_key == KeyEvent.VK_UP)
{
snake.setUp_two();
}
else if(temp_key == KeyEvent.VK_DOWN)
{
snake.setDown_two();
}
else if(temp_key == KeyEvent.VK_LEFT)
{
snake.setLeft_two();
}
else if(temp_key == KeyEvent.VK_RIGHT)
{
snake.setRight_two();
}
else if(temp_key == KeyEvent.VK_W)
{
snake.setUp_one();
}
else if(temp_key == KeyEvent.VK_A)
{
snake.setLeft_one();
}
else if(temp_key == KeyEvent.VK_S)
{
snake.setDown_one();
}
else if(temp_key == KeyEvent.VK_D)
{
snake.setRight_one();
}
else if(temp_key == KeyEvent.VK_ENTER)
{
snake.reStart_the_game();
}
else if(temp_key == KeyEvent.VK_ESCAPE)
{
snake.setStop();
if (JOptionPane.showConfirmDialog(null, "Do You Really Want to exit?","Exit?", JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE)== JOptionPane.YES_OPTION)
{
JOptionPane.showConfirmDialog(null,"Written By LI_HING. "+"04/26/2017. "+"LI_HING@student.smc.edu ","See You!",JOptionPane.DEFAULT_OPTION);
System.exit(0);
}
else
{
snake.setStart();
}
}
}
}
@Override
public void actionPerformed(ActionEvent args)
{
snake.repaint();
}
public static void main(String[] args){
// Logic to start the game
}
}
When you compiles the above classes with the java compiler, use the following command to compile them :-
1. javac -d . <classname>.java
This command will automatically create a folder with the name, which has been specified with the package keyword in java code for both the classes, converts them into their respecitve classes files, and then these class files will be stored in the package folder. Thus, specifying the package name with the package keyword in java code, helps to modularise the code into different classes in a particular package, which is a good programming practice in Java programming language.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.