手机站
网通分站
电信主站
密 码:
用户名:
当前位置 : 主页>程序设计>Java技术>列表

以前用java写的贪吃蛇游戏

来源:互联网 作者:west263.com 时间:2008-02-23
西部数码-全国虚拟主机10强!40余项虚拟主机管理功能,全国领先!双线多线虚拟主机南北访问畅通无阻!免费赠送企业邮局,.CN域名,自助建站480元起,免费试用7天,满意再付款! P4主机租用799元/月.月付免压金!

}
}
/*
*画布重置,恢复画布的原始状态
*/
public void reset()
{
for(int i = 0; i < colorFlags.length; i )
for(int j = 0; j<colorFlags[i].length; j )
colorFlags[i][j] = false;
repaint();
}
/*
*根据窗口大小调整方格的大小
*/
public void fanning()
{
boxWidth = getSize().width / cols;
boxHeight = getSize().height / rows;
}
/*
* 获取画布(row,col)位置颜色的值
*/
public boolean getColorFlag(int row, int col)
{
return colorFlags[row][col];
}
/*
* 设置画布(row,col)位置颜色的值
*/
public void setColorFlag(int row, int col, boolean colorFlag)
{
colorFlags[row][col] = colorFlag;
}
}
/*
*蛇身线程类,控制蛇的移动及其方向
*/
import javax.swing.*;
import java.util.*;

class SnakeBody extends Thread
{
private LinkedList snakeList;
private int iniSnakeBodyLength;
private GreedSnakeGame game;
private GameCanvas canvas;
public final static int DOWN = -1;
public final static int LEFT = -2;
public final static int UP = 1;
public final static int RIGHT = 2;
private final static int PER_LEVEL_SPEED_UP = 10;
private final static int PER_FOOD_SCORE = 10;
private final static int PER_LEVEL_SCORE = 20 *PER_FOOD_SCORE;
private int direction = LEFT;
private boolean running = true,pause = false;
private int timeInterval = 200,curLevelScore;
private ArrayList food;
/*
*构造函数
*/
public SnakeBody(final GreedSnakeGame game,int iniSnakeBodyLength)
{
this.game = game;
this.iniSnakeBodyLength = iniSnakeBodyLength;
curLevelScore = 0;
food = new ArrayList(5);
canvas = GameCanvas.getCanvasInstance();
/*
* 初始化蛇身
*/
snakeList = new LinkedList();
int rows = canvas.getRows();
int cols = canvas.getCols();
for(int i = 0; i < iniSnakeBodyLength; i )
{
snakeList.add(new SnakeNode(rows / 2, cols / 2 i));
canvas.setColorFlag(rows / 2, cols / 2 i, true);
}
createFood();
canvas.repaint();
}
/*
*暂停移动
*/
public void pauseMove()
{
pause = true;
}
/*
*恢复移动
*/
public void resumeMove()
{
pause = false;
}
/*
*停止移动
*/
public void stopMove()
{
running = false;
}
/*
*每次创建食物
*/
public void createFood()
{
for(int i = 0;i < 5;i )
{
int x = (int)(Math.random() * canvas.getCols());
int y = (int)(Math.random() * canvas.getRows());
if(canvas.getColorFlag(x,y))
i--;
else
food.add(new SnakeNode(x,y));
canvas.setColorFlag(x,y,true);
}
canvas.repaint();
}
/*
* 改变蛇的移动方向
*/
public void changeDirection(int direction)
{
this.direction = direction;
}
/*
*在没有改变方向时,控制蛇的移动,以及吃食物
*/
private boolean moveOn()
{
SnakeNode snakeHead = (SnakeNode)snakeList.getFirst();
int x = snakeHead.getRow();
int y = snakeHead.getCol();
boolean isFood = false,isBody = false;
switch(direction)
{
case LEFT: y--; break;
case RIGHT: y ; break;
case DOWN: x ; break;
case UP: x--; break;
default: break;
}
if((x >= 0 && x < canvas.getCols()) &&( y >=0 && y < canvas.getRows()))
{
int i = 0;
for(;i < food.size();i )
if(x == ((SnakeNode)food.get(i)).getRow() && y == ((SnakeNode)food.get(i)).getCol())
{
isFood = true;
break;
}
for(int j=0;j < snakeList.size()-1 ;j )
if(x == ((SnakeNode)snakeList.get(j)).getRow() && y == ((SnakeNode)snakeList.get(j)).getCol())
{
isBody = true;
break;
}
if(isFood)
{
int score = game.getScore();
score = PER_FOOD_SCORE;
game.setScore(score);
curLevelScore = PER_FOOD_SCORE;
snakeList.addFirst(new SnakeNode(x,y));
food.remove(i);

if(food.size() == 0)
{
if(curLevelScore >= PER_LEVEL_SCORE)
{
int level = game.getLevel();
level ;
game.setLevel(level);
curLevelScore -=PER_LEVEL_SCORE;
}
createFood();
}
}
else if(isBody)
{
JOptionPane.showMessageDialog(null,"You Failed","Game Over",
JOptionPane.INFORMATION_MESSAGE);
running = false;
}
else
{
snakeHead = new SnakeNode(x,y);
snakeList.addFirst(snakeHead);

文章整理:西部数码--专业提供域名注册虚拟主机服务
http://www.west263.com
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!