package com.zs;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.*;
public class TankGame {
public static void main(String[] args) {
// TODO Auto-generated method stub
MyFrame mf = new MyFrame();
}
}
//事件处理:1.事件源 2.监听器对象(监听事件源,并对事件进行处理)3.把监听器注册到事件源
//MyFrame就是事件源,可以发出事件,事件消息让坦克根据我按下的键位移动,把事件传给给监听器对象,让它对事件消息进行处理
class MyFrame extends JFrame{
MyPanel mp;
public MyFrame() {
mp = new MyPanel();
this.add(mp);
//注册监听器
this.addKeyListener(mp);
this.setTitle("坦克大战1.0版");
this.setSize(800, 600);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
//MyPanel监听器类,它的对象就是监听器对象,它负责接收事件源传递过来的事件,并对事件消息进行处理,该类的某些方法对事件处理
class MyPanel extends JPanel implements KeyListener{
Hero hero;
public MyPanel() {
hero = new Hero(100,200);//x = 100; y = 200;
}
//覆盖paint方法,在paint方法画出坦克
public void paint(Graphics g) {
super.paint(g);
//设置面板大小
this.setSize(600, 400);
this.setBackground(Color.BLACK);
g.setColor(Color.yellow);
//画我的坦克
drawTank(hero.getX(), hero.getY(), 0, hero.getDirect(), g);
}
public void drawTank(int x,int y,int type,int direct,Graphics g) {
//int x,int y表示坦克坐标位置,int type表示坦克类型,type的值是0,表示我方坦克,用黄颜色,type的值是1,表示敌方坦克,用红色表示
//int direct坦克方向,direct的值是0,表示坦克向上,1表示坦克向右,2表示坦克向下,3表示坦克向左,Graphics g画笔
//判断类型,根据值来修改画笔的颜色
if(type==0) {
//type的值是0,表示我方坦克,用黄颜色,type的值是1,表示敌方坦克,用红色表示
g.setColor(Color.YELLOW);
}
if(type == 1) {
g.setColor(Color.RED);
}
//判断坦克方向
switch (direct) {
case 0:
//画方向向上的坦克
//画左边矩形
g.fill3DRect(x-10, y-15, 5, 30, false);
//画右边矩形
g.fill3DRect(x+5, y-15, 5, 30, false);
//中间矩形
g.fill3DRect(x-5, y-10, 10, 20, false);
//中间圆圈
g.fillOval(x-4, y-4, 8, 8);
//炮筒
g.drawLine(x, y, x, y-15);
break;
case 1:
//画方向向右的坦克
//上面矩形
g.fill3DRect(x-15, y-10, 30, 5, false);
//下面矩形
g.fill3DRect(x-15, y+5, 30, 5, false);
//中间矩形
g.fill3DRect(x-10, y-5, 20, 10, false);
//中间圆圈
g.fillOval(x-4, y-4, 8, 8);
//炮筒
g.drawLine(x, y, x+15, y);
break;
case 2:
//画方向向下的坦克
//自己完成
//画左边矩形
g.fill3DRect(x-10, y-15, 5, 30, false);
//画右边矩形
g.fill3DRect(x+5, y-15, 5, 30, false);
//中间矩形
g.fill3DRect(x-5, y-10, 10, 20, false);
//中间圆圈
g.fillOval(x-4, y-4, 8, 8);
//炮筒
g.drawLine(x, y, x, y+15);
break;
case 3:
//画方向向左的坦克
//自己完成
//上面矩形
g.fill3DRect(x-15, y-10, 30, 5, false);
//下面矩形
g.fill3DRect(x-15, y+5, 30, 5, false);
//中间矩形
g.fill3DRect(x-10, y-5, 20, 10, false);
//中间圆圈
g.fillOval(x-4, y-4, 8, 8);
//炮筒
g.drawLine(x, y, x-15, y);
break;
}
}
@Override
public void keyPressed(KeyEvent arg0) {
//w按下让坦克往上移动,d键位让坦克向右移动,s键位让坦克向下移动,a键位让坦克往左移动
if(arg0.getKeyChar()=='w') {
//w键位被按下了,让坦克调用moveUp方法,让坦克往上移动
//调整坦克的方向
hero.setDirect(0);
hero.moveUp();
}else if(arg0.getKeyChar()=='d') {
//d键位被按下了,让坦克调用moveRight方法,让坦克往右移动
hero.setDirect(1);
hero.moveRight();
}else if(arg0.getKeyChar()=='s') {
//s键位被按下了,让坦克调用moveDown方法,让坦克往下移动
hero.setDirect(2);
hero.moveDown();
}else if(arg0.getKeyChar()=='a') {
//a键位被按下了,让坦克调用moveLeft方法,让坦克往左移动
hero.setDirect(3);
hero.moveLefe();
}
//重绘,刷新
this.repaint();
}
@Override
public void keyReleased(KeyEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void keyTyped(KeyEvent arg0) {
// TODO Auto-generated method stub
}
}
//坦克类,是所有坦克的父类
class Tank{
int x;//坦克的坐标位置
int y;//坦克的坐标位置
int direct;//坦克方向,direct的值是0,表示坦克向上,1表示坦克向右,2表示坦克向下,3表示坦克向左
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 int getDirect() {
return direct;
}
public void setDirect(int direct) {
this.direct = direct;
}
}
//我方坦克类
class Hero extends Tank{
int speed=5;//坦克移动速度
public Hero(int x, int y) {
super(x, y);
}
//有移动能力,发射子弹能力
public void moveUp() {
if(y<20)y=20;
y-=speed;//等价 y = y - speed;
}
public void moveRight() {
if(x>580)x=580;
x+=speed;
}
public void moveDown(){
if(y>380)y=380;
y+=speed;
}
public void moveLefe() {
if(x<20)x=20;
x-=speed;
}
}
万水千山总是情,给个打赏行不行。
打赏
