package com.diaodiao;
import java.awt.Color;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.List;
public class TankClient extends Frame {
/**
*
*/
private static final long serialVersionUID = 1L;
public static final int GAME_WIDTH = 800;
public static final int GAME_HEIGHT = 600;
private Image offScreenImage = null;
Tank myTank = new Tank(50, 50, this, true, Tank.Direction.STOP);
private List<Missile> missiles = new ArrayList<Missile>();
private List<Tank> tanks = new ArrayList<Tank>();
private List<Explode> explodes = new ArrayList<Explode>();
public void paint(Graphics g) {
g.drawString("missiles count:" + missiles.size(), 10, 50);
g.drawString("explodes count:" + explodes.size(), 10, 70);
g.drawString("tanks count:" + tanks.size(), 10, 90);
for(int i = 0; i < missiles.size(); i++) {
Missile m = missiles.get(i);
m.hitTanks(tanks);
if(myTank != null) {
m.hitTank(myTank);
}
m.draw(g);
}
for(int i =0; i < explodes.size(); i++) {
Explode e = explodes.get(i);
e.draw(g);
}
for(int i =0; i < tanks.size(); i++) {
Tank enemyTank = tanks.get(i);
enemyTank.draw(g);
}
if(myTank != null) {
myTank.draw(g);
}
}
public void update(Graphics g) {
if(offScreenImage == null) {
offScreenImage = this.createImage(GAME_WIDTH, GAME_HEIGHT);
}
Graphics gOffScreen = offScreenImage.getGraphics();
Color c = gOffScreen.getColor();
gOffScreen.setColor(Color.GREEN);
gOffScreen.fillRect(0, 0, GAME_WIDTH, GAME_HEIGHT);
gOffScreen.setColor(c);
paint(gOffScreen);
g.drawImage(offScreenImage, 0, 0, null); //把虚拟图片画到窗体上
}
public void lauchFrame() {
for(int i = 0; i < 10 ; i++) {
tanks.add(new Tank(50 + 40*(i+1), 50, this, false, Tank.Direction.D));
}
this.setLocation(300, 100);
this.setSize(GAME_WIDTH, GAME_HEIGHT);
this.setTitle("TankWar");
this.addWindowListener(new WindowAdapter(){
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
this.addKeyListener(new KeyMonitor());
this.setBackground(Color.GREEN);
this.setResizable(false);
this.setVisible(true);
new Thread(new paintThread()).start();
}
/**
* @param args
*/
public static void main(String[] args) {
TankClient tc = new TankClient();
tc.lauchFrame();
}
private class paintThread implements Runnable {
@Override
public void run() {
while(true) {
repaint();
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
private class KeyMonitor extends KeyAdapter{
@Override
public void keyPressed(KeyEvent e) {
if(myTank != null)
myTank.keyPressed(e);
}
@Override
public void keyReleased(KeyEvent e) {
if(myTank != null)
myTank.keyReleased(e);
}
}
public List<Missile> getMissiles() {
return missiles;
}
public void setMissiles(List<Missile> missiles) {
this.missiles = missiles;
}
public List<Tank> getTanks() {
return tanks;
}
public void setTanks(List<Tank> tanks) {
this.tanks = tanks;
}
public List<Explode> getExplodes() {
return explodes;
}
public void setExplodes(List<Explode> explodes) {
this.explodes = explodes;
}
}
package com.diaodiao;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;
import java.util.Random;
public class Tank {
public static final int WIDTH = 30;
public static final int HEIGHT = 30;
public static final int XSPEED = 5;
public static final int YSPEED = 5;
TankClient tc;
private int x, y;
private static Random r = new Random();
private boolean bL = false, bU = false, bR = false, bD = false;
private boolean good;
private boolean live = true;
enum Direction {L, LU, U, RU, R, RD, D, LD, STOP};
private int step = r.nextInt(12) + 3;
private Direction dir = Direction.STOP;
private Direction ptDir = Direction.D;
public Tank(int x, int y, TankClient tc, boolean good, Direction dir) {
this.tc = tc;
this.x = x;
this.y = y;
this.good = good;
this.dir = dir;
}
public void draw(Graphics g) {
if(!live) {
if(!good)
tc.getTanks().remove(this);
else
tc.myTank = null;
return ;
}
Color c = g.getColor();
if(good)
g.setColor(Color.RED);
else
g.setColor(Color.BLUE);
g.fillOval(x, y, WIDTH, HEIGHT);
g.setColor(c);
switch(ptDir) {
case L:
g.drawLine(x + Tank.WIDTH / 2, y + Tank.HEIGHT / 2, x, y + Tank.HEIGHT / 2);
break;
case U:
g.drawLine(x + Tank.WIDTH / 2, y + Tank.HEIGHT / 2, x + Tank.WIDTH / 2, y);
break;
case R:
g.drawLine(x + Tank.WIDTH / 2, y + Tank.HEIGHT / 2, x + Tank.WIDTH, y + Tank.HEIGHT / 2);
break;
case D:
g.drawLine(x + Tank.WIDTH / 2, y + Tank.HEIGHT / 2, x + Tank.WIDTH / 2, y + Tank.HEIGHT);
break;
case LU:
g.drawLine(x + Tank.WIDTH / 2, y + Tank.HEIGHT / 2, x, y);
break;
case RU:
g.drawLine(x + Tank.WIDTH / 2, y + Tank.HEIGHT / 2, x + Tank.WIDTH, y);
break;
case RD:
g.drawLine(x + Tank.WIDTH / 2, y + Tank.HEIGHT / 2, x + Tank.WIDTH, y + Tank.HEIGHT);
break;
case LD:
g.drawLine(x + Tank.WIDTH / 2, y + Tank.HEIGHT / 2, x, y + Tank.HEIGHT);
break;
}
move();
}
public void move() {
switch(dir) {
case L:
x -= XSPEED;
break;
case U:
y -= YSPEED;
break;
case R:
x += XSPEED;
break;
case D:
y += YSPEED;
break;
case LU:
x -= XSPEED;
y -= YSPEED;
break;
case RU:
x += XSPEED;
y -= YSPEED;
break;
case RD:
x += XSPEED;
y += YSPEED;
break;
case LD:
x -= XSPEED;
y += YSPEED;
break;
case STOP:
break;
}
if(dir != Direction.STOP) {
ptDir = dir;
}
if(x < 0) x = 0;
if(y < 30) y = 30;
if(x + Tank.WIDTH > TankClient.GAME_WIDTH) x = TankClient.GAME_WIDTH - Tank.WIDTH;
if(y + Tank.HEIGHT > TankClient.GAME_HEIGHT) y = TankClient.GAME_HEIGHT - Tank.HEIGHT;
if(!good) {
Direction[] dirs = Direction.values();
if(step == 0) {
step = r.nextInt(12) + 3;
int rn = r.nextInt(dirs.length);
dir = dirs[rn];
}
step --;
if(r.nextInt(40) > 38) this.fire();
}
}
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
switch(key) {
case KeyEvent.VK_LEFT:
bL = true;
break;
case KeyEvent.VK_UP:
bU = true;
break;
case KeyEvent.VK_RIGHT:
bR = true;
break;
case KeyEvent.VK_DOWN:
bD = true;
break;
}
locateDirection();
}
public void keyReleased(KeyEvent e) {
int key = e.getKeyCode();
switch(key) {
case KeyEvent.VK_CONTROL:
fire();
break;
case KeyEvent.VK_LEFT:
bL = false;
break;
case KeyEvent.VK_UP:
bU = false;
break;
case KeyEvent.VK_RIGHT:
bR = false;
break;
case KeyEvent.VK_DOWN:
bD = false;
break;
}
locateDirection();
}
void locateDirection() {
if(bL && !bU && !bR && !bD) {
dir = Direction.L;
}else if(bL && bU && !bR && !bD) {
dir = Direction.LU;
}else if(!bL && bU && !bR && !bD) {
dir = Direction.U;
}else if(!bL && bU && bR && !bD) {
dir = Direction.RU;
}else if(!bL && !bU && bR && !bD) {
dir = Direction.R;
}else if(!bL && !bU && bR && bD) {
dir = Direction.RD;
}else if(!bL && !bU && !bR && bD) {
dir = Direction.D;
}else if(bL && !bU && !bR && bD) {
dir = Direction.LD;
}else if(!bL && !bU && !bR && !bD) {
dir = Direction.STOP;
}
}
public void fire() {
if(!live) {
return ;
}
int x = this.x + Tank.WIDTH/2 - Missile.WIDTH/2;
int y = this.y + Tank.HEIGHT/2 - Missile.HEIGHT/2;
Missile m = new Missile(x, y, ptDir, tc, good);
tc.getMissiles().add(m);
}
public Rectangle getRect() {
return new Rectangle(x, y, WIDTH, HEIGHT);
}
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 boolean isLive() {
return live;
}
public void setLive(boolean live) {
this.live = live;
}
public boolean isGood() {
return good;
}
public void setGood(boolean good) {
this.good = good;
}
}
package com.diaodiao;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.util.List;
public class Missile {
public static final int WIDTH = 10;
public static final int HEIGHT = 10;
public static final int XSPEED = 10;
public static final int YSPEED = 10;
private int x, y;
private boolean good;
private boolean live = true;
TankClient tc;
Tank.Direction dir;
public Missile(int x, int y, Tank.Direction dir, TankClient tc, boolean good) {
this.x = x;
this.y = y;
this.dir = dir;
this.tc = tc;
this.good = good;
}
public void draw(Graphics g) {
if(!live) {
tc.getMissiles().remove(this);
return ;
}
Color c = g.getColor();
if(good)
g.setColor(Color.RED);
else
g.setColor(Color.BLUE);
g.fillOval(x, y, WIDTH, HEIGHT);
g.setColor(c);
move();
}
public void move() {
switch(dir) {
case L:
x -= XSPEED;
break;
case U:
y -= YSPEED;
break;
case R:
x += XSPEED;
break;
case D:
y += YSPEED;
break;
case LU:
x -= XSPEED;
y -= YSPEED;
break;
case RU:
x += XSPEED;
y -= YSPEED;
break;
case RD:
x += XSPEED;
y += YSPEED;
break;
case LD:
x -= XSPEED;
y += YSPEED;
break;
}
if(x < 0 || y < 0 || x > TankClient.GAME_WIDTH || y > TankClient.GAME_HEIGHT) {
live = false;
}
}
public Rectangle getRect() {
return new Rectangle(x, y, WIDTH, HEIGHT);
}
public boolean hitTank(Tank t) {
if(this.getRect().intersects(t.getRect()) && this.good != t.isGood()&& t.isLive()) {
t.setLive(false);
this.live = false;
Explode e = new Explode(t.getX(), t.getY(), tc);
tc.getExplodes().add(e);
return true;
}
return false;
}
public boolean hitTanks(List<Tank> tanks) {
for(int i = 0; i < tanks.size(); i++) {
Tank t = tanks.get(i);
if(this.hitTank(t)) {
return true;
}
}
return false;
}
public boolean isGood() {
return good;
}
public void setGood(boolean good) {
this.good = good;
}
}
package com.diaodiao;
import java.awt.*;
public class Explode {
private TankClient tc;
int[] diameter = { 4, 7, 12, 18, 26, 32, 49, 30, 14, 6 };
int step = 0;
int x, y;
public Explode(int x, int y, TankClient tc) {
this.x = x;
this.y = y;
this.tc = tc;
}
public void draw(Graphics g) {
if (step == diameter.length) {
tc.explodes.remove(this);
return;
}
Color c = g.getColor();
g.setColor(Color.ORANGE);
g.fillOval(x, y, diameter[step], diameter[step]);
g.setColor(c);
step++;
}
}
分享到:
相关推荐
"TankWar1.9.rar" 是一个压缩包文件,其中包含了一个名为 "TankWar1.9" 的项目,很显然,这是一个关于坦克大战游戏的早期版本。这个游戏是开发者初次尝试制作的成果,旨在提供一个学习和交流的平台,鼓励大家通过...
《JAVA网络版坦克大战》是一款基于JAVA编程语言开发的在线多人对战游戏,它将经典的游戏体验与现代网络技术相结合,提供了丰富的互动性和娱乐性。在这个版本中,开发者可能运用了JAVA的多线程、网络通信以及游戏设计...
通常,类似“TankWar1.9”这样的游戏文件包,会包含游戏的所有资源和源代码,这为学习者提供了宝贵的学习资源。通过这些资源,学习者可以深入分析游戏的代码结构,理解如何使用Java开发图形用户界面(GUI),以及...
其他版本如TankWar0.2到TankWar1.9,可能是项目开发的不同迭代版本,展示了游戏从无到有,从简单到复杂的发展过程,对于学习者来说,通过对比不同版本的代码,可以深入理解软件开发的迭代和优化过程。 总的来说,...
在`TankWar1.9`项目中,会涉及到类、对象的创建,以及封装、继承和多态等面向对象编程的基本特性。例如,坦克、地图、子弹等游戏元素都可能被设计为独立的类,每个类都有其特定的行为和属性。 其次,游戏的控制逻辑...
【Java开发的TankWar游戏】 Java编程语言以其跨平台性和丰富的类库被广泛应用于各种软件开发,包括游戏制作。"TankWar"是一个基于Java开发的小型游戏,它展示了如何使用Java进行游戏编程,同时提供了源代码供学习者...
《Java TankWar:一款经典坦克对战游戏的解析与学习》 Java TankWar是一款基于Java编程语言开发的经典坦克对战游戏。它不仅包含了游戏的基本框架,还包括了源代码、可执行的jar包以及详细的doc文档,为开发者和游戏...
JAVA 游戏坦克大战 TankWar.zipJAVA 游戏坦克大战 TankWar.zip JAVA 游戏坦克大战 TankWar.zipJAVA 游戏坦克大战 TankWar.zip JAVA 游戏坦克大战 TankWar.zipJAVA 游戏坦克大战 TankWar.zip JAVA 游戏坦克大战 ...
TankWar2.6(无敌发射游戏).TankWar2.6(无敌发射游戏).
TankWar游戏制作的思想,步骤,总结情况
在游戏开发领域,"TankWar"是一款经典的2D射击小游戏,深受编程爱好者和初级游戏开发者喜爱。本项目提供了一套完整的开发文档和源代码,旨在帮助学习者深入理解游戏开发的基本流程和技术要点。本文将详细解析其中的...
《TankWar源码详解——揭示Android游戏开发的秘密》 在Android平台上,开发一款游戏往往是一项技术与创意并重的工作。今天我们将深入探讨的"TankWar源码",就是这样一个典型的例子,它为我们揭示了Android游戏开发...
《坦克大战TankWar_a5源码解析》 在Android开发领域,学习并分析优秀的源码是提升技术能力的重要途径。本篇文章将深入探讨名为“TankWar”的Android游戏源码,帮助开发者了解其背后的编程思想、设计模式以及Android...
本篇文章将深入探讨如何利用Android SDK开发一款名为"TankWar"的坦克战斗游戏。我们将从游戏设计、编程基础、图形绘制到碰撞检测等关键环节进行详细讲解。 一、游戏设计 "TankWar"是一款基于2D的坦克对战游戏,玩家...
在“TankWar-master.zip”这个压缩包中,我们预计会找到这个游戏项目的源代码、资源文件、场景设置、动画配置等相关内容。以下是一些可能包含的关键知识点: 1. **Unity Engine基础知识**:理解Unity的基本工作流程...
《坦克大战》(TankWar)是一款经典的双人对战游戏,深受玩家喜爱。它起源于1985年,由日本公司Namco开发并首次在任天堂FC(Family Computer)平台上发布,是那个时代电子游戏的代表作之一。游戏的核心玩法是通过控制...
《Java编程:坦克大战游戏TankWar0.1详解》 在IT行业中,编程学习与实践总是相辅相成的,而“坦克大战”作为一个经典的计算机游戏,常常被用来作为教学案例,帮助开发者学习和理解游戏编程的基本原理。本文将深入...
《TankWar坦克大战代码解析与技术探讨》 “TankWar坦克大战”是一款经典的电子游戏,以其简单的操作和激烈的战斗体验深受玩家喜爱。本篇文章将深入剖析TankWar的代码结构,探讨其中的关键技术和实现原理,旨在为对...
如今,它被封装在名为“TankWar.zip”的压缩包中,让我们有机会重温那段激情燃烧的岁月。这个压缩包包含了一个基于Java编程语言实现的坦克大战游戏,让我们一同深入探索其中蕴含的IT知识点。 首先,我们要明白Java...
坦克大战(TankWar) 坦克大战(TankWar) 坦克大战(TankWar)