1. package cn.edu.ahu.RapidSurvial;
  2. import java.awt.Graphics;
  3. import java.awt.Image;
  4. import java.awt.Rectangle;
  5. import java.awt.Toolkit;
  6. import java.util.List;
  7. /**
  8. * 炸弹类
  9. * @author Your风之恋(AHU - java - 课程设计)
  10. *
  11. */
  12. public class Bomb {
  13. public static final int BWIDTH = 20;    //炸弹宽度
  14. public static final int BHEIGHT = 5;    //炸弹高度
  15. public static final int BXSPEED = 10;   //炸弹x方向上的速度
  16. public static final int BYSPEED = 10;   //炸弹y方向上的速度
  17. int x;                                  //炸弹的左上角 x点的位置
  18. int y;                                  //炸弹的左上角 y点的位置
  19. int w;                                  //炸弹的宽度
  20. int h;                                  //炸弹的高度
  21. RapidSurvialManager rsm;                //持有RapidSurvialManager的引用
  22. Fighter.Direction dir;                  //炸弹的方向
  23. boolean isLive = true;                  //是否有效
  24. boolean isEnemy;                        //区分敌我的量
  25. public static int sid = 0;              //记录战果
  26. private static Toolkit tk =
  27. Toolkit.getDefaultToolkit();
  28. private static Image[] bombImage = null;
  29. static {
  30. bombImage = new Image[] {
  31. tk.getImage(Bomb.class.getClassLoader().getResource("images/Bomb_LTR.png")),
  32. tk.getImage(Bomb.class.getClassLoader().getResource("images/Bomb_RTL.png"))
  33. };
  34. }
  35. //构造方法
  36. public Bomb(int x, int y) {
  37. this.x = x;
  38. this.y = y;
  39. this.w = BWIDTH;
  40. this.h = BHEIGHT;
  41. }
  42. //构造方法
  43. public Bomb(int x, int y, RapidSurvialManager rsm) {
  44. this(x, y);
  45. this.rsm = rsm;
  46. }
  47. //构造方法
  48. public Bomb(int x, int y, RapidSurvialManager rsm, Fighter.Direction dir, boolean isEnemy) {
  49. this(x, y, rsm);
  50. this.dir = dir;
  51. this.isEnemy = isEnemy;
  52. }
  53. //画出自己的方法
  54. public void draw(Graphics g) {
  55. if(!isLive) {
  56. rsm.bombs.remove(this);
  57. return;
  58. }
  59. if(!isEnemy) {
  60. g.drawImage(bombImage[0], x, y, null);
  61. } else {
  62. g.drawImage(bombImage[1], x, y, null);
  63. }
  64. setPostion();
  65. }
  66. //根据方向计算下一重画的位置
  67. private void setPostion() {
  68. switch(dir) {
  69. case LTR:
  70. x += BXSPEED;
  71. break;
  72. case RTL:
  73. x -= BXSPEED;
  74. break;
  75. }
  76. //出界处理
  77. if(x < 0 || y < 0 ||
  78. x > RapidSurvialManager.MAINWIDTH ||
  79. y > RapidSurvialManager.MAINHEIGHT) {
  80. isLive = false;
  81. }
  82. }
  83. //返回自己的大小
  84. public Rectangle getRect() {
  85. return new Rectangle(x, y, w, h);
  86. }
  87. //此方法用于与敌机一个子弹的碰撞检测
  88. public boolean hitBomb(Bomb b) {
  89. if(this.isLive
  90. && this.getRect().intersects(b.getRect())
  91. && b.isLive
  92. && b.isEnemy != this.isEnemy) {
  93. Explode e = new Explode(x + BWIDTH, y + BHEIGHT, rsm);
  94. rsm.explodes.add(e);
  95. this.isLive = false;
  96. b.isLive = false;
  97. return true;
  98. }
  99. return false;
  100. }
  101. //此方法用于与敌机一群子弹的碰撞检测
  102. public boolean hitBombs(List<Bomb> bombs) {
  103. for(int i = 0; i < bombs.size(); i++) {
  104. if(hitBomb(bombs.get(i))){
  105. return true;
  106. }
  107. }
  108. return false;
  109. }
  110. public boolean hitFighter(Fighter f) {
  111. if(this.isLive
  112. && this.getRect().intersects(f.getRect())
  113. && f.isLive
  114. && f.isEnemy != this.isEnemy) {
  115. Explode e = new Explode(x + BWIDTH, y + BHEIGHT, rsm);
  116. rsm.explodes.add(e);
  117. if(!f.isEnemy) {
  118. f.setLifeValue(f.getLifeValue() - 1);
  119. if(f.getLifeValue() <= 0) {
  120. f.isLive = false;
  121. }
  122. } else {
  123. f.isLive = false;
  124. sid ++;
  125. }
  126. this.isLive = false;
  127. return true;
  128. }
  129. return false;
  130. }
  131. //此方法用于与一群战机的碰撞检测
  132. public boolean hitFighters(List<Fighter> enemys) {
  133. for(int i = 0; i < enemys.size(); i++) {
  134. if(hitFighter(enemys.get(i))){
  135. return true;
  136. }
  137. }
  138. return false;
  139. }
  140. }
  1. package cn.edu.ahu.RapidSurvial;
  2. import java.awt.Color;
  3. import java.awt.Graphics;
  4. /**
  5. * 爆炸类
  6. * @author Your风之恋(AHU - java - 课程设计)
  7. *
  8. */
  9. public class Explode {
  10. int x;                                          //产生爆炸的x点坐标
  11. int y;                                          //产生爆炸的y点坐标
  12. RapidSurvialManager rsm;                        //持有RapidSurvialManager的引用
  13. boolean isLive = true;                          //是否有效,是:true,否:false
  14. int [] diameter = {4, 7, 12, 23, 34, 12, 6};    //模拟爆炸产生的半径
  15. int step = 0;                                   //爆炸的步数
  16. //构造函数
  17. public Explode(int x, int y, RapidSurvialManager rsm) {
  18. this.x = x;
  19. this.y = y;
  20. this.rsm = rsm;
  21. }
  22. //画出自己的方法
  23. public void draw(Graphics g) {
  24. if(!isLive) {
  25. rsm.explodes.remove(this);
  26. return;
  27. }
  28. Color c = g.getColor();
  29. g.setColor(Color.ORANGE);
  30. if(step == diameter.length) {
  31. isLive = false;
  32. step = 0;
  33. return;
  34. }
  35. g.fillOval(x, y, diameter[step], diameter[step]);
  36. g.setColor(c);
  37. step ++;
  38. }
  39. }
  1. package cn.edu.ahu.RapidSurvial;
  2. import java.awt.Color;
  3. import java.awt.Graphics;
  4. import java.awt.Image;
  5. import java.awt.Rectangle;
  6. import java.awt.Toolkit;
  7. import java.awt.event.KeyEvent;
  8. public class Fighter {
  9. public static final int FWIDTH = 70;    //战机宽度
  10. public static final int FHEIGHT = 10;   //战机高度
  11. public static final int FXSPEED = 4;    //战机在x方向上的速度
  12. public static final int FYSPEED = 4;    //战机在y方向上的速度
  13. int x;                                  //战机在x方向上的位置
  14. int y;                                  //战机在y方向上的位置
  15. int w;                                  //战机的宽度
  16. int h;                                  //战机的高度
  17. Direction dir;                          //方向
  18. RapidSurvialManager rsm;                //持有RapidSurvialManager的引用
  19. private boolean isUp = false;           //键盘↑ 是否被按下,初始化为false
  20. private boolean isDown = false;         //键盘↓ 是否被按下,初始化为false
  21. private boolean isRight = false;        //键盘→ 是否被按下,初始化为false
  22. private boolean isLeft = false;         //键盘← 是否被按下,初始化为false
  23. enum Direction {LTR, RTL};              //两个方向,LTR:从左向右,RTL:从右向左
  24. boolean isEnemy;                        //区分敌我的量,是敌人:true,否则为false
  25. boolean isLive = true;                  //判读是否存活,活着:true,否则为false
  26. private int lifeValue = 10;             //我方战机的生命值
  27. int speed;                              //产生一个速度值
  28. BloodBar bb = new BloodBar();           //可视的血量
  29. static int isRelive = 0;                //是否复活
  30. private int superStarCounts = 1;        //初始大决数
  31. private static Toolkit tk =
  32. Toolkit.getDefaultToolkit();
  33. private static Image[] fighterImage = null;
  34. static {
  35. fighterImage = new Image[] {
  36. tk.getImage(Fighter.class.getClassLoader().getResource("images/EnemysFighter.png")),
  37. tk.getImage(Fighter.class.getClassLoader().getResource("images/MyFighter_LTR.png"))
  38. };
  39. }
  40. //构造函数
  41. Fighter(int x , int y,boolean isEnemy) {
  42. this.x = x;
  43. this.y = y;
  44. this.w = FWIDTH;
  45. this.h = FHEIGHT;
  46. this.isEnemy = isEnemy;
  47. }
  48. //构造函数
  49. Fighter(int x, int y, boolean isEnemy, RapidSurvialManager rsm) {
  50. this(x, y, isEnemy);
  51. this.rsm = rsm;
  52. this.dir = Direction.LTR;
  53. }
  54. //构造函数
  55. Fighter(int x, int y,boolean isEnemy, RapidSurvialManager rsm, Direction dir, int speed) {
  56. this(x, y, isEnemy, rsm);
  57. this.dir = dir;
  58. this.speed = speed;
  59. }
  60. //设置lifeValue值
  61. public void setLifeValue(int lifeValue) {
  62. this.lifeValue = lifeValue;
  63. }
  64. //得到lifeValue值
  65. public int getLifeValue() {
  66. return lifeValue;
  67. }
  68. //设置superStarCounts值
  69. public void setSuperStarCounts(int superStarCounts) {
  70. this.superStarCounts = superStarCounts;
  71. }
  72. //得到superStarCounts值
  73. public int getSuperStarCounts() {
  74. return superStarCounts;
  75. }
  76. //用此方画出战机
  77. public void draw(Graphics g) {
  78. if(!isLive) {
  79. if(isEnemy) {
  80. rsm.enemys.remove(this);
  81. }
  82. return;
  83. }
  84. if(isEnemy) {
  85. g.drawImage(fighterImage[0], x, y, null);
  86. go();
  87. } else {
  88. g.drawImage(fighterImage[1], x, y, null);
  89. setPostion();
  90. bb.draw(g);
  91. }
  92. }
  93. //让敌军动起来的方法
  94. public void go() {
  95. switch(dir) {
  96. case LTR:
  97. x += speed;
  98. break;
  99. case RTL:
  100. x -= speed;
  101. break;
  102. }
  103. }
  104. //对按键被按下经行处理
  105. public void keyPressed(KeyEvent e) {
  106. int key = e.getKeyCode();
  107. switch(key) {
  108. case KeyEvent.VK_UP:
  109. isUp = true;
  110. break;
  111. case KeyEvent.VK_RIGHT:
  112. isRight = true;
  113. break;
  114. case KeyEvent.VK_DOWN:
  115. isDown = true;
  116. break;
  117. case KeyEvent.VK_LEFT:
  118. isLeft = true;
  119. break;
  120. }
  121. setPostion();
  122. }
  123. //对按键被释放经行处理
  124. public void keyReleased(KeyEvent e) {
  125. int key = e.getKeyCode();
  126. switch(key) {
  127. case KeyEvent.VK_UP:
  128. isUp = false;
  129. break;
  130. case KeyEvent.VK_RIGHT:
  131. isRight = false;
  132. break;
  133. case KeyEvent.VK_DOWN:
  134. isDown = false;
  135. break;
  136. case KeyEvent.VK_LEFT:
  137. isLeft = false;
  138. break;
  139. case KeyEvent.VK_Q:
  140. fire();
  141. break;
  142. case KeyEvent.VK_F2:
  143. if(!isLive) {
  144. isLive = true;
  145. lifeValue = 10;
  146. isRelive ++;
  147. }
  148. break;
  149. case KeyEvent.VK_W:
  150. if(getSuperStarCounts() == 0) {
  151. return;
  152. }
  153. if(!isLive) {
  154. return;
  155. }
  156. setSuperStarCounts(getSuperStarCounts() - 1);
  157. superFire();
  158. break;
  159. }
  160. }
  161. //根据按键的组合确定下一次的位置
  162. private void setPostion() {
  163. if(isUp && !isRight && !isDown && !isLeft) {
  164. y -= FYSPEED;
  165. }
  166. if(!isUp && isRight && !isDown && !isLeft) {
  167. x += FXSPEED;
  168. }
  169. if(!isUp && !isRight && isDown && !isLeft) {
  170. y += FYSPEED;
  171. }
  172. if(!isUp && !isRight && !isDown && isLeft) {
  173. x -= FXSPEED;
  174. }
  175. if(isUp && isRight && !isDown && !isLeft) {
  176. x += FXSPEED;
  177. y -= FYSPEED;
  178. }
  179. if(isUp && !isRight && !isDown && isLeft) {
  180. y -= FYSPEED;
  181. x -= FXSPEED;
  182. }
  183. if(!isUp && isRight && isDown && !isLeft) {
  184. x += FXSPEED;
  185. y += FYSPEED;
  186. }
  187. if(!isUp && !isRight && isDown && isLeft) {
  188. x -= FXSPEED;
  189. y += FYSPEED;
  190. }
  191. //对战机的出界处理
  192. if(x <= 0) {
  193. x = 0;
  194. }
  195. if(y < 45) {
  196. y = 45;
  197. }
  198. if(x + Fighter.FWIDTH > RapidSurvialManager.MAINWIDTH) {
  199. x = RapidSurvialManager.MAINWIDTH - Fighter.FWIDTH;
  200. }
  201. if(y + Fighter.FHEIGHT + 52> RapidSurvialManager.MAINHEIGHT) {
  202. y = RapidSurvialManager.MAINHEIGHT - Fighter.FHEIGHT - 52;
  203. }
  204. }
  205. //战机的开火处理
  206. public Bomb fire() {
  207. if(!isLive) {
  208. return null;
  209. }
  210. int x, y;
  211. if(!isEnemy) {
  212. x = this.x + Fighter.FWIDTH;
  213. y = this.y + Fighter.FHEIGHT / 2 - Bomb.BHEIGHT / 2 + 22;
  214. } else {
  215. x = this.x;
  216. y = this.y + Fighter.FHEIGHT / 2 - Bomb.BHEIGHT / 2 + 22;
  217. }
  218. Bomb b = new Bomb(x, y, rsm, dir, isEnemy);
  219. rsm.bombs.add(b);
  220. return b;
  221. }
  222. //放大决的方法
  223. public SuperLine superFire() {
  224. if(!isLive) {
  225. return null;
  226. }
  227. SuperLine s = new SuperLine(x, rsm, dir);
  228. rsm.superLines.add(s);
  229. return s;
  230. }
  231. //得到自己的大小,用于碰撞检测
  232. public Rectangle getRect() {
  233. return new Rectangle(x, y, w, h*4);
  234. }
  235. //血块类
  236. private class BloodBar {
  237. public void draw(Graphics g) {
  238. Color c = g.getColor();
  239. if(lifeValue <= 5) {
  240. g.setColor(Color.RED);
  241. g.drawRect(x+20, y-20, w * 2 / 3, 4);
  242. int w = FWIDTH * lifeValue / 10;
  243. g.fillRect(x+20, y-20, w * 2 / 3, 4);
  244. g.drawString(""+ lifeValue, x, y-13);
  245. } else {
  246. g.setColor(Color.GREEN);
  247. g.drawRect(x+20, y-20, w * 2 / 3, 4);
  248. int w = FWIDTH * lifeValue / 10;
  249. g.fillRect(x+20, y-20, w * 2 / 3, 4);
  250. g.drawString(""+ lifeValue, x, y-13);
  251. g.setColor(c);
  252. }
  253. }
  254. }
  255. //吃SuperStar的方法
  256. public boolean eat(SuperStar ss) {
  257. if(this.isLive
  258. && ss.isLive
  259. && this.getRect().intersects(ss.getRect())) {
  260. ss.isLive = false;
  261. setSuperStarCounts(getSuperStarCounts() + 1);
  262. return true;
  263. }
  264. return false;
  265. }
  266. public int getScore() {
  267. return Bomb.sid - isRelive * 50;
  268. }
  269. }
  1. package cn.edu.ahu.RapidSurvial;
  2. import java.awt.Color;
  3. import java.awt.Graphics;
  4. import java.awt.Rectangle;
  5. import java.util.List;
  6. /**
  7. * 游戏中的大决类
  8. * @author Your风之恋(AHU - java - 课程设计)
  9. *
  10. */
  11. public class SuperLine {
  12. public static final int SXSPEED = 10;       //大决的移动速度
  13. public static final int LWIDTH = 10;        //大决的显示宽度
  14. int x;                                      //大决开始x坐标
  15. int y;                                      //大决开始y坐标
  16. Fighter.Direction dir;                      //方向
  17. RapidSurvialManager rsm;                    //持有RapidSurvialManager的引用
  18. boolean isLive = true;                      //是否有效,是:true,否:false
  19. //构造函数
  20. SuperLine(int x, RapidSurvialManager rsm) {
  21. this.x = x;
  22. this.y = LWIDTH;
  23. this.rsm = rsm;
  24. }
  25. //构造函数
  26. SuperLine(int x, RapidSurvialManager rsm, Fighter.Direction dir) {
  27. this(x, rsm);
  28. this.dir = dir;
  29. }
  30. //画方法
  31. public void draw(Graphics g) {
  32. if(!isLive) {
  33. rsm.superLines.remove(this);
  34. return;
  35. }
  36. Color c = g.getColor();
  37. g.setColor(Color.PINK);
  38. g.fillRect(x, 0, LWIDTH, RapidSurvialManager.MAINHEIGHT);
  39. g.setColor(c);
  40. go();
  41. }
  42. //移动自己的方法
  43. public void go() {
  44. switch(dir) {
  45. case LTR:
  46. x += SXSPEED;
  47. break;
  48. case RTL:
  49. x -= SXSPEED;
  50. break;
  51. }
  52. if(x >= RapidSurvialManager.MAINWIDTH) {
  53. isLive = false;
  54. }
  55. }
  56. //拿到自己的大小,为碰撞检测服务
  57. public Rectangle getRect() {
  58. return new Rectangle(x, 0, y,RapidSurvialManager.MAINHEIGHT);
  59. }
  60. //与一个战机经行碰撞检测
  61. public boolean hitFighter(Fighter f) {
  62. if(this.isLive
  63. && this.getRect().intersects(f.getRect())
  64. && f.isLive) {
  65. Explode e = new Explode(f.x, f.y,rsm);
  66. rsm.explodes.add(e);
  67. f.isLive = false;
  68. Bomb.sid ++;
  69. return true;
  70. }
  71. return false;
  72. }
  73. //与一群战机经行碰撞检测
  74. public boolean hitFighters(List<Fighter> enemys) {
  75. for(int i = 0; i < enemys.size(); i++) {
  76. if(this.hitFighter(enemys.get(i))) {
  77. return true;
  78. }
  79. }
  80. return false;
  81. }
  82. }
  1. package cn.edu.ahu.RapidSurvial;
  2. import java.awt.Graphics;
  3. import java.awt.Image;
  4. import java.awt.Rectangle;
  5. import java.awt.Toolkit;
  6. /**
  7. * 超星类
  8. * @author Your风之恋(AHU - java - 课程设计)
  9. *
  10. */
  11. public class SuperStar {
  12. public static final int SSWIDTH = 20;       //超星的宽度
  13. public static final int SSHEIGHT = 20;      //超星的高度
  14. int x;                                      //超星初始化x点坐标
  15. int y;                                      //超星初始化y点坐标
  16. int w;                                      //超星的宽度
  17. int h;                                      //超星的高度
  18. int speed;                                  //超星的移动速度
  19. boolean isLive = true;                      //是否有效,是:true,否:false
  20. RapidSurvialManager rsm;                    //持有RapidSurvialManager的引用
  21. Fighter.Direction dir;                      //方向
  22. private static Toolkit tk =
  23. Toolkit.getDefaultToolkit();
  24. private static Image[] superStarImage = null;
  25. static {
  26. superStarImage = new Image[] {
  27. tk.getImage(SuperStar.class.getClassLoader().getResource("images/SuperStar_RTL.png")),
  28. };
  29. }
  30. //构造方法
  31. public SuperStar(int x, int y, RapidSurvialManager rsm, Fighter.Direction dir,int speed) {
  32. this.x = x;
  33. this.y = y;
  34. this.speed = speed;
  35. this.w = SSWIDTH;
  36. this.h = SSHEIGHT;
  37. this.rsm = rsm;
  38. this.dir = dir;
  39. }
  40. //画方法
  41. public void draw(Graphics g) {
  42. if(!isLive) {
  43. rsm.superStars.remove(this);
  44. return;
  45. }
  46. g.drawImage(superStarImage[0], x, y, null);
  47. go();
  48. }
  49. //移动自己的方法
  50. public void go() {
  51. switch(dir) {
  52. case LTR:
  53. x += speed;
  54. break;
  55. case RTL:
  56. x -= speed;
  57. break;
  58. }
  59. if(x <= 0) {
  60. isLive = false;
  61. }
  62. }
  63. //得到自己的大小
  64. public Rectangle getRect() {
  65. return new Rectangle(x, y, w, h);
  66. }
  67. }
  1. package cn.edu.ahu.RapidSurvial;
  2. import java.awt.Color;
  3. import java.awt.Font;
  4. import java.awt.Frame;
  5. import java.awt.Graphics;
  6. import java.awt.Image;
  7. import java.awt.event.KeyAdapter;
  8. import java.awt.event.KeyEvent;
  9. import java.awt.event.WindowAdapter;
  10. import java.awt.event.WindowEvent;
  11. import java.util.ArrayList;
  12. import java.util.List;
  13. import java.util.Random;
  14. import cn.edu.ahu.RapidSurvial.Fighter.Direction;
  15. /**
  16. * 游戏的主类,用于管理其他图形元素
  17. * @author Your风之恋(AHU - java - 课程设计)
  18. *
  19. */
  20. public class RapidSurvialManager extends Frame{
  21. private static final long serialVersionUID = 1L;
  22. public static final int MAINWIDTH = 1200;       //主窗口的宽度
  23. public static final int MAINHEIGHT = 600;       //主窗口的高度
  24. public static final int MAINXPOINT = 30;        //主窗口初始化x点的位置
  25. public static final int MAINYPOINT = 50;        //主窗口初始化y点的位置
  26. public static Random r= new Random();           //添加随机数产生器,用于控制敌方的AI
  27. Image bufferImage = null;                       //双缓冲的缓冲图像,用双缓冲取消屏幕闪烁
  28. public static int time = 0;
  29. Fighter myFighter = new Fighter(50, 300, false, this);      //创建主战机
  30. List<Bomb> bombs = new ArrayList<Bomb>();                   //炮弹集合
  31. List<Fighter> enemys = new ArrayList<Fighter>();            //敌机集合
  32. List<Explode> explodes = new ArrayList<Explode>();          //爆炸集合
  33. List<SuperLine> superLines = new ArrayList<SuperLine>();    //大决集合
  34. List<SuperStar> superStars = new ArrayList<SuperStar>();    //超星集合
  35. //主线程入口
  36. public  static void main(String[] args) {
  37. RapidSurvialManager rsm = new RapidSurvialManager();
  38. rsm.launchGameFrame();
  39. }
  40. //重写paint()方法
  41. public void paint(Graphics g) {
  42. //显示一些基本信息,便于判断集合中的元素是否被删除
  43. Color c = g.getColor();
  44. g.setColor(Color.ORANGE);
  45. Font font = g.getFont();
  46. g.setFont(new Font("SansSerif", Font.ROMAN_BASELINE, 15));
  47. g.drawString("游戏时间: " + showTime(), 25, 50);
  48. g.drawString("消灭敌机: " + Bomb.sid, 25, 70);
  49. g.drawString("剩余绝招: " + myFighter.getSuperStarCounts(), 150, 50);
  50. g.drawString("游戏得分: " + myFighter.getScore(),150, 70);
  51. g.setFont(font);
  52. g.setColor(Color.RED);
  53. g.drawLine(MAINXPOINT + MAINWIDTH * 4 / 5, MAINYPOINT - 25,
  54. MAINXPOINT + MAINWIDTH * 4 / 5, MAINYPOINT + MAINHEIGHT);
  55. g.setColor(c);
  56. //画出主战机
  57. myFighter.draw(g);
  58. for(int i = 0; i < superStars.size(); i++) {
  59. SuperStar ss = superStars.get(i);
  60. myFighter.eat(ss);
  61. ss.draw(g);
  62. }
  63. //判断如果超星数少于0,则添加
  64. if (superStars.size() <= 0) {
  65. for(int i = 0; i < r.nextInt(2); i++) {
  66. boolean flag1 = true;
  67. boolean flag2 = true;
  68. int x = 0, y = 0;
  69. while(flag1 || flag2) {
  70. int t1 = r.nextInt(3000);
  71. int t2 = r.nextInt(1000);
  72. if(t1 <= 1100 && t1 >= 1000) {
  73. x = t1;
  74. flag1 = false;
  75. }
  76. if(t2 <= 550 && t2 >= 30) {
  77. y = t2;
  78. flag2 = false;
  79. }
  80. }
  81. if(r.nextInt(500) > 495) {
  82. superStars.add(new SuperStar(x, y, this, Direction.RTL, r.nextInt(6) + 4));
  83. }
  84. }
  85. }
  86. //画出大决
  87. for(int i = 0; i < superLines.size(); i++) {
  88. SuperLine s = superLines.get(i);
  89. s.hitFighters(enemys);
  90. //s.hitBombs(bombs);
  91. s.draw(g);
  92. }
  93. //如果敌机数量少于3,则继续添加
  94. if(enemys.size() <= 2) {
  95. for(int i = 0; i < r.nextInt(10); i++) {
  96. boolean flag1 = true;
  97. boolean flag2 = true;
  98. int x = 0, y = 0;
  99. while(flag1 || flag2) {
  100. int t1 = r.nextInt(3000);
  101. int t2 = r.nextInt(1000);
  102. if(t1 <= 1100 && t1 >= 1000) {
  103. x = t1;
  104. flag1 = false;
  105. }
  106. if(t2 <= 550 && t2 >= 30) {
  107. y = t2;
  108. flag2 = false;
  109. }
  110. }
  111. enemys.add(new Fighter(x, y, true, this, Direction.RTL,r.nextInt(6) + 4));
  112. }
  113. }
  114. //画出炸弹
  115. for(int i = 0; i < bombs.size(); i++) {
  116. Bomb b = bombs.get(i);
  117. b.hitFighters(enemys);
  118. b.hitFighter(myFighter);
  119. b.hitBombs(bombs);
  120. b.draw(g);
  121. }
  122. //画出敌机
  123. for(int i = 0; i < enemys.size(); i++) {
  124. Fighter f = enemys.get(i);
  125. if(f.x <= 0) {
  126. f.isLive = false;
  127. }
  128. if(f.isEnemy) {
  129. if((r.nextInt(50)) > 48) {
  130. f.fire();
  131. }
  132. }
  133. f.draw(g);
  134. }
  135. //画出爆炸
  136. for(int i = 0; i < explodes.size(); i++) {
  137. Explode e = explodes.get(i);
  138. e.draw(g);
  139. }
  140. }
  141. //统一的主界面的调用函数
  142. public void launchGameFrame() {
  143. this.setBounds(MAINXPOINT, MAINYPOINT, MAINWIDTH, MAINHEIGHT);
  144. this.setBackground(Color.BLACK);
  145. this.setLayout(null);
  146. //关闭窗口的处理
  147. this.addWindowListener(new WindowAdapter() {
  148. public void windowClosing(WindowEvent e) {
  149. setVisible(false);
  150. System.exit(0);
  151. }
  152. });
  153. //按键处理
  154. this.addKeyListener(new KeyAdapter() {
  155. public void keyPressed(KeyEvent e) {
  156. myFighter.keyPressed(e);
  157. }
  158. public void keyReleased(KeyEvent e) {
  159. myFighter.keyReleased(e);
  160. }
  161. });
  162. //在窗口显示前开始线程重画
  163. new Thread(new PaintThread()).start();
  164. this.setResizable(false);
  165. this.setVisible(true);
  166. }
  167. //重写update()方法,实现双缓冲取消图像闪烁的情况
  168. public void update(Graphics g) {
  169. if(bufferImage == null) {
  170. bufferImage  =
  171. this.createImage(MAINWIDTH, MAINHEIGHT);
  172. }
  173. Graphics gBuffer = bufferImage.getGraphics();
  174. Color c = gBuffer.getColor();
  175. gBuffer.setColor(Color.BLACK);
  176. gBuffer.fillRect(0, 0, MAINWIDTH, MAINHEIGHT);
  177. gBuffer.setColor(c);
  178. paint(gBuffer);
  179. g.drawImage(bufferImage, 0, 0,null);
  180. }
  181. //绘图线程---用于不断的重绘
  182. class PaintThread implements Runnable {
  183. int step = 0;
  184. public void run() {
  185. while(true) {
  186. repaint();
  187. try {
  188. Thread.sleep(20);
  189. } catch (InterruptedException e) {
  190. e.printStackTrace();
  191. }
  192. step ++;
  193. if(step % 50 == 0) {
  194. time ++;
  195. step = 0;
  196. }
  197. }
  198. }
  199. }
  200. public String showTime() {
  201. int hour = time / 3600;
  202. int minite = (time - hour*3600) / 60;
  203. int second = (time - hour*3600) % 60;
  204. return hour + ":" + minite + ":" + second;
  205. }
  206. }

工程源码+ 文档+可执行文件:http://download.csdn.net/detail/haifengzhilian/4494594

java课程设计源码(游戏:急速生存)相关推荐

  1. Java课程设计源码——学生信息管理系统 SQL

    2022.6.9 更新 这个课程作业的代码有小伙伴反馈有问题(数据库无法连接等),但博主最近学业不用Java了,暂时没时间看,如果有同学有解决方案,可以在下面评论帮助一下其他同学.实在抱歉啦. pac ...

  2. java桌面通讯录源码_TONGXUNLU JAVA通讯录源码 JAVA课程设计源码 讯友桌面通讯录 通讯录管理 - 下载 - 搜珍网...

    讯友桌面通讯录/.classpath 讯友桌面通讯录/.project 讯友桌面通讯录/.settings/org.eclipse.jdt.core.prefs 讯友桌面通讯录/bin/com/zzk ...

  3. JAVA课程设计(小游戏贪吃蛇)完整源码附素材(二)

    目录 JAVA课程设计(小游戏贪吃蛇)完整源码附素材(一) JAVA课程设计(小游戏贪吃蛇)完整源码附素材(二) JAVA课程设计(小游戏贪吃蛇)完整源码附素材(三) 前言 1. 任务描述 1.1  ...

  4. java简单计算器课程设计_java仿windows简易计算器课程设计 源码+报告

    [实例简介] java仿windows简易计算器课程设计 源码+报告 课直接运行. [实例截图] [核心代码] Java课设-简易计算器 └── Java课设-简易计算器 ├── Java课程设计.d ...

  5. html课堂考勤系统源码,考勤管理系统课程设计源码

    考勤管理系统课程设计源码 源码描述: 主体分两个大块 员工信息 个人信息查询,员工信息修改,修改密码,添加用户,删除用户 企业管理 考勤登记,基本工资设置,员工考勤,自动生成变动工资表,自动生成福利费 ...

  6. C语言源码做的运动会管理系统课程设计(源码+课程设计报告)

    一.课程设计的目的: C语言程序设计课程设计是计算机科学与技术专业重要的实践性教学环节之一,本次设计结合实际应用的要求,使课程设计既覆盖C语言的知识点,又接近工程实际需要.目的是通过课程设计的综合训练 ...

  7. C语言源码做的职工工资管理系统课程设计(源码+课程设计报告)

    一.课程设计的目的: C语言课程设计是计算机科学与技术专业重要的实践性教学环节之一,本次设计结合实际应用的要求,使课程设计既覆盖C语言程序设计的知识点,又接近工程实际需要.本次设计的目的是通过课程设计 ...

  8. java猜数字游戏总结,java课程设计——猜数字游戏

    java课程设计--猜数字游戏 目目 录录 前言. 1 正文. 1 1 1.设计任务与要求.设计任务与要求 1 1.1 1.1 设计任务与要求设计任务与要求 1 1.2 1.2 选题目的与意义选题目的 ...

  9. C语言源码做的班级档案管理系统课程设计(源码+课程设计报告)

    一.课程设计的目的: C语言课程设计是计算机科学与技术专业重要的实践性教学环节之一,本次设计结合实际应用的要求,使课程设计既覆盖C语言程序设计的知识点,又接近工程实际需要.本次设计的目的是通过课程设计 ...

  10. oracle课程设计代码,Oracle 课程设计源码

    创建主表空间: create tablespace test datafile 'D:\OracleSpace\test' size 20m extent management local; 创建用户 ...

最新文章

  1. vsftpd的主配置文件是什么linux,linux下vsftpd配置文件选项详细说明
  2. 把Windows CA根证书安装到iPhone
  3. Java 自增(++) 和 C语言中自增的区别
  4. boost::hana::is_convertible用法的测试程序
  5. docker 删除_docker 批量删除镜像
  6. P5488 差分与前缀和(多项式/生成函数)
  7. WPF的二维绘图(一)——DrawingContext
  8. 查看mysql数据插入时间_[译] MySQL 最佳实践 —— 高效插入数据
  9. 还记得八皇后的解法吗
  10. Python字符串count()
  11. 已知拱高和弦长,求弧长、半径、角度
  12. UE4 Websocket
  13. 【分享】VMwareESXI详细黑群晖教程 DS36156
  14. 29-基于单片机的防盗报警系统仿真
  15. 奋斗吧,程序员——第八章 衣带渐宽终不悔,为伊消得人憔悴
  16. BAT某公司的一套面试题
  17. 次世代3D建模高低模的搭配方式你知道吗?
  18. 很久之前写个密码生成工具,可定制。
  19. Guava:Striped锁
  20. 淘宝商品详情接口(商品详情页面数据接口)

热门文章

  1. java 向路由器发送报文_9.IP选路 - loda0128的个人空间 - OSCHINA - 中文开源技术交流社区...
  2. python入门神器_Python入门之神器,助你快速上手!
  3. 2B领域最大的媒体沙龙又来了,你以什么姿势参加?
  4. 如何使用MATLAB绘制ggplot风格图片(散点图及折线图)
  5. 解决mac 下蓝牙卡顿问题
  6. 流媒体学习之路——Google的新拥塞算法SQP详解(编写中)
  7. 【验证小bai】乐于助人·比特序列匹配电路RTL验证环境笔试实操
  8. 红米手机如何抓取蓝牙log。
  9. win10创建新的计算机用户名和密码,win10如何新建一个账号用户
  10. NPOI 在word中插入 表格 包括 合并单元格