想尝试Java版《植物大战僵尸》的编译和运行吗


1、引言

《植物大战僵尸》是由美国宝开游戏公司(PopCap Games)开发的一款单机游戏,自2009年发布以来,风靡全球。有些城市里面的小朋友,就是通过了这款游戏,认识了向日葵、核桃、卷心菜、豌豆、蘑菇,现在我们还能买到《植物大战僵尸》的玩具公仔。

当你玩这款游戏而兴奋时,有没有想过游戏是怎么开发的,有没有幻想过阅读它的源代码,甚至修改源代码?

我一直有这个想法,并且在网上找到了一个Java简易版的《植物大战僵尸》,代码写得非常简单、清晰,并且用IDEA开发工具,实现了编译和运行。

如果你也有这个想法,跟随这篇文章,一起过把瘾吧!

2、源代码下载

我们进入github网站,搜索PlantsVSZombies:

搜索后,可以找到一个Java版本的《植物大战僵尸》游戏项目arminkz/PlantsVsZombies:

下载该项目的源代码:

下载该项目后,得到压缩文件PlantsVsZombies-master.zip,解压后得到PlantsVsZombies-master目录,该目录的结构如下:

目录中有nbproject目录,表示该项目使用NetBeans工具开发,考虑到大多数人的习惯,我们使用IDEA开发工具吧。

下面描述编译和运行的详细过程。

3、建立工程

启动,新建工程:

新建Maven工程:

设置工程名为plants:

这是工程建立后的视图:

4、添加源代码

将下载的PlantsVsZombies-mastersrc目录中的内容,拷贝到plants工程的srcmainjava目录,这是添加后的IDEA工程视图:

将PlantsVsZombies-mastersrc目录中的images子目录,整体拷贝到plants工程的srcmainresources目录,这是添加后的IDEA工程视图:

5、学习源代码

我们可以发现这款《植物大战僵尸》游戏的代码写得非常好。例如,这是太阳功能对应类Sun.java的代码,总共才77行,代码可以直接看懂,特别简练清晰:

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
/**
* Created by Armin on 6/27/2016.
*/
public class Sun extends JPanel implements MouseListener {
private GamePanel gp;
private Image sunImage;
private int myX;
private int myY;
private int endY;
private int destruct = 200;
public Sun(GamePanel parent, int startX, int startY, int endY) {
this.gp = parent;
this.endY = endY;
setSize(80, 80);
setOpaque(false);
myX = startX;
myY = startY;
setLocation(myX, myY);
sunImage = new ImageIcon(this.getClass().getResource("images/sun.png")).getImage();
addMouseListener(this);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(sunImage, 0, 0, null);
}
public void advance() {
if (myY < endY) {
myY += 4;
} else {
destruct--;
if (destruct < 0) {
gp.remove(this);
gp.getActiveSuns().remove(this);
}
}
setLocation(myX, myY);
}
@Override
public void mouseClicked(MouseEvent e) {
}
@Override
public void mousePressed(MouseEvent e) {
}
@Override
public void mouseReleased(MouseEvent e) {
gp.setSunScore(gp.getSunScore() + 25);
gp.remove(this);
gp.getActiveSuns().remove(this);
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
}

整个程序的入口类是GameWindow,从JFrame派生,用于显示主窗口,这是代码:

import javax.swing.*;
import java.awt.event.ActionEvent;
/**
* Created by Armin on 6/25/2016.
*/
public class GameWindow extends JFrame {
enum PlantType {
None,
Sunflower,
Peashooter,
FreezePeashooter
}
//PlantType activePlantingBrush = PlantType.None;
public GameWindow() {
setSize(1012, 785);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setLayout(null);
JLabel sun = new JLabel("SUN");
sun.setLocation(37, 80);
sun.setSize(60, 20);
GamePanel gp = new GamePanel(sun);
gp.setLocation(0, 0);
getLayeredPane().add(gp, new Integer(0));
PlantCard sunflower = new PlantCard(new ImageIcon(this.getClass().getResource("images/cards/card_sunflower.png")).getImage());
sunflower.setLocation(110, 8);
sunflower.setAction((ActionEvent e) -> {
gp.setActivePlantingBrush(PlantType.Sunflower);
});
getLayeredPane().add(sunflower, new Integer(3));
PlantCard peashooter = new PlantCard(new ImageIcon(this.getClass().getResource("images/cards/card_peashooter.png")).getImage());
peashooter.setLocation(175, 8);
peashooter.setAction((ActionEvent e) -> {
gp.setActivePlantingBrush(PlantType.Peashooter);
});
getLayeredPane().add(peashooter, new Integer(3));
PlantCard freezepeashooter = new PlantCard(new ImageIcon(this.getClass().getResource("images/cards/card_freezepeashooter.png")).getImage());
freezepeashooter.setLocation(240, 8);
freezepeashooter.setAction((ActionEvent e) -> {
gp.setActivePlantingBrush(PlantType.FreezePeashooter);
});
getLayeredPane().add(freezepeashooter, new Integer(3));
getLayeredPane().add(sun, new Integer(2));
setResizable(false);
setVisible(true);
}
public GameWindow(boolean b) {
Menu menu = new Menu();
menu.setLocation(0, 0);
setSize(1012, 785);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
getLayeredPane().add(menu, new Integer(0));
menu.repaint();
setResizable(false);
setVisible(true);
}
static GameWindow gw;
public static void begin() {
gw.dispose();
gw = new GameWindow();
}
public static void main(String[] args) {
gw = new GameWindow(true);
}
}

6、编辑pom.xml文件

由于这款《植物大战僵尸》游戏仅仅使用标准JDK提供的功能,所以我们这个工程的Maven,不用于管理依赖,仅仅用于打包。

我们在pom.xml文件中插入节点,pom.xml文件添加的部分如下:


plants



org.apache.maven.plugins
maven-jar-plugin



*.properties
*.txt
*.xml



true

lib/

false

GameWindow



./resources/


${project.build.directory}




org.apache.maven.plugins
maven-dependency-plugin


copy-dependencies
package

copy-dependencies



${project.build.directory}/lib/







maven-resources-plugin


copy-resources
package

copy-resources




src/main/resources


${project.build.directory}/resources





7、编译代码

我们使用Maven命令,对工程进行编译,编译后得到plants.jar文件:

8、开始玩游戏

软件编译完成后,我们在命令行下执行java -jar plants.jar,即可启动游戏:

程序启动后,显示这个熟悉的主界面:

点击“ADVENTURE”(冒险),启动游戏,界面如下:

当然,我们还可以继续玩:

9、总结

一个不到2000行的程序,不可能完整地实现功能强悍的《植物大战僵尸》游戏,但是这个版本,对于游戏开发入门者和感兴趣者,非常有用。

展开阅读全文

页面更新:2024-05-29

标签:僵尸   大战   植物   视图   源代码   代码   文件   目录   工程   游戏

1 2 3 4 5

上滑加载更多 ↓
推荐阅读:
友情链接:
更多:

本站资料均由网友自行发布提供,仅用于学习交流。如有版权问题,请与我联系,QQ:4156828  

© CopyRight 2020-2024 All Rights Reserved. Powered By 71396.com 闽ICP备11008920号-4
闽公网安备35020302034903号

Top