|
楼主
吾爱游客
发表于 2018-10-10 23:04
|自己
1、申请ID:pwisaxiaobai
2、个人邮箱:
3、原创技术文章:
/**说明:java编写贪吃蛇,部分功能还未实现,比如食物不能出现在蛇身上,蛇碰到自己的身体应该结束游戏,这些功能,在一个线程sleep的时间内蛇应该不能向相反的方向移动;如果有兴趣的,可以自己下去实现一下,由于时间问题,我就不在这里实现le,如果实现不的同志们,可以找我交流交流*/
//先来一张结果图,页面稍微有点丑,大家就见谅见谅
//源码(写的不好的地方请大神们多多包涵):大家在写的时候,可以写吧框架先搭起来,也就是在每个方法里输出一些提示,把逻辑建立起来,然后具体的实现过程可以慢慢的写
package cn.sw.snake.entriy;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.event.KeyEvent;
import java.util.LinkedList;
import cn.sw.snake.util.Util;
//蛇类
public class Snake {
// 定义蛇身:采用点的方式存储
public static LinkedList<Point> snakeBoby = new LinkedList<Point>();
// 设置当前方向:1表示向右-1表示向左2表示向上-2表示向下
private int direction = 1;
// 记录下次按下方向
private int oldDirection = 0;
// 初始化蛇身
public Snake() {
for (int i = 0; i < 3; i++) {
snakeBoby.add(new Point(
(((Util.width - 200) / 2) - i * Util._width),
Util.height / 2));
}
}
// 画出自己
public void drawMe(Graphics2D g) {
g.setColor(Color.blue);
for (Point p : snakeBoby) {
int x = p.x;
int y = p.y;
g.fillRect(x, y, Util._width, Util._height);
}
System.out.println("蛇正在画出自己...");
}
// 蛇的移动:采用去尾加头,一开始让他自动往右走
public void move() {
Point header = snakeBoby.getFirst();
if (this.direction + this.oldDirection == 0) {
this.direction = this.oldDirection;
}
switch (this.direction) {
case 1:
int rightHeader = header.x + Util._width;
snakeBoby.removeLast();
snakeBoby.addFirst(new Point(rightHeader, header.y));
break;
case -1:
int leftHeader = header.x - Util._width;
snakeBoby.removeLast();
snakeBoby.addFirst(new Point(leftHeader, header.y));
break;
case 2:
int upHeader = header.y - Util._width;
snakeBoby.removeLast();
snakeBoby.addFirst(new Point(header.x, upHeader));
break;
case -2:
int downHeader = header.y + Util._width;
snakeBoby.removeLast();
snakeBoby.addFirst(new Point(header.x, downHeader));
break;
}
System.out.println("蛇正在移动...");
}
// 蛇的改变方向
public void changeDirection(int direction) {
this.oldDirection = this.direction;
switch (direction) {
case KeyEvent.VK_UP:
this.direction = 2;
break;
case KeyEvent.VK_DOWN:
this.direction = -2;
break;
case KeyEvent.VK_LEFT:
this.direction = -1;
break;
case KeyEvent.VK_RIGHT:
this.direction = 1;
break;
}
System.out.println("蛇正在改变方向...");
}
// 蛇吃食物
public void eatFood(Food food) {
System.out.println("蛇正在吃食物...");
}
// 蛇吃障碍物...
public void eatGround(Ground ground) {
System.out.println("蛇正在吃障碍物...");
}
}
package cn.sw.snake.entriy;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Point;
import cn.sw.snake.util.Util;
//食物类
public class Food {
public static Point p;
// 初始化食物
public Food() {
int x = Util.getRandom(1, 19);
int y = Util.getRandom(1, 18);
p = new Point(x, y);
}
// 画出自己
public void drawMe(Graphics2D g, String total) {
g.setColor(Color.black);
Font font = new Font("分数:", Font.BOLD, 16);
g.setFont(font);
g.drawString("分数:", 380, 100);
g.setColor(Color.red);
g.drawString(total, 420, 100);
g.setColor(Color.green);
g.fillRect(p.x * 15, p.y * 15, Util._width, Util._height);
System.out.println("食物正在画出自己...");
}
}
//障碍物类:(补充:这里只写了边框障碍物,如果有时间大家可以在动态生成一些障碍物)
package cn.sw.snake.entriy;
import java.awt.Graphics2D;
import cn.sw.snake.util.Util;
public class Ground {
// 画出自己
public void drawMe(Graphics2D g) {
Util.init(g);
System.out.println("障碍物正在画出自己...");
}
}
//一些静态方法,来辅助完成编写
package cn.sw.snake.util;
import java.awt.Color;
import java.awt.Graphics2D;
import java.util.Random;
public class Util {
// 面板大小
public static final int width = 500;
public static final int height = 300;
// 将面板分为小块,并设置每个小块的大小
public static final int _width = 15;
public static final int _height = 15;
// 初始化页面
public static void init(Graphics2D g) {
// 1.画出面板上的障碍物边框:采用一排一排的画法
g.setColor(Color.gray);
g.fillRect(0, 0, 300, 15);
g.fillRect(0, 270, 300, 15);
g.fillRect(0, 15, 15, 270);
g.fillRect(285, 15, 15, 270);
}
}
//面板类
package cn.sw.snake.veiw;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import cn.sw.snake.controll.Controll;
import cn.sw.snake.entriy.Food;
import cn.sw.snake.entriy.Ground;
import cn.sw.snake.entriy.Snake;
import cn.sw.snake.thread.SnakeThread;
//其实这个类不应该实现ActionListener接口,而是由Cotroll实现,这里为了方便就直接实现了
public class ViewAllThing extends JPanel implements ActionListener {
private Snake snake;
private Food food;
private Ground ground;
private SnakeThread t;
private JFrame frame;
private Controll c;
// 初始化SnakeThread,JFrame,Controll
public void initSnakeThread(SnakeThread t, JFrame frame, Controll c) {
this.c = c;
this.t = t;
this.frame = frame;
// this.setLayout(null);
// Button button = new Button("开始游戏");
// button.setBounds(370, 150, 70, 30);
// button.addActionListener(this);
// TextField text = new TextField("分数:");
// text.setBounds(370, 50, 70, 30);
// this.add(button);
// this.valIDAte();
}
// 创建画板,用来显示蛇、食物、障碍物
public void display(Snake snake, Food food, Ground ground) {
System.out.println("页面正在显示...");
this.snake = snake;
this.food = food;
this.ground = ground;
repaint();
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
snake.drawMe(g2d);
food.drawMe(g2d, c.getTotal() + "");
ground.drawMe(g2d);
}
@Override
public void actionPerformed(ActionEvent e) {
this.addKeyListener(c);
this.setFocusable(true);
t.start();
frame.add(this);
}
}
//控制器类:用于监听键盘事件即监听蛇的方向
package cn.sw.snake.controll;
import java.awt.Point;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import cn.sw.snake.entriy.Food;
import cn.sw.snake.entriy.Ground;
import cn.sw.snake.entriy.Snake;
import cn.sw.snake.util.Util;
import cn.sw.snake.veiw.ViewAllThing;
public class Controll extends KeyAdapter {
private Snake snake;
private Food food;
private Ground ground;
private int total = 0;
public int getTotal() {
return total;
}
// 键盘监听器,即键盘适配器
public void keyPressed(KeyEvent e) {
// 获取将要改变的方向
int keyCode = e.getKeyCode();
snake.changeDirection(keyCode);
eatFoodBySnake(food, snake);
eatGroundBySnake(snake, ground);
}
// 蛇的监听器:用于监听蛇的事件,蛇是否碰到食物、障碍物
public void eatFoodBySnake(Food food, Snake snake) {
int snake_x = Snake.snakeBoby.getFirst().x;
int snake_y = Snake.snakeBoby.getFirst().y;
int food_x = Food.p.x;
int food_y = Food.p.y;
if ((snake_x == food_x * 15) && (snake_y == food_y * 15)) {
// Point newP = new Point(Util.getRandom(0, 20), Util.getRandom(0,
// 20));
// Food.p = newP;
total = total + 5;
Snake.snakeBoby.addLast(new Point(food_x * 15, food_y * 15));
Food.p.x = Util.getRandom(1, 19);
Food.p.y = Util.getRandom(1, 18);
}
System.out.println("判断蛇是否碰到了食物...");
}
public void eatGroundBySnake(Snake snake, Ground ground) {
System.out.println("判断蛇是否碰到了障碍物...");
}
// 控制游戏的开始
public void startGame(Snake snake, Food food, Ground ground,
ViewAllThing view) {
this.snake = snake;
this.food = food;
this.ground = ground;
view.display(snake, food, ground);
snake.move();
eatFoodBySnake(food, snake);
eatGroundBySnake(snake, ground);
}
}
//线程类:用于启动游戏,让蛇不停move
package cn.sw.snake.thread;
import cn.sw.snake.controll.Controll;
import cn.sw.snake.entriy.Food;
import cn.sw.snake.entriy.Ground;
import cn.sw.snake.entriy.Snake;
import cn.sw.snake.veiw.ViewAllThing;
public class SnakeThread extends Thread {
private Snake snake;
private Controll c;
private Food food;
private Ground ground;
private ViewAllThing view;
// 编写一个构造方法,用于初始化snake
public SnakeThread(Snake snake, Food food, Ground ground, Controll c,
ViewAllThing view) {
this.snake = snake;
this.c = c;
this.food = food;
this.ground = ground;
this.view = view;
}
// 该线程用于让蛇不停的移动
public void run() {
while (true) {
c.startGame(snake, food, ground, view);
try {
sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
//游戏入口
package cn.sw.sanke.test;
import java.awt.Button;
import java.awt.Font;
import javax.swing.JFrame;
import cn.sw.snake.controll.Controll;
import cn.sw.snake.entriy.Food;
import cn.sw.snake.entriy.Ground;
import cn.sw.snake.entriy.Snake;
import cn.sw.snake.thread.SnakeThread;
import cn.sw.snake.veiw.ViewAllThing;
public class TestGame extends JFrame {
public static void main(String[] args) {
// 创建所有实体对象
Snake snake = new Snake();
Food food = new Food();
Ground ground = new Ground();
// 创建控制器
Controll c = new Controll();
// 创建面板类
ViewAllThing view = new ViewAllThing();
JFrame frame = new JFrame("贪吃蛇version1.0.1");
// 创建线程
SnakeThread myThread = new SnakeThread(snake, food, ground, c, view);
view.initSnakeThread(myThread, frame, c);
// 创建视图类、和加载开始按钮
view.setLayout(null);
view.display(snake, food, ground);
Button button = new Button("开始游戏");
Font font = new Font("开始游戏", Font.BOLD, 12);
button.setFont(font);
button.setBounds(370, 150, 70, 30);
button.addActionListener(view);
view.add(button);
frame.setSize(500, 315);
frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setResizable(false);
frame.add(view);
frame.setVisible(true);
}
} |
|