Java实现贪吃蛇

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

项目效果图:

源代码:

import java.awt.Color;

import java.awt.Container;

import java.awt.Dimension;

import java.awt.Graphics;

import java.awt.Graphics2D;

import java.awt.Point;

import ponentAdapter; import ponentEvent; import java.awt.event.KeyAdapter;

import java.awt.event.KeyEvent;

import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent;

import java.awt.event.MouseMotionAdapter; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent;

import java.awt.geom.Rectangle2D;

import java.util.ArrayList;

import java.util.HashMap;

import java.util.Map;

import java.util.*;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JOptionPane;

import javax.swing.JPanel;

/* author: 蒋冰*/

public class KeyBoard extends JFrame{

private int direction = 0;

private int judge = 0;

private Panel panel = new Panel();

private RectLabel label[] = new RectLabel[1200];

private HashMap map= new HashMap();

private ArrayList list = new ArrayList();

private Point[] point = new Point[1200];

private int targetX = 0;

private int targetY = 0;

private static int goal = 3;

private int speed;

private RectLabel targetLabel = new RectLabel();

private static KeyBoard frame = new KeyBoard();

public static void main(String[] args){

frame.setVisible(true);

}

public KeyBoard(){

super();

addWindowListener(new WindowAdapter() {

@Override

public void windowActivated(WindowEvent arg0) {

map.put(KeyEvent.VK_RIGHT, KeyEvent.VK_LEFT);

map.put(KeyEvent.VK_LEFT, KeyEvent.VK_RIGHT);

map.put(KeyEvent.VK_UP, KeyEvent.VK_DOWN);

map.put(KeyEvent.VK_DOWN, KeyEvent.VK_UP);

map.put(0, 0);

map.put(KeyEvent.VK_G, 0);

Random ra = new Random();

targetX = ra.nextInt(30)*10 + 50;

targetY = ra.nextInt(20)*10 + 50;

targetLabel.setLocation(targetX, targetY);

}

});

setBackground(Color.gray);

setLayout(null);

setBounds(400,200,400,300);

addKeyListener(new KeyAdapter(){

public void keyPressed(KeyEvent e){

if(e.getKeyCode() == map.get(direction)){

return;

}else

direction = e.getKeyCode();

}

});

for(int i=1; i

label[i] = new RectLabel();

label[i].setBounds(-10,-10, 10, 10);

list.add(label[i]);

}

panel.setBounds(-10, -10, 10, 10);

panel.addComponentListener(new ComponentAdapter() {

@Override

public void componentMoved(ComponentEvent arg0) {

point[0] = panel.getLocation();

if(Math.abs(point[0].x-targetX)<5&&Math.abs(point[0].y-targetY)<5){ Random ra = new Random();

targetX = ra.nextInt(30)*10 + 50;

targetY = ra.nextInt(20)*10 + 50;

while(true){

for(int i=1; i

Point p = label[i].getLocation();

if((p.x==targetX)&&(p.y==targetY)){

break;

}else{

judge ++;

}

}

if(judge==goal-2){

break;

}else{

judge=0;

targetX = ra.nextInt(30)*10 + 50;

targetY = ra.nextInt(20)*10 + 50;

}

}

targetLabel.setLocation(targetX, targetY);

label[goal] = new RectLabel();

label[goal].setBounds(-10, -10, 10, 10);

point[goal-1] = new Point();

list.add(label[goal]);

相关文档
最新文档