| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 865 人关注过本帖
标题:第一个游戏---贪吃蛇
只看楼主 加入收藏
dadongzicool
Rank: 6Rank: 6
等 级:贵宾
威 望:11
帖 子:209
专家分:474
注 册:2009-3-20
结帖率:100%
收藏
 问题点数:0 回复次数:3 
第一个游戏---贪吃蛇
************************************************************************************

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);
  }
  
 }

}
***********************************************************************************
搜索更多相关主题的帖子: 游戏 贪吃 
2010-03-09 12:28
wll07
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2010-3-24
收藏
得分:0 
试试看

我只是菜鸟,我不打酱油
2010-03-24 11:02
pywepe
Rank: 6Rank: 6
等 级:侠之大者
威 望:4
帖 子:296
专家分:483
注 册:2009-4-5
收藏
得分:0 
itcast
传播智客

java群
62635216
欢迎加入
2010-03-30 18:20
编程未来高手
Rank: 1
来 自:山西晋城
等 级:新手上路
帖 子:6
专家分:8
注 册:2010-4-2
收藏
得分:0 
我看过这个的视频教程  传智播客

我努力 我成功
2010-04-06 22:37
快速回复:第一个游戏---贪吃蛇
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.023752 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved