當前位置:首頁 » 編程語言 » 貪吃蛇代碼java

貪吃蛇代碼java

發布時間: 2025-09-20 15:04:45

Ⅰ 求個簡單點的java程序 100行左右。 需要解釋。

貪吃蛇游戲 望採納

import java.awt.Button;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.Point;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.*;

import javax.swing.JFrame;
import javax.swing.JOptionPane;

public class Snake extends JFrame implements KeyListener{
int Count=0;
Button[][] grid = new Button[20][20];
ArrayList<Point> snake_list=new ArrayList<Point>();
Point bean=new Point(-1,-1);//保存隨機豆子【坐標】
int Direction = 1; //方向標志 1:上 2:下 3:左 4:右
//構造方法
public Snake()
{
//窗體初始化
this.setBounds(400,300,390,395);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GridLayout f=new GridLayout(20,20);
this.getContentPane().setBackground(Color.gray);
this.setLayout(f);

//初始化20*20個按鈕
for(int i=0;i<20;i++)
for(int j=0;j<20;j++)
{
grid[i][j]=new Button();
this.add(grid[i][j]);
grid[i][j].setVisible(false);
grid[i][j].addKeyListener(this);
grid[i][j].setBackground(Color.blue);
}
//蛇體初始化
grid[10][10].setVisible(true);
grid[11][10].setVisible(true);
grid[12][10].setVisible(true);
grid[13][10].setVisible(true);
grid[14][10].setVisible(true);

//在動態數組中保存蛇體按鈕坐標【行列】信息
snake_list.add(new Point(10,10));
snake_list.add(new Point(11,10));
snake_list.add(new Point(12,10));
snake_list.add(new Point(13,10));
snake_list.add(new Point(14,10));

this.rand_bean();
this.setTitle("總分:0");
this.setVisible(true);
}

//該方法隨機一個豆子,且不在蛇體上,並使豆子可見
public void rand_bean(){
Random rd=new Random();
do{
bean.x=rd.nextInt(20);//行
bean.y=rd.nextInt(20);//列
}while(snake_list.contains(bean));
grid[bean.x][bean.y].setVisible(true);
grid[bean.x][bean.y].setBackground(Color.red);
}
//判斷擬增蛇頭是否與自身有碰撞
public boolean is_cross(Point p){
boolean Flag=false;
for(int i=0;i<snake_list.size();i++){
if(p.equals(snake_list.get(i) )){
Flag=true;break;
}
}
return Flag;
}
//判斷蛇即將前進位置是否有豆子,有返回true,無返回false
public boolean isHaveBean(){
boolean Flag=false;
int x=snake_list.get(0).x;
int y=snake_list.get(0).y;
Point p=null;
if(Direction==1)p=new Point(x-1,y);
if(Direction==2)p=new Point(x+1,y);
if(Direction==3)p=new Point(x,y-1);
if(Direction==4)p=new Point(x,y+1);
if(bean.equals(p))Flag=true;
return Flag;
}

//前進一格
public void snake_move(){

if(isHaveBean()==true){//////////////有豆子吃
Point p=new Point(bean.x,bean.y);//【很重要,保證吃掉的是豆子的復制對象】
snake_list.add(0,p); //吃豆子
grid[p.x][p.y].setBackground(Color.blue);
this.Count++;
this.setTitle("總分:"+Count);
this.rand_bean(); //再產生一個豆子
}else{///////////////////無豆子吃
//取原蛇頭坐標
int x=snake_list.get(0).x;
int y=snake_list.get(0).y;
//根據蛇頭坐標推算出擬新增蛇頭坐標
Point p=null;
if(Direction==1)p=new Point(x-1,y);//計算出向上的新坐標
if(Direction==2)p=new Point(x+1,y);//計算出向下的新坐標
if(Direction==3)p=new Point(x,y-1);//計算出向左的新坐標
if(Direction==4)p=new Point(x,y+1);//計算出向右的新坐標
//若擬新增蛇頭碰壁,或纏繞則游戲結束
if(p.x<0||p.x>19|| p.y<0||p.y>19||is_cross(p)==true){
JOptionPane.showMessageDialog(null, "游戲結束!");
System.exit(0);
}
//向蛇體增加新的蛇頭坐標,並使新蛇頭可見
snake_list.add(0,p);
grid[p.x][p.y].setVisible(true);
//刪除原蛇尾坐標,使蛇尾不可見
int x1=snake_list.get(snake_list.size()-1).x;
int y1=snake_list.get(snake_list.size()-1).y;
grid[x1][y1].setVisible(false);
snake_list.remove(snake_list.size()-1);
}
}

@Override
public void keyPressed(KeyEvent e) {
if(e.getKeyCode()==KeyEvent.VK_UP && Direction!=2) Direction=1;
if(e.getKeyCode()==KeyEvent.VK_DOWN && Direction!=1) Direction=2;
if(e.getKeyCode()==KeyEvent.VK_LEFT && Direction!=4) Direction=3;
if(e.getKeyCode()==KeyEvent.VK_RIGHT && Direction!=3) Direction=4;
}
@Override
public void keyReleased(KeyEvent e) { }
@Override
public void keyTyped(KeyEvent e) { }

public static void main(String[] args) throws InterruptedException {
Snake win=new Snake();
while(true){
win.snake_move();
Thread.sleep(300);
}
}
}

Ⅱ 如何用Java語言寫一個貪吃蛇游戲

  • 設計游戲,首先就要設計界面。首先看一下我設計的一個界面。界面分為左邊的游戲區與右邊的控制區。游戲區包含「得分信息」和貪吃蛇的游戲區,右邊控制區有「開始」「暫停」「停止」按鈕,等級選擇單選框以及游戲排行榜。

Ⅲ 求各種各樣的小游戲的源代碼,比如:貪吃蛇、推箱子、俄羅斯方塊、五子棋等,最好是.NET的,JAVA也行。

我有java的,你可以看看:一個拼圖
import java.lang.Math.*;
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;

class MainFrame extends JFrame implements ActionListener{ //定義整個框架
private JButton[] jb = new JButton[8];
private JButton jbs = new JButton("開 局");
private JButton jbres = new JButton("重新開始");
private JPanel jp1 = new JPanel();
private JPanel jp2 = new JPanel();
private int[] n = new int[9];
private int[] n1 = new int[9];
private int position = 8,p,q;
private boolean bl,startbl=false;
private JLabel jl = new JLabel();
private int count = 0;
private JLabel jl1 = new JLabel(" "+Integer.toString(0));

public MainFrame(){ //框架的構造方法
int i;
for(int j = 0; j < n.length; j++){
n[j] = j;
n1[j] = n[j];
}
for(i = 0; i < jb.length; i++){ //給每個按鈕賦相應的值,並注監聽器
jb[i] = new JButton(Integer.toString(i+1));
jb[i].setFont(new Font("宋體",Font.BOLD,48));
jp2.add(jb[i]);
jb[i].addActionListener(this);
}
for(i = 0; i < n.length; i++){
if(n[i] == position)
jp2.add(jl);
else
jp2.add(jb[n[i]]);
}
jp2.setLayout(new GridLayout(3,3));//注冊監聽器
jbs.addActionListener(this);
jbres.addActionListener(this);
jp1.add(jbs);
jp1.add(jbres);
jp1.add(jl1);
jp1.setLayout(new FlowLayout()); //將jp1設置為流布局
setLayout(new BorderLayout()); //整體布局為邊界布局
this.add("North",jp1);
this.add("Center",jp2);
this.setTitle("拼圖游戲");
this.setBounds(100,100,300,350);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //實現關閉按鈕
this.setResizable(false);
this.setVisible(true);
}

public void actionPerformed(ActionEvent e){ //實現按鈕的事件

if(e.getSource()==jbres){ // 重新開始按鈕事件
for(int j = 0; j<n.length;j++)
n[j] = n1[j];
reShow();
startbl=true;
count = 0;
jl1.setText(" "+Integer.toString(0));
}

else if(e.getSource()==jbs) //開局按鈕事件
this.Init();
else if(startbl){ //按鈕1-8移動事件
for(int i = 0; i < jb.length; i++)
if(e.getSource() == jb[i]){
//System.out.println(i+1);
for(int a=0;a<n.length;a++){
if(n[a]==i)
p=a;
if(n[a]==position)
q=a;
}
}

if(p != 0 && p != 1 && p != 2)
if((p-3) == q)
swap(p,q);

if(p != 0 && p != 3 && p != 6)
if((p-1) == q)
swap(p,q);

if(p != 2 && p != 5 && p != 8)
if((p+1) == q)
swap(p,q);

if(p != 6 && p != 7 && p != 8)
if((p+3) == q)
swap(p,q);

}
}

public void swap(int x,int y){ //按鈕1-8與空白圖片交換
int z;
z = n[x];
n[x] = n[y];
n[y]=z;
jl1.setText(" "+Integer.toString(++count));
reShow();
win();

}

public void Init(){ //隨機產生游戲界面
int i=0,j,x;
boolean bl ;
while(i<9){
bl = true;
x=(int)(Math.random()*9);
for(j=0;j<i;j++)
if(n[j] == x)
bl=false;
if(bl){
n [i++] = x;
n1[i-1] = x;
}
}
reShow();
startbl=true;
count = 0;
jl1.setText(" "+Integer.toString(0));

}

public void reShow(){ //對游戲界面的重寫
for(int i = 0; i < n.length; i++){
if(n[i] == position)
jp2.add(jl);
else

jp2.add(jb[n[i]]);
}
jp2.revalidate();
}

public void win(){ //判斷是否成功
boolean winbl=true;
for(int i=0;i<n.length;i++)
if(n[i]!=i)
winbl=false;
if(winbl){
JOptionPane.showMessageDialog(this,"祝賀你,你成功了! "+"你用了"+Integer.toString(count)+"步","",JOptionPane.INFORMATION_MESSAGE);
startbl=false;
}
}

}

public class Collage { // 主函數類
public static void main(String[] args){
new MainFrame();
}
}
自已以前編的,不是很好,你就參考參考吧

熱點內容
自己電腦怎麼架設雲伺服器 發布:2025-09-20 17:28:46 瀏覽:632
php下載地址 發布:2025-09-20 17:28:20 瀏覽:51
手機透明文件夾 發布:2025-09-20 17:27:44 瀏覽:144
上傳星標截圖 發布:2025-09-20 17:24:38 瀏覽:7
廈門電信代理伺服器ip 發布:2025-09-20 17:24:26 瀏覽:130
三星s6熱點怎麼設密碼 發布:2025-09-20 17:23:33 瀏覽:50
mtp大容量存儲 發布:2025-09-20 17:19:08 瀏覽:292
風水網源碼 發布:2025-09-20 17:17:34 瀏覽:545
php字元串整數 發布:2025-09-20 17:10:00 瀏覽:130
php雙引號變數 發布:2025-09-20 17:09:10 瀏覽:928