diff --git a/G group Project evaluation 1/2dsnake/bin/Gameplay.class b/G group Project evaluation 1/2dsnake/bin/Gameplay.class new file mode 100644 index 0000000..2700501 Binary files /dev/null and b/G group Project evaluation 1/2dsnake/bin/Gameplay.class differ diff --git a/G group Project evaluation 1/2dsnake/bin/Main.class b/G group Project evaluation 1/2dsnake/bin/Main.class new file mode 100644 index 0000000..bd95622 Binary files /dev/null and b/G group Project evaluation 1/2dsnake/bin/Main.class differ diff --git a/G group Project evaluation 1/2dsnake/downmouth.png b/G group Project evaluation 1/2dsnake/downmouth.png new file mode 100644 index 0000000..ee9e2df Binary files /dev/null and b/G group Project evaluation 1/2dsnake/downmouth.png differ diff --git a/G group Project evaluation 1/2dsnake/enemy.png b/G group Project evaluation 1/2dsnake/enemy.png new file mode 100644 index 0000000..80674f9 Binary files /dev/null and b/G group Project evaluation 1/2dsnake/enemy.png differ diff --git a/G group Project evaluation 1/2dsnake/leftmouth.png b/G group Project evaluation 1/2dsnake/leftmouth.png new file mode 100644 index 0000000..6fc7a41 Binary files /dev/null and b/G group Project evaluation 1/2dsnake/leftmouth.png differ diff --git a/G group Project evaluation 1/2dsnake/rightmouth.png b/G group Project evaluation 1/2dsnake/rightmouth.png new file mode 100644 index 0000000..a3bcd28 Binary files /dev/null and b/G group Project evaluation 1/2dsnake/rightmouth.png differ diff --git a/G group Project evaluation 1/2dsnake/snakeimage.png b/G group Project evaluation 1/2dsnake/snakeimage.png new file mode 100644 index 0000000..a43ea41 Binary files /dev/null and b/G group Project evaluation 1/2dsnake/snakeimage.png differ diff --git a/G group Project evaluation 1/2dsnake/snaketitle.jpg b/G group Project evaluation 1/2dsnake/snaketitle.jpg new file mode 100644 index 0000000..2891bca Binary files /dev/null and b/G group Project evaluation 1/2dsnake/snaketitle.jpg differ diff --git a/G group Project evaluation 1/2dsnake/src/Gameplay.java b/G group Project evaluation 1/2dsnake/src/Gameplay.java new file mode 100644 index 0000000..028b751 --- /dev/null +++ b/G group Project evaluation 1/2dsnake/src/Gameplay.java @@ -0,0 +1,401 @@ +import java.awt.Color; +import java.awt.Font; +import java.awt.Graphics; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.awt.event.KeyEvent; +import java.awt.event.KeyListener; +import java.util.Random; + +import javax.swing.ImageIcon; +import javax.swing.JPanel; +import javax.swing.Timer; + + + +public class Gameplay extends JPanel implements KeyListener, ActionListener{ + + private int[] snakexlength=new int[750]; + private int[] snakeylength=new int[750]; + + + private int[] enemyxpos= {25,50,75,100,125,150,175,200,225,250,275,300, + 325,350,375,400,425,450,475,500,525,550,575,600,625,650,675,700 + ,725,750,775,800,825,850}; + private int[] enemyypos= {75,100,125,150,175,200,225,250,275,300,325,350, + 375,400,425,450,475,500,525,550,575,600,625}; + + private ImageIcon enemyimage; + + private Random random =new Random(); + + private int xpos=random.nextInt(34); + private int ypos=random.nextInt(23); + + private boolean left=false; + private boolean right=false; + private boolean up=false; + private boolean down=false; + + private int scores =0; + + private int moves =0; + + + private ImageIcon rightmouth; + private ImageIcon leftmouth; + private ImageIcon upmouth; + private ImageIcon downmouth; + + private int lengthofsnake=3 ; + + private Timer timer; + private int delay =50; + + + private ImageIcon snakeimage; + + + +private ImageIcon titleImage; + + + public Gameplay() + { + addKeyListener(this); + setFocusable(true); + setFocusTraversalKeysEnabled(false); + timer =new Timer(delay ,this); + timer.start(); + + } + + + + public void paint(Graphics g) + { + //start of gmae + if(moves ==0) + { + snakexlength[2]=50; + snakexlength[1]=75; + snakexlength[0]=100; + + snakeylength[2]=100; + snakeylength[1]=100; + snakeylength[0]=100; + + } + + + //draw title border + g.setColor(Color.white); + g.drawRect(24,10,851,55); + + //draw title + titleImage =new ImageIcon("snaketitle.jpg"); + titleImage.paintIcon(this, g, 25, 11); + + //draw border for gameplay + g.setColor(Color.WHITE); + g.drawRect(24, 74, 851,577); + + //draw background for gamepaly + g.setColor(Color.black); + g.fillRect(25, 75, 850, 575); + + //draw scores + g.setColor(Color.white); + g.setFont(new Font("arial", Font.PLAIN, 22)); + g.drawString("Scores - "+scores, 750, 40); + + //draw length of snake + /*g.setColor(Color.white); + g.setFont(new Font("arial", Font.PLAIN, 14)); + g.drawString("length - "+lengthofsnake, 780, 50);*/ + + rightmouth = new ImageIcon("rightmouth.png"); + rightmouth.paintIcon(this , g , snakexlength[0] , snakeylength[0]); + + for(int a=0;a=0;r--) + { + snakeylength[r+1]=snakeylength[r]; + } + for(int r=lengthofsnake;r>=0;r--) + { + if(r==0) + { + snakexlength[r] = snakexlength[r]+25; + } + else + { + snakexlength[r]=snakexlength[r-1]; + } + if(snakexlength[r]>850) + { + snakexlength[r] =25; + } + + } + repaint(); + + } + if(left) + { + for(int r=lengthofsnake-1;r>=0;r--) + { + snakeylength[r+1]=snakeylength[r]; + } + for(int r=lengthofsnake;r>=0;r--) + { + if(r==0) + { + snakexlength[r] = snakexlength[r]-25; + } + else + { + snakexlength[r]=snakexlength[r-1]; + } + if(snakexlength[r]<25) + { + snakexlength[r] =850; + } + + } + repaint(); + + } + if(up) + { + for(int r=lengthofsnake-1;r>=0;r--) + { + snakexlength[r+1]=snakexlength[r]; + } + for(int r=lengthofsnake;r>=0;r--) + { + if(r==0) + { + snakeylength[r] = snakeylength[r]-25; + } + else + { + snakeylength[r]=snakeylength[r-1]; + } + if(snakeylength[r]<75) + { + snakeylength[r] =625; + } + + } + repaint(); + } + if(down) + { + for(int r=lengthofsnake-1;r>=0;r--) + { + snakexlength[r+1]=snakexlength[r]; + } + for(int r=lengthofsnake;r>=0;r--) + { + if(r==0) + { + snakeylength[r] = snakeylength[r]+25; + } + else + { + snakeylength[r]=snakeylength[r-1]; + } + if(snakeylength[r]>625) + { + snakeylength[r] =75; + } + + } + repaint(); + } + + } + + + @Override + public void keyTyped(KeyEvent e) { + + + } + + @Override + public void keyPressed(KeyEvent e) { + + if(e.getKeyCode()== KeyEvent.VK_SPACE) + { + moves=0; + scores=0; + lengthofsnake=3; + repaint(); + } + + + + + if(e.getKeyCode()== KeyEvent.VK_RIGHT) + { + moves++; + right=true; + if(!left) + { + right=true; + } + else + { + right=false; + left=true; + } + + up=false; + down=false; + } + + if(e.getKeyCode()== KeyEvent.VK_LEFT) + { + moves++; + left=true; + if(!right) + { + left=true; + } + else + { + left=false; + right=true; + } + + up=false; + down=false; + } + + if(e.getKeyCode()== KeyEvent.VK_UP) + { + moves++; + up=true; + if(!down) + { + up=true; + } + else + { + up=false; + down=true; + } + + left=false; + right=false; + } + + if(e.getKeyCode()== KeyEvent.VK_DOWN) + { + moves++; + down=true; + if(!up) + { + down=true; + } + else + { + down=false; + up=true; + } + + left=false; + right=false; + } + + } + + @Override + public void keyReleased(KeyEvent e) { + // TODO Auto-generated method stub + + } + + + +} diff --git a/G group Project evaluation 1/2dsnake/src/Main.java b/G group Project evaluation 1/2dsnake/src/Main.java new file mode 100644 index 0000000..24cf723 --- /dev/null +++ b/G group Project evaluation 1/2dsnake/src/Main.java @@ -0,0 +1,17 @@ +import java.awt.Color; +import javax.swing.JFrame; +public class Main { + + public static void main(String[] args) { + + JFrame obj=new JFrame(); + Gameplay gameplay=new Gameplay(); + obj.setBounds(10,10,905,700); + obj.setBackground(Color.DARK_GRAY); + obj.setResizable(false); + obj.setVisible(true); + obj.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + obj.add(gameplay); + } + +} diff --git a/G group Project evaluation 1/2dsnake/upmouth.png b/G group Project evaluation 1/2dsnake/upmouth.png new file mode 100644 index 0000000..ae25c9d Binary files /dev/null and b/G group Project evaluation 1/2dsnake/upmouth.png differ