貪吃蛇小游戲編寫心得


https://github.com/Devilzero/6

http://www.cnblogs.com/liangjianming/p/4510346.html

package 貪吃蛇;

import java.awt.*; 

import java.awt.event.*; 

public class GreedSnake //主類 

 

/** 

* @param args 

*/ 

public static void main(String[] args) { 

new MyWindow(); 

 

 

class MyPanel extends Panel implements KeyListener,Runnable//自定義面板類,繼承了鍵盤和線程接口 

 

/**

 * 

 */

private static final long serialVersionUID = 6206093104274956657L;

Button snake[]; //定義蛇按鈕 

int shu=0; //蛇的節數 

int food[]; //食物數組 

boolean result=true; //判定結果是輸 還是贏 

Thread thread; //定義線程 

static int weix,weiy; //食物位置 

boolean t=true; //判定游戲是否結束 

int fangxiang=0; //蛇移動方向 

int x=0,y=0; //蛇頭位置 

MyPanel() 

 

setLayout(null); 

snake=new Button[20]; 

food=new int [20]; 

thread=new Thread(this); 

 

for(int j=0;j<20;j++) 

food[j]=(int)(Math.random()*99);//定義20個隨機食物 

 

weix=(int)(food[0]*0.1)*60; //十位*60為橫坐標 

weiy=(int)(food[0]%10)*40; //個位*40為縱坐標 

for(int i=0;i<20;i++) 

snake[i]=new Button(); 

 

add(snake[0]); 

snake[0].setBackground(Color.black); 

snake[0].addKeyListener(this); //為蛇頭添加鍵盤監視器 

snake[0].setBounds(0,0,10,10); 

setBackground(Color.cyan); 

 

public void run() //接收線程 

 

while(t) 

 

if(fangxiang==0)//向右 

try 

x+=10; 

snake[0].setLocation(x, y);//設置蛇頭位置 

 

if(x==weix&&y==weiy) //吃到食物 

shu++; 

weix=(int)(food[shu]*0.1)*60; 

weiy=(int)(food[shu]%10)*40; 

repaint(); //重繪下一個食物 

add(snake[shu]); //增加蛇節數和位置 

snake[shu].setBounds(snake[shu-1].getBounds()); 

Thread.sleep(100); //睡眠100ms 

catch(Exception e){} 

else if(fangxiang==1)//向左 

try 

x-=10; 

snake[0].setLocation(x, y); 

if(x==weix&&y==weiy) 

shu++; 

weix=(int)(food[shu]*0.1)*60; 

weiy=(int)(food[shu]%10)*40; 

repaint(); 

add(snake[shu]); 

snake[shu].setBounds(snake[shu-1].getBounds()); 

 

Thread.sleep(100); 

catch(Exception e){} 

else if(fangxiang==2)//向上 

try 

y-=10; 

snake[0].setLocation(x, y); 

if(x==weix&&y==weiy) 

shu++; 

weix=(int)(food[shu]*0.1)*60; 

weiy=(int)(food[shu]%10)*40; 

repaint(); 

add(snake[shu]); 

snake[shu].setBounds(snake[shu-1].getBounds()); 

Thread.sleep(100); 

catch(Exception e){} 

else if(fangxiang==3)//向下 

try 

y+=10; 

snake[0].setLocation(x, y); 

if(x==weix&&y==weiy) 

shu++; 

weix=(int)(food[shu]*0.1)*60; 

weiy=(int)(food[shu]%10)*40; 

repaint(); 

add(snake[shu]); 

snake[shu].setBounds(snake[shu-1].getBounds()); 

Thread.sleep(100); 

catch(Exception e){} 

int num1=shu; 

while(num1>1)//判斷是否咬自己的尾巴 

if(snake[num1].getBounds().x==snake[0].getBounds().x&&snake[num1].getBounds().y==snake[0].getBounds().y) 

t=false; 

result=false; 

repaint(); 

num1--; 

if(x<0||x>=this.getWidth()||y<0||y>=this.getHeight())//判斷是否撞牆 

t=false; 

result=false; 

repaint(); 

int num=shu; 

while(num>0) //設置蛇節位置 

snake[num].setBounds(snake[num-1].getBounds()); 

num--; 

 

if(shu==15) //如果蛇節數等於15則勝利 

t=false; 

result=true; 

repaint(); 

 

 

public void keyPressed(KeyEvent e) //按下鍵盤方向鍵 

if(e.getKeyCode()==KeyEvent.VK_RIGHT)//右鍵 

if(fangxiang!=1)//如果先前方向不為左 

fangxiang=0; 

 

else if(e.getKeyCode()==KeyEvent.VK_LEFT) 

{ if(fangxiang!=0) 

fangxiang=1; 

 

else if(e.getKeyCode()==KeyEvent.VK_UP) 

{ if(fangxiang!=3) 

fangxiang=2; 

 

else if(e.getKeyCode()==KeyEvent.VK_DOWN) 

{ if(fangxiang!=2) 

fangxiang=3; 

 

 

public void keyTyped(KeyEvent e) 

 

public void keyReleased(KeyEvent e) 

 

public void paint(Graphics g) //在面板上繪圖 

int x1=this.getWidth()-1; 

int y1=this.getHeight()-1; 

g.setColor(Color.red); 

g.fillOval(weix, weiy, 10, 10);//食物 

g.drawRect(0, 0, x1, y1); //牆 

if(t==false&&result==false) 

g.drawString("GAME OVER!", 250, 200);//輸出游戲失敗 

else if(t==false&&result==true) 

g.drawString("YOU WIN!", 250, 200);//輸出游戲成功 

 

class MyWindow extends Frame implements ActionListener//自定義窗口類 

/**

 * 

 */

private static final long serialVersionUID = -7766509774310515166L;

MyPanel my; 

Button btn; 

Panel panel; 

MyWindow() 

super("GreedSnake"); 

my=new MyPanel(); 

btn=new Button("begin"); 

panel=new Panel(); 

btn.addActionListener(this); 

panel.add(new Label("請先按begin后請再按Tab鍵選定蛇")); 

panel.add(btn); 

panel.add(new Label("按上下左右鍵控制蛇行動")); 

add(panel,BorderLayout.NORTH); 

add(my,BorderLayout.CENTER); 

setBounds(100,100,610,500); 

setVisible(true); 

validate(); 

addWindowListener(new WindowAdapter() 

public void windowClosing(WindowEvent e) 

System.exit(0); 

}); 

public void actionPerformed(ActionEvent e)//按下begin按鈕 

 

if(e.getSource()==btn) 

try 

my.thread.start(); //開始線程 

my.validate(); 

catch(Exception ee){} 

 

}

 

x+=10; 

snake[0].setLocation(x, y);//設置蛇頭位置 

 

if(x==weix&&y==weiy) //吃到食物 

shu++; 

weix=(int)(food[shu]*0.1)*60; 

weiy=(int)(food[shu]%10)*40; 

repaint(); //重繪下一個食物 

add(snake[shu]); //增加蛇節數和位置 

snake[shu].setBounds(snake[shu-1].getBounds());

 

Button snake[]; //定義蛇按鈕 

int shu=0; //蛇的節數 

int food[]; //食物數組 

boolean result=true; //判定結果是輸 還是贏 

Thread thread; //定義線程 

static int weix,weiy; //食物位置 

boolean t=true; //判定游戲是否結束 

int fangxiang=0; //蛇移動方向 

int x=0,y=0; //蛇頭位置 

學習心得
通過這次課程設計,我對java又有了更深刻的認識。也有了更深一步的喜愛,決定一定要努力學好java,並能更好的應用於以后的工作中。 
這次貪吃蛇的設計,做的不是很完美,也沒想象中的那么好,但也獲得了許多寶貴的經驗:

1) 在設計程序之前,務必要對自己所要設計的題目和內容有一個系統的,大概的了解,知道所設計的題目和內容包含哪些資源。 
2) 設計程序采用什么編程語言並不是非常重要,關鍵是要有一個清晰的思路和一個完整的軟件流程圖,所有要先把設計原理與思路搞清楚,再把流程圖畫出來,這樣設計起來就簡單多了。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM