免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2552 | 回复: 0
打印 上一主题 下一主题

java程序-贪吃蛇 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-09-30 14:00 |只看该作者 |倒序浏览

java版贪吃蛇小Game.







部分代码.(刚学时写的作业,大家不要见笑,供比我还菜的菜鸟学习用.)


部分源代码:
//PaneDemo类
public class PanelDemo {
    public static PanelDemoFrame frame;
    public static void main(String[] args) {
        frame = new PanelDemoFrame();
        frame.setVisible(true);
    }
}

//PaneDemoFrame 类
ublic class PanelDemoFrame extends JFrame implements WindowListener,ActionListener{
  public static gamePanel game;
  public static score score1;
  JPanel p1;
  JLabel lab1,lab2,lab3,lab4;
  Color cor;
        public PanelDemoFrame() {
        setTitle("PanelDemo");
        this.setBounds(300,220,430,370);
        this.setVisible(true);
        //菜单制作
    ..........................................

..................................................
........................................................
        menuFilecontinue.addActionListener(this);
        menuFilePause.addActionListener(this);
        menuFileExit.addActionListener(this);
        menuVersion.addActionListener(this);
        menuHelp.addActionListener(this);
        setRed.addActionListener(this);
        setGreen.addActionListener(this);
        setBlue.addActionListener(this);
        setdef.addActionListener(this);
        //菜单制作结束
        addWindowListener(this);
        p1=new JPanel();
        p1.setLayout(new GridLayout(4,1));
        lab1=new JLabel();
        lab2=new JLabel();
        lab3=new JLabel();
        lab4=new JLabel();
        lab1.setText("  级别(速度): "+score1.Speed );
       .................................................

.........................
        
        p1.add(lab1);
        p1.add(lab2);
        p1.add(lab3);
        p1.add(lab4);
        game=new gamePanel();
        score1=new score();
        p1.setBorder(BorderFactory.createTitledBorder("成绩单"));
        this.getContentPane().add("East",score1);
        this.getContentPane().add("Center",game);
        game.requestFocus();
        }
        ////////////////////////////////////////////////
         public void actionPerformed(ActionEvent e)
{
  String arg=(String)e.getActionCommand();

...................................................
...................................................
...................................................
    else if(arg.equals("蓝色"))
  {
   game.setBackground(Color.blue);
   repaint();
  }
  else if(arg.equals("绿色"))
  {
   game.setBackground(Color.green);
   repaint();
  }
  else if(arg.equals("默认"))
  {
   game.setBackground(Color.lightGray);
   repaint();
  }
  
}
     
}


///游戏面板类
public class gamePanel extends JPanel implements KeyListener,Runnable  {
Vetex vex;
public static int x,y;
    Thread runner;
    public static int status =2; //1-向上 2-向下 3-向左 4-向右
public gamePanel()
{
  this.setBackground(Color.green);
  this.requestFocus();
  addKeyListener(this);
  this.setBorder(BorderFactory.createTitledBorder("游戏窗口"));
  vex=new Vetex();
  runner=new Thread(this);
  runner.start();
}

public void paintComponent(Graphics g)
{
  super.paintComponent(g);
  ///////////////////////////////////
  
  for (int j=0;j



//分数面班
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class score extends JPanel {
public static int Score=0;
    public static int Speed=0;
    JLabel lab;
Color cor;
    public score()
{
  this.setBackground(Color.cyan);
  this.setBorder(BorderFactory.createTitledBorder("成绩单"));
  lab=new JLabel();
  lab.setText("                              ");
        add(lab);
    }
public void paintComponent(Graphics g)
{
  super.paintComponent(g);
  cor=new Color(195,50,150);
  g.drawString("  级别(速度): "+Speed,10,40);
  g.drawString("       得分:  "+Score ,10,120);
  g.drawString("版权所有(C)2006",10,210);
  g.drawString("  制作人-王老虎 ",10,300);
}

}


///////////////////
//
版本  帮助  对话框省略
//游戏结束控制
     JPanel p1,p2;
     Button btn1;
     JLabel lab1;
     String str[];
     public gameOver1() {
      super(PanelDemo.frame,"游戏结束",true);
      this.setBounds(400,320,200,100);
      p1=new JPanel();
      p2=new JPanel();
      str=new String[2];
      btn1=new Button("确定");
      lab1=new JLabel();
      lab1.setText(" GAME OVER!");
      p1.add(lab1);
      p2.add(btn1);
      btn1.addActionListener(this);
      this.getContentPane().add(p1,BorderLayout.CENTER);
      this.getContentPane().add(p2,BorderLayout.SOUTH);
      }
     public void actionPerformed(ActionEvent e)
     {
      if(e.getActionCommand()=="确定"){
       System.exit(0);
       }
     }
      
   


//////////////////////////////////
蛇的创建
Cell object,initline;
    public Vetex()
{
  object=new Cell(random.nextInt(28),random.nextInt(28));
  initline=new Cell(random.nextInt(29),random.nextInt(9));
  Line.addElement(initline);
}
public void move()
{
    Line.insertElementAt(nextLocation(),0);
  if(nextLocation().x!=object.x||nextLocation().y!=object.y)
  {
   Line.removeElementAt(Line.size()-1);
  }
  else
  {
   y==object.y){ break;}
    // else
    // {test=true; break;}
       //}
      //}
      PanelDemoFrame.score1.Score+=10;
   PanelDemoFrame.score1.Speed=PanelDemoFrame.score1.Score/200;
   PanelDemoFrame.score1.repaint();
  
    }
}
public Cell nextLocation()
{
  int x,y;

  switch(gamePanel.status)
  {
   case 1: y-=1;  break;
   case 2: y+=1;  break;
   case 3: x-=1;  break;
   case 4: x+=1;  break;
   default : break;
  }
  return new Cell(x,y);
}
public boolean isdad()
{
  for(int i=0;i


本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u1/49228/showart_393096.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP