今天将坦克及其两个子类从驱动类中脱离出来
实现功能有
1、显示敌方坦克,
2、自己的坦克可以向四个方向移动
有两个类
MyTankGame2.java
/*
* 功能:坦克游戏1.0
* 1.画出坦克
* 2.我的坦克可以上下左右移动
*/
package com.tank2;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.*;
public class MyTankGame2 extends JFrame{
MyPanel mp;
public static void main(String[] args) {
MyTankGame2 mtg=new MyTankGame2();
}
public MyTankGame2(){
mp=new MyPanel();
this.add(mp);
this.addKeyListener(mp);
this.setTitle("坦克大战");
this.setSize(550,400);
this.setLocation(350, 200);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
}
class MyPanel extends JPanel implements KeyListener{
Hero hero=null;
Vector<EnemyTank> ets =new Vector<EnemyTank>();
int ensize=3;
public MyPanel(){
hero=new Hero(100,230);
for(int i=0;i<ensize;i++){
EnemyTank et=new EnemyTank((i+1)*50,0);
et.setColor(0);
et.setDirect(2);
ets.add(et);
}
}
public void paint(Graphics g){
super.paint(g);
g.fillRect(0, 0, 400, 300);
//画出自己的坦克
this.drawTank(hero.getX(), hero.getY(), g, hero.getDirect(), 1);
//画出敌人的坦克
for(int i=0;i<ets.size();i++){
this.drawTank(ets.get(i).getX(), ets.get(i).getY(), g, ets.get(i).getDirect(), 0);
}
}
//画出坦克的方法
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);
break;
}
//判断什么方向(以左上角为参照点)
switch(direct){
case 0:
g.fill3DRect(x,y, 5, 30, false);
g.fill3DRect(x+15,y, 5, 30, false);
g.fill3DRect(x+5,y+5, 10, 20, false);
g.fillOval(x+5, y+10,10, 10);
g.drawLine(x+10, y+15, x+10,y);
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.fill3DRect(x,y, 5, 30, false);
g.fill3DRect(x+15,y, 5, 30, false);
g.fill3DRect(x+5,y+5, 10, 20, false);
g.fillOval(x+5, y+10,10, 10);
g.drawLine(x+10, y+15, 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
//a左 w上 s下 d右
public void keyPressed(KeyEvent e) {
if(e.getKeyCode()==KeyEvent.VK_W){
this.hero.setDirect(0);
this.hero.moveUp();
}else if(e.getKeyCode()==KeyEvent.VK_D){
this.hero.setDirect(1);
this.hero.moveRight();
}else if(e.getKeyCode()==KeyEvent.VK_S){
this.hero.setDirect(2);
this.hero.moveDown();
}else if(e.getKeyCode()==KeyEvent.VK_A){
this.hero.setDirect(3);
this.hero.moveLeft();
}
this.repaint();
}
@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
}
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
}
Members.java
package com.tank2;
class Tank{
int x=0;
int y=0;
//0表示上 1表示右 2小时下 3表示左
int direct=0;
int speed=1;
int type;
int color;
public Tank(int x,int y){
this.x=x;
this.y=y;
}
public int getSpeed() {
return speed;
}
public void setSpeed(int speed) {
this.speed = speed;
}
public int getDirect() {
return direct;
}
public void setDirect(int direct) {
this.direct = direct;
}
public void setX(int x){
this.x=x;
}
public int getX(){
return x;
}
public void setY(int y){
this.y=y;
}
public int getY(){
return y;
}
public int getType() {
return type;
}
public void setType(int type) {
this.type = type;
}
public int getColor() {
return color;
}
public void setColor(int color) {
this.color = color;
}
}
class EnemyTank extends Tank{
public EnemyTank(int x,int y){
super(x,y);
}
}
class Hero extends Tank{
public Hero(int x, int y) {
super(x, y);
}
public void moveUp(){
y-=speed;
}
public void moveRight(){
x+=speed;
}
public void moveDown(){
y+=speed;
}
public void moveLeft(){
x-=speed;
}
}
运行结果如下图所示: