members.java
package tank2;
//坦克类
class Tank{
//坦克的横坐标
int x =0;
//坦克的纵坐标
int y =0;
//坦克方向
//0表示上,1表示右,2表示下,3表示左
//坦克速度;
int speed = 10;
int color = 0;
public int getColor() {
return color;
}
public void setColor(int color) {
this.color = color;
}
public int getSpeed() {
return speed;
}
public void setSpeed(int speed) {
this.speed = speed;
}
int dir = 0;
public int getDir() {
return dir;
}
public void setDir(int dir) {
this.dir = dir;
}
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 Tank(int x,int y) {
this.x = x;
this.y = y;
}
}
class Helo extends Tank{
Shot s = null;
public Helo(int x,int y) {
super(x,y);
}
// 坦克向上移动
public void moveUP() {
this.y -=this.speed;
}
// 坦克向右移动
public void moveRIGHT() {
this.x += this.speed;
}
// 坦克向下移动
public void moveDOWN() {
this.y += this.speed;
}
// 坦克向左移动
public void moveLEFT() {
this.x -= this.speed;
}
public void ShotEnemy() {
switch (this.dir) {
case 0:
s = new Shot(x+10,y,0);
break;
case 1:
s = new Shot(x+30, y+10,1);
break;
case 2:
s = new Shot(x+10,y+30,2);
break;
case 3:
s = new Shot(x, y+10,3);
break;
}
//启动子弹线程
Thread t = new Thread(s);
t.start();
}
}
class Enemy extends Tank{
public Enemy(int x,int y) {
super(x,y);
}
int dir =2;
}
//子弹类
class Shot implements Runnable{
int x = 0;
int y =0;
int dir;
int speed=1;
boolean isalive = true;
public Shot(int x,int y,int dir) {
this.x=x;
this.y=y;
this.dir =dir;
}
@Override
public void run() {
// TODO Auto-generated method stub
while (true) {
try {
Thread.sleep(50);
} catch (Exception e) {
// TODO: handle exception
}
switch(dir) {
case 0:
y -=speed;
break;
case 1:
x += speed;
break;
case 2:
y += speed;
break;
case 3:
x -=speed;
break;
}
System.out.println("子弹坐标x=" + x + "y=" + y);
//子弹不终结;
//判断子弹是否碰到边缘
if (x<0 || x>400 || y<0 || y>300) {
isalive = false;
break;
}
}
}
}
主类:
package tank2;
import javax.swing.JFrame;
/*
*
*
* 坦克游戏1.0
画出坦克
*/
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.Vector;
public class tank_1 extends JFrame{
mypane mp = null;
public static void main(String[] args) {
// TODO Auto-generated method stub
tank_1 tk = new tank_1();
System.out.println();
}
public tank_1(){
mp = new mypane();
Thread t = new Thread(mp);
t.start();
this.add(mp);
this.addKeyListener(mp);
this.setSize(400,300);
this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
this.setVisible(true);
}
}
// 我的面板
class mypane extends JPanel implements KeyListener,Runnable{
//定义一个我的坦克
Helo hero = null;
//定义敌人的坦克
Vector<Enemy> ets = new Vector<Enemy>();
int size =3;
//构造函数
public mypane() {
hero = new Helo(100, 100);
//初始化敌人的坦克
for (int i =0;i<size;i++){
//创建一个敌人的坦克对象
Enemy et = new Enemy((i+1)*50, 0);
//加入对象数组
et.setColor(1);
ets.add(et);
}
}
//重新paint
public void paint(Graphics g) {
super.paint(g);
//填充背景
g.fillRect(0, 0, 400, 300);
this.drawTank(hero.getX(), hero.getY(),g, this.hero.dir, 0);
//画出子弹
if (hero.s !=null && hero.s.isalive ==true) {
g.draw3DRect(hero.s.x, hero.s.y, 1, 1, false);
}
this.repaint();
//画出敌人的坦克:
for (int i=0;i<ets.size();i++) {
this.drawTank(ets.get(i).getX(), ets.get(i).getY(), g, ets.get(i).dir, ets.get(i).color);
}
/*g.setColor(Color.CYAN);
// 画出我的坦克;(可以封装成一个函数)
//左边矩形
//g.fillRect(hero.getX(), y, 5, 30);
g.fill3DRect(hero.getX(), y, 5, 30,false);
// 画出右边的矩形
//g.fillRect(hero.getX()+15, y, 5, 30);
g.fill3DRect(hero.getX()+15, y, 5, 30,false);
g.setColor(Color.white);
//画出中间矩形
//g.fillRect(hero.getX()+5, y+5, 10, 20);
g.fill3DRect(hero.getX()+5, y+5, 10, 20,false);
//画出圆形
g.fillOval(hero.getX()+5, y+10,10,10);
//画出线
g.drawLine(hero.getX()+10, y+10,hero.getX()+10,10);*/
}
//画坦克的函数
//x,y定义坦克的横坐标和纵坐标。
public void drawTank(int x,int y,Graphics g,int direct,int type) {
//判断是什么类型的坦克
switch (type) {
case 0:
g.setColor(Color.CYAN);
break;
case 1:
g.setColor(Color.yellow);
default:
break;
}
//判断方向
switch (direct) {
//向上
case 0:
// 画出我的坦克;(可以封装成一个函数)
//左边矩形
//g.fillRect(x, y, 5, 30);
g.fill3DRect(hero.getX(),hero.getY(), 5, 30,false);
// 画出右边的矩形
//g.fillRect(x+15, y, 5, 30);
g.fill3DRect(hero.getX()+15, hero.getY(), 5, 30,false);
//画出中间矩形
//g.fillRect(x+5, y+5, 10, 20);
g.fill3DRect(hero.getX()+5, hero.getY()+5, 10, 20,false);
//画出圆形
g.fillOval(hero.getX()+5, hero.getY()+10,10,10);
//画出线
g.drawLine(hero.getX()+10, hero.getY()+10,hero.getX()+10,hero.getY()-2);
break;
case 1:
//炮筒向右
g.fill3DRect(x, y, 30,5,false);
g.fill3DRect(x, y+15, 30,5, false);
g.fill3DRect(x+5, y+5, 20,10, false);
g.fillOval(x+10, y+5, 10, 10);
g.drawLine(x+15, y+10, x+30, y+10);
break;
case 2:
//左边矩形
//g.fillRect(x, y, 5, 30);
g.fill3DRect(x,y, 5, 30,false);
// 画出右边的矩形
//g.fillRect(x+15, y, 5, 30);
g.fill3DRect(x+15, y, 5, 30,false);
//画出中间矩形
//g.fillRect(x+5, y+5, 10, 20);
g.fill3DRect(x+5, y+5, 10, 20,false);
//画出圆形
g.fillOval(x+5, y+10,10,10);
//画出线
g.drawLine(x+10, y+10,x+10,y+30);
break;
case 3:
g.fill3DRect(x, y, 30,5,false);
g.fill3DRect(x, y+15, 30,5, false);
g.fill3DRect(x+5, y+5, 20,10, false);
g.fillOval(x+10, y+5, 10, 10);
g.drawLine(x+15, y+10, x, y+10);
break;
}
}
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
//对asdw做处理w向上
//s向下
//a向左d
//d向右
@Override
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
System.out.println("pk!!");
if (e.getKeyCode() == KeyEvent.VK_W) {
this.hero.setDir(0);
System.out.println("up");
this.hero.moveUP();
} else if (e.getKeyCode() == KeyEvent.VK_D) {
System.out.println("right");
this.hero.setDir(1);
this.hero.moveRIGHT();
} else if (e.getKeyCode() == KeyEvent.VK_S) {
System.out.println("down");
this.hero.setDir(2);
this.hero.moveDOWN();
} else if (e.getKeyCode() == KeyEvent.VK_A) {
System.out.println("left");
this.hero.setDir(3);
this.hero.moveLEFT();
}
if (e.getKeyCode() == KeyEvent.VK_J) {
this.hero.ShotEnemy();
}
//必须重新绘制panel
this.repaint();
}
@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
}
@Override
public void run() {
// TODO Auto-generated method stub
try {
Thread.sleep(100);
} catch (Exception e) {
// TODO: handle exception
}
//chonbghui
this.repaint();
}
}