好久没更新,坚持,不要灰心。!
坦克大战2.0.
//各个成员类
//定义一个坦克类
class Tank
{
int x=0;
int y=0;
int direct=0;
int Type=0;
public int getType() {
return Type;
}
public void setType(int type) {
Type = type;
}
//设置坦克的速度
int speed=1;
public int getSpeed() {
return speed;
}
public void setSpeed(int speed) {
this.speed = speed;
}
//坦克的方向 0 向上 1 向下 2 向左 3 向右
public int getDirect() {
return direct;
}
public void setDirect(int direct) {
this.direct = direct;
}
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 EnemyTank extends Tank
{
public EnemyTank(int x,int y)
{
super(x,y);
}
}
//我的坦克
class Hero extends Tank
{
//子弹
Shot s=null;
public Hero(int x,int y)
{
super(x,y);
}
//开火
public void fire()
{
switch(this.direct)
{
case 0:
s=new Shot(x+10,y,0);
break;
case 1:
s=new Shot(x+10, y+30,1);
break;
case 2:
s=new Shot(x, y+10,2);
break;
case 3:
s=new Shot(x+30, y+10,3);
break;
}
Thread t=new Thread(s);
t.start();
}
public void moveUp()
{
y-=speed;
}
public void moveDown()
{
y+=speed;
}
public void moveLeft()
{
x-=speed;
}
public void moveRight()
{
x+=speed;
}
}
//子弹类
class Shot implements Runnable
{
int x=0;
int y=0;
int direct;
int speed=3;
boolean isLive=true;
public Shot(int x,int y,int direct)
{
this.x=x;
this.y=y;
this.direct=direct;
}
public void run()
{
while(true)
{try {
Thread.sleep(50);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
switch(direct)
{
case 0:
y-=speed;
break;
case 1:
y+=speed;
break;
case 2:
x-=speed;
break;
case 3:
x+=speed;
break;
}
System.out.println("x="+x+" y="+y);
if(x<0||x>400||y<0||y>400){
this.isLive=false;
break;
}
}
}
}
//执行块
package com.tank2;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.*;
import java.util.*;
public class Demo1_1 extends JFrame{
MyPanel mp=null;
public static void main(String[] args) {
Demo1_1 dm=new Demo1_1();
}
//构造函数
public Demo1_1()
{
mp=new MyPanel();
Thread t=new Thread(mp);
t.start();
this.add(mp);
this.addKeyListener(mp);
this.setSize(400,300);
this.setLocation(200,200);
this.setTitle("坦克大战");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
}
//我的面板
class MyPanel extends JPanel implements KeyListener,Runnable
{
//定义一个我的坦克;(到时候再封装成一个函数)
Hero hero=null;
//定义敌人的坦克组
Vector<EnemyTank> ets=new Vector<EnemyTank>();
int ensize=3;
public MyPanel()
{
hero=new Hero(100,100);
hero.setDirect(0);
hero.setType(1);
//初始化敌人的坦克
for(int i=0;i<ensize;i++)
{
EnemyTank et=new EnemyTank((i+1)*50, 0);//创建一辆敌人的坦克,加入到组中
et.direct=1;
et.Type=0;
ets.add(et);
}
}
public void paint(Graphics g)
{
super.paint(g);
g.fill3DRect(0, 0, 400, 300, false);
//画出我的坦克;
this.drawTank(hero.getX(), hero.getY(), g, hero.getDirect(), hero.getType());
//画子弹
if(hero.s!=null&&hero.s.isLive==true)
{
g.draw3DRect(hero.s.x, hero.s.y, 3, 3, false);
}
//画敌人的坦克
for(int i=0;i<ets.size();i++)
{
this.drawTank(ets.get(i).getX(), ets.get(i).getY(), g, ets.get(i).getDirect(), ets.get(i).getType());
}
}
//画坦克的函数;
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+8, 9, 10);
g.drawLine(x+10, y+15, x+10, y);
break;
case 1:
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+8, 9, 10);
g.drawLine(x+10, y+15, x+10, y+30);
break;
case 2:
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+8, y+4, 10, 10);
g.drawLine(x+15, y+10,x, y+10);
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+8, y+4, 10, 10);
g.drawLine(x+15, y+10,x+30, y+10);
break;
}
}
@Override
public void keyPressed(KeyEvent e) {//键按下处理:a 表示向左 s 表示向下
// TODO Auto-generated method stub
if(e.getKeyCode()==KeyEvent.VK_W)
{
//设置坦克的方向
this.hero.setDirect(0);
this.hero.moveUp();
}else if(e.getKeyCode()==KeyEvent.VK_S)
{
this.hero.setDirect(1);
this.hero.moveDown();
}else if(e.getKeyCode()==KeyEvent.VK_A)
{
this.hero.setDirect(2);
this.hero.moveLeft();
}else if(e.getKeyCode()==KeyEvent.VK_D)
{
this.hero.setDirect(3);
this.hero.moveRight();
}
if(e.getKeyCode()==KeyEvent.VK_J){
this.hero.fire();}
//必须重绘
this.repaint();
}
@Override
public void keyReleased(KeyEvent e) {
}
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
@Override
public void run() {
// TODO Auto-generated method stub
while(true)
{
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}this.repaint();
}
}
}
可能遇到了最难的关卡。先冲过去试试看。