package snake; // a list of import data import java.awt.*; //puts all java events into the game import java.awt.event.ActionEvent; //all of the event methods and Vector import java.awt.event.ActionListener; import java.awt.event.InputEvent; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import java.util.Vector; import javax.swing.ActionMap; //all of the gui's swing methods import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.KeyStroke; //Each circle in snake body class Snake_mode{ int centerx, centery, radius, pcenterx, pcentery; Snake_mode(){ radius = 10; //radius is equal 10 } } //actual guide for game public class Snakethegame2 extends JFrame { snake panel = null; //sets the panels and Jmenubar to no value JMenuBar menu = null; JMenuBar info = null; JMenu help = null; JMenu controls = null; JMenuItem play = null; JMenuItem restart = null; JMenuItem instructions = null; JMenuItem email = null; JMenuItem Phonenumber = null; boolean firstplay = true; Snakethegame2(){ //name of class this.setResizable(false); //bars for the Jframe menu = new JMenuBar(); //starts up a menu bar info = new JMenuBar(); //allows to print a info bar //items for the game board in drop down bar play = new JMenuItem("Play Game"); //lets user see the bar says the quotated marks controls = new JMenu("Controls for Game"); //controls for the user to see help = new JMenu("Help"); //help on the user bar restart = new JMenuItem("Restart Game"); //name for user to click and restart game instructions = new JMenuItem("Instructions"); //instruction bar for game email = new JMenuItem("Email: Mason_schaefer@yahoo.com"); //email for help Phonenumber = new JMenuItem("For Phone Number, Email Me, Thanks!"); //phone number for help restart.setMnemonic(KeyEvent.VK_R); //shift r allows users to reset game restart.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_R, InputEvent.SHIFT_MASK)); restart.setMnemonic(KeyEvent.VK_I); //shift I allows user to see the instructions instructions.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_I, InputEvent.SHIFT_MASK)); play.setMnemonic(KeyEvent.VK_P); //alows user to play the game with shift play.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_P, InputEvent.SHIFT_MASK)); //adds play, restart, and instruction buttons on J-frame controls.add(play); //adds control play button controls.add(restart); //adds control button restard controls.add(instructions); //adds instructions button //adds email and phone number box into Jframe help.add(email); //adds email help.add(Phonenumber); //addes number //adds these bars to the actual Jframe menu.add(controls); //adds menu bar controls menu.add(help); //adds menu bar help //sets the menu bar this.setJMenuBar(menu); panel = new snake(); //new panel layout this.setLayout(new BorderLayout()); // sets new border this.setMinimumSize(new Dimension(800, 595));//sets the minimize screen of the output panel.dim = this.getSize(); //allows user to open bigger //boarder for the actual game this.add(panel, BorderLayout.CENTER); this.setLocationRelativeTo(null); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); play.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent eee){play();}}); //allows users to start restart.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent ee){restart();}}); //allows user to restart instructions.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent ee){instruct();}}); //allows user to see instructions this.addKeyListener(new KeyAdapter(){public void keyPressed(KeyEvent e){panel.keyPress(e);}}); //key pressed to see listener this.setVisible(true); this.pack(); } //****************************************************************************************** public void play(){ //this function if for the player to start thread for starting game if(firstplay){ panel.t.start(); firstplay = false; play.setEnabled(false); } } //************************************************************************************************ //function for restarting game, it will resize the snake to its initial position and initial length public void restart(){ int pos = 0; panel.vector.clear(); Snake_mode[] mode = new Snake_mode[5]; //body parts of the snake for(int i =0; i < 5; i++){ mode[i] = new Snake_mode(); mode[i].centerx = 200; mode[i].centery = 150-pos; mode[i].pcentery = 150-pos; mode[i].pcenterx = 200; panel.vector.add(mode[i]); //adds body part pos = pos+10; } panel.gameover = false; panel.dir = 'D'; panel.repaint(); } //function to be called on the click of instructions public void instruct(){ //message to user of game JOptionPane.showMessageDialog(this, "1. Shift+R - Restart \n2. J/Z - Increase Snake speed \n3. K/X - Decrease Snake speed \n4. LEFT/A - Turn left \n5. RIGHT/D - Turn right \n6. UP/W - go up \n7. DOWN/S - go down \n8. Shift+I - to view Instructions", "Instructions",1); } //object of the game class public static void main(String[] ar){ new Snakethegame2(); } } //a panel where all the drawings take place and it is placed. class snake extends JPanel implements Runnable{ Vector vector = null; Snake_mode[] modes = null; int centx = 0, centy = 0; int inc = 10, pos = 0; char dir = 'D'; Dimension dim = null; Thread t = null; boolean b = true, gameover = false; //drawing of snake to be replaced once the game resets snake(){ vector = new Vector(); modes = new Snake_mode[5]; for(int i =0; i < 5; i++){ modes[i] = new Snake_mode(); modes[i].centerx = 200; modes[i].centery = 150-pos; modes[i].pcentery = 150-pos; modes[i].pcenterx = 200; vector.add(modes[i]); pos = pos+10; } //a new thread t = new Thread(this); this.setLayout(null); this.setBackground(Color.black); //background color is set to black this.setDoubleBuffered(true); //double buffered dim = this.getSize(); this.addKeyListener(new KeyAdapter(){public void keyTyped(KeyEvent e){keyPress(e);}}); this.setVisible(true); } int time = 70; boolean gamepause = true; /*allows the user press the the up,down,left,or right buttons along with a,w,s,d buttons to move the snake*/ void keyPress(KeyEvent e){ if(e.getKeyCode() == KeyEvent.VK_DOWN ||e.getKeyCode() == KeyEvent.VK_S){ if(dir != 'U') dir = 'D'; } else if(e.getKeyCode() == KeyEvent.VK_UP || e.getKeyCode() == KeyEvent.VK_W){ if(dir != 'D') //if the user presses up or "w" the snake will turn up dir = 'U'; } else if(e.getKeyCode() == KeyEvent.VK_LEFT || e.getKeyCode() == KeyEvent.VK_A){ if(dir != 'R') //if the user presses left or "a" then the snake will turn left dir = 'L'; } else if(e.getKeyCode() == KeyEvent.VK_RIGHT || e.getKeyCode() == KeyEvent.VK_D){ if(dir != 'L') //if the user presses right or "d" then the snake will turn right dir = 'R'; } else if(e.getKeyCode() == KeyEvent.VK_ESCAPE) //if escape is pressed then the window closes System.exit(0); else if(e.getKeyCode() == KeyEvent.VK_SPACE){ //pauses the snake game if(gamepause){ t.suspend(); //invoking suspend on the the java program gamepause = false; } else{ t.resume(); //invoking resume on the java program gamepause = true; } } else if(e.getKeyCode() == KeyEvent.VK_J || e.getKeyCode() == KeyEvent.VK_Z){ time--; //slows down the snake by either pressing "j" or "z" } else if(e.getKeyCode() == KeyEvent.VK_K || e.getKeyCode() == KeyEvent.VK_X){ time++; //speed up the snake by either pressing "k" or "x" } } //generatres the food for the gameboard int foodx = 20+(int)(Math.random()*56)*10; int foody = 20+(int)(Math.random()*52)*10; int score = 0; //allows the user to paint colors on the board public void paintComponent(Graphics g){ super.paintComponent(g); Graphics2D g2 = (Graphics2D)g; //score equals the size of the body score = vector.size()-5; g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); int red = 30+(int)(Math.random()*220); //randomly goes through colors of food to make an assortment of colors int green = 30+(int)(Math.random()*220); //food color int blue = 30+(int)(Math.random()*220); //food color int black = 30+(int)(Math.random()*220); //food color g2.setColor(Color.white);//sets the color of gameboard g2.drawRect(600, 0, 199, 550); //drawing a rectangle g2.setColor(Color.red); //naming the color of the rectange a certain color g2.setFont(new Font("Arial", 1, 50)); //type of font g2.drawString("Snake", 630, 80); //snake is the type of font used, on the red rectangle g2.setFont(new Font("Arial", 1, 20)); //type of font used g2.drawString("By Mason Schaefer", 610, 160); //my name g2.setFont(new Font("Arial", 1, 20));// type of font g2.drawString("Score: "+score,650, 230); //total score accumulated g2.setFont(new Font("Arial", 1, 20)); //type of font used g2.drawString("High Score: ", 650, 200); //unused at the moment, work in progress but prints to the board g2.fillRect(0, 0, dim.width-200, dim.height); //board of the game g2.setColor(Color.white); //color of the board g2.fillRect(10, 10, dim.width-220, dim.height-65); g2.setColor(new Color(red, green, blue, black)); //color of the changing food so all users of color blind can see g2.fillOval(foodx, foody, 10, 10); //filling g2.setColor(Color.red); //fills the color of red on it //if game over then it will be display this on the screen if(gameover){ g2.setColor(Color.green); //prints texts in green g2.setFont(new Font("Monotype Corsiva", 1, 120)); g2.setColor(Color.green); //prints in green "you suck" g2.drawString("YOU SUCK!", 60, 260); g2.setColor(Color.green); //type of text g2.setFont(new Font("typewriter",1,14)); g2.setColor(Color.green); //prints the phrase get a job g2.drawString("Get A Job!", 260,300 ); g2.setColor(Color.green); //tells who its made by g2.drawString(" Proudly Made By", 220, 330); g2.setColor(Color.green); //tells the users name g2.drawString("Mason Schaefer", 255, 370); } //if the snake eats the game then it will fill an oval and add a piece of body to the snake else{ g2.fillOval(vector.get(0).centerx, vector.get(0).centery, vector.get(0).radius, vector.get(0).radius); for(int i=1; i < vector.size(); i++){ g2.fillOval(vector.get(i).centerx, vector.get(i).centery, vector.get(i).radius, vector.get(i).radius); vector.get(i).pcenterx = vector.get(i).centerx; //piece of snake body vector.get(i).pcentery = vector.get(i).centery; //piece of snake body vector.get(i).centerx = vector.get(i-1).pcenterx; //piece of snake body vector.get(i).centery = vector.get(i-1).pcentery; //piece of snake body } } } //important function which will see collision of snake with wall or itself, therefore rendering game over void wrongWay(int x, int y){ if(x < 10 || x > dim.width-220 || y < 10 || y > dim.height-61){ gameover = true; try{Thread.sleep(1000);}catch(Exception e){} repaint(); } // if the food is eaten and the wrongway is not entitled then the snake will add a circle to itself else if(x==foodx && y==foody){ Snake_mode modes = new Snake_mode(); //establishing a new snake mode modes.centerx = vector.get(vector.size()-1).pcenterx; modes.centery = vector.get(vector.size()-1).pcentery; vector.add(modes);//addes modes foodx = 20+(int)(Math.random()*56)*10; //randomly plops down food foody = 20+(int)(Math.random()*52)*10; //randomly pops down food } //if the gameover is true, then the thread sleeps, catch is accepting e and resetting snake for(int i=1; i < vector.size(); i++){ if(x == vector.get(i).centerx && y == vector.get(i).centery){ gameover = true; try{Thread.sleep(1000);}catch(Exception e){} repaint(); // when the game is reset then the board is reset } } } //controls for the game in a while loop public void run(){ while (true){ switch(dir){ case 'L'://controls when the buttons are pressed for moving to move the body of the snake vector.get(0).centerx = (vector.get(0).centerx-inc); vector.get(0).pcenterx = vector.get(0).centerx; wrongWay(vector.get(0).centerx, vector.get(0).centery); repaint(); break; case 'R': //controls when the buttons are pressed for moving to move the body of the snake vector.get(0).centerx = (vector.get(0).centerx+inc); vector.get(0).pcenterx = vector.get(0).centerx; wrongWay(vector.get(0).centerx, vector.get(0).centery); repaint(); break; case 'U': //controls when the buttons are pressed for moving to move the body of the snake vector.get(0).centery = (vector.get(0).centery-inc); vector.get(0).pcentery = vector.get(0).centery; wrongWay(vector.get(0).centerx, vector.get(0).centery); repaint(); break; case 'D': //controls when the buttons are pressed for moving to move the body of the snake vector.get(0).centery = (vector.get(0).centery+inc); vector.get(0).pcentery = vector.get(0).centery; wrongWay(vector.get(0).centerx, vector.get(0).centery); repaint(); break; } try{ Thread.sleep(time); } catch(Exception e){} } } }