首页 > 作文

Java编写实现坦克大战小游戏

更新时间:2023-04-04 10:39:21 阅读: 评论:0

本文实例为大家分享了java实现坦克大战小游戏的具体代码,供大家参考,具体内容如下

创作背景:n年前的学期末课题设计,从b站上学的,一个代码一个代码敲出来的。

小游戏介绍:

红色坦克是我们的操纵坦克,黑色是敌人坦克。
上下左右键控制坦克移动方向
按ctrl键发射炮弹
红色坦克可以穿墙,黑色不可以

具体页面如下:

奉上全部源代码:

tank.java

import java.awt.*;import java.awt.event.*;import java.util.*;public class tank { private int x; private int y; private int oldx; private int oldy; private int life = 100;private boolean bl = fal ,bu = fal ,br = fal ,bd= fal;//产生随机数private static random r = new random();//九种坦克运动方向enum direction{l,lu,u,ru,r,rd,d,ld,stop};//初始化坦克方向private direction dir = direction.stop;//初始化炮筒方向private direction ptdir = direction.u;//坦克移动速度private static final int xspeed = 5;private static final int yspeed = 5;//坦克大小private static final int width = 30;private static final int hight =30;//定义tankclient类tankclient tc;public int getlife(){ return life; }public void tlife(int life){ this.life =life;}private boolean good =true ;//定义坦克类型,敌方还是我方public boolean isgood(){ return good;}//定义坦克状态private boolean live = true;//设置enemy坦克随机移动步数private static int step = r.nextint(12)+3;//构造坦克状态方法public boolean islive (){ return live;}public void tlive(boolean live){ this.live = live;}//构造方法public tank(int x, int y) {  this.x = x;  this.y = y; }public tank (int x,int y,boolean good ,direction dir,tankclient tc){ this (x,y); this.good = good; this.dir = dir ; this.tc = tc;}public void draw (graphics g) {  if (!live)  {   if (!good)   {    tc.tanks.remove(this);   }   return;  }  color c = g.getcolor();//?  if(good==true)   {g.tcolor(color.red);}//定义我方坦克颜色  el g.tcolor(color.black);//定义敌方坦克颜色  g.filloval(x,y, width, hight);//定义坦克位置及大小  g.tcolor(c);//?  move();  switch (ptdir)//画炮筒  {  ca l:   g.drawline(x + tank.width / 2, y + tank.hight / 2, x, y     + tank.hight / 2);   break;  ca lu:   g.drawline(x + tank.width / 2, y + tank.hight / 2, x, y);   break;  ca u:   g.drawline(x + tank.width / 2, y + tank.hight / 2, x + tank.width     / 2, y);   break;  ca ru:   g.drawline(x + tank.width / 2, y + tank.hight / 2, x + tank.width,     y);   break;  ca r:   g.drawline(x + tank.width / 2, y + tank.hight / 2, x + tank.width,     y + tank.hight / 2);   break;  ca rd:   g.drawline(x + tank.width / 2, y + tank.hight / 2, x + tank.width,     y + tank.hight);   break;  ca d:   g.drawline(x + tank.width / 2, y + tank.hight / 2, x + tank.width     / 2, y + tank.hight);   break;  ca ld:   g.drawline(x + tank.width / 2, y + tank.hight / 2, x, y     + tank.hight);   break;  } }private void stay(){ this.x=oldx; this.y=oldy; }//坦克移动void move (){ this.oldx=x; this.oldy=y; switch(dir) { ca l: x-=xspeed; break; ca lu: x-=xspeed; y-=yspeed; break; ca u: y-=yspeed; break; ca ru: x+=xspeed; y-=yspeed; break; ca r: x+=xspeed; break; ca rd: x+=xspeed; y+=yspeed; break; ca d: y+=yspeed; break; ca ld: x-=xspeed; y+=yspeed; break; ca stop: break; } if (this.dir!=direction.stop) {  ptdir = dir; } if (x<0) x=0; if (y<20) y=20; if (x>tankclient.winwidth-tank.width) x=tankclient.winwidth-tank.width; if (y>tankclient.winhigh-tank.hight) y=tankclient.winhigh-tank.hight; //让enemy坦克自由移动 if (!good) {  direction[] dirs= direction.values();//将枚举转化为数组  if (step ==0)  {   step = r.nextint(12)+3;   int rn = r.nextint(dirs.length);//产生随机数   dir = dirs[rn];  }  step--; if (r.nextint(40)>38)   {this.fire();} }}//坦克方向void localdirection(){ if (bl&&!bu&&!br&&!bd) dir= direction.l; el if (bl&&bu&&!br&&!bd) dir= direction.lu; el if (!bl&&bu&&!br&&!bd) dir= direction.u; el if (!bl&&bu&&br&&!bd) dir= direction.ru; el if (!bl&&!bu&&br&&!bd) dir= direction.r; el if (!bl&&!bu&&br&&bd) dir= direction.rd; el if (!bl&&!bu&&!br&&bd) dir= direction.d; el if (bl&&!bu&&!br&&bd) dir= direction.ld; el dir =direction.stop;}public void keypresd(keyevent e) {  int key = e.getkeycode();   switch (key)  {   ca keyevent.vk_control:    fire();    break;   ca keyevent.vk_left:    bl=true;    break;   ca keyevent.vk_up:    bu=true;    break;   ca keyevent.vk_right:    br=true;    break;   ca keyevent.vk_down:    bd=true;    break;  }  localdirection();//获取执行方向 }public void keyrel夸女生ead(keyevent e) { int key = e.getkeycode(); switch (key){ ca keyevent.vk_left:  bl=fal;  break; ca keyevent.vk_up:  bu=fal;  b登高杜甫赏析reak; ca keyevent.vk_right:  br=fal;  break; ca keyevent.vk_down:  bd=fal;  break;} localdirection(); }//定义坦克发射子弹类 public missile fire() {  if (!live) return null;  int x= this.x+tank.width/2-missile.width/2;  int y= this.y +tank.hight/2-missile.hight/2;  missile m = new missile(x,y,ptdir,good,tc);  tc.missiles.add(m);  return m; }//《碰撞检测》获取坦克矩形属性 public rectangle getrect() {  return new rectangle(x,y,width,hight); } //《碰撞检测》 public boolean tankhitwall(wall w) {  if(this.getrect().intercts(w.getrect()))  {   stay();   return true;  }  return fal; } public boolean tankhittank(java.util.list<tank> tanks) {  for (int i=0;i<tanks.size();i++)  {   tank t = tanks.get(i);     if (this!=t)   {    if (this.live&&t.islive()&&this.getrect().intercts(t.getrect()))    {     this.stay();     t.stay();     return true ;    }   }  }  return fal; }}

wall.java

import java.awt.*;public class wall {  int x;  int y;  int width;  int height;  tankclient tc; public wall (int x,int y,int width,int height, tankclient tc) {  this.x= x;  this.y= y;  this.width = width;  this.height = height;  this.tc = tc; } public void draw(graphics g) {  color c = g.getcolor();  g.tcolor(color.gray);  g.fillrect(x, y, width, height);  g.tcolor(c);   } //《碰撞检测》获取矩形属性  public rectangle getrect()  {   return new rectangle(x,y,width,height);  } }

tankclient.java

import java.awt.*;import java.awt.event.*;import java.util.list;import java.util.arraylist;public class tankclient extends frame { tank mytank = new tank(400,430,true,tank.direction.stop,this);//创建自己的坦克 list <tank> tanks= new arraylist<tank>(); //创建敌人坦克容器 list<explode> explodes = new arraylist<explode>();//创建爆炸容器 list<missile> missiles = new arraylist<missile>();//定义容器,存放炮弹 wall wall1 = new wall(100,100,30,400,this); wall wall2 = new wall(350,400,400,30,this); /*  * 定义窗口大小变量  */ public static final int winwidth=800; public static final int winhigh=600; //定义框架大小 public void launchframe() {  //添加10辆敌人坦克  for (int i = 0;i<10;i++)  {   tanks.add(new tank(50+40*i,100,fal,tank.direction.d,this));  }  this.tlocation(40,40);//定义窗口位置  this.tsize(winwidth,winhigh);//设置窗口大小  this.ttitle("坦克大战");//设置窗口标题  //设置监听器,使窗口关闭  this.addwindowlistener(new windowadapter()    {     public void windowclosing(windowevent e)     {      system.exit(0);     }    });  this.tbackground(color.white);//设置窗口背景颜色  this.tvisible(true);//设置窗口可见  this.tresizable(fal);//设置为不可调整窗口大小  this.addkeylistener(new keymonitor());  new thread(new paintthread()).start(); } //定义一个新图片为空,双缓冲 image offscreenimage = null; /*  * 定义画板  */ public void paint(graphics g) { //g.drawstring("当前炮弹数"+missiles.size(), 40, 80);  //g.drawstring("当前爆炸数"+explodes.size(), 40, 100);  g.drawstring("tank数量"+tanks.size(),40,40);  g.drawstring("mytank血量"+mytank.getlife(),40,60);  for (int i=0;i<missiles.size();i++)  {   missile m = missiles.get(i);     m.hittanks(tanks);   m.hittank(mytank);   m.hitwall(wall1);   m.hitwall(wall2);   m.draw(g);  }  for (int i=0;i<explodes.size();i++)  {   explode e = explodes.get(i);   e.draw(g);  }  for (int i = 0;北京欢迎您歌词i<tanks.size();i++)  {   tank t = tanks.get(i);   t.tankhitwall(wall1);   t.tankhitwall(wall2);   t.tankhittank清华百年(tanks);   t.draw(g);  }  mytank.draw(g);  wall1.draw(g);  wall2.draw(g); } //重写update 刷新屏幕先调用update方法再调用画笔工具,故刷新屏幕让其直接调用update方法 public void update (graphics g) {  if (offscreenimage == null )  {   offscreenimage = this.createimage(winwidth,winhigh);  }  graphics goffscreen = offscreenimage.getgraphics();  color c = goffscreen.getcolor();  goffscreen.tcolor(color.white);  goffscreen.fillrect(0,0,winwidth,winhigh);  goffscreen.tcolor(c);  paint(goffscreen);  g.drawimage(offscreenimage,0,0,null); } public static void main(string[] args) {  tankclient tankclient = new tankclient();  tankclient.launchframe(); } //线程类--刷新屏幕 private class paintthread implements runnable {  public void run()  {   while(true)   {    repaint(); //刷新屏幕    try    {     thread.sleep(40);    }catch (interruptedexception e)    {     e.printstacktrace();    }   }  } } /**  *  * 内部类 添加键盘监听  *  */  private class keymonitor extends keyadapter {   //按键时   public void keypresd(keyevent e) {    mytank.keypresd(e);   }   //松键时   public void keyrelead(keyevent e)   {    mytank.keyrelead(e);   }     }}

explode.java

import java.awt.*;public class explode { int x; int y; private boolean live = true; private tankclient tc ; int[] diameter = {30,40,50,40,30,10,5}; int step = 0; //结构 public explode (int x ,int y,tankclient tc) {  this.x =x;  this.y = y;  this.tc = tc; } public void draw (graphics g) {  if(!live){  tc.explodes.remove(this);   return ;  }  if (step ==diameter.length) {   live = fal ;   step = 0;   return ;  }  color c = g.getcolor();  g.tcolor(color.red);  g.filloval(x, y, diameter[step], diameter[step]);  g.tcolor(c);  step++; }}

missile.java

import java.awt.*;import java.util.list;//定义子弹类public class missile { //定义子弹速度 public static final int xspeed=10; public static final int yspeed=10; //定义子弹大小 public static final int width =10; public static final int hight =10; private boolean live= true ;//定义炮弹状态 //定义发出的炮弹归属 private boolean good; public boolean islive() {  return live; } int x,y ;//子弹坐标 tankclient tc; tank.direction dir; public missile (int x,int y ,tank.direction dir) {  this.x=x;  this.y=y;  this.dir=dir; } public missile(int x,int y,tank.direction dir,boolean good,tankclient tc) {  this(x,y,dir);  this.good = good;  this.tc=tc; } public void draw (graphics g) { if (!live) {  tc.missiles.remove(this); }  color c= g.getcolor();  if (good)  {   g.tcolor(color.red);  }el {   g.tcolor(color.black);  }  g.filloval(x, y, width,hight);//设置炮弹大小  g.tcolor(c);  move(); } void move () {  switch(dir)  {  ca l:  x-=xspeed;  break;  ca lu:  x-=xspeed;  y-=yspeed;  break;  ca u:  y-=yspeed;  break;  ca ru:  x+=xspeed;  y-=yspeed;  break;  ca r:  x+=xspeed;  break;  ca rd:  x+=xspeed;  y+=yspeed;  break;  ca d:  y+=yspeed;  break;  ca ld:  x-=xspeed;  y+=yspeed;  break;  }  if (x<0||y<0||x>tankclient.winwidth||y>tankclient.winhigh)  {   live =fal;  } }//《碰撞检测》获取子弹矩形属性 public rectangle getrect() {  return new rectangle(x,y,width,hight); }//《碰撞检测》 public boolean hittank(tank t) {  if(this.live&&this.getrect().intercts(t.getrect())&&t.islive()&& this.good!=t.isgood())  { if (t.isgood())   {    t.tlife(t.getlife()-20);    if (t.getlife()<=0)    {t.tlive(fal);}   }el{t.tlive(fal);}   this.live = fal;   explode e = new explode(x-10,y-10,tc);   tc.explodes.add(e);   return true;  }  return fal; } public 应征公民体格检查标准boolean hittanks(list<tank> tanks) {  for (int i=0;i<tanks.size();i++)  {   if (hittank(tanks.get(i)))   {    return true;   }  }  return fal ; } //撞墙碰撞检测  public boolean hitwall (wall w)  {   if (this.live&&this.getrect().intercts(w.getrect()))   {    this.live =fal;    return true;   }   return fal ;  }}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持www.887551.com。

本文发布于:2023-04-04 10:39:19,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/zuowen/ca797972f3dd0d4ac1f5f46d697cd4c0.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

本文word下载地址:Java编写实现坦克大战小游戏.doc

本文 PDF 下载地址:Java编写实现坦克大战小游戏.pdf

标签:坦克   定义   炮弹   大小
相关文章
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图