亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

  免費注冊 查看新帖 |

Chinaunix

  平臺 論壇 博客 文庫
最近訪問板塊 發(fā)新帖
查看: 2586 | 回復(fù): 0
打印 上一主題 下一主題

java程序-貪吃蛇 [復(fù)制鏈接]

論壇徽章:
0
跳轉(zhuǎn)到指定樓層
1 [收藏(0)] [報告]
發(fā)表于 2007-09-30 14:00 |只看該作者 |倒序瀏覽

java版貪吃蛇小Game.







部分代碼.(剛學(xué)時寫的作業(yè),大家不要見笑,供比我還菜的菜鳥學(xué)習用.)


部分源代碼:
//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);
        //菜單制作結(jié)束
        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



//分數(shù)面班
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("版權(quán)所有(C)2006",10,210);
  g.drawString("  制作人-王老虎 ",10,300);
}

}


///////////////////
//
版本  幫助  對話框省略
//游戲結(jié)束控制
     JPanel p1,p2;
     Button btn1;
     JLabel lab1;
     String str[];
     public gameOver1() {
      super(PanelDemo.frame,"游戲結(jié)束",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);
       }
     }
      
   


//////////////////////////////////
蛇的創(chuàng)建
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
您需要登錄后才可以回帖 登錄 | 注冊

本版積分規(guī)則 發(fā)表回復(fù)

  

北京盛拓優(yōu)訊信息技術(shù)有限公司. 版權(quán)所有 京ICP備16024965號-6 北京市公安局海淀分局網(wǎng)監(jiān)中心備案編號:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年舉報專區(qū)
中國互聯(lián)網(wǎng)協(xié)會會員  聯(lián)系我們:huangweiwei@itpub.net
感謝所有關(guān)心和支持過ChinaUnix的朋友們 轉(zhuǎn)載本站內(nèi)容請注明原作者名及出處

清除 Cookies - ChinaUnix - Archiver - WAP - TOP