Java writing greedy Snake games _java

Source: Internet
Author: User

Don't say much nonsense, directly to the code:

Frame.java

Package snake;
Import Java.awt.Graphics;
Import Java.awt.Menu;
Import Java.awt.MenuBar;
Import Java.awt.MenuItem;
Import java.awt.event.ActionEvent;
Import Java.awt.event.ActionListener;
Import java.awt.event.KeyEvent;
 
Import Java.awt.event.KeyListener;
 
Import Javax.swing.JFrame;
  public class Frame extends JFrame implements KeyListener {/** * */Boolean isAlive;
  Boolean Ispause;
  Panel panel;
  Character direction;
 
  Private static final long serialversionuid = 1L;
    Public Frame () {//TODO auto-generated constructor stub setdefaultcloseoperation (Jframe.exit_on_close);
    SetSize (300,300);
    Addkeylistener (this);
    panel = new Panel ();
    Add (panel);
    SetVisible (TRUE);
    IsAlive = true;
    Ispause = false; 
    Direction = new Character (' d ');
    MenuBar MenuBar = new MenuBar ();
    Menu menu = New Menu ("menu");
    MenuItem reset = new MenuItem ("Newgame");
     
    MenuItem pause= New MenuItem ("pause"); Pause.addactionlistener (New ActionListener () {@Override public void actionperformed (ActionEvent e) {//TODO auto-generated met
        Hod stub if (!ispause) ispause= true; 
      else ispause= false; 
     
    }
       
    });
        Reset.addactionlistener (new ActionListener () {@Override public void actionperformed (ActionEvent e) {
      TODO auto-generated Method stub reset ();
     
    }
       
    });
    Menu.add (reset);
    Menu.add (pause);
    Menubar.add (menu);
     
     
  Setmenubar (MenuBar);
    public void Reset () {panel.reset ();
  IsAlive = true; @Override public void keytyped (KeyEvent e) {//TODO auto-generated method stub} @Override Publ IC void keypressed (KeyEvent e) {//TODO auto-generated Method stub if (E.getkeycode () ==keyevent.vk_up) direction
    = ' W ';
    if (E.getkeycode () ==keyevent.vk_down) direction = ' s ';
    if (E.getkeycode () ==keyevent.vk_left) direction = ' a '; if (e.getkeycodE () ==keyevent.vk_right) direction = ' d ';  @Override public void keyreleased (KeyEvent e) {//TODO auto-generated method stub} public void
  Paint (Graphics g) {panel.repaint (); }
 
}

Launch.java

Package snake;
Import Java.util.Timer;
 
Import Java.util.TimerTask;
  public class Launch extends TimerTask {frame frame = new frame (); Public Launch () {//TODO auto-generated constructor stub} boolean Crashwall () {snakebody SB = Fram
    E.panel.snake.getfirst (); if ((sb.x<0) | | (sb.y<0) | | (Sb.x>=panel.line) | |
      (Sb.y>=panel.line))
    return true;
  else return false;
    } void Initial () {Frame.panel.snake.add (Newbody ()); 
  Frame.panel.food = Newbody (); @Override public void Run () {//TODO auto-generated Method stub if (Frame.panel.snake.isEmpty ()) Initi
    Al ();
        if (frame.isalive) if (!frame.ispause) {if (Gostraight ()) frame.isalive = false;
      Frame.repaint ();
  } if (Crashwall ()) frame.isalive = false;
 
    } snakebody Newbody () {snakebody sb = new Snakebody ();
    Boolean overlap = true;
      while (overlap) {Overlap =false; sb.x = (int) (Math.random ()* (panel.line-2) +1);
      SB.Y = (int) (Math.random () * (panel.line-2) +1); if (!frame.panel.snake.isempty ()) for (Snakebody s:frame.panel.snake) if (Sb.equals (s)) overlap =TR
    Ue
  return SB;
  } void Eat (Snakebody sb) {Frame.panel.snake.addFirst (SB);
     
    Boolean Gostraight () {Boolean result = false;
    Snakebody SB =new snakebody (Frame.panel.snake.getFirst ());
    Frame.panel.snake.removeLast ();
    if (frame.direction== ' W ') Sb.turnup ();
    if (frame.direction== ' s ') Sb.turndown ();
    if (frame.direction== ' a ') sb.turnleft ();
     
    if (frame.direction== ' d ') sb.turnright ();
    for (Snakebody s:frame.panel.snake) {if (Sb.equals (s)) result = true;
    } frame.panel.snake.addFirst (SB);
      if (Sb.equals (Frame.panel.food)) {if (frame.direction== ' W ') frame.panel.food.turnUp ();
      if (frame.direction== ' s ') Frame.panel.food.turnDown (); if (frame.direction== ' A ') frame.panel.food.turnLeft ();
      if (frame.direction== ' d ') frame.panel.food.turnRight ();
      Eat (Frame.panel.food);
    Frame.panel.food = Newbody ();
  return result; public static void Main (string[] args) {//TODO auto-generated method stub Launch timertask = new L
     Aunch ();
     
     Timer timer = new timer ();
  Timer.schedule (timertask,0,500); }
 
}

Panel.java

Package snake;
Import Java.awt.Color;
Import Java.awt.Graphics;
 
Import java.util.LinkedList;
 
 Import Javax.swing.JPanel;
  public class Panel extends JPanel {/** * * */private static final long serialversionuid = 1L;
  Public linkedlist<snakebody> snake = new linkedlist<snakebody> ();
  static final int line = 10;
    
  Snakebody food = new Snakebody ( -99,-99);  
  The public Panel () {//TODO auto-generated constructor stub} is public void Reset () {snake.clear ();
    public void Paint (Graphics g) {G.setcolor (color.white);
 
  
    G.fillrect (0, 0, getwidth (), getheight ());
      for (Snakebody sb:snake) {g.setcolor (color.black);
      G.drawrect (Sb.x*getwidth ()/line,sb.y*getheight ()/line,getwidth ()/line,getheight ());
      G.setcolor (Color.orange);
    G.fillrect (Sb.x*getwidth ()/line,sb.y*getheight ()/line,getwidth ()/line,getheight ());
    } g.setcolor (color.red); G.fillrect (Food.x*getwidth ()/line,food.y*getheiGht ()/line,getwidth ()/line,getheight ()/line); }
}

Snakebody.java

Package snake;
 
 Class Snakebody {
   
  int x;
  int y;
   
  Public Snakebody () {
    //TODO auto-generated constructor stub
    x = 0;
    y = 0;
  }
   
  Public snakebody (int a,int b) {
    x = A;
    y = b;
  }
  Public Snakebody (Snakebody SB) {This
    (SB.X,SB.Y);
  }
   
  public void Turnup () {
    y--;
  }
  public void Turndown () {
    y++;
  }
  public void TurnLeft () {
    x--;
  }
  public void TurnRight () {
    x + +;
  }
   
  Boolean equals (Snakebody s) {
    if (x==s.x) && (Y==S.Y)) return true;
    else return          false;
  }
 

The above is described in this article to share the greedy snake all the code, I hope to be able to master Java Help.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.