韩顺平java坦克大战1.0版本_源代码
/**画坦克1.0
*/
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.awt.event.MouseMotionListener; import java.awt.event.WindowEvent; import java.awt.event.WindowListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MyTankGame extends JFrame {
MyPanel mp=null;
public static void main(String[] args)
{
MyTankGame mtk=new MyTankGame();
}
public MyTankGame()
{
mp=new MyPanel();
this.add(mp);//把面板加入窗体
//注册监听
this.addMouseListener(mp);
this.addKeyListener(mp);
this.addMouseMotionListener(mp);
this.addWindowListener(mp);
this.setTitle("坦克大战");//窗体标题
this.setSize(600,400);//大小,宽,高(像素)
this.setLocation(300,300);//显示位置。左边距,上边距
//禁止用户改变窗口大小
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);//显示
}
}
//我的面板
class MyPanel extends JPanel implements
WindowListener,MouseListener,MouseMotionListener,KeyListener
{//定义一个我的坦克
Hero hero=null;
Diren diren=null;
public MyPanel()
{
hero=new Hero(300,200);
diren=new Diren(100,0);
//diren.start();
}
public void paint(Graphics g)
{
super.paint(g);
//画坦克(到时候封装到一个函数)
//画左边矩形
//g.setColor(Color.black);
g.fillRect(0, 0, 600, 400);
this.drawTank(hero.getX(),hero.getY(), g,hero.dstct,1);
this.drawTank(diren.getX(),diren.getY(), g,1,0);
this.drawTank(diren.getX()+50,diren.getY(), g,1,0);
this.drawTank(diren.getX()+100,diren.getY(), g,1,0);
}
//画坦克的函数
public void drawTank(int x,int y,Graphics g,int direct,int type)
{
switch (type)
{//0我的坦克
case 0:g.setColor(Color.blue);
break;
//1敌人坦克
case 1:g.setColor(Color.GREEN);
break;
}
// 判断方向
switch (direct)
{//0向上
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,true);//中
g.setColor(Color.RED);
g.drawLine(x+10, y+15, x+10,y);//线
g.setColor(Color.YELLOW);
g.fillOval(x+5, y+10, 10, 10);//圆
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,true);
g.setColor(Color.RED);
g.drawLine(x+10, y+15, x+10,y+30);
g.setColor(Color.YELLOW);
g.fillOval(x+5, y+10, 10, 10);
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,true);
g.setColor(Color.RED);
g.drawLine(x+15, y+10, x,y+10);
g.setColor(Color.YELLOW);
g.fillOval(x+10, y+5, 10, 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,true);
g.setColor(Color.RED);
g.drawLine(x+15, y+10, x+30,y+10);
g.setColor(Color.YELLOW);
g.fillOval(x+10, y+5, 10, 10);
break;
}
}
public void keyPressed(KeyEvent e)
{
// 键按下监听
System.out.println("按下:"+e.getKeyChar());
if(e.getKeyCode()==KeyEvent.VK_DOWN)
{
hero.setY(hero.getY()+hero.getSpeed());
hero.dstct=1;
}
else if(e.getKeyCode()==KeyEvent.VK_UP)
{
hero.setY(hero.getY()-hero.getSpeed());
hero.dstct=0;
}
else if(e.getKeyCode()==KeyEvent.VK_LEFT)
{
hero.setX(hero.getX()-hero.getSpeed());
hero.dstct=2;
}
else if(e.getKeyCode()==KeyEvent.VK_RIGHT)
{
hero.setX(hero.getX()+hero.getSpeed());
hero.dstct=3;
}
this.repaint();
}
public void keyReleased(KeyEvent e) {
// 键松开监听
}
public void keyTyped(KeyEvent e) {
// 键的一个值被输出
}
public void mouseClicked(MouseEvent e) {
//点击
System.out.println("鼠标被点击了x="+e.getX()+"y="+e.getY());
}
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
//进入
System.out.println("鼠标进入x="+e.getX()+"y="+e.getY());
}
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
//离开
System.out.println("鼠标离开x="+e.getX()+"y="+e.getY());
}
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
//压下
System.out.println("鼠标被按下x="+e.getX()+"y="+e.getY());
}
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
System.out.println("鼠标被松开x="+e.getX()+"y="+e.getY());
//松开
}
public void mouseDragged(MouseEvent e) {
// 拖拽
System.out.println("鼠标被拖拽x="+e.getX()+"y="+e.getY());
}
public void mouseMoved(MouseEvent e) {
// 移动
System.out.println("鼠标被移动x="+e.getX()+"y="+e.getY());
}
public void windowActivated(WindowEvent e) {
// 激活TODO Auto-generated method stub
}
public void windowClosed(WindowEvent e) {
// 窗口关闭
}
public void windowClosing(WindowEvent e) {
//正在关闭 TODO Auto-generated method stub
}
public void windowDeactivated(WindowEvent e) {
// TODO Auto-generated method stub
}
public void windowDeiconified(WindowEvent e) {
// TODO Auto-generated method stub
}
public void windowIconified(WindowEvent e) {
// TODO Auto-generated method stub
}
public void windowOpened(WindowEvent e) {
// 打开TODO Auto-generated method stub
}
}
//坦克父类
class Tank
{//坦克横坐标
int x=0;
//坦克纵坐标
int y=0;
int speed=5;//速度
static int dstct=0;
public Tank(int x, int y)
{
this.x=x;
this.y=y;
}
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 static int getDstct() {
return dstct;
}
public static void setDstct(int dstct) {
Tank.dstct = dstct;
}
public int getSpeed() {
return speed;
}
public void setSpeed(int speed) {
this.speed = speed;
}
}
//我的坦克
class Hero extends Tank
{
public Hero(int x ,int y )
{
super(x, y);
}
// public int tankup // {
// y=y-getSpeed(); // }
// public int tankdown // {
// y=y+speed;
// }
// public int tankleft // {
// x=x-speed;
// }
// public int tankright // {
// x=x+speed;
// }
//
}
//敌人坦克
class Diren extends Tank implements Runnable
{
public Diren(int x ,int y )
{
super(x, y);
}
public void run()
{while(true)
{ //休眠一秒
//1000秒=1秒.进入堵塞。BLOCKED.
try
{
Thread.sleep(1000);
} catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
}
本文档为【韩顺平java坦克大战1.0版本_源代码】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑,
图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
该文档来自用户分享,如有侵权行为请发邮件ishare@vip.sina.com联系网站客服,我们会及时删除。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。
本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。
网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。