展开全部
看了这套题目感觉很有兴趣,就花了一e69da5e6ba903231313335323631343130323136353331333264623831个中午亲手给你写了一个类似的例子,相信可以帮助你对这个游戏有很好的理解,从右向左那个是僵尸,点一下鼠标就出现植物,我只是起到一个抛砖引玉的作用。代码如下(绝对可以用的代码):
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.util.Vector;
import javax.swing.JFrame;
import javax.swing.event.MouseInputAdapter;
public class PlantsAndZombies extends JFrame {
private static final long serialVersionUID = 1L;
public static final int screenWidth=800;
public static final int screenHeight=600;
Printer printer;
Zombies zombies=new Zombies();
Thread T_Zombies;
Bullet bullet=new Bullet();
Thread T_Bullet;
public PlantsAndZombies(){
this.setSize(new Dimension(screenWidth,screenHeight));
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
this.addMouseListener(new Shoot(this));
this.setVisible(true);
printer=new Printer( this.getGraphics());
printer.Add(zombies);
printer.Add(bullet);
T_Zombies=new Thread(zombies);
T_Zombies.start();
T_Bullet=new Thread(bullet);
T_Bullet.start();
}
public void Shoot(){
bullet.getTarget(zombies);
}
public static void main(String[] args){
PlantsAndZombies game=new PlantsAndZombies();
}
public void run() {
while(true){
}
}
}
interface Drawable{
void drawMe(Graphics g);
}
class Zombies implements Drawable,Runnable{
public boolean isLive=true;
public int x=PlantsAndZombies.screenWidth;
public int y=500;
public void run() {
while(true){
if(x>10){
x-=20;
}else x=PlantsAndZombies.screenWidth;
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public void drawMe(Graphics g){
g.drawRect(x,y,20,50);
}
}
class Bullet implements Drawable,Runnable{
private int x=0;
private int y=500;
private Zombies _z;
private float a,b,c;
private float step;
public void getTarget(Zombies z){
_z=z;
// 用三点确定一个抛物线的方法,计算弹道
int x1=0;
int y1=500;
int x2=(z.x-6*20)/2;
int y2=300; // 抛物线高度200个像素
int x3=z.x-6*20; // 假设击中僵尸用3秒钟,在这3秒钟内僵尸向前移动了6*20个像素
int y3=500;
a=(float)((y2-y1)*(x3-x2)-(y3-y2)*(x2-x1))/(float)((x2*x2-x1*x1)*(x3-x2)-(x3*x3-x2*x2)*(x2-x1));
b=(float)((y2-y1)-a*(x2*x2-x1*x1))/(float)(x2-x1);
c=y1-a*x1*x1-b*x1;
step=(float)(x3-x1)/(float)(3*20);
}
public void run() {
while(true){
try {
x+=step;
y=(int)(a*x*x+b*x+c);
if(y>500){
_z.isLive=false;
}
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public void drawMe(Graphics g) {
g.drawRect(x,y,20,20);
}
}
class Printer extends Thread{
private Vector v=new Vector();
private Graphics _g;
public Printer(Graphics g){
_g=g;
this.start();
}
public void Add(Drawable o){
v.add(o);
}
public void run(){
while(true){
_g.clearRect(0,0,PlantsAndZombies.screenWidth,PlantsAndZombies.screenHeight);
for(Drawable o:v){
o.drawMe(_g);
}
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
class Shoot extends MouseInputAdapter{
private PlantsAndZombies _adaptee;
public Shoot(PlantsAndZombies adaptee){
_adaptee=adaptee;
}
public void mouseClicked(MouseEvent e) {
_adaptee.Shoot();
}
}

本文通过一个Java代码示例展示了如何模拟抛物线轨迹,以帮助理解类似游戏的运行机制。代码包括了 Zombies 和 Bullet 类,实现了子弹沿抛物线轨迹击中僵尸的效果。

1829

被折叠的 条评论
为什么被折叠?



