欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

java编写贪吃蛇小游戏

程序员文章站 2024-03-02 15:37:58
废话不多说,直接奉上代码: frame.java package snake; import java.awt.graphics; import ja...

废话不多说,直接奉上代码:

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 method 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
  public 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 = frame.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())
      initial();
    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 =true;
    }
     
    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 launch();
     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);
    
  public panel() {
    // todo auto-generated constructor stub
  }
   
  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()/line);
      g.setcolor(color.orange);
      g.fillrect(sb.x*getwidth()/line,sb.y*getheight()/line,getwidth()/line,getheight()/line);
    }
    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;
  }
 
}

以上所述就是本文给大家分享的贪吃蛇的全部代码了,希望能够对大家熟练掌握java有所帮助。