java 贪吃蛇

来源:互联网 发布:个人征信所用数据 编辑:程序博客网 时间:2024/06/16 10:03

//这个是Snake.java**********************************************

import java.awt.Color;

import java.awt.Graphics;

import java.awt.Image;

import java.util.ArrayList;

import java.util.Calendar;

import java.util.GregorianCalendar;

import java.awt.event.*;

 

import javax.swing.*;

/*

 * Time : 2011.11.24

 

 * 

 * */

 

public class Snake extends JPanel

{

final int height = 20;

final int width = 30;

final int unit = 20;

private ArrayList<SNode> Snake = new ArrayList();      //存蛇身                       

private int Direction = 1;                           //存方向

private SNode NewNode = new SNode(1,1,Color.BLACK);  //存随机点

private int Length = 0;                             //存蛇长

Timer timer = new Timer(200 ,new TimerListener());

ImageIcon imgico = createImageIcon("images/11.png");

Image img = imgico.getImage();                          //这个是蛇头像

 

public int getDirection() {

return Direction;

}

public void setDirection(int direction) {

Direction = direction;

}

public SNode getNewNode() {

return NewNode;

}

public void setNewNode(SNode newNode) {

NewNode = newNode;

}

public int getLength() {

return Length;

}

public void setLength(int length) {

Length = length;

}

public Snake()

{

this.Snake.add(new SNode((int)height / 2, (int)width / 2,Color.black));

this.Snake.add(new SNode((int)height / 2 + 1, (int)width / 2,Color.red));

this.Snake.add(new SNode((int)height / 2 + 2, (int)width / 2,Color.blue));

this.Direction = 2;

CreateNode();

this.Length = 3;

timer.start();

addKeyListener(new KeyAdapter()                                      //键盘事件测试ok

{

public void keyPressed(KeyEvent e)

{

int direction = 0;

switch(e.getKeyCode())

{

case KeyEvent.VK_UP: direction = 1; break;

case KeyEvent.VK_DOWN: direction = -1; break;

case KeyEvent.VK_LEFT: direction = 2; break;

case KeyEvent.VK_RIGHT: direction = -2; break;

}

if(direction + Direction != 0 && direction != Direction)//防止反向  & 不许加速(加速感觉过于动感)

{

Direction = direction;

Move(direction);

repaint();

}

}

});

}

 

public void Move(int dir)       //移动

{

timer.stop();                     //感觉在这里不把定时器暂停掉的话键盘事件和定时事件响应间隔很近就显得很假

int nextX = Snake.get(0).getX();

int nextY = Snake.get(0).getY();

switch(dir)

{

case  1 : nextY--; break;

case -1 : nextY++; break;

case  2 : nextX--; break;

case -2 : nextX++; break;

default : break;

}

 

if((nextX == NewNode.getX()) && (nextY == NewNode.getY()))  //若碰上食物

{

Eat();

timer.start(); 

return;

}

 

if(nextX > width-1 || nextX < 0 || nextY > height-1 || nextY < 0)   //越界(测试无误)

{

GameOver("您撞墙啦~~~!");      //撞墙(ok)

return;

}

 

for(int i = 0; i < Length ; i++)          //自撞(测试无误)

{

if((Snake.get(i).getX() == nextX) &&(Snake.get(i).getY() == nextY)) 

{

GameOver("您撞到自己啦~~~!");

return;

}

}

 

for(int j = Length-1 ; j > 0 ; j--)

{

Snake.get(j).setX(Snake.get(j-1).getX());

Snake.get(j).setY(Snake.get(j-1).getY());

}

Snake.get(0).setX(nextX);

Snake.get(0).setY(nextY);

repaint();

timer.start();

}

 

public void Eat()                                            //吃

{

SNode nd = new SNode(NewNode.getX(),NewNode.getY(),NewNode.getColor());

Snake.add(new SNode());

Length++;

for(int j = Length-1 ; j > 0 ; j--)

{

Snake.get(j).setX(Snake.get(j-1).getX());

Snake.get(j).setY(Snake.get(j-1).getY());

Snake.get(j).setColor(Snake.get(j-1).getColor());  //----daice

}

Snake.get(0).setX(NewNode.getX());

Snake.get(0).setY(NewNode.getY());

Snake.get(0).setColor(NewNode.getColor());

CreateNode();

repaint();

}

 

public void CreateNode()                                             //产生随机点

{

boolean flag = true;

int newX = 0;

int newY = 0;

while(flag)

{

newX = (int)(Math.random() * (width-1));

newY = (int)(Math.random() * (height-1));

 

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

{

if((Snake.get(i).getX() == newX) && (Snake.get(i).getY() == newY))

break;

}

flag = false;

}

Color color = new Color(50 + (int)(Math.random()*205),                         //颜色也随机一下

50 + (int)(Math.random()*205),

50 + (int)(Math.random()*205));

NewNode.setX(newX);

NewNode.setY(newY);

NewNode.setColor(color);

Snake.get(0).setColor(NewNode.getColor());

}

 

public void GameOver(String str)                              //GameOver

{

Calendar cal = new GregorianCalendar();

str = str + "\n 点击确定键结束游戏..." + "\n当前时间: " + cal.getTime().toString();

JOptionPane.showMessageDialog(null, str, "游戏结束 ———————By Mr.Xu", 1);

System.exit(0);

}

 

protected void paintComponent(Graphics g)

{

super.paintComponent(g);

g.setColor(NewNode.getColor());                                              //画随机产生的那个点

g.fillOval(NewNode.getX() * unit ,NewNode.getY() * unit , unit , unit);

g.setColor(NewNode.getColor());                                    

g.drawRect(0, 0, width * unit , height * unit );                       //画大框

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

{

g.setColor(Snake.get(i).getColor());

g.fillOval(Snake.get(i).getX() * unit, Snake.get(i).getY() * unit, unit, unit);

}

g.drawImage(img,Snake.get(0).getX() * unit - 3, Snake.get(0).getY() * unit - 3, unit + 6, unit + 6 ,this);

}

 

protected static ImageIcon createImageIcon(String path)

{

        java.net.URL imgURL = Snake.class.getResource(path);

        if (imgURL != null) {

            return new ImageIcon(imgURL);

        } else {

            System.err.println("Couldn't find file: " + path);  //err   红字

            return null;

        }

}

public class TimerListener implements ActionListener                         //定时器事件

{

public void actionPerformed(ActionEvent arg0) 

{

Move(Direction);

repaint();

}

}

 

public static void main(String args[])

{

// TODO Auto-generated method stub

JFrame frame = new JFrame("贪吃蛇   ———————————————————-bY mR.xU");

Snake snk = new Snake();

snk.setBackground(Color.white);

frame.add(snk);

frame.setSize(617,439);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setLocationRelativeTo(null);

frame.setVisible(true);

snk.requestFocus();  //JPanel要响应键盘事件,必须设置焦点

}

}

 

 

//这个是SNode.java********************************************

import java.awt.*;

 

public class SNode extends Object

{

private int x;

private int y;

private Color color;

 

public int getX() {

return x;

}

public void setX(int x) {

this.x = x;

}

public int getY() {

return y;

}

public void setY(int y) {

this.y = y;

}

public Color getColor() {

return color;

}

public void setColor(Color color) {

this.color = color;

}

 

public SNode()

{

x = 0;

y = 0;

color = Color.white;

}

public SNode(int x,int y,Color clr)

{

this.x = x;

this.y = y;

this.color = clr;

}

}

//************************************************

截图:~

 

 

//这个是SnakeApplet.java***********************************

//浏览器运行支持

 

import javax.swing.*;

 

public class SnakeApplet extends JApplet

{

public void init()

{

Snake snk = new Snake();

add(snk);

snk.requestFocus();

}

}

 

 

 

 

//Over .    蛇头像略猥琐.   凑合着看吧,而且眼睛只能向左弯,不知道Java有没有旋转图片的函数~~~

//目前还有个bug,启动游戏有一定几率键盘控制无效,必须重启游戏,不知道为啥 -_-

0 0
原创粉丝点击