韩顺平_循序渐进学java_从入门到精通_课件_笔记_坦克大战全过程(1)即第三十四讲到三十九讲

完全正确的代码!陆续更新中!(有些不完全是韩老师的代码,加入了自己的一些东西)
坦克大战1.0版
/*
* 作者:树的梦想
* 日期:2012.03.04
* 功能:做1.0版坦克大战游戏:画出一辆tanker就行了!
* 过程:
*/
package tanker1;

import java.awt.*;
import javax.swing.*;
public class Tanker1.0 extends JFrame{
MyPanel mp=null;
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Tanker1.0 tanker1.0=new Tanker1.0();
}
//构造函数
public Tanker1.0(){
mp=new MyPanel();

this.add(mp);
//属性设置
this.setTitle("画一辆简单的坦克");
this.setSize(500, 500);
this.setLocation(400, 200);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
//我的面板
class MyPanel extends JPanel{
//定义一辆我的坦克
Hero hero=null;
//构造函数
public MyPanel()
{
hero=new Hero(10,10);
}

//重定义paint
public void paint (Graphics g){
//调用父类初始化
super.paint(g);
//设置颜色背景,500为窗体大小
g.fillRect(0, 0, 500, 500);
this.drawTank(hero.getX(),hero.getY(),g,0,1);

}
//画出坦克的函数
public void drawTank(int x,int y,Graphics g,int direcct,int type)
{
//判断类型
switch(type)
{
//我的坦克
case 0:
g.setColor(Color.red);
break;
//敌人的坦克
case 1:
g.setColor(Color.green);
break;
}
int direct = 0;
//判断方向
switch (direct)
{
//向上
case 0:
//画出我的坦克(到时再封装成一个函数)
//1.画出左边的小矩形
g.fill3DRect(x, y, 5, 30,false);
//1.画出右边的小矩形
g.fill3DRect(x+15, y, 5, 30,false);
//3.画中间矩形
g.fill3DRect(x+5, y+5, 10, 20,false);
//4.画出圆形
g.fillOval(x+4, y+7, 10, 15);
//5.画出炮筒
g.drawLine(x+10, y-4, x+10, y+20);
break;
}
}
}
//坦克类
class Tank
{
//x表示坦克的横坐标
private int x=0;
public int getX(){
return x;
}
public void setX(int x){
this.x=x;
}

//坦克的纵坐标
private int y=0;
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 Hero extends Tank
{
public Hero (int x,int y)
{
super(x,y);

}
}
}
坦克大战2.0—4.0版
/*
* 作者:树的梦想
* 日期:2012.03.04
* 功能:做2.0-4.0版坦克大战游戏:一辆tanker能调转的移动和若干敌人坦克!
* 过程:
*/

package tanker4;

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
public class MyTankGame2 extends JFrame {

MyPanel mp=null;
public static void main(String[] args) {
MyTankGame2 mytank

game1=new MyTankGame2();

}
//构造函数
public MyTankGame2()
{
mp= new MyPanel();
this.add(mp);
//注册监听
this.addKeyListener(mp);
this.setSize(500,500);
this.setLocation(100,100);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);

}

}

//我的面板
class MyPanel extends JPanel implements KeyListener
{
//定义一个我的坦克
Hero hero=null;
//定义敌人的坦克组
Vector ets =new Vector();

int emsize=3;
//构造函数
public MyPanel()
{
hero =new Hero(10,10);
hero.setX(250);
hero.setY(440);
//初始化敌人的坦克
for (int i=0;i{
//创建敌人的一辆坦克
EnemyTank et=new EnemyTank((i+1)*120,0);
et.setDirect(2);
//加入敌人的坦克
ets.add(et);
}

}
//重新paint
public void paint(Graphics g)
{
super.paint(g);
//设置背景颜色,500为窗体的大小
g.fillRect(0, 0, 500, 500);
//画出敌人的坦克
for(int i=0;i{
this.drawTank(ets.get(i).getX(), ets.get(i).getY(), g,ets.get(i).getDirect(), 1);

}
//画出自己的坦克
this.drawTank(hero.getX(), hero.getY(), g,hero.getDirect(), 0);

}
//画出坦克的函数
public void drawTank (int x,int y ,Graphics g,int direct ,int type)
{
//判断类型
switch (type)
{
//我的坦克
case 0 :
g.setColor(Color.red);
break;
//敌人坦克
case 1:
g.setColor(Color.green);
break;
}
//判断方向
switch (direct)
{
//向上
case 0 :
//画出我的坦克(到时再封装成一个函数)
//1.画出左边的巨型
g.fill3DRect(x, y, 5, 30,false);
//2.画出右边的矩形
g.fill3DRect(x+15, y, 5, 30,false);
//3.画中间矩形
g.fill3DRect(x+5, y+5, 10, 20,false);
//4.画出圆形
g.fillOval(x+4, y+7, 10, 15);
//5.画出炮筒
g.drawLine(x+10, y-4, x+10, y+20);
break;
//向左
case 1:
//1.画出上边的巨型
g.fill3DRect(x, y, 30, 5,false);
//2.画出下边的矩形
g.fill3DRect(x, y+15, 30, 5,false);
//3.画中间矩形
g.fill3DRect(x+5, y+5, 20, 10,false);
//4.画出圆形
g.fillOval(x+7, y+4, 15, 10);
//5.画出炮筒
g.drawLine(x+20, y+10, x+35, y+10);
break;
//向下
case 2 :
//画出我的坦克(到时再封装成一个函数)
//1.画出左边的巨型
g.fill3DRect(x, y, 5, 30,false);
//2.画出右边的矩形
g.fill3DRect(x+15, y, 5, 30,false);
//3.画中间矩形
g.fill3DRect(x+5, y+5, 10, 20,false);
//4.画出圆形
g.fillOval(x+4, y+7, 10, 15);
//5.画出炮筒
g.drawLine(x+10, y+20, x+10, y+35);
break;
//向右
case 3:
//1.画出上边的巨型
g.fill3DRect(x, y, 30, 5,false);
//2.画出下边的矩形
g.fill3DRect(x, y+15, 30, 5,false);
//3.画中间矩形
g.fill3DRect(x+5, y+5, 20, 10,fals

e);
//4.画出圆形
g.fillOval(x+7, y+4, 15, 10);
//5.画出炮筒
g.drawLine(x-4, y+10, x+20, y+10);
break;
}

}
@Override
//案件处理,用a(左边)s(向下)d(向右)w(向上)
public void keyPressed(KeyEvent e) {
if(e.getKeyCode()==KeyEvent.VK_W)
{
//设置我的坦克的方向
//向上
this.hero.setDirect(0);
this.hero.moveup();

} 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();
}else if(e.getKeyCode()==KeyEvent.VK_D)
{
//向右
this.hero.setDirect(1);
this.hero.moveright();
}
this.repaint();

}
@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub

}
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub

}

}


5.0版坦克大战游戏


/*
* 作者:树的梦想
* 日期:2012.03.04
* 功能:做5.0版坦克大战游戏:坦克能打出子弹了!
* 过程:
*/

package tanker5;

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
public class MyTank3 extends JFrame {

MyPanel mp=null;
public static void main(String[] args) {
MyTank3 mytankgame1=new MyTank3();

}
//构造函数
public MyTank3()
{
mp= new MyPanel();

this.add(mp);
//启动线程
Thread t=new Thread(mp);
t.start();
//注册监听
this.addKeyListener(mp);
this.setTitle("坦克移动和打出子弹喔");
this.setSize(500,500);
this.setLocation(100,100);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);

}

}

//我的面板
class MyPanel extends JPanel implements KeyListener,Runnable
{
int x,y;
boolean islive=true;//判断子弹是否还活着
//定义一个我的坦克
Hero hero=null;

//定义敌人的坦克组
Vector ets =new Vector();

int emsize=3;
//构造函数
public MyPanel()
{
hero =new Hero(10,10);
hero.setX(250);
hero.setY(440);
//初始化敌人的坦克
for (int i=0;i{
//创建敌人的一辆坦克
EnemyTank et=new EnemyTank((i+1)*120,0);
et.setDirect(2);
//加入敌人的坦克
ets.add(et);
}

}
//重新paint
public void paint(Graphics g)
{
super.paint(g);
//设置背景颜色,500为窗体的大小
g.fillRect(0, 0, 500, 500);
//画出敌人的坦克
for(int i=0;i{
this.drawTank(ets.get(i).getX(), ets.get(i).getY(), g,ets.get(i).getDirect(), 1);

}
//画出自己的坦克
this.drawTank(hero.getX(), hero.getY(), g,hero.getDirect(), 0);

//画出子弹
if(hero.s!=null)
{
g.draw3DRect(hero.s.x, hero.s.y, 2, 2, false);
}
}
//画出坦克的函数
public void drawTank (

int x,int y ,Graphics g,int direct ,int type)
{
//判断类型
switch (type)
{
//我的坦克
case 0 :
g.setColor(Color.red);
break;
//敌人坦克
case 1:
g.setColor(Color.green);
break;
}
//判断方向
switch (direct)
{
//向上
case 0 :
//画出我的坦克(到时再封装成一个函数)
//1.画出左边的巨型
g.fill3DRect(x, y, 5, 30,false);
//2.画出右边的矩形
g.fill3DRect(x+15, y, 5, 30,false);
//3.画中间矩形
g.fill3DRect(x+5, y+5, 10, 20,false);
//4.画出圆形
g.fillOval(x+4, y+7, 10, 15);
//5.画出炮筒
g.drawLine(x+10, y-4, x+10, y+20);
break;
//向左
case 1:
//1.画出上边的巨型
g.fill3DRect(x, y, 30, 5,false);
//2.画出下边的矩形
g.fill3DRect(x, y+15, 30, 5,false);
//3.画中间矩形
g.fill3DRect(x+5, y+5, 20, 10,false);
//4.画出圆形
g.fillOval(x+7, y+4, 15, 10);
//5.画出炮筒
g.drawLine(x+20, y+10, x+35, y+10);
break;
//向下
case 2 :
//画出我的坦克(到时再封装成一个函数)
//1.画出左边的巨型
g.fill3DRect(x, y, 5, 30,false);
//2.画出右边的矩形
g.fill3DRect(x+15, y, 5, 30,false);
//3.画中间矩形
g.fill3DRect(x+5, y+5, 10, 20,false);
//4.画出圆形
g.fillOval(x+4, y+7, 10, 15);
//5.画出炮筒
g.drawLine(x+10, y+20, x+10, y+35);
break;
//向右
case 3:
//1.画出上边的巨型
g.fill3DRect(x, y, 30, 5,false);
//2.画出下边的矩形
g.fill3DRect(x, y+15, 30, 5,false);
//3.画中间矩形
g.fill3DRect(x+5, y+5, 20, 10,false);
//4.画出圆形
g.fillOval(x+7, y+4, 15, 10);
//5.画出炮筒
g.drawLine(x-4, y+10, x+20, y+10);
break;
}

}
@Override
//案件处理,用a(左边)s(向下)d(向右)w(向上)
public void keyPressed(KeyEvent e) {

if(e.getKeyCode()==KeyEvent.VK_W)
{
//设置我的坦克的方向
//向上
this.hero.setDirect(0);
this.hero.moveup();

} 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();
}else if(e.getKeyCode()==KeyEvent.VK_D)
{
//向右
this.hero.setDirect(1);
this.hero.moveright();
}

if(e.getKeyCode()==KeyEvent.VK_J)
{
//判断玩家是否按下j;
//开火
this.hero.ShotEnemy();
}



}
@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub

}
@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 (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
this

.repaint();
}
}
}

package tanker5;
//子弹类
class Shot implements Runnable
{
int x,y;
int speed=20;
int direct;
boolean islive=true;//判断子弹是否还活着

public Shot(int x,int y,int direct)
{
this.x=x;
this.y=y;
this.direct=direct;
}
@Override
public void run() {
// TODO Auto-generated method stub
while(true)
{
try {
Thread.sleep(10);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//子弹射出的方向
switch(direct)
{
case 0:
//上
y--;
break;
case 1:
//右
x++;
break;
case 2:
//下
y++;
break;
case 3:
//左
x--;
break;
}
System.out.println("子弹的坐标x=="+this.x+" y=="+this.y);
//判断该子弹是否碰到边缘
if(x<0||x>=500||y<0||y>=500){
islive=false;
break;
}
}
}
}
//坦克类
class Tank
{
//设置坦克的速度
int speed=4;
//设置坦克颜色
int color;

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;
}

//x表示坦克的横坐标
int x=0;
//坦克的纵坐标
int y=0;
//坦克方向
//0表示上,1表示右,2表示下,3表示左
protected int direct =0;
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 ShotEnemy(){
switch(this.direct)
{
case 0:
s=new Shot(x+10,y-10,0);

break;
case 1:
s=new Shot(x+40,y+10,1);
break;
case 2:
s=new Shot(x+10,y+40,2);
break;
case 3:
s=new Shot(x-10,y+10,3);
break;
}
//启动子弹线程
Thread t=new Thread(s);
t.start();
}

private int Direct() {
// TODO Auto-generated method stub
return 0;
}

//坦克向上移动
public void moveup()
{
y=y-speed;
}
//坦克向下移动
public void movedown()
{
y=y+speed;
}
//坦克向左移动
public void moveleft()
{
x=x-speed;
}
//坦克向右移动
public void moveright()
{
x=x+speed;
}

}







相关文档
最新文档