java贪吃蛇源码 按键监听,paint重写 linkedlist集合类

GameFramepackage snakeDemo;import javax.swing.JFrame;/** * 游戏界面 * @author 赵阳阳 */public class GameFrame extends JFrame {/** * 游戏界面构造方法 */public GameFrame() {this.setTitle("贪吃蛇by@我是只小sheep");this.setSize(640, 320);this.setLocationRelativeTo(null);this.setResizable(false);this.setDefaultCloseOperation(EXIT_ON_CLOSE);this.setContentPane(new GameJPanel());this.setVisible(true);}public static void main(String[] args) {new GameFrame();}}/Imgpackage snakeDemo;import java.awt.Image;import javax.swing.ImageIcon;public class Img {public static final Image BG = new ImageIcon("image/bg01.png").getImage(); public static final Image BLOCK = new ImageIcon("image/rect.png").getImage();public static final Image WINDOW = new ImageIcon("image/window.png").getImage(); public static final Image STOP = new ImageIcon("image/1.png").getImage(); public static final int WINDOW_W = WINDOW.getWidth(null); public static final int WINDOW_H = WINDOW.getHeight(null);public static final int STOP_W = STOP.getWidth(null); public static final int STOP_H = STOP.getHeight(null); }///GameJPanelpackage snakeDemo;import java.awt.Color;import java.awt.Font;import java.awt.Graphics;import java.awt.Point;import java.awt.event.KeyEvent;import java.awt.event.KeyListener;import java.util.Iterator;import java.util.LinkedList;import java.util.Random;import javax.swing.JOptionPane;import javax.swing.JPanel;public class GameJPanel extends JPanel implements KeyListener{private Thread td;/** * 食物坐标点 */private Point foodPoint = new Point(10, 10);/** * 地图底层数组宽度 */private static final int MAP_MAX_X = 20;/** * 地图底层数组高度 */private static final int MAP_MAX_Y = 13;/** * 地图底层数组 */private static int[][] map = new int[MAP_MAX_X][MAP_MAX_Y];/** * 蛇的链表 */LinkedList snakeBody = new LinkedList();/** * 行进方向 上 = 0; 下 = 1 左 = 2 右 = 3 */private static final int UP = 0;private static final int DOWN = 1;private static final int LEFT = 2;private static final int RIGHT = 3;private int goingDeractiom = 0;private int score = 0;private int level = 0;private boolean isPlaying = true;private Layer LayerMain = new Layer(10, 10, 414, 274);private Layer LayerScore = new Layer(430, 10, 194, 274);/** * 构造器 */public GameJPanel() {this.setLayout(null);//初始化底层数组initMap();//初始化蛇initSnake();initThread();td.start();this.addKeyListener(this);}private void initThread() {td = new Thread(){@Overridepublic void run() {while (true) {while (isPlaying) {runningSnake();tryToEatFood();repaint();checkHit();checkLvlUp();}}}};}private void checkLvlUp() {if (score%1000 == 0) {level++;score += 100;}}private void checkHit() {Point point = snakeBody.getFirst();for (int i = 1; i < snakeBody.size(); i++) {Point p = snakeBody.get(i);if (point.equals(p)) {JOptionPane.showMessageDialog(null, "撞到自己身体了!");System.exit(0);}}}@Overridepublic void paint(Graphics g) {g.drawImage(Img.BG, 0, 0, null);LayerMain.createWindow(g);LayerScore.createWindow(g);initMap();setSnakeToMap();drawMap(g);drawBlockByXY(g, foodPoint.x, foodPoint.y, level+1);drawStrings(g);if (!isPlaying) {g.drawImage(Img.STOP, 170,10,440,280,0,0,Img.STOP_W,Img.STOP_H,null);}requestFocus();}private void drawStrings(Graphics g) {g.setFont(new Font("黑体", Font.BOLD, 30));g.setColor(Color.blue);g.drawString("分数:", 440, 50);g.drawString(score+"/"+(level*1000), 440, 90);g.drawString("等级:", 440, 130);g.drawString(""+level, 440, 170);g.drawString("贪吃蛇V1.0", 440, 210);g.drawString("by赵阳阳", 440, 250);}private void runningSnake() {try {Thread.sleep(400-50*level);addSnakeBody();deleteSnakeBody();} catch (InterruptedException e) {e.printStackTrace();}}/** * 初始化底层地图 */private void initMap() {for (int i = 0; i < MAP_MAX_X; i++) {for (int j = 0; j < MAP_MAX_Y; j++) {map[i][j] = 0;}}}/** * 绘制蛇 */private void drawMap(Graphics g){for (int i = 0; i < MAP_MAX_X; i++) {for (int j = 0; j < MAP_MAX_Y; j++) {switch (map[i][j]) {case 1:drawBlockByXY(g,i,j,level);break;}}}}/** * 在地图上绘制蛇的位置的数值 */private void setSnakeToMap() {Iterator points = snakeBody.iterator();while (points.hasNext()) {Point point = points.next();try{map[point.x][point.y] = 1;}catch(Exception e){td.stop();JOptionPane.showMessageDialog(null, "撞墙了!");System.exit(0);}}}/** * 蛇身加长方法 */public void addSnakeBody() {Point p = snakeBody.getFirst();switch (goingDeractiom) {case UP:Point newUpPoint = new Point(p.x, p.y - 1);snakeBody.addFirst(newUpPoint);break;case DOWN:Point newDownPoint = new Point(p.x, p.y + 1);snakeBody.addFirst(newDownPoint);break;case LEFT:Point newLeftPoint = new Point(p.x - 1, p.y);snakeBody.addFirst(newLeftPoint);break;case RIGHT:Point newRightPoint = new Point(p.x + 1, p.y);snakeBody.addFirst(newRightPoint);break;}}/** * 删除蛇身体最后的一个元素 */public void deleteSnakeBody() {snakeBody.pollLast();}/** * 绘制单个蛇身体 */private void drawBlockByXY(Graphics g , int x, int y, int style) {g.drawImage(Img.BLOCK, x*20+17, y*20+17,(x+1)*20+17, (y+1)*20+17, 32*style, 0, 32*(style+1), 32, null);}/** * 初始化蛇 */private void initSnake() {Random r = new Random();snakeBody.add(new Point(r.nextInt(MAP_MAX_X),r.nextInt(13)));}/** * 键盘监听重写的方法 */@Overridepublic void keyTyped(KeyEvent e) {}@Overridepublic void keyPressed(KeyEvent e) {// 根据按键来设置相应方向switch (e.getKeyCode()) {// 上case KeyEvent.VK_UP:if (goingDeractiom == DOWN)break;goingDeractiom = UP;break;// 下case KeyEvent.VK_DOWN:if (goingDeractiom == UP)break;goingDeractiom = DOWN;break;// 左case KeyEvent.VK_LEFT:if (goingDeractiom == RIGHT)break;goingDeractiom = LEFT;break;// 右case KeyEvent.VK_RIGHT:if (goingDeractiom == LEFT)break;goingDeractiom = RIGHT;break;// 右case KeyEvent.VK_SPACE:if (isPlaying) {isPlaying = false;}else {isPlaying = true;}repaint();break;}}/** * 随机制造食物的方法 */private void tryToEatFood() {if (snakeBody.getFirst().equals(foodPoint)) {Random random = new Random();Point point;do {int x = random.nextInt(MAP_MAX_X);int y = random.nextInt(MAP_MAX_Y);point = new Point(x, y);} while (snakeBody.contains(point));foodPoint = point;addSnakeBody();score += 100;}}@Overridepublic void keyReleased(KeyEvent e) {}}//Layerpackage snakeDemo;import java.awt.Graphics;import java.awt.Image;public class Layer {protected static final int PADDDING = 30;protected static final int BORDER = 7;protected int x;protected int y;protected int w;protected int h;protected Layer(int x, int y, int w, int h) {this.x = x;this.y = y;this.w = w;this.h = h;}protected void createWindow(Graphics g) {g.drawImage(Img.WINDOW, x, y, x + BORDER, y + BORDER, 0, 0, BORDER, BORDER,null);g.drawImage(Img.WINDOW, x + BORDER, y, w - BORDER + x, BORDER + y, BORDER, 0,64 - BORDER, BORDER, null);g.drawImage(Img.WINDOW, x + w - BORDER, y, x + w, y + BORDER, Img.WINDOW_W- BORDER, 0, Img.WINDOW_W, BORDER, null);g.drawImage(Img.WINDOW, x, y + BORDER, x + BORDER, y + h - BORDER, 0, BORDER,BORDER, Img.WINDOW_H - BORDER, null);g.drawImage(Img.WINDOW, x + BORDER, y + BORDER, x + w - BORDER, y + h - BORDER,BORDER, BORDER, Img.WINDOW_W - BORDER, Img.WINDOW_H - BORDER, null);g.drawImage(Img.WINDOW, x + w - BORDER, y + BORDER, x + w, y + h - BORDER,Img.WINDOW_W - BORDER, BORDER, Img.WINDOW_W, Img.WINDOW_H - BORDER, null);g.drawImage(Img.WINDOW, x, y + h - BORDER, x + BORDER, y + h, 0, Img.WINDOW_H- BORDER, BORDER, Img.WINDOW_H, null);g.drawImage(Img.WINDOW, x + BORDER, y + h - BORDER, x + w - BORDER, y + h,BORDER, Img.WINDOW_W - BORDER, Img.WINDOW_W - BORDER, Img.WINDOW_H, null);g.drawImage(Img.WINDOW, x + w - BORDER, y + h - BORDER, x + w, y + h,Img.WINDOW_W - BORDER, Img.WINDOW_H - BORDER, Img.WINDOW_W, Img.WINDOW_H, null);}protected void drawImageCenter(Image img,Graphics g){int imgW = img.getWidth(null);int imgH = img.getHeight(null);g.drawImage(img, x+(w-imgW>>1), y + (h-imgH>>1), null);}}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值