看完马士兵的坦克大战之后写出来的,有些粗糙,但是基本功能算是实现了……
整个程序使用GUI实现,只要谈谈其中的算法吧:
蛇不停移动的算法: 游戏开始的时候,蛇默认长度是5,每一节是装在一个集合里面的,移动的时候,蛇尾先移动到倒数第二节的位置,倒数第2节移动到倒数第三节的位置…… 直到第二节移动到蛇头的位置,最后一步就根据键盘的输入移动蛇头了,这样就完成了一次移动;
如果向上移动,代码如下:
if(up){
for(j=i-1;j>0;j--){
points.get(j).x = points.get(j-1).x;
points.get(j).y = points.get(j-1).y;
}
points.get(0).y -= 20;
j = i-1;
}
蛇的不停移动用一个不停循环的线程来实现:
public class MThread implements Runnable{
public void run(){
while(aLive){
move();
try{
Thread.sleep(time);
}catch(InterruptedException e){
e.printStackTrace();
}
}
}
}
程序中一共有三个类:Snake(蛇类) 、Food(食物类)、 GreedSnake(游戏主程序)
Snake代码如下:
- import java.util.*;
- import java.awt.*;
- import java.awt.event.*;
- class Snake{
- Point po = null;
- int size = 0;
- GreedSnake gs;
- private int time = 150;
- ArrayList<Point> points = new ArrayList<Point>();
- Point head;
-
- private int step = 0;
- private int scoreAdd = 0;
- private boolean up = false,down = false,left = false,right = false;
- private boolean aLive = true;
- private Rectangle rect = null;
- enum Direction {U,D,L,R};
- Direction dir = Direction.U;
- public Snake(Point po,int size,GreedSnake gs){
- this.po = po;
- this.size = size;
- this.gs = gs;
- for(int i=0;i<size;i++){
- addPoint(po);
- po.x-=20;
- po = new Point(po.x,po.y);
- }
- new Thread(new MThread()).start();
- }
- public boolean isAlive(){
- return this.aLive;
- }
-
- public void Live(boolean aLive){
- this.aLive = aLive;
- }
- public void addPoint(Point p){
- points.add(p);
- }
-
- public void speedUp(){
- time-=20;
- }
- public void directionReset(){
- up = false; down = false; left = false; right = false;
- }
- //设置蛇在移动时不能反方向移动
- public void keyPressed(KeyEvent e){
- if(up||down){
- switch(e.getKeyCode()){
- case KeyEvent.VK_UP :
- break;
- case KeyEvent.VK_DOWN :
- break;
- case KeyEvent.VK_LEFT :
- directionReset();
- left = true;
- break;
- case KeyEvent.VK_RIGHT :
- directionReset();
- right = true;
- break;
- }
- }
- if(left||right){
- switch(e.getKeyCode()){
- case KeyEvent.VK_UP :
- directionReset();
- up = true;
- break;
- case KeyEvent.VK_DOWN :
- directionReset();
- down = true;
- break;
- case KeyEvent.VK_LEFT :
- break;
- case KeyEvent.VK_RIGHT :
- break;
- }
- }
- if(!up&&!down&&!left&&!right){
- switch(e.getKeyCode()){
- case KeyEvent.VK_UP :
- directionReset();
- up = true;
- break;
- case KeyEvent.VK_DOWN :
- directionReset();
- down = true;
- break;
- case KeyEvent.VK_LEFT :
- break;
- case KeyEvent.VK_RIGHT :
- directionReset();
- right = true;
- break;
- }
- }
- }
-
- //蛇的移动算法
- public void move(){
-
- int i = points.size();
- int j;
-
- if(up){
- for(j=i-1;j>0;j--){
- points.get(j).x = points.get(j-1).x;
- points.get(j).y = points.get(j-1).y;
- }
- points.get(0).y -= 20;
- j = i-1;
- }
-
- else if(down){
-
- for(j=i-1;j>0;j--){
- points.get(j).x = points.get(j-1).x;
- points.get(j).y = points.get(j-1).y;
- }
- points.get(0).y += 20;
- j = i-1;
- }
- else if(left){
-
- for(j=i-1;j>0;j--){
- points.get(j).x = points.get(j-1).x;
- points.get(j).y = points.get(j-1).y;
- }
- points.get(0).x -= 20;
- j = i-1;
- }
- else if(right){
-
- for(j=i-1;j>0;j--){
- points.get(j).x = points.get(j-1).x;
- points.get(j).y = points.get(j-1).y;
- }
- points.get(0).x += 20;
- j = i-1;
- }
-
- head = points.get(0);
- rect = new Rectangle(head.x,head.y,20,20);
- if(head.x<20||head.x>780|head.y<30||head.y>580){
- aLive = false;
- }
- for(int t=1;t<points.size();t++){
- if(head.x == points.get(t).x && head.y == points.get(t).y){
- aLive = false;
- }
- }
- }
- //蛇吃到食物时的处理
- public void eat(){
- for(int i=0;i<gs.foods.size();i++){
- if(rect.contains(gs.foods.get(i).x, gs.foods.get(i).y) &&gs.foods.get(i).isAlive()){
- gs.foods.get(i).setAlive(false);
- gs.foods.remove(i);
- grow();
- }
- }
- }
- public void grow(){
- Point lastPoint = points.get(points.size()-1);
- Point newEnd = new Point(lastPoint.x,lastPoint.y);
- addPoint(newEnd);
- if(step == 5) {
- speedUp();
- scoreAdd+=3;
- step = 0;
- }
- step++;
- }
- public void paint(Graphics g){
- Color c = g.getColor();
- g.setColor(Color.GREEN);
- g.drawString(" head area:"+head.x+","+head.y,30,50);
- g.drawString(" scores:"+(points.size()-5)*(5+scoreAdd),30,70);
- Iterator<Point> i = points.iterator();
- while(i.hasNext()){
- Point p = i.next();
- g.setColor(Color.GREEN);
- g.fillRect(p.x,p.y,20,20);
- g.setColor(Color.YELLOW);
- g.fillOval(p.x,p.y,20,20);
- }
- g.setColor(c);
- eat();
- }
- //创建一个线程让蛇不停地移动
- public class MThread implements Runnable{
- public void run(){
- while(aLive){
- move();
- try{
- Thread.sleep(time);
- }catch(InterruptedException e){
- e.printStackTrace();
- }
- }
- }
- }
-
- }
复制代码
GreedSnake 代码: 额 …………总算完了 告诉我怎么贴代码 不会啊!! 上传附件吧 诶。。。
|
|