第一个类:监听器:
import java.awt.Point;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.Random;
public class Controller extends KeyAdapter implements SnakeListener{//处理按键事件和逻辑
private Snake snake;
private Food food;
private Ground ground;
private GamePanel gamePanel;
public Controller(Snake snake, Food food, Ground ground, GamePanel gamePanel) {
super();
this.snake = snake;
this.food = food;
this.ground = ground;
this.gamePanel = gamePanel;
}
public void KeyPressed(KeyEvent e){
switch(e.getKeyCode()){
case KeyEvent.VK_UP:
snake.changeDirection(Snake.UP);
break;
case KeyEvent.VK_DOWN:
snake.changeDirection(Snake.DOWN);
break;
case KeyEvent.VK_RIGHT:
snake.changeDirection(Snake.RIGHT);
break;
case KeyEvent.VK_LEFT:
snake.changeDirection(Snake.LEFT);
break;
}
}
public void snakeMoved(Snake snake){
gamePanel.display(snake, food, ground);
}
public Point getPoint(){
Random random = new Random();
int x = random.nextInt(Global.WIDTH);
int y = random.nextInt(Global.HEIGHT);
return new Point(x,y);
}
public void newGame(){
snake.start();
food.newFood(getPoint());
}
}
第二个类:蛇
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.KeyListener;
import java.util.HashSet;
import java.util.LinkedList;
public class Snake{
public static final int UP = -1;
public static final int DOWN = 1;
public static final int RIGHT = 2;
public static final int LEFT = -2;
private int direction = RIGHT;
private int newDirection = RIGHT;
private Point oldTail;
private LinkedList<Point> body = new LinkedList<Point>();
private HashSet<SnakeListener> listener = new HashSet<SnakeListener>();
private KeyListener liste;
public void addKeyListener(KeyListener liste){
this.liste = liste;
}
public Snake(){
int x = Global.CELL_SIZE / 2;
int y = Global.CELL_SIZE / 2;
for(int i = 0; i < 3; i ++){
body.addFirst(new Point(x++,y));
}
}
public void addSnakeListener(SnakeListener l) {
listener.add(l);
}
public class SnakeDriver implements Runnable{
public void run(){
while(true){
move();
for(SnakeListener l : listener){
l.snakeMoved(Snake.this);
}
try{
Thread.sleep(1000);
}
catch(InterruptedException ex){
ex.printStackTrace();
}
}
}
}
public void move(){
oldTail = body.removeLast();
int x = body.getFirst().x;
int y = body.getFirst().y;
switch(direction){
case UP:
y--;
if(y<0)
y = Global.HEIGHT;
break;
case DOWN:
y++;
if(y>= Global.HEIGHT)
y = 0;
break;
case RIGHT:
x++;
if(x>= Global.WIDTH)
x = 0;
break;
case LEFT:
x--;
if(x<0)
x = Global.WIDTH;
break;
}
body.addFirst(new Point(x,y));
}
public void changeDirection(int dir){
if(this.direction+direction!=0)
direction = dir;
}
public void start() {
new Thread(new SnakeDriver()).start();
}
public void drawMe(Graphics g){
g.setColor(Color.BLUE);
for(Point point : body){
g.fill3DRect(point.x*Global.CELL_SIZE, point.y*Global.CELL_SIZE,
Global.CELL_SIZE, Global.CELL_SIZE, true);
}
}
}
第三个类:游戏界面
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JPanel;
public class GamePanel extends JPanel{
Snake snake;
Food food;
Ground ground;
public GamePanel(Snake snake, Food food, Ground ground) {
this.snake = snake;
this.food = food;
this.ground = ground;
}
public void display(Snake snake,Food food,Ground ground){
this.food = food;
this.ground = ground;
this.snake = snake;
this.repaint();
}
protected void paintComponent(Graphics g){
g.setColor(Color.WHITE);
g.fillRect(0, 0, Global.WIDTH*Global.CELL_SIZE,
Global.HEIGHT*Global.CELL_SIZE);
if(snake != null && food != null && ground != null){
snake.drawMe(g);
food.drawMe(g);
ground.drawMe();
}
}
}
其他的几个类
import java.awt.Graphics;
import java.awt.Point;
import java.util.Random;
public class Food extends Point{
public void newFood(Point p){
this.setLocation(p);
}
public void drawMe(Graphics g) {
// TODO Auto-generated method stub
g.fill3DRect(x*Global.CELL_SIZE, y*Global.CELL_SIZE,
Global.CELL_SIZE,Global.CELL_SIZE, true);
}
}
public class Global{
public final static int CELL_SIZE = 15;
public final static int WIDTH = 20;
public final static int HEIGHT = 20;
}
public class Ground{
public void drawMe() {
}
}
public interface SnakeListener{
public void snakeMoved(Snake snake);
}
7 个解决方案
#1
求高手指教!
#2
public void KeyPressed(KeyEvent e){}
//keyPressed K应该是小写吧
#3
是我搞错了。汗死!谢谢了。想了这么久就这错了,我草。
#4
LZ 要细心哦 特别不能大意 在不清楚的时候可以查API 有这么好的资源为什么我们不用呢 多查几遍还更有助与我记忆...
#5
嗯,我新手,写着写着就粗心了。汗死,写这么多代码压力挺大的。基础还是不够扎实。
#6
诶 貌似我这个初学者也不是很细心啊
#7
哥们你是看的传智播客的吧 其实我也是学的那的
#1
求高手指教!
#2
public void KeyPressed(KeyEvent e){}
//keyPressed K应该是小写吧
#3
是我搞错了。汗死!谢谢了。想了这么久就这错了,我草。
#4
LZ 要细心哦 特别不能大意 在不清楚的时候可以查API 有这么好的资源为什么我们不用呢 多查几遍还更有助与我记忆...
#5
嗯,我新手,写着写着就粗心了。汗死,写这么多代码压力挺大的。基础还是不够扎实。
#6
诶 貌似我这个初学者也不是很细心啊
#7
哥们你是看的传智播客的吧 其实我也是学的那的