植物大战僵尸简单版教程java版

作者:叁念


1.闲来无事,做了个简单版的植物大战僵尸,不多说直接看效果图:

植物大战僵尸简单版教程java版_第1张图片


2.程序的项目结构如下:
植物大战僵尸简单版教程java版_第2张图片


3.先上素材
图片素材 链接:https://pan.baidu.com/s/1dGqqIkx 密码:forj

项目源码 链接:https://pan.baidu.com/s/1dHaXVhB 密码:unb4


4.下面贴上代码

MainGame .java


import javax.swing.JFrame;

import com.yujie.draw.GameDrawPanel;

/**
 * 程序主入口
 * 
 * @author yujie
 *
 */
public class MainGame {

    public static void main(String[] args) {
        JFrame jFrame = new JFrame();
        jFrame.setBounds(0, 0, 1400, 600);
        jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jFrame.setResizable(false);
        GameDrawPanel gameDrawPanel = new GameDrawPanel();
        new Thread(gameDrawPanel).start();
        jFrame.addMouseListener(gameDrawPanel);
        jFrame.addMouseMotionListener(gameDrawPanel);
        jFrame.add(gameDrawPanel);
        jFrame.setVisible(true);
    }
}

GameDrawPanel .java

package com.yujie.draw;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.net.URL;
import java.util.ArrayList;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.Timer;

import com.yujie.Factory.ZombieFactory;
import com.yujie.model.Hammer;
import com.yujie.model.Zombie;

/**
 * 游戏绘制面板
 * 
 * @author yujie
 *
 */
public class GameDrawPanel extends JPanel implements Runnable, MouseMotionListener, MouseListener {
    private static final long serialVersionUID = 1L;

    private int zombieActionPicNum = 0;// 僵尸动作图片序列号
    private static ArrayList zombieList;// 僵尸列表
    private Image bgpic = new ImageIcon("img/background.jpg").getImage();// 背景图片
    private Hammer hammer = new Hammer();// 锤子

    private int score = 0;// 分数
    public static boolean isGameStart = true;// 是否正常游戏

    public GameDrawPanel() {
        // 设置光标隐藏
        URL classUrl = this.getClass().getResource("");
        Image imageCursor = Toolkit.getDefaultToolkit().getImage(classUrl);
        setCursor(Toolkit.getDefaultToolkit().createCustomCursor(imageCursor, new Point(0, 0), "cursor"));
        // 初始化一个僵尸链表
        zombieList = new ArrayList<>();
        new Thread() {// 生产僵尸
            @Override
            public void run() {
                super.run();
                zombieList.add(ZombieFactory.getRandomZombie());
                System.out.println("加入僵尸");
                new Timer((int) (Math.random() * 3) * 1000, new ActionListener() {

                    @Override
                    public void actionPerformed(ActionEvent e) {
                        zombieList.add(ZombieFactory.getRandomZombie());
                    }
                }).start();
            }
        }.start();
    }

    /**
     * 绘制游戏
     */
    @Override
    public void paint(Graphics g) {
        super.paint(g);

        if (isGameStart) {
            // 绘制背景
            g.drawImage(bgpic, 0, 0, null);
            g.setFont(new Font("heiti ", Font.BOLD, 40));
            g.drawString("叁念于2018制作", 600, 300);
            // 绘制分数
            g.setFont(new Font("heiti ", Font.BOLD, 40));
            g.drawString("分数:" + score, 1000, 50);
            // 绘制锤子
            g.drawImage(hammer.getHammerStyle(), hammer.getX(), hammer.getY(), 60, 100, null);
            g.setColor(Color.RED);
            g.drawRect(hammer.getX(), hammer.getY(), 50, 50);
            // 绘制僵尸
            for (Zombie zombie : zombieList) {
                g.drawImage(zombie.getZombieStyle().get(zombieActionPicNum), zombie.getX(), zombie.getY(), null);
                g.setColor(Color.BLACK);
                g.drawRect(zombie.getX(), zombie.getY(), 50, 100);
            }
        } else {
            g.drawImage(bgpic, 0, 0, null);
            g.drawImage(hammer.getHammerStyle(), hammer.getX(), hammer.getY(), 60, 100, null);
            g.setColor(Color.RED);
            g.drawRect(hammer.getX(), hammer.getY(), 50, 50);
            g.setFont(new Font("heiti ", Font.BOLD, 60));
            g.setColor(Color.red);
            g.drawString("Game Over! You score:" + score, 420, 200);
            g.fillRect(660, 360, 150, 50);
            g.setFont(new Font("heiti ", Font.BOLD, 30));
            g.setColor(Color.BLACK);
            g.drawString("再来一次?", 665, 400);
        }
    }

    /**
     * 数据操控
     */
    @Override
    public void run() {
        while (true) {
            // 设置僵尸动作pic
            zombieActionPicNum++;
            if (zombieActionPicNum > 7)
                zombieActionPicNum = 0;
            // 设置僵尸移动
            for (Zombie zombie : zombieList) {
                zombie.setX(zombie.getX() - 5);
                repaint();

                // 判断游戏是否失败
                if (zombie.getX() < 100)
                    isGameStart = false;
            }
            try {
                // 模拟帧率
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            // 重绘
            repaint();

        }
    }

    /**
     * 鼠标点击
     */
    @Override
    public void mouseClicked(MouseEvent e) {
        for (int i = 0; i < zombieList.size(); i++) {
            Zombie zombie = zombieList.get(i);
            if (zombie.getX() < hammer.getX() + 40 && zombie.getX() > hammer.getX()
                    && zombie.getY() < hammer.getY() + 80 && zombie.getY() > hammer.getY()) {
                System.out.println("消灭僵尸");
                score += 10;
                zombieList.remove(i);
            }
        }
        if (!isGameStart) {
            if (hammer.getX() > 660 && hammer.getX() < 810 && hammer.getY() > 360 && hammer.getY() < 510) {
                isGameStart = true;
                zombieList.removeAll(zombieList);
                score = 0;
            }
        }

    }

    /**
     * 鼠标按下
     */
    @Override
    public void mousePressed(MouseEvent e) {
        hammer.setHammerStyle(new ImageIcon("img/hammer/hammer_down.png").getImage());
        repaint();
    }

    /**
     * 鼠标松开
     */
    @Override
    public void mouseReleased(MouseEvent e) {
        hammer.setHammerStyle(new ImageIcon("img/hammer/hammer.png").getImage());
        repaint();
    }

    /**
     * 鼠标移动
     */
    @Override
    public void mouseMoved(MouseEvent e) {
        hammer.setX(e.getX() - 30);
        hammer.setY(e.getY() - 50);
        repaint();
    }

    /**
     * 鼠标进入
     */
    @Override
    public void mouseEntered(MouseEvent e) {
    }

    /**
     * 鼠标退出
     */
    @Override
    public void mouseExited(MouseEvent e) {
    }

    /**
     * 鼠标拖动
     */
    @Override
    public void mouseDragged(MouseEvent e) {
    }

}

ZombieFactory .java

package com.yujie.Factory;

import com.yujie.model.Zombie;
/**
 * 僵尸工厂
 * @author yujie
 *
 */
public class ZombieFactory {
    public static final int ZombieNormal = 1;// 普通僵尸
    public static final int ZombieBucket = 2;// 铁桶僵尸
    public static final int ZombieConehead = 3;// 蠢(圆锥帽)僵尸

    /**
     * 生产一个指定类型的僵尸
     * 
     * @param type
     *            僵尸类型
     * @return Zombie
     */
    public static Zombie getZombie(int type) {
        switch (type) {
        case ZombieNormal:
            return new com.yujie.model.ZombieNormal();
        case ZombieBucket:
            return new com.yujie.model.ZombieBucket();
        case ZombieConehead:
            return new com.yujie.model.ZombieConehead();
        }
        return null;
    }

    public static Zombie getRandomZombie() {
        int type = (int) (Math.random() * 3) + 1;
        return getZombie(type);
    }
}

Hammer .java

package com.yujie.model;

import java.awt.Image;

import javax.swing.ImageIcon;

/**
 * 锤子
 * 
 * @author yujie
 *
 */
public class Hammer {

    private int x = 600;
    private int y = 300;
    private Image hammerStyle;

    public Hammer() {
        this.x = 600;
        this.y = 300;
        this.hammerStyle = new ImageIcon("img/hammer/hammer.png").getImage();
    }

    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }

    public Image getHammerStyle() {
        return hammerStyle;
    }

    public void setHammerStyle(Image hammerStyle) {
        this.hammerStyle = hammerStyle;
    }

}

Zombie .java

package com.yujie.model;

import java.awt.Image;
import java.util.ArrayList;
import java.util.LinkedList;
/**
 * 僵尸抽象类
 * @author yujie
 *
 */
public abstract class Zombie {
    private int x = 1420;
    private int y = (int) (Math.random() * 5) * 110;
    LinkedList ZombieStyle = new LinkedList<>();

    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }

    public LinkedList getZombieStyle() {
        return ZombieStyle;
    }

    public void setZombieStyle(LinkedList zombieStyle) {
        ZombieStyle = zombieStyle;
    }

}

ZombieBucket .java

package com.yujie.model;

import javax.swing.ImageIcon;
/**
 * 铁桶僵尸
 * @author yujie
 *
 */
public class ZombieBucket extends Zombie {
    public ZombieBucket() {
        super();
        for (int i = 1; i < 9; i++) {
            getZombieStyle().add(new ImageIcon("img/zombie_bucket/z_02_0" + i + ".png").getImage());
        }
    }
}

ZombieConehead .java

package com.yujie.model;

import javax.swing.ImageIcon;
/**
 * 垃圾桶僵尸
 * @author yujie
 *
 */
public class ZombieConehead extends Zombie {
    public ZombieConehead() {
        super();
        for (int i = 1; i < 9; i++) {
            getZombieStyle().add(new ImageIcon("img/zombie_conehead/z_01_0" + i + ".png").getImage());
        }
    }

}

ZombieNormal .java

package com.yujie.model;

import javax.swing.ImageIcon;

/**
 * 普通僵尸
 * 
 * @author yujie
 *
 */
public class ZombieNormal extends Zombie {
    public ZombieNormal() {
        super();
        for (int i = 1; i < 9; i++) {
            getZombieStyle().add(new ImageIcon("img/zombie_normal/z_00_0" + i + ".png").getImage());
        }
    }
}

你可能感兴趣的:(有趣的小项目)