三周前,就与同学约定好了,花三周时间写个贪吃蛇,就用自己现在所学的Java,我想可以啊。学校里教不出什么东西,那就自己搞吧!于是我们信誓旦旦开始了几天。不料五一一来,把所有的都拉下了。最后一周了,没办法,通宵吧!于是连续几天苦干,终于写出来了.虽然有点粗糙,因为学校还没开始教GUI和线程,以后我会改善这代码的
import java.awt.*;
import java.awt.event.*;
import java.util.*;
//===================该类是控制蛇移动的窗口===================================================
class snakeCanvas extends Panel implements Runnable, KeyListener
{
private Label b[] ; //蛇的主体
private Button food ; // 食物
private Thread thread; //线程
private int count = 1, i; //count表示蛇的数量
private int xFood, yFood ; //表示食物的坐标
private int x = 5, y = 0; //控制蛇头的坐标
private boolean up = false, down = false, right = true, left = false; //用来控制蛇的方向
private boolean Game = true ; // 判断游戏是否结束
snakeCanvas()
{
setSize( 510, 515 );
thread = new Thread(this);
setLayout(null);
setBackground(Color.green); //设置背景颜色
b = new Label[50] ;
//对蛇的初始化
for ( int i = 0; i < 30; i++ )
{
b[i] = new Label("口") ;
b[i].setSize( 10, 10 ) ;
}
b[0].setBounds(5, 0, 10, 10);
add(b[0]); //添加组件
//====================食物的随机坐标=================================
xFood = 10*(int)( ( Math.random() + 0.01 ) * 50 ) + 5 ;
yFood = 10*(int)( ( Math.random() + 0.01 ) * 50 ) ;
}
public void keyTyped( KeyEvent e){}
public void keyReleased(KeyEvent e){}
//=========键盘事件=========================
public void keyPressed(KeyEvent e)
{
if ( e.getKeyCode() == KeyEvent.VK_DOWN && up == false )
{
up = false ;
down = true ;
right = false ;
left = false ;
}
else if (e.getKeyCode() == KeyEvent.VK_UP && down == false )
{
up = true;
down = false ;
right = false ;
left = false ;
}
else if (e.getKeyCode() == KeyEvent.VK_LEFT && right == false )
{
up = false;
down = false ;
right = false ;
left = true ;
}
else if (e.getKeyCode() == KeyEvent.VK_RIGHT && left == false )
{
up = false;
down = false ;
right = true ;
left = false ;
}
}
public void start() //线程启动
{
requestFocus(); //获取键盘焦点
addKeyListener(this) ;
thread.start();
}
public void stop()
{
Game = false ;
}
public void restart() // 再来一次游戏
{
x = 5 ;
y = 0 ;
up = false;
down = false ;
right = true ;
left = false ;
Game = true ;
repaint();
}
public void run() //运行线程
{
while(Game) //判断是否结束游戏
{
for (; x <= 500; x += 10 )
{
/*
**此循环是控制向右方向,并且判断蛇是否碰到自己的身体,如果碰到身体,设置Game为false,游戏结束
*/
if ( right && Game )
{
try{
//如果蛇吃到食物
if ( x == xFood && y == yFood )
{
xFood = 10*(int)( ( Math.random() + 0.01 ) * 50 ) + 5 ;
yFood = 10*(int)( ( Math.random() + 0.01 ) * 50 ) ;
add( b[count++] );
repaint();
}
//设置蛇的移动位置
for ( i = count-1; i > 0; i-- )
{
b[i].setLocation( b[i-1].getX(), b[i-1].getY() );
}
b[i].setLocation( x, y );
//判断蛇是否碰到身体,如有就设置Game为false
for ( i = 4; i < count ; i++ )
{
if ( x == b[i].getX() && y == b[i].getY() )
{
Game = false ;
break ;
}
}
thread.sleep(100);
}catch(InterruptedException e){
System.out.println("n");
}
}
else
break;
}
//判断蛇是否碰墙以及游戏是否结束
if ( x > 500 || Game == false )
{
Game = false ;
repaint();
continue ;
}
for (; y <= 500; y += 10 )
{
/*
**此循环是控制向下方向,并且判断蛇是否碰到自己的身体,如果碰到身体,设置Game为false,游戏结束
*/
if ( down && Game )
{
try
{
//如果蛇吃到食物
if ( x == xFood && y == yFood )
{
xFood = 10*(int)( ( Math.random() + 0.01 ) * 50 ) + 5 ;
yFood = 10*(int)( ( Math.random() + 0.01 ) * 50 ) ;
add( b[count++] );
repaint();
}
//设置蛇的移动位置
for ( i = count-1; i > 0; i-- )
{
b[i].setLocation( b[i-1].getX(), b[i-1].getY() );
}
b[i].setLocation( x, y );
//判断蛇是否碰到身体,如有就设置Game为false
for ( i = 4; i < count ; i++ )
{
if ( x == b[i].getX() && y == b[i].getY() )
{
Game = false ;
break ;
}
}
thread.sleep(100);
}catch(InterruptedException e){
System.out.println("n");
}
}
else
break;
}
//判断蛇是否碰墙以及游戏是否结束
if ( y > 500 || Game == false)
{
Game = false ;
repaint();
continue;
}
for (; x >= 5; x -= 10 )
{
/*
**此循环是控制向右方向,并且判断蛇是否碰到自己的身体,如果碰到身体,设置Game为false,游戏结束
*/
if ( left && Game )
{
try{
//如果蛇吃到食物
if ( x == xFood && y == yFood )
{
xFood = 10*(int)( ( Math.random() + 0.01 ) * 50 ) + 5 ;
yFood = 10*(int)( ( Math.random() + 0.01 ) * 50 ) ;
add( b[count++] );
repaint();
}
//设置蛇的移动位置
for ( i = count-1; i > 0; i-- )
{
b[i].setLocation( b[i-1].getX(), b[i-1].getY() );
}
b[i].setLocation( x, y );
//判断蛇是否碰到身体,如有就设置Game为false
for ( i = 4; i < count ; i++ )
{
if ( x == b[i].getX() && y == b[i].getY() )
{
Game = false ;
break ;
}
}
thread.sleep(100);
}catch(InterruptedException e){
System.out.println("n");
}
}
else
break;
}
//判断蛇是否碰墙以及游戏是否结束
if ( x < 5 ||Game == false )
{
Game = false ;
repaint();
continue ;
}
for (; y >= 0; y -= 10 )
{
if ( up && Game )
{
/*
**此循环是控制向右方向,并且判断蛇是否碰到自己的身体,如果碰到身体,设置Game为false,游戏结束
*/
try{
//如果蛇吃到食物
if ( x == xFood && y == yFood )
{
xFood = 10*(int)( ( Math.random() + 0.01 ) * 50 ) + 5 ;
yFood = 10*(int)( ( Math.random() + 0.01 ) * 50 ) ;
add( b[count++] );
repaint();
}
//设置蛇的移动位置
for ( i = count-1; i > 0; i-- )
{
b[i].setLocation( b[i-1].getX(), b[i-1].getY() );
}
b[i].setLocation( x, y );
//判断蛇是否碰到身体,如有就设置Game为false
for ( i = 4; i < count ; i++ )
{
if ( x == b[i].getX() && y == b[i].getY() )
{
Game = false ;
break ;
}
}
thread.sleep(100);
}catch(InterruptedException e){
System.out.println("n");
}
}
else
break;
}
//判断蛇是否碰墙以及游戏是否结束
if ( y < 0 || Game == false )
{
Game = false ;
repaint();
continue ;
}
}
//stop( this) ;
}
public void paint(Graphics g)
{
g.drawRect( 5, 1, 500, 509) ; //描绘边框
g.drawRect( xFood, yFood, 10, 10) ; //描绘食物
if ( !Game ) // 判断游戏是否结束,是的话就显示you failed!
g.drawString("you failed!", 250, 250);
}
}
//===================该类是整个的窗口=====================================================
class canvas extends Frame
{
Thread thread;
snakeCanvas s;
public canvas()
{
thread = new Thread();
setSize(510, 625 );
setLayout( null );
setLocation(200,50);
Label title = new Label("这是一个贪吃蛇小游戏(有点难看,不要介意哦!)") ;
title.setBounds( 120, 30, 250, 30 );
add( title ) ;
Button start = new Button("开始");
start.setBounds( 100, 60, 80, 50 );
add( start );
Button stop = new Button("停止");
stop.setBounds( 220, 60, 80, 50 ) ;
add( stop );
Button restart = new Button("再来一次") ;
restart.setBounds(350, 60, 80, 50 );
add( restart ) ;
s = new snakeCanvas();
s.setLocation( 0, 110 );
add(s);
setVisible(true);
validate();
start.addActionListener(new start()); //按钮监听器 + 匿名类
stop.addActionListener( new stop() );
restart.addActionListener(new restart()); //按钮监听器 + 匿名类
addWindowListener( new WindowAdapter() //键盘事件
{
public void windowClosing( WindowEvent e)
{System.exit(0);}
}) ;
}
public class start implements ActionListener //开始按钮事件
{
public void actionPerformed(ActionEvent e)
{
s.start();
}
}
public class stop implements ActionListener //停止按钮事件
{
public void actionPerformed(ActionEvent e)
{
s.stop();
}
}
public class restart implements ActionListener //重开始按钮事件
{
public void actionPerformed( ActionEvent e )
{
s.restart();
}
}
}
//================================================================================
public class snake
{
public static void main(String args[])
{
canvas c = new canvas();
}
}