第一个游戏---贪吃蛇
************************************************************************************package cn.itcast.snake.controller;
import java.awt.Point;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.Random;
import cn.itcast.snake.entities.Food;
import cn.itcast.snake.entities.Ground;
import cn.itcast.snake.entities.Snake;
import cn.itcast.snake.listener.SnakeListener;
import cn.itcast.snake.util.Global;
import cn.itcast.snake.view.GamePanel;
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;
}
@Override
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
switch(e.getKeyCode()){
case KeyEvent.VK_UP:
snake.changeDirection(Snake.UP);
break;
case KeyEvent.VK_DOWN:
snake.changeDirection(Snake.DOWN);
break;
case KeyEvent.VK_LEFT:
snake.changeDirection(Snake.LEFT);
break;
case KeyEvent.VK_RIGHT:
snake.changeDirection(Snake.RIGHT);
break;
}
}
public void snakeMoved(Snake snake) {
// TODO Auto-generated method stub
if(food.isSnakeEatFood(snake)){
snake.eatFood();
food.newFood(ground.getPoint());
}
if(ground.isSnakeEatRock(snake)){
snake.die();
}
if(snake.isSnakeEatBody()){
snake.die();
}
gamePanel.display(snake, food, ground);
}
public void newGame(){
snake.start();
food.newFood(ground.getPoint());
}
}
************************************************************************************
package cn.itcast.snake.entities;
import java.awt.Graphics;
import java.awt.Point;
import cn.itcast.snake.util.Global;
public class Food extends Point{
public void newFood(Point p){
this.setLocation(p);
}
public boolean isSnakeEatFood(Snake snake){
System.out.println("Food's isSnakeEatFood");
return this.equals(snake.getHead());
}
public void drawMe(Graphics g){
System.out.println("Food's drawMe");
g.fill3DRect(x * Global.CELL_SIZE, y * Global.CELL_SIZE, Global.CELL_SIZE, Global.CELL_SIZE, true);
}
}
*********************************************************************************************************
package cn.itcast.snake.entities;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.util.Random;
import cn.itcast.snake.util.Global;
public class Ground {
private int rocks[][] = new int [Global.WIDTH][Global.HEIGHT];
public Ground(){
for(int x = 0;x < Global.WIDTH;x++){
rocks[x][0] = 1;
rocks[x][Global.HEIGHT - 1] = 1;
}
}
public Point getPoint(){
Random random = new Random();
int x = 0,y = 0;
do{
x = random.nextInt(Global.WIDTH);
y = random.nextInt(Global.HEIGHT);
}while(rocks[x][y] == 1);
return new Point(x,y);
}
public boolean isSnakeEatRock(Snake snake){
System.out.println("Ground's isSnakeEatRock");
for(int x = 0;x < Global.WIDTH;x++){
for(int y = 0;y < Global.HEIGHT;y++){
if(rocks[x][y] == 1
&& (x == snake.getHead().x && y == snake.getHead().y)){
return true;
}
}
}
return false;
}
public void drawMe(Graphics g) {
System.out.println("Ground's drawMe");
g.setColor(Color.ORANGE);
for(int x = 0;x < Global.WIDTH;x++){
for(int y = 0;y < Global.HEIGHT;y++){
if(rocks[x][y] == 1){
//g.setColor(Color.ORANGE);
g.fill3DRect(x * Global.CELL_SIZE, y * Global.CELL_SIZE, Global.CELL_SIZE, Global.CELL_SIZE, true);
}
}
}
}
}
************************************************************************************
package cn.itcast.snake.entities;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Set;
import cn.itcast.snake.listener.SnakeListener;
import cn.itcast.snake.util.Global;
public class Snake {
public static final int UP = -1;
public static final int DOWN = 1;
public static final int LEFT = -2;
public static final int RIGHT = 2;
//private int direction;
private int oldDirection , newDirection;
private Point oldTail;
private LinkedList<Point> body = new LinkedList<Point>();
private Set<SnakeListener> listeners = new HashSet<SnakeListener>();
private boolean life;
public Snake(){
init();
}
public void init(){
int x = Global.WIDTH / 2;
int y = Global.HEIGHT / 2;
for(int i = 0;i < 3;i++){
body.addFirst(new Point(x + i,y));
}
//direction = RIGHT;
oldDirection = newDirection = RIGHT;
life = true;
}
public void move(){
System.out.println("Snake's move");
//去尾
oldTail = body.removeLast();
int x = body.getFirst().x;
int y = body.getFirst().y;
if(!(oldDirection + newDirection == 0)){
oldDirection = newDirection;
}
//switch(direction){
switch(oldDirection){
case UP :
y--;
if(y < 0){
y = Global.HEIGHT - 1;
}
break;
case DOWN :
y++;
if(y > Global.HEIGHT){
y = 0;
}
break;
case LEFT :
x--;
if(x < 0 ){
x = Global.WIDTH - 1;
}
break;
case RIGHT :
x++;
if(x > Global.WIDTH){
x = 0;
}
break;
}
Point newHead = new Point(x,y);
//加头
body.addFirst(newHead);
}
public void changeDirection(int direction ){
System.out.println("Snake's changeDirection");
//if(!(direction + this.direction == 0)){
//this.direction = direction;
//}
newDirection = direction;
}
public void eatFood(){
System.out.println("Snake's eatFood");
body.addLast(oldTail);
}
public boolean isSnakeEatBody(){
System.out.println("Snake's isEatBody");
for(int i = 1;i < body.size();i++){
if(body.get(i).equals(this.getHead()))
return true;
}
return false;
}
public void drawMe(Graphics g){
System.out.println("Snake's drawMe");
g.setColor(Color.BLUE);
for(Point p : body){
g.fill3DRect(p.x * Global.CELL_SIZE, p.y * Global.CELL_SIZE, Global.CELL_SIZE, Global.CELL_SIZE, true);
}
}
private class SnakeDriver implements Runnable{
public void run() {
// TODO Auto-generated method stub
while(life){
move();
for(SnakeListener l : listeners){
l.snakeMoved(Snake.this);
}
try{
Thread.sleep(300);
}catch(InterruptedException e){
e.printStackTrace();
}
}
}
}
public void start(){
new Thread(new SnakeDriver()).start();
}
public void addSnakeListener(SnakeListener l){
if(l != null){
this.listeners.add(l);
}
}
public Point getHead() {
// TODO Auto-generated method stub
return body.getFirst();
}
public void die(){
life = false;
}
}
************************************************************************************
package cn.itcast.snake.game;
import java.awt.BorderLayout;
import javax.swing.JFrame;
import cn.itcast.snake.controller.Controller;
import cn.itcast.snake.entities.Food;
import cn.itcast.snake.entities.Ground;
import cn.itcast.snake.entities.Snake;
import cn.itcast.snake.util.Global;
import cn.itcast.snake.view.GamePanel;
public class Game {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Snake snake = new Snake();
Food food = new Food();
Ground ground = new Ground();
GamePanel gamePanel = new GamePanel();
Controller controller = new Controller(snake,food,ground,gamePanel);
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gamePanel.setSize(Global.WIDTH * Global.CELL_SIZE,Global.HEIGHT * Global.CELL_SIZE);
frame.setSize(Global.WIDTH * Global.CELL_SIZE + 20,Global.HEIGHT * Global.CELL_SIZE + 40);
frame.add(gamePanel,BorderLayout.CENTER);
gamePanel.addKeyListener(controller);
snake.addSnakeListener(controller);
frame.addKeyListener(controller);
frame.setVisible(true);
controller.newGame();
}
}
****************************************************************************************
package cn.itcast.snake.listener;
import cn.itcast.snake.entities.Snake;
public interface SnakeListener {
void snakeMoved(Snake snake);
}
***************************************************************************************
package cn.itcast.snake.util;
public class Global {
public static final int CELL_SIZE = 20;
public static final int WIDTH = 15;
public static final int HEIGHT = 15;
}
**********************************************************************************
package cn.itcast.snake.view;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JPanel;
import cn.itcast.snake.entities.Food;
import cn.itcast.snake.entities.Ground;
import cn.itcast.snake.entities.Snake;
import cn.itcast.snake.util.Global;
public class GamePanel extends JPanel{
private Snake snake;
private Food food;
private Ground ground;
public void display(Snake snake,Food food,Ground ground){
System.out.println("GamePanel's display");
this.snake = snake;
this.food = food;
this.ground = ground;
this.repaint();
}
@Override
protected void paintComponent(Graphics g) {
// TODO Auto-generated method stub
//重新显示
g.setColor(Color.RED);
g.fillRect(0, 0, Global.WIDTH * Global.CELL_SIZE, Global.WIDTH * Global.CELL_SIZE);
if(ground != null && ground != null && ground != null){
this.ground.drawMe(g);
this.snake.drawMe(g);
this.food.drawMe(g);
}
}
}
***********************************************************************************