Java 实战 -贪吃蛇(3)绘制画板,实现键盘监听

Java 实战 —贪吃蛇

面板绘制,实现键盘监听

package com.snake.windows;

import com.snake.model.DieTitle;
import com.snake.model.Food;
import com.snake.model.ScoreSystem;
import com.snake.model.Snake;
import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import static com.snake.constant.Constant.*;

/**
 * @author 晓龙
 * @version 1.8.271
 * @ProtectName MySnake
 * @ClassName MyPanel
 * @Description TODO
 * @createTime 2020年12月20日 01:06:00
 */
public class MyPanel extends JPanel implements KeyListener {
     
    /**
     * 创建蛇,食物,积分,死亡标题
     */
    Snake snake = new Snake();       //实例化蛇
    Food food = new Food();          //实例化食物
    DieTitle dieTitle = new DieTitle();  //实例化死亡标题
    ScoreSystem score = new ScoreSystem(); //实例化积分系统
    /**
     * 构造器
     */
    public MyPanel(){
     
        //获取键盘的监听事件
        this.setFocusable(true);
        this.addKeyListener(this);
    }
    /**
     *画板
     */
    @Override
    protected void paintComponent(Graphics g) {
     
        super.paintComponent(g);
        //设置背景颜色  
        setBackground(new Color(0xC3BFC1C1, true));
        //绘制游戏区域  
        g.setColor(new Color(0xEAEAEA));
        g.fillRect(0,SIZE*4,SIZE*MY_WIDTH,SIZE*(MY_HEIGHT-4));
        //绘制title文字
        g.setColor(Color.GRAY);
        g.setFont(new Font("微软雅黑", Font.BOLD, 45));
        g.drawString("贪吃蛇小游戏", 10, 70);
        //绘制作者文字
        g.setColor(Color.GRAY);
        g.setFont(new Font("微软雅黑", Font.ITALIC, 20));
        g.drawString("—By晓龙", 280, 70);
        g.setColor(new Color(0xCBCBCD));
        //画横线
        for (int i = 0; i <MY_HEIGHT-4 ; i++) {
                    
      g.drawLine(0,SIZE*4+i*SIZE,SIZE*MY_WIDTH,SIZE*4+i*SIZE);
        }
        //画竖线
        for (int i = 0; i < MY_WIDTH; i++) {
     
            g.drawLine(i*SIZE,SIZE*4,i*SIZE,SIZE*MY_HEIGHT);
        }
        //画食物
        g.setColor(new Color(0x0CA3D0));
        g.fillOval(food.getFoodX(),food.getFoodY(),SIZE,SIZE);
        //画蛇身
        for (int i = 1; i < snake.getLenght(); i++) {
              g.drawImage(snake.getBodyIm(),snake.getSnakeX(i),snake.getSnakeY(i),null);
        }
        //画蛇头
        g.drawImage(snake.getHeadImR(), snake.getSnakeX(0), snake.getSnakeY(0), null);
        //写开始提示
        if(snake.isStart()==false){
     
            g.setColor(new Color(1));
            g.setFont(new Font("微软雅黑",Font.BOLD,40));
            g.drawString("按下空格  开始游戏",SIZE*8,SIZE*MY_HEIGHT/2);
        }
        //画死亡提示
        if (snake.isFail() == true) {
     
            g.setColor(new Color(0xFF0000));
            g.setFont(new Font("微软雅黑", Font.BOLD, 35));
            g.drawImage(dieTitle.getDieImage(),dieTitle.getDieTitleImX(),dieTitle.getDieTitleImY(),null);
            g.drawString("最终得分:"+score.getScoreNum(),dieTitle.getDieTitleImX()+SIZE*2,dieTitle.getDieTitleImY()-SIZE*2);
            g.drawString("最终长度:"+score.getLengthNum(),dieTitle.getDieTitleImX()+SIZE*2,dieTitle.getDieTitleImY()-SIZE*2+45);
        }
        //画积分
        g.setColor(new Color(0x000000));
        g.setFont(new Font("微软雅黑", Font.ITALIC, 20));
        g.drawString(score.getScoreName()+score.getScoreNum(),score.getX(),score.getY());
        g.drawString(score.getLengthName()+score.getLengthNum(),score.getX(),score.getY()+30);
    }
    /**
     * 刷新画板,让画板动起来
     */
    public void snakeRun(){
     
            while (true){
     
                snake.snakeMove();  //调用蛇移动
                eat();
                die();
                try {
     
                    Thread.sleep(240);
                } catch (InterruptedException e) {
     
                    e.printStackTrace();
                }
                repaint();   //重新绘制画板
            }
    }
    /**
     * 方法:蛇吃到食物
     */
    private void eat(){
     
        if (snake.getSnakeX(0) == food.getFoodX() && snake.getSnakeY(0) ==food.getFoodY()){
     
            //蛇长度加1
            snake.setLenght(snake.getLenght()+1);
            //刷新食物
            food.foodInit();
            //增加分数
            score.scoreAugment();
        }
    }
    /**
     * 方法:蛇死亡
     */
    private void die(){
     
        for (int i = snake.getLenght()-1; i >0 ; i--) {
     
            if (snake.getSnakeX(0)==snake.getSnakeX(i) && snake.getSnakeY(0)==snake.getSnakeY(i)){
     
                //改变状态,游戏结束
                snake.setFail(true);
            }
        }
    }
    /**
     * 实现键盘监听接口方法
     */
    @Override
    public void keyPressed(KeyEvent e) {
     
        //获得键盘按下按钮
        int keyCode = e.getKeyCode();
        //判断按钮
        if (keyCode == KeyEvent.VK_SPACE){
     
            //如果死亡了
            if(snake.isFail() == true){
     
                //初始化蛇
                snake.snakeInit();
                //初始化食物
                food.foodInit();
                //初始化积分
                score.scoreInit();
                snake.setFail(false);
            }else{
     
                snake.setStart(!snake.isStart());
            }
        }
        if(snake.getFx() != 'D'){
      //如果方向为下,则不可往上走
           //按下W,往上走
           if(keyCode == KeyEvent.VK_W || keyCode == KeyEvent.VK_UP){
     
               snake.setFx('U');
               snake.setFxNum(0);
           }
        }
        if(snake.getFx() != 'U') {
     
            //按下S,往下走
            if (keyCode == KeyEvent.VK_S || keyCode == KeyEvent.VK_DOWN) {
     
                snake.setFx('D');
                snake.setFxNum(1);
            }
        }
        if(snake.getFx() != 'R') {
     
            //按下A,往左走
            if (keyCode == KeyEvent.VK_A || keyCode == KeyEvent.VK_LEFT) {
     
                snake.setFx('L');
                snake.setFxNum(2);
            }
        }
        if(snake.getFx() != 'L') {
     
            //按下D,往右走
            if (keyCode == KeyEvent.VK_D || keyCode == KeyEvent.VK_RIGHT) {
     
                snake.setFx('R');
                snake.setFxNum(3);
            }
        }
    }
    @Override
    public void keyTyped(KeyEvent e) {
     

    }
    @Override
    public void keyReleased(KeyEvent e) {
     

    }
}

你可能感兴趣的:(笔记,游戏,java)