趣味JAVA——飞机大战源码

╰+攻爆jí腚メ 2023-02-12 11:20 84阅读 0赞

趣味JAVA——飞机大战源码

代码结构:

在这里插入图片描述

运行截图:

登录界面:
在这里插入图片描述
查看排行榜:
在这里插入图片描述
进入界面:
在这里插入图片描述
游戏界面:
在这里插入图片描述

主要代码展示

1.Bee.java

  1. package com.jyb.airplane.bee;
  2. import java.util.Random;
  3. import com.jyb.airplane.flyingObject.FlyingObject;
  4. import com.jyb.airplane.ownplane.OwnPlane;
  5. import com.jyb.airplane.view.ShootGame;
  6. public class Bee extends FlyingObject {
  7. Random r = new Random();
  8. private int xSpeed = r.nextInt(2) + 1; //x坐标走动步数
  9. private int ySpeed = r.nextInt(2) + 1; //y坐标走动步数
  10. private int awardType; //奖励类型
  11. public Bee() {
  12. image = ShootGame.bee;
  13. width = image.getWidth();
  14. height = image.getHeight();
  15. Random r = new Random();
  16. x = r.nextInt(ShootGame.WIDTH - this.width);
  17. y = -this.height;
  18. awardType = r.nextInt(10); //随机生成奖励类型
  19. }
  20. public int getType() {
  21. return awardType;
  22. }
  23. @Override
  24. public void step(OwnPlane ownPlane) {
  25. if (x >= ShootGame.WIDTH - this.width)
  26. xSpeed = -1;
  27. if (x <= 0)
  28. xSpeed = 1;
  29. x += xSpeed;
  30. if (ownPlane.getScore() < 200000) {
  31. ySpeed = r.nextInt(2) + 1;
  32. } else if (ownPlane.getScore() >= 200000
  33. && ownPlane.getScore() < 400000) {
  34. ySpeed = r.nextInt(2) + 2;
  35. } else if (ownPlane.getScore() >= 400000
  36. && ownPlane.getScore() < 600000) {
  37. ySpeed = r.nextInt(2) + 3;
  38. } else if (ownPlane.getScore() >= 600000
  39. && ownPlane.getScore() < 800000) {
  40. ySpeed = r.nextInt(2) + 4;
  41. } else if (ownPlane.getScore() >= 800000
  42. && ownPlane.getScore() < 1000000) {
  43. ySpeed = r.nextInt(2) + 5;
  44. } else {
  45. ySpeed = r.nextInt(2) + 6;
  46. }
  47. y += ySpeed;
  48. }
  49. @Override
  50. public boolean outOfBounds() {
  51. return this.y > ShootGame.HEIGHT;
  52. }
  53. }

3.Bullet.java

  1. package com.jyb.airplane.bullet;
  2. import com.jyb.airplane.flyingObject.FlyingObject;
  3. import com.jyb.airplane.ownplane.OwnPlane;
  4. import com.jyb.airplane.view.ShootGame;
  5. //子弹只是飞行物,因此继承飞行物类即可
  6. public class Bullet extends FlyingObject {
  7. private int speed = 3; //子弹走步步数,只有y坐标在变
  8. public Bullet(int x, int y) { //子弹的步数随着英雄机的变化而变化
  9. image = ShootGame.bullet;
  10. width = image.getWidth();
  11. height = image.getHeight();
  12. this.x = x;
  13. this.y = y;
  14. }
  15. @Override
  16. public void step(OwnPlane ownPlane) {
  17. y -= speed;
  18. }
  19. @Override
  20. public boolean outOfBounds() {
  21. // TODO 自动生成的方法存根
  22. return this.y < -this.height;
  23. }
  24. }

4.UserDao.java

  1. package com.jyb.airplane.dao;
  2. import java.io.BufferedReader;
  3. import java.io.BufferedWriter;
  4. import java.io.FileInputStream;
  5. import java.io.FileOutputStream;
  6. import java.io.InputStream;
  7. import java.io.InputStreamReader;
  8. import java.io.OutputStream;
  9. import java.io.OutputStreamWriter;
  10. import java.text.SimpleDateFormat;
  11. import java.util.ArrayList;
  12. import java.util.List;
  13. import java.util.Random;
  14. import com.jyb.airplane.entity.User;
  15. public class UserDao implements ImplUserDao {
  16. private String path = "./user.properties";
  17. @Override
  18. public List<User> readAll() {
  19. List<User> list = new ArrayList<User>();
  20. InputStream is = null;
  21. InputStreamReader isr = null;
  22. BufferedReader br = null;
  23. try {
  24. is = new FileInputStream(path);
  25. isr = new InputStreamReader(is, "GB2312");
  26. br = new BufferedReader(isr);
  27. String context = "";
  28. try {
  29. while ((context = br.readLine()) != null) {
  30. if (context.length() == 0) {
  31. continue;
  32. }
  33. String[] messages = context.split(";");
  34. User e = new User(
  35. messages[0],
  36. messages[1],
  37. messages[2],
  38. new SimpleDateFormat("yyyy-MM-dd").parse(messages[3]),
  39. new SimpleDateFormat("yyyy-MM-dd").parse(messages[4]),
  40. Integer.parseInt(messages[5]));
  41. list.add(e);
  42. }
  43. } catch (Exception e) {
  44. e.printStackTrace();
  45. }
  46. } catch (Exception e) {
  47. e.printStackTrace();
  48. } finally {
  49. try {
  50. if (br!=null) {
  51. br.close();
  52. }
  53. if (isr!=null) {
  54. isr.close();
  55. }
  56. if (is!=null) {
  57. is.close();
  58. }
  59. } catch (Exception e) {
  60. e.printStackTrace();
  61. }
  62. }
  63. return list;
  64. }
  65. @Override
  66. public boolean writeAll(List<User> list) {
  67. boolean flag = false;
  68. OutputStream os = null;
  69. OutputStreamWriter osw = null;
  70. BufferedWriter bw = null;
  71. try {
  72. os = new FileOutputStream(path);
  73. osw = new OutputStreamWriter(os, "GB2312");
  74. bw = new BufferedWriter(osw);
  75. for (User u : list) {
  76. StringBuilder sb = new StringBuilder();
  77. sb.append(u.getUserId() + ";");
  78. sb.append(u.getNickName() + ";");
  79. sb.append(u.getPassword() + ";");
  80. sb.append(new SimpleDateFormat("yyyy-MM-dd")
  81. .format(u.getRegistTime()) + ";");
  82. sb.append(new SimpleDateFormat("yyyy-MM-dd")
  83. .format(u.getCreateShoreRecordTime()) + ";");
  84. sb.append(u.getShore());
  85. bw.write(sb.toString());
  86. bw.newLine();
  87. }
  88. bw.flush();
  89. flag = true;
  90. } catch (Exception e) {
  91. e.printStackTrace();
  92. } finally {
  93. try {
  94. if(bw != null) {
  95. bw.close();
  96. }
  97. if(osw != null) {
  98. osw.close();
  99. }
  100. if(os != null) {
  101. os.close();
  102. }
  103. } catch (Exception e) {
  104. e.printStackTrace();
  105. }
  106. }
  107. return flag;
  108. }
  109. @Override
  110. public String getNewID() {
  111. Random r = new Random();
  112. StringBuilder sb = new StringBuilder();
  113. char[] numbers = new char[]
  114. { '0', '1', '2', '3', '4', '5',
  115. '6', '7', '8', '9', 'a', 'b',
  116. 'c', 'd', 'e', 'f', 'g', 'h',
  117. 'i', 'j', 'k', 'l', 'm', 'n',
  118. 'o', 'p', 'q', 'r', 's', 't',
  119. 'u', 'v', 'w', 'x', 'y', 'z'};
  120. for (int i = 0; i < numbers.length; i++) {
  121. sb.append(numbers[r.nextInt(numbers.length)]);
  122. }
  123. return sb.toString();
  124. }
  125. }

5.User.java

  1. package com.jyb.airplane.entity;
  2. import java.io.Serializable;
  3. import java.util.Date;
  4. public class User implements Serializable, Comparable<User> {
  5. /** * */
  6. private static final long serialVersionUID = 1L;
  7. private String userId; // 用户编号
  8. private String nickName; // 用户昵称
  9. private String password; // 用户密码
  10. private Date registTime; // 用户注册时间
  11. private Date createShoreRecordTime; // 用户最高分纪录创建时间
  12. private Integer shore; // 用户得分
  13. @Override
  14. public String toString() {
  15. return "\n用户信息: [用户编号=" + userId + ", " + "用户昵称=" + nickName + ", " + "密码=" + password + "\n, " + "注册时间="
  16. + registTime + ", " + "创建时间=" + createShoreRecordTime + ", " + "得分=" + shore + "]";
  17. }
  18. public User() {
  19. super();
  20. // TODO Auto-generated constructor stub
  21. }
  22. public User(String userId, String nickName, String password, Date registTime, Date createShoreRecordTime,
  23. Integer shore) {
  24. super();
  25. this.userId = userId;
  26. this.nickName = nickName;
  27. this.password = password;
  28. this.registTime = registTime;
  29. this.createShoreRecordTime = createShoreRecordTime;
  30. this.shore = shore;
  31. }
  32. public String getUserId() {
  33. return userId;
  34. }
  35. public void setUserId(String userId) {
  36. this.userId = userId;
  37. }
  38. public String getNickName() {
  39. return nickName;
  40. }
  41. public void setNickName(String nickName) {
  42. this.nickName = nickName;
  43. }
  44. public String getPassword() {
  45. return password;
  46. }
  47. public void setPassword(String password) {
  48. this.password = password;
  49. }
  50. public Date getRegistTime() {
  51. return registTime;
  52. }
  53. public void setRegistTime(Date registTime) {
  54. this.registTime = registTime;
  55. }
  56. public Date getCreateShoreRecordTime() {
  57. return createShoreRecordTime;
  58. }
  59. public void setCreateShoreRecordTime(Date createShoreRecordTime) {
  60. this.createShoreRecordTime = createShoreRecordTime;
  61. }
  62. public Integer getShore() {
  63. return shore;
  64. }
  65. public void setShore(Integer shore) {
  66. this.shore = shore;
  67. }
  68. @Override
  69. public int compareTo(User u) {
  70. if (u.shore > this.shore) {
  71. return 1;
  72. } else if (u.shore == this.shore) {
  73. return 0;
  74. } else {
  75. return -1;
  76. }
  77. }
  78. }

6.OwnPlane.java

  1. package com.jyb.airplane.ownplane;
  2. import java.awt.image.BufferedImage;
  3. import com.jyb.airplane.bullet.Bullet;
  4. import com.jyb.airplane.flyingObject.FlyingObject;
  5. import com.jyb.airplane.view.ShootGame;
  6. //英雄机是飞行物
  7. public class OwnPlane extends FlyingObject {
  8. private int life; //生命值
  9. private int doubleFire; //火力值
  10. private BufferedImage[] images; //英雄机图片数组
  11. private int index; //协助图片切换
  12. private int score; //打死敌人后得分
  13. public OwnPlane() {
  14. image = ShootGame.ownPlane0;
  15. width = image.getWidth();
  16. height = image.getHeight();
  17. x = 150;
  18. y = 400;
  19. life = 3; //设置生命数为3
  20. doubleFire = 0; //设置火力值为单倍
  21. images = new BufferedImage[]{ ShootGame.ownPlane0, ShootGame.ownPlane1};
  22. index = 0;
  23. score = 0;
  24. }
  25. @Override
  26. public void step(OwnPlane ownPlane) {
  27. // 每100毫秒切换一次图片
  28. image = images[index++ / 10 % images.length];
  29. }
  30. public Bullet[] shoot() {
  31. int xStep = this.width / 9;
  32. if (doubleFire > 0) { //双发
  33. Bullet[] bullets = new Bullet[2];
  34. bullets[0] = new Bullet(this.x + 2 * xStep, this.y);
  35. bullets[1] = new Bullet(this.x + 6 * xStep, this.y);
  36. doubleFire -= 2; //发射双倍火力,每次减2,限制双倍火力的持续时间
  37. return bullets;
  38. } else { //单发
  39. Bullet[] bullets = new Bullet[1];
  40. bullets[0] = new Bullet(this.x + 4 * xStep, this.y);
  41. return bullets;
  42. }
  43. }
  44. public void moveTo(int x, int y) {
  45. this.x = x - this.width / 2;
  46. this.y = y - this.height / 2;
  47. }
  48. @Override
  49. public boolean outOfBounds() {
  50. //英雄机永不越界
  51. return false;
  52. }
  53. public void setLife(int life) {
  54. this.life = life;
  55. }
  56. //得到生命值
  57. public int getLife() {
  58. return life;
  59. }
  60. //增加火力值
  61. public void addDoubleFire(){
  62. doubleFire += 60;
  63. }
  64. //火力值清零
  65. public void setDoubleFire(int doubleFire){
  66. this.doubleFire = doubleFire;
  67. }
  68. //得到双倍火力的剩余时间
  69. public int getDoubleFireTime() {
  70. return doubleFire / 2;
  71. }
  72. //英雄机撞敌人
  73. public boolean hit(FlyingObject fo){
  74. int x1 = fo.x - this.width / 2;
  75. int x2 = fo.x + this.width / 2;
  76. int y1 = fo.y - this.height / 2;
  77. int y2 = fo.y + this.height / 2;
  78. int hx = this.x + this.width / 2;
  79. int hy = this.y + this.height / 2;
  80. return hx > x1 && hx < x2 && hy > y1 && hy < y2;
  81. }
  82. //得到分数
  83. public int getScore() {
  84. return score;
  85. }
  86. //设置分数
  87. public void setScore(int score) {
  88. this.score = score;
  89. }
  90. }

有任何疑问和和源码需求敬请关注公众号【蜗牛资源社】

欢迎交流学习!

发表评论

表情:
评论列表 (有 0 条评论,84人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Java飞机大战项目

    飞机大战游戏是一款十分有趣的射击类小游戏,流畅的画面,高难度的挑战。游戏中,玩家驾驶英雄机,在空中进行战斗。点击并移动自己的英雄机,发射炮弹,打掉敌飞机以及蜜蜂,来获得分数和奖