坦克大战游戏代码
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
坦克大战的源代码
import java.awt.*;
import java.util.Random;
public class Boold {
int x, y, w = 10, h = 10;
/*//指明血块运动轨迹,由pos中的点组成
int [][] pos = {{250, 250}, {250, 260}, {250, 275}, {260,280},
{270, 285}, {285, 299}, {295, 280}, {270, 260}, {270, 400},
{260, 2700}, {250, 260}};
private int step = 0;*/
private boolean live = true;
MyFrame mf = null;
private static Random r = new Random();
private int XPoint = r.nextInt(600) + 100, YPoint = r.nextInt(400) + 80;
public boolean isLive() {
return live;
}
public void setLive(boolean live) {
this.live = live;
}
/*public Boold (){
x = pos[0][0];
y = pos[0][1];
}*/
private void location () {
XPoint = r.nextInt(600) + 100;
YPoint = r.nextInt(400) + 80;
}
public void draw (Graphics g) {
if (!live) {
location ();
this.live = true;
}
Color c = g.getColor();
g.setColor(Color.magenta);
g.fillRect(XPoint, YPoint, w, h);
g.setColor(c);
//move ();
}
/*void move () {
step ++;
if (step == pos.length){
step = 0;
}
x = pos[step][0];
y = pos[step][1];
}*/
public Rectangle getRect (){
return new Rectangle(XPoint, YPoint, w, h);
}
}
import java.awt.*;
public class Explode {
int x, y;
int[] diameter = {5,15,28,44,50,27,4};
int step = 0;
private boolean live = true;
MyFrame mf = null;
public Explode(int x, int y, MyFrame mf){
this.x = x;
this.y = y;
this.mf = mf;
}
public void draw(Graphics g){
if(!live){
mf.explodes.remove(this);
return;
}
if(step == diameter.length){
live = false;
step = 0;
return;
}
Color c = g.getColor();
g.setColor(Color.orange);
g.fillOval(x, y, diameter[step], diameter[step]);
g.setColor(c);
step ++;
}
}
import java.awt.*;
import java.util.List;
public class Missile {
public static final int XSPEED = 10;
public static final int YSPEED = 10;
public static final int WIDTH = 10, HEIGHT = 10;
private boolean live = true;
private boolean good;
MyFrame mf = null;
public boolean isLive() {
return live;
}
int x,y;
Tank.Direction dir;
public Missile(int x, int y, boolean good, Tank.Direction dir, MyFrame mf){
this(x,y,dir);
this.good = good;
this.mf = mf;
}
public Missile(int x, int y, Tank.Direction dir) {
this.x = x;
this.y = y;
this.dir = dir;
}
public void draw(Graphics g){
if(!live){
mf.missiles.remove(this);
return;
}
Color c = g.getColor();
g.setColor(Color.black);
g.fillOval(x, y, WIDTH, HEIGHT);
g.setColor(c);
move();
}
private void move() {
switch(dir){
case L :
x -= XSPEED;
break;
case LU :
x -= XSPEED;
y -= YSPEED;
break;
case U :
y -= YSPEED;
break;
case RU :
x += XSPEED;
y -= YSPEED;
break;
case R
:
x += XSPEED;
break;
case RD :
x += XSPEED;
y += YSPEED;
break;
case D :
y += YSPEED;
break;
case LD :
x -= XSPEED;
y += YSPEED;
break;
}
if(x<0 || y<0 || x > MyFrame.CLIENT_WIDTH || y> MyFrame.CLIENT_HEIGHT){
live = false;
}
}
public Rectangle getRect(){
return new Rectangle (x, y, WIDTH, HEIGHT);
}
public boolean hitTank(Tank t){
if(this.live && this.getRect().intersects(t.getRect()) && t.isLive() && this.good != t.isGood()){
if(t.isGood()){
t.setLife(t.getLife() - 20);
if(t.getLife() <= 0) {
t.setLive(false);
}
}
else{
t.setLive(false);
}
this.live = false;
Explode e = new Explode (x, y, mf);
mf.explodes.add(e);
return true;
}
return false;
}
public boolean hitTanks(List
for(int i=0;i
return true;
}
}
return false;
}
public boolean hitWall (Wall w) {
if (this.live && this.getRect().intersects(w.getRect())) {
this.live = false;
return true;
}
return false;
}
}
import java.awt.*;
import java.awt.event.*;
import java.util.*;
/**
* 坦克类:包含坦克的移动,行为
* @author zhukeyu
*
*/
public class Tank {
private int x,y;
private int oldX, oldY;
public static final int XSPEED = 5,YSPEED = 5;
public static final int WIDTH = 30, HEIGHT = 30;
private static Random r = new Random();
private BooldBar bb;
private int life = 100;
public int getLife() {
return life;
}
public void setLife(int life) {
this.life = life;
}
private int step = r.nextInt(20) +3;
MyFrame mf = null;
enum Direction {L,LU,U,RU,R,RD,D,LD,STOP};
private boolean bL = false,bU = false,bR = false,bD = false;
private boolean live = true;
public boolean isLive() {
return live;
}
public void setLive(boolean live) {
this.live = live;
}
private Direction dir = Direction.STOP;
private Direction ptDir = Direction.D;
private boolean good ;
public boolean isGood() {
return good;
}
public Tank(int x, int y, boolean good) {
this.x = x;
this.y = y;
this.good = good;
}
public Tank(int x ,int y,boolean good, Direction dir, MyFrame mf){
this(x,y,good);
this.dir = dir;
this.mf = mf;
}
public void draw(Graphics g){
if(!live){
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);
if(good) {
bb = new BooldBar();
bb.draw(g);
}
switch(ptDir){ //画出炮筒
case L :
g.drawLine(x+Tank.WIDTH/2, y+Tank.HEIGHT/2, x, y+Tank.HEIGHT/2);
break;
case LU :
g.drawLine(x+Tank.WIDTH/2, y+Tank.HEIGHT/2, x, y);
break;
case U :
g.drawLine(x+Tank.WIDTH/2, y+Tank.HEIGHT/2, x+Tank.HEIGHT/2, y);
break;
case R
U :
g.drawLine(x+Tank.WIDTH/2, y+Tank.HEIGHT/2, x+Tank.WIDTH, y);
break;
case R :
g.drawLine(x+Tank.WIDTH/2, y+Tank.HEIGHT/2, x+Tank.WIDTH, y+Tank.HEIGHT/2);
break;
case RD :
g.drawLine(x+Tank.WIDTH/2, y+Tank.HEIGHT/2, x+Tank.WIDTH, y+Tank.HEIGHT);
break;
case D :
g.drawLine(x+Tank.WIDTH/2, y+Tank.HEIGHT/2, x+Tank.WIDTH/2, y+Tank.HEIGHT);
break;
case LD :
g.drawLine(x+Tank.WIDTH/2, y+Tank.HEIGHT/2, x, y+Tank.HEIGHT);
break;
}
move();
}
void move(){
this.oldX = x;
this.oldY = y;
switch(dir){
case L :
x -= XSPEED;
break;
case LU :
x -= XSPEED;
y -= YSPEED;
break;
case U :
y -= YSPEED;
break;
case RU :
x += XSPEED;
y -= YSPEED;
break;
case R :
x += XSPEED;
break;
case RD :
x += XSPEED;
y += YSPEED;
break;
case D :
y += YSPEED;
break;
case LD :
x -= XSPEED;
y += YSPEED;
break;
case STOP :
break;
}
if (this.dir != Direction.STOP) {
this.ptDir = this.dir;
}
if (x<20) x = 20;
if (y<25) y = 25;
if (x + Tank.WIDTH > MyFrame.CLIENT_WIDTH) x = MyFrame.CLIENT_WIDTH-Tank.WIDTH;
if (y + Tank.HEIGHT > MyFrame.CLIENT_HEIGHT) y = MyFrame.CLIENT_HEIGHT-Tank.HEIGHT;
if(!isGood()) {
Direction [] dirs = Direction.values();
if(step == 0){
step = r.nextInt(20) + 3;
int rn = r.nextInt(dirs.length);
dir = dirs[rn];
}
step --;
if(r.nextInt(50) > 47) this.fire();
}
}
public void keyPressed(KeyEvent e){
int key = e.getKeyCode();
switch(key){
case KeyEvent.VK_RIGHT :
bR = true;
break;
case KeyEvent.VK_F2 :
if(!this.live) {
this.live = true;
this.life = 100;
if(!mf.b.isLive()) {
mf.b.setLive(true);
}
}
break;
case KeyEvent.VK_LEFT :
bL = true;
break;
case KeyEvent.VK_DOWN :
bD = true;
break;
case KeyEvent.VK_UP :
bU = true;
break;
}
locationDirection();
}
public void keyReleased(KeyEvent e) {
int key = e.getKeyCode();
switch(key){
case KeyEvent.VK_CONTROL :
fire();
break;
case KeyEvent.VK_RIGHT :
bR = false;
break;
case KeyEvent.VK_LEFT :
bL = false;
break;
case KeyEvent.VK_DOWN :
bD = false;
break;
case KeyEvent.VK_UP :
bU = false;
break;
case KeyEvent.VK_A :
superFire();
}
locationDirection();
}
void locationDirection(){
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 Missile fire(){
if(!live) return null;
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, good, ptDir, this.mf);
mf.missiles.add(m);
return m;
}
public void superFire () {
Direction [] dirs = Direction.values();
for (int i =0;i<8;i++) {
fire(dirs[i]);
}
}
public Missile fire(Direction dir){
if(!live) return null;
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, good, dir, this.mf);
mf.missiles.add(m);
return m;
}
public Rectangle getRect(){
return new Rectangle (x, y, WIDTH, HEIGHT);
}
private void stay (){
x = oldX;
y = oldY;
}
public boolean connectedWithWall (Wall w) {
if (this.live && this.getRect().intersects(w.getRect())) {
this.stay();
return true;
}
return false;
}
public boolean connectedWithTank (java.util.List
for (int i=0;i
if (this != t){
if (this.live && t.isLive() && this.getRect().intersects(t.getRect())) {
this.stay();
t.stay();
return true;
}
}
}
return false;
}
private class BooldBar {
void draw (Graphics g) {
Color c = g.getColor();
g.setColor(Color.red);
g.drawRect(x, y-5, WIDTH, 5);
int w = WIDTH * life/100;
g.fillRect(x, y-5, w, 5);
g.setColor(c);
}
}
public boolean eatBoold (Boold b){
if (this.live && mf.b.isLive() && this.getLife()<= 60 && this.getRect().intersects(b.getRect())){
this.life = 100;
b.setLive(false);
return true;
}
return false;
}
}
import java.awt.*;
public class Wall {
int x, y, w, h;
MyFrame mf = null;
public Wall(int x, int y, int w, int h, MyFrame mf) {
this.x = x;
this.y = y;
this.w = w;
this.h = h;
this.mf = mf;
}
public void draw (Graphics g) {
g.fillRect(x, y, w, h);
}
public Rectangle getRect () {
return new Rectangle (x, y, w, h);
}
}
import java.awt.*;
import java.awt.event.*;
import java.util.List;
import java.util.ArrayList;
import java.util.Random;
public class TankWarClient {
public static void main(String[] args) {
new MyFrame().launchFrame();
}
}
class MyFrame extends Frame{
private static final long serialVersionUID = 1L;
Image offScreenImage = null;
public static final int CLIENT_WIDTH = 800;
public static final int CLIENT_HEIGHT = 600;
private int XPoint, YPoint;
private static Random r = new Random();
Wall w1 = new Wall (100, 300, 20, 100, this), w2 = new Wall (300, 300, 150, 20, this);
Tank myTank = new Tank (350, 350, true, Tank.Direction.STOP, this);
List
List
List
Boold b
= new Boold();
public void paint(Graphics g) {
g.drawString("Missiles' Counts : " + missiles.size(), 10, 50);
g.drawString("Explodes' Counts: "+explodes.size(), 10, 70);
g.drawString("Tanks' Count: "+tanks.size(), 10, 90);
g.drawString("Tank's life " + myTank.getLife(), 10, 110);
w1.draw(g);
w2.draw(g);
if(tanks.size() <= 3) {
for(int i=0;i<6 - tanks.size();i++){
XPoint = r.nextInt(700) + 50;
YPoint = r.nextInt(500) + 50;
Tank t = new Tank(XPoint, YPoint, false, Tank.Direction.D, this);
tanks.add(t);
}
}
for(int i=0;i
m.hitTanks(tanks);
m.hitTank(myTank);
m.hitWall(w1);
m.hitWall(w2);
m.draw(g);
/*if(!m.isLive()){
missiles.remove(m);
}
else m.draw(g);*/
}
for (int i=0;i
e.draw(g);
}
for(int i =0;i< tanks.size();i++){
Tank t = tanks.get(i);
t.connectedWithWall(w1);
t.connectedWithWall(w2);
t.connectedWithTank(tanks);
if(!t.isLive()){
if(!t.isGood()){
tanks.remove(t);
}
}
else{
t.draw(g);
}
}
myTank.draw(g);
b.draw(g);
myTank.eatBoold(b);
}
public void update(Graphics g) {
if(offScreenImage == null){
offScreenImage = this.createImage(CLIENT_WIDTH, CLIENT_HEIGHT);
}
Graphics gOffScreen = offScreenImage.getGraphics();
Color c = gOffScreen.getColor();
gOffScreen.setColor(Color.green);
gOffScreen.fillRect(0, 0, CLIENT_WIDTH, CLIENT_HEIGHT);
gOffScreen.setColor(c);
paint(gOffScreen);
g.drawImage(offScreenImage, 0, 0, null);
}
public void launchFrame(){
this.setBounds(300, 100, CLIENT_WIDTH, CLIENT_HEIGHT);
this.setBackground(Color.GREEN);
this.setTitle("TankWar");
for(int i=0;i<6;i++){
XPoint = r.nextInt(700) + 50;
YPoint = r.nextInt(500) + 50;
Tank t = new Tank(XPoint, YPoint, false, Tank.Direction.D, this);
tanks.add(t);
}
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
this.addKeyListener(new KeyMonitor());
this.setResizable(false);
this.setVisible(true);
new Thread(new RepaintThread()).start();
}
private class RepaintThread implements Runnable{
public void run() {
try {
while(true){
repaint();
Thread.sleep(50);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
private class KeyMonitor extends KeyAdapter{
public void keyReleased(KeyEvent e) {
myTank.keyReleased(e);
}
public void keyPressed(KeyEvent e) {
myTank.keyPressed(e);
}
}
}