博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java 坦克大战
阅读量:4353 次
发布时间:2019-06-07

本文共 4376 字,大约阅读时间需要 14 分钟。

package com.sunzhiyan04;

import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

import javax.swing.*;

import java.util.*;

public class Tank_1 extends JFrame{

Mypanel_1 jp1 = null;

private static final long serialVersionUID = 1L;
public static void main(String[] args) {
// TODO Auto-generated method stub
Tank_1 tank = new Tank_1();
}
public Tank_1() {
// TODO Auto-generated constructor stub
jp1 = new Mypanel_1();
this.add(jp1);
this.addKeyListener(jp1);
this.setTitle("Tank_1");
this.setSize(300,300);
this.setLocation(200,200);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setVisible(true);
}

}

class Mypanel_1 extends JPanel implements KeyListener{
//实例化一个我的坦克
Hero hero = null;
Vector<EnemtyTank> ets = new Vector<>();
int ensize = 3;
public Mypanel_1(){
hero = new Hero(10,10);
for(int i=0;i<ensize;i++ ){
EnemtyTank et = new EnemtyTank((i+1)*50,0);
ets.add(et);
}
//this.addKeyListener(this);
}
public void paint(Graphics g){
super.paint(g);
//设置背景为一块黑色的矩形运动
g.fillRect(0, 0, 400, 300);
//调用画坦克的方法
this.drawTank(hero.getX(), hero.getY(), g, hero.direct, hero.type);
//画子弹
if(hero.shot !=null){
g.draw3DRect(hero.getX(), hero.getY(), 10, 10, false);
}
for(int i=0;i<ets.size();i++){
this.drawTank(ets.get(i).getX(), ets.get(i).getY(), g, ets.get(i).direct, ets.get(i).type);
}
}
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.BLUE);
break;
}
switch(direct)
{
case 0:
case 2:
//画出我的坦克(届时封装成成一个画坦克的方法)
//1.左边的矩形
g.fill3DRect(x,y, 5, 30, false);
//2.右边的矩形
g.fill3DRect(x+15,y, 5, 30, false);
//2.中间的矩形
g.fill3DRect(x+5,y+5, 10, 20, false);
//3.画出圆形
g.fillOval(x+5,y+10, 10, 10);
if(direct == 0){
//3.画出线
g.drawLine(x+10,y+15, x+10, y);
}else if(direct == 2){
//3.画出线
g.drawLine(x+10,y+15, x+10, y+30);
}
break;
case 1:
case 3:
//画出我的坦克(届时封装成成一个画坦克的方法)
//1.左边的矩形
g.fill3DRect(x,y,30, 5, false);
//2.右边的矩形
g.fill3DRect(x,y+15, 30, 5, false);
//2.中间的矩形
g.fill3DRect(x+5,y+5, 20, 10, false);
//3.画出圆形
g.fillOval(x+10,y+5, 10, 10);
if(direct == 1){
//3.画出线
g.drawLine(x+15,y+10, x+30, y+10);
}else if(direct == 3){
//3.画出线
g.drawLine(x+15,y+10, x, y+10);
}
break;
}

}

@Override

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

@Override

public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
//设置我坦克的方向
if(e.getKeyCode() == KeyEvent.VK_W){
//上
this.hero.direct = 0;
this.hero.moveUP();
}else if(e.getKeyCode() == KeyEvent.VK_D){
this.hero.direct = 1;
this.hero.moveRIGHT();
//右
}else if(e.getKeyCode() == KeyEvent.VK_S){
//下
this.hero.direct = 2;
this.hero.moveDOWN();
}else if(e.getKeyCode() == KeyEvent.VK_A){
//左
this.hero.direct = 3;
this.hero.moveLEFT();
}
if(e.getKeyCode() == KeyEvent.VK_J){
//判断玩家是否按下J键
this.hero.shotEnemy();
}
this.repaint();
}

@Override

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

}

 

 

 

 

 

 

 

 

//子弹类
class Shot implements Runnable
{
int x ;
int y;
int direct;
public Shot(int x,int y){
this.x = x;
this.y = y;
}
public void run(){
}
}
//坦克类
class Tank{
int x;
int y;
int direct;
int type;
int speed = 2;
public int getDirect() {
return direct;
}
public void setDirect(int direct) {
this.direct = direct;
}
public int getType() {
return type;
}
public void setType(int type) {
this.type = type;
}

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 void moveUP(){
this.y -= this.speed;
}
public void moveRIGHT(){
this.x += this.speed;
}
public void moveDOWN(){
this.y += this.speed;
}
public void moveLEFT(){
this.x -= this.speed;
}

public Tank(int x,int y){

this.x = x;
this.y = y;
}
}

//定义我的坦克

class Hero extends Tank{
//子弹是坦克的一个属性
Shot shot = null;
public Hero(int x, int y) {
super(x, y);
// TODO Auto-generated constructor stub
}
//子弹发射方法
public void shotEnemy(){
switch(this.direct){
case 0:
shot = new Shot(this.x+10,this.y);
break;
case 1:
shot = new Shot(this.x+30,this.y+10);
break;
case 2:
shot = new Shot(this.x+10,this.y+30);
break;
case 3:
shot = new Shot(this.x,this.y+10);
break;
}
}
}
//定义我的坦克
class EnemtyTank extends Tank{

public EnemtyTank(int x, int y) {

super(x, y);
// TODO Auto-generated constructor stub
}
}

 

 

转载于:https://www.cnblogs.com/sunxun/p/3842012.html

你可能感兴趣的文章
白盒测试总结
查看>>
Docker入门系列(四):让你的服务跨越多台机器
查看>>
《搭建更新DNS集群服务》RHEL6
查看>>
web基础----->模板引擎Velocity的使用(一)
查看>>
html和css入门 (四)
查看>>
Homestead 常用命令 + laravel 开发环境
查看>>
[GraphQL] Use GraphQLList with GraphQLObject Types
查看>>
译图智讯VIN码识别助力汽配商转型升级
查看>>
【BT+Log】BT抓取log方法以及log级别修改(HCI+Logcat)
查看>>
Cortex-M3开发经验(一):函数指针的应用
查看>>
xdebug配置注意事项
查看>>
SSL证书申请,如何快速通过SSL文件验证。
查看>>
Building Tool(Maven/Gradle)
查看>>
大二下周总结(4)
查看>>
js表格拖拽
查看>>
css中的声明冲突
查看>>
(Python基础)字典的使用
查看>>
[转]使用Stopwatch类实现高精度计时
查看>>
BLE的广播类型
查看>>
C语言访问MCU寄存器的两种方式
查看>>