• 飞翔的小鸟


    运行游戏如下:

     

    碰到柱子就结束游戏

     App

    GameApp类

    1. package App;
    2. import main.GameFrame;
    3. public class GameApp {
    4. public static void main(String[] args) {//游戏的入口
    5. new GameFrame();
    6. }
    7. }

    main

    Barrier 类

    1. package main;
    2. import util.Constant;
    3. import util.GameUtil;
    4. import java.awt.*;
    5. import java.awt.image.BufferedImage;
    6. import java.util.Random;
    7. /**
    8. * 障碍物类
    9. */
    10. public class Barrier {
    11. //矩形参数
    12. private Rectangle rect;
    13. private boolean mob = true;
    14. //障碍物移动速度
    15. private int speed = 3;
    16. //障碍物需要的三个图片
    17. private static BufferedImage[] img;
    18. //障碍物的状态
    19. private boolean visible;
    20. static {
    21. final int COUNT = 3;
    22. //类加载的时候将三个图片初始化
    23. img = new BufferedImage[COUNT];
    24. for (int i = 0; i < COUNT; i++) {
    25. img[i] = GameUtil.loadBufferedImage(Constant.BARRIER_IMG_PATH[i]);
    26. }
    27. }
    28. //位置
    29. private int x, y;
    30. //宽度和高度
    31. private int width, height;
    32. //障碍物的类型
    33. private int type;
    34. public static final int TYPE_TOP_NORMAL = 0;
    35. public static final int TYPE_BOTTOM_NORMAL = 2;
    36. public static final int TYPE_HOVER_NORMAL = 4;
    37. public static final int TYPE_MOBILE = 6;
    38. //获得障碍物的宽度和高度
    39. public static final int BARRIRE_WIDTH = img[0].getWidth();
    40. public static final int BARRIRE_HEIGHT = img[0].getHeight();
    41. public static final int BARRIRE_HEAD_WIDTH = img[1].getWidth();
    42. public static final int BARRIRE_HEAD_HEIGHT = img[1].getHeight();
    43. public Barrier() {
    44. rect = new Rectangle();
    45. }
    46. public Barrier(int x, int y, int height, int type) {
    47. this.x = x;
    48. this.y = y;
    49. this.height = height;
    50. this.type = type;
    51. this.width = BARRIRE_WIDTH;
    52. }
    53. //根据不同的类型绘制障碍物
    54. public void draw(Graphics g) {
    55. switch (type) {
    56. case TYPE_TOP_NORMAL:
    57. drawTopMormal(g);
    58. break;
    59. case TYPE_BOTTOM_NORMAL:
    60. drawNomalTop(g);
    61. break;
    62. case TYPE_HOVER_NORMAL:
    63. drawHoverNormal(g);
    64. break;
    65. case TYPE_MOBILE:
    66. drawMobile(g);
    67. break;
    68. }
    69. }
    70. //绘制从上向下的障碍物
    71. private void drawTopMormal(Graphics g) {
    72. //求出所需要的障碍物的块数
    73. int count = (height - BARRIRE_HEAD_HEIGHT) / BARRIRE_HEIGHT + 1;
    74. //for循坏绘制障碍物
    75. for (int i = 0; i < count; i++) {
    76. g.drawImage(img[0], x, y + i * BARRIRE_HEIGHT, null);
    77. }
    78. //绘制头
    79. int y = height - BARRIRE_HEAD_HEIGHT;
    80. g.drawImage(img[2], x - (BARRIRE_HEAD_WIDTH - BARRIRE_WIDTH) / 2, y, null);
    81. x -= speed;
    82. if (x < 50) {
    83. visible = false;
    84. }
    85. rect(g);
    86. }
    87. //绘制从下向上的障碍物
    88. private void drawNomalTop(Graphics g) {
    89. //求出所需要的障碍物的块数
    90. int count = height / BARRIRE_HEIGHT + 1;
    91. //for循环绘制障碍物
    92. for (int i = 0; i < count; i++) {
    93. g.drawImage(img[0], x, Constant.FRAM_HEIGNT - i * BARRIRE_HEIGHT, null);
    94. }
    95. //绘制头
    96. int y = Constant.FRAM_HEIGNT - height;
    97. g.drawImage(img[1], x - (BARRIRE_HEAD_WIDTH - BARRIRE_WIDTH) / 2, y, null);
    98. x -= speed;
    99. if (x < -50) {
    100. visible = false;
    101. }
    102. rect(g);
    103. }
    104. //绘制中间的障碍物
    105. private void drawHoverNormal(Graphics g) {
    106. //求出所需要的障碍物的块数
    107. int count = (height-BARRIRE_HEAD_HEIGHT)/BARRIRE_HEIGHT;
    108. //绘制上头
    109. g.drawImage(img[1],x,y,null);
    110. //for循环绘制障碍物
    111. for (int i = 0; i < count; i++) {
    112. g.drawImage(img[0], x, y+BARRIRE_HEAD_HEIGHT+i*BARRIRE_HEIGHT, null);
    113. }
    114. rect(g);
    115. //绘制下头
    116. int y11 = y+height-BARRIRE_HEAD_HEIGHT;
    117. g.drawImage(img[2],x,y11,null);
    118. x -= speed;
    119. if (x < -50) {
    120. visible = false;
    121. }
    122. }
    123. //绘制移动的障碍物
    124. private void drawMobile(Graphics g) {
    125. //求出所需要的障碍物的块数
    126. int count = (height-BARRIRE_HEAD_HEIGHT)/BARRIRE_HEIGHT;
    127. //绘制上头
    128. g.drawImage(img[1],x,y,null);
    129. //for循环绘制障碍物
    130. for (int i = 0; i < count; i++) {
    131. g.drawImage(img[0], x, y+BARRIRE_HEAD_HEIGHT+i*BARRIRE_HEIGHT, null);
    132. }
    133. rect(g);
    134. //绘制下头
    135. int y11 = y+height-BARRIRE_HEAD_HEIGHT;
    136. g.drawImage(img[2],x,y11,null);
    137. x -= speed;
    138. if (x < -50) {
    139. visible = false;
    140. }
    141. if (mob) {
    142. y+=5;
    143. if (y >= 250) {
    144. mob=false;
    145. }
    146. }else if (!mob){
    147. y-=5;
    148. if (y <= 100) {
    149. mob=true;
    150. }
    151. }
    152. }
    153. /**
    154. * 绘制障碍物碰撞矩形
    155. *
    156. * @return
    157. */
    158. public void rect(Graphics g) {
    159. int x1 = this.x;
    160. int y1 = this.y;
    161. int w1 = img[0].getWidth();
    162. // g.setColor(Color.blue);
    163. // g.drawRect(x1, y1, w1, height);
    164. setRecyangle(x1, y1, w1, height);
    165. }
    166. /**
    167. * 障碍物的碰撞矩形参数
    168. *
    169. * @return
    170. */
    171. public void setRecyangle(int x, int y, int width, int height) {
    172. rect.x = x;
    173. rect.y = y;
    174. rect.width = width;
    175. rect.height = height;
    176. }
    177. //判断什么时候绘制下一组障碍物
    178. public boolean isInFrame() {
    179. return 600 - x > 150;
    180. }
    181. public int getX() {
    182. return x;
    183. }
    184. public void setX(int x) {
    185. this.x = x;
    186. }
    187. public int getY() {
    188. return y;
    189. }
    190. public void setY(int y) {
    191. this.y = y;
    192. }
    193. public int getHeight() {
    194. return height;
    195. }
    196. public void setHeight(int height) {
    197. this.height = height;
    198. }
    199. public int getType() {
    200. return type;
    201. }
    202. public void setType(int type) {
    203. this.type = type;
    204. }
    205. public boolean isVisible() {
    206. return visible;
    207. }
    208. public void setVisible(boolean visible) {
    209. this.visible = visible;
    210. }
    211. public Rectangle getRect() {
    212. return rect;
    213. }
    214. }

    Barrierpool 类 

    1. package main;
    2. import java.util.ArrayList;
    3. import java.util.List;
    4. /**
    5. * 为了避免反复的创建和销毁对象,使用对象池来提前创建好一些对象。
    6. * 使用的时候从池中获得,使用完毕后,归还
    7. */
    8. public class Barrierpool {
    9. //用于管理池中所有对象的容器
    10. private static List pool = new ArrayList<>();
    11. //池中初始的对象个数
    12. public static final int initCount = 16;
    13. //对象池中最大个数
    14. public static final int maxCOunt = 20;
    15. static {
    16. //初始化池中的对象
    17. for (int i = 0; i < initCount; i++) {
    18. pool.add(new Barrier());
    19. }
    20. }
    21. /**
    22. * 从池中获取一个对象
    23. */
    24. public static Barrier getPool(){
    25. int size = pool.size();
    26. //如果池中有对象才可以拿
    27. if (size > 0) {
    28. //移除并返回对象
    29. System.out.println("拿走一个");
    30. return pool.remove(size-1);
    31. }else {
    32. //池中没有对象了 只能new
    33. System.out.println("新的对象");
    34. return new Barrier();
    35. }
    36. }
    37. /**
    38. * 将对象归还容器中
    39. */
    40. public static void setPool(Barrier barrier){
    41. if (pool.size() < maxCOunt) {
    42. pool.add(barrier);
    43. System.out.println("容器归还了");
    44. }
    45. }
    46. }

    Bird 类

    1. package main;
    2. import static util.Constant.*;
    3. import util.GameUtil;
    4. import java.awt.*;
    5. import java.awt.image.BufferedImage;
    6. /**
    7. * 小鸟类
    8. */
    9. public class Bird {
    10. //小鸟矩形对象
    11. private Rectangle rect;
    12. //小鸟加速度
    13. private int acceleration;
    14. //小鸟的生命
    15. public boolean life = true;
    16. //存放小鸟图片
    17. private BufferedImage[] images;
    18. public static final int BIRD_IMG_COUNT = 3;
    19. //鸟的状态
    20. private int state;
    21. public static final int STATE_NORMAR = 0;//平着飞
    22. public static final int STATE_UP = 1;//向上飞
    23. public static final int STATE_DOWN = 2;//向下飞
    24. //小鸟的位置
    25. private int x=200,y=200;
    26. //小鸟移动方向 上下
    27. private boolean up=false,down=false;
    28. //小鸟移动速度
    29. private int speed=4;
    30. //构造方法中对资源初始化
    31. public Bird() {
    32. images = new BufferedImage[BIRD_IMG_COUNT];
    33. for (int i = 0; i < BIRD_IMG_COUNT; i++) {
    34. images[i] = GameUtil.loadBufferedImage(BIRD_IMG[i]);
    35. }
    36. int w = images[0].getWidth();
    37. int h = images[0].getHeight();
    38. rect = new Rectangle(w,h);
    39. }
    40. //绘制小鸟
    41. public void draw(Graphics g) {
    42. flyLogic();
    43. g.drawImage(images [state], x, y, null);
    44. //绘制小鸟的矩形
    45. g.drawRect(x,y,(int)rect.getWidth(), rect.height);
    46. rect.x=this.x;
    47. rect.y=this.y;
    48. }
    49. //控制小鸟移动方向
    50. public void flyLogic(){
    51. if (up){
    52. acceleration--;
    53. y+=acceleration;
    54. if (acceleration < -10) {
    55. acceleration=-10;
    56. }
    57. if (y<20){
    58. y=20;
    59. acceleration=0;
    60. }
    61. }
    62. if (!up){
    63. acceleration++;
    64. y+=acceleration;
    65. if (acceleration > 10) {
    66. acceleration=10;
    67. }
    68. if (y>475){
    69. y=475;
    70. acceleration=0;
    71. }
    72. }
    73. }
    74. public void fly(int fly){
    75. switch (fly){
    76. case 1:
    77. state=1;
    78. up=true;
    79. break;
    80. case 5:
    81. state=2;
    82. up=false;
    83. break;
    84. }
    85. }
    86. public Rectangle getRect() {
    87. return rect;
    88. }
    89. /**
    90. * 重新绘制小鸟的位置
    91. */
    92. public void restartDraw(){
    93. life=true;
    94. x=200;
    95. y=200;
    96. }
    97. }

     Cloud 类

    1. package main;
    2. import java.awt.*;
    3. import java.awt.image.BufferedImage;
    4. /**
    5. * 云彩类
    6. */
    7. public class Cloud {
    8. //云彩图片
    9. private BufferedImage img;
    10. //云彩速度
    11. private int speed;
    12. //云彩的位置
    13. private int x,y;
    14. public Cloud(){}
    15. public Cloud(BufferedImage img, int speed, int x, int y) {
    16. this.img = img;
    17. this.speed = speed;
    18. this.x = x;
    19. this.y = y;
    20. }
    21. public void draw(Graphics g){
    22. x-=speed;
    23. g.drawImage(img,x,y,null);
    24. }
    25. /**
    26. * 用于判断云彩是否飞出屏幕以外
    27. */
    28. public boolean isOutFrame(){
    29. if (x < -100) {
    30. return true;
    31. }
    32. return false;
    33. }
    34. }

    GameBackGround 类

    1. package main;
    2. import static util.Constant.*;
    3. import util.Constant;
    4. import util.GameUtil;
    5. import java.awt.*;
    6. import java.awt.image.BufferedImage;
    7. /**
    8. * 游戏背景类
    9. */
    10. public class GameBackGround {
    11. //背景需要的资源图片
    12. private BufferedImage bkimg;
    13. //构造器初始化资源
    14. public GameBackGround(){
    15. bkimg = GameUtil.loadBufferedImage(Constant.BK_IMG_OATH);
    16. }
    17. //绘制图片
    18. public void draw(Graphics g){
    19. //填充背景色
    20. g.setColor(BK_COLOR);
    21. g.fillRect(0,0,FRAM_WIDTH,FRAM_HEIGNT);
    22. g.setColor(Color.black);
    23. //得到图片的高度和宽度
    24. int height = bkimg.getHeight();
    25. int weight = bkimg.getWidth();
    26. //循环的次数
    27. int count = Constant.FRAM_WIDTH/weight+1;
    28. for (int i = 0; i < count; i++) {
    29. g.drawImage(bkimg,weight*i,Constant.FRAM_HEIGNT-height,null);
    30. }
    31. }
    32. }

    GameBarrierLayer  类

    1. package main;
    2. import util.Constant;
    3. import java.awt.*;
    4. import java.io.*;
    5. import java.util.ArrayList;
    6. import java.util.List;
    7. import java.util.Random;
    8. /**
    9. * 游戏中障碍物层
    10. */
    11. public class GameBarrierLayer {
    12. private GameTime gameTime;
    13. private int txt;
    14. private Random random = new Random();
    15. private List barriers;
    16. public GameBarrierLayer() {
    17. barriers = new ArrayList<>();
    18. gameTime = new GameTime();
    19. }
    20. //绘制障碍物
    21. public void draw(Graphics g, Bird bird) {
    22. for (int i = 0; i < barriers.size(); i++) {
    23. Barrier barrier = barriers.get(i);
    24. if (barrier.isVisible()) {
    25. barrier.draw(g);
    26. } else {
    27. Barrier remove = barriers.remove(i);
    28. Barrierpool.setPool(remove);
    29. i--;
    30. }
    31. }
    32. collideBird(bird);
    33. logic(g);
    34. }
    35. public void logic(Graphics g) {
    36. if (barriers.size() == 0) {
    37. ran();
    38. gameTime.begin();
    39. insert(600, 0, numberTop, 0);
    40. insert(600, 500 - numberDown, numberDown, 2);
    41. } else {
    42. long differ = gameTime.differ();
    43. g.setColor(Color.white);
    44. g.setFont(new Font("微软雅黑", 1, 20));
    45. g.drawString("坚持了:" + differ + "秒", 30, 50);
    46. //显示最高成绩
    47. txt = getTxt();
    48. if (differ <= txt){
    49. g.drawString("最高成绩:" + txt , 200, 50);
    50. }else {
    51. setTxt(String.valueOf(differ));
    52. g.drawString("最高成绩:" + getTxt() , 200, 50);
    53. }
    54. //判断最后一个障碍物是否完全进入屏幕内
    55. Barrier last = barriers.get(barriers.size() - 1);
    56. if (last.isInFrame()) {
    57. ran();
    58. if (number < 50){
    59. insert(600,32,440,4);
    60. }else if (number>450){
    61. insert(600,125,200,6);
    62. }else {
    63. insert(600, 0, numberTop, 0);
    64. insert(600, 500 - numberDown, numberDown, 2);
    65. }
    66. }
    67. }
    68. }
    69. //用于储存游戏数据
    70. File file = new File("C:\\game.txt");
    71. /**
    72. * 从文件获取数据
    73. * @return
    74. */
    75. public int getTxt() {
    76. BufferedReader in = null;
    77. try {
    78. in = new BufferedReader(new FileReader(file));
    79. } catch (FileNotFoundException e) {
    80. e.printStackTrace();
    81. }
    82. int read = 0;
    83. try {
    84. read = Integer.parseInt(in.readLine());
    85. } catch (IOException e) {
    86. e.printStackTrace();
    87. }
    88. try {
    89. in.close();
    90. } catch (IOException e) {
    91. e.printStackTrace();
    92. }
    93. return read;
    94. }
    95. /**
    96. * 写入数据到文件
    97. * @param str
    98. */
    99. public void setTxt(String str) {
    100. FileWriter out = null;
    101. try {
    102. out = new FileWriter(file);
    103. } catch (IOException e) {
    104. e.printStackTrace();
    105. }
    106. try {
    107. out.write(str);
    108. } catch (IOException e) {
    109. e.printStackTrace();
    110. }
    111. try {
    112. out.close();
    113. } catch (IOException e) {
    114. e.printStackTrace();
    115. }
    116. }
    117. /**
    118. * 用于从池中获取对象,并吧参数封装成barrier 存入barriers数组中
    119. */
    120. public void insert(int x, int y, int num, int type) {
    121. Barrier top = Barrierpool.getPool();
    122. top.setX(x);
    123. top.setY(y);
    124. top.setHeight(num);
    125. top.setType(type);
    126. top.setVisible(true);
    127. barriers.add(top);
    128. }
    129. //上方障碍物高度
    130. private int numberTop;
    131. //下方障碍物高度
    132. private int numberDown;
    133. private int number;
    134. //产生两个100-500之间的随机高度
    135. public void ran() {
    136. numberTop = random.nextInt(400) + 100;
    137. numberDown = random.nextInt(400) + 100;
    138. number = random.nextInt(500);
    139. //如果管道重合,则重新随机
    140. if (numberTop + numberDown > 450) {
    141. ran();
    142. }
    143. }
    144. /**
    145. * 判断障碍物和小鸟发生碰撞
    146. */
    147. public boolean collideBird(Bird bird) {
    148. for (int i = 0; i < barriers.size(); i++) {
    149. Barrier barrier = barriers.get(i);
    150. //判断矩形是否相交
    151. if (barrier.getRect().intersects(bird.getRect())) {
    152. System.out.println("装上啦");
    153. bird.life = false;
    154. }
    155. }
    156. return false;
    157. }
    158. /**
    159. * 用于清空障碍物的池子
    160. */
    161. public void restant(){
    162. barriers.clear();
    163. }
    164. }

    GameFrame 类

    1. package main;
    2. import static util.Constant.*;
    3. import java.awt.*;
    4. import java.awt.event.*;
    5. import java.awt.image.BufferedImage;
    6. /**
    7. * 游戏的主窗口类,所有的关于游戏中绘制的内容都在此类中完成。
    8. */
    9. public class GameFrame extends Frame {
    10. //实例化gamebackGround类
    11. private GameBackGround gameBackGround;
    12. //实例化Bird类
    13. private Bird bird;
    14. //实例化GameBarrierLayer类
    15. private GameBarrierLayer gameBarrierLayer;
    16. //实例化GameFrontGround类
    17. private GameFrontGround gameFrontGround;
    18. //存放图片的图片
    19. private BufferedImage buffimg = new BufferedImage(FRAM_WIDTH,FRAM_HEIGNT,BufferedImage.TYPE_4BYTE_ABGR);
    20. //构造方法中初始化一些参数
    21. public GameFrame(){
    22. //窗口是否可见
    23. setVisible(true);
    24. //窗口的大小
    25. setSize(FRAM_WIDTH,FRAM_HEIGNT);
    26. //窗口的标题
    27. setTitle(FRAM_TITLE);
    28. //窗口的初始化位置
    29. setLocation(FRAM_X,FRAM_Y);
    30. //窗口的大小不可改变
    31. setResizable(false);
    32. //窗口的关闭事件
    33. addWindowListener(new WindowAdapter() {
    34. @Override
    35. public void windowClosing(WindowEvent e) {
    36. System.exit(0);//结束程序
    37. }
    38. });
    39. //初始化游戏对象
    40. initGamg();
    41. new run().start();
    42. //添加按键监听
    43. addKeyListener(new KeyAdapter() {
    44. @Override
    45. public void keyPressed(KeyEvent e) {
    46. add(e);
    47. }
    48. @Override
    49. public void keyReleased(KeyEvent e) {
    50. minu(e);
    51. }
    52. });
    53. }
    54. //对游戏中的对象初始化
    55. public void initGamg(){
    56. gameBackGround = new GameBackGround();
    57. bird = new Bird();
    58. gameFrontGround = new GameFrontGround();
    59. gameBarrierLayer = new GameBarrierLayer();
    60. }
    61. class run extends Thread{
    62. @Override
    63. public void run() {
    64. while (true) {
    65. repaint();
    66. try {
    67. Thread.sleep(33);
    68. } catch (InterruptedException e) {
    69. e.printStackTrace();
    70. }
    71. }
    72. }
    73. }
    74. /**
    75. * 所有的我们需要绘制的内容都在此方法中进行调用绘制
    76. */
    77. @Override
    78. public void update(Graphics g) {
    79. if(bird.life){
    80. //得到图片的画笔
    81. Graphics graphics = buffimg.getGraphics();
    82. gameBackGround.draw(graphics);
    83. bird.draw(graphics);
    84. gameFrontGround.draw(graphics);
    85. gameBarrierLayer.draw(graphics,bird);
    86. //一次性的将图片绘制到屏幕中
    87. g.drawImage(buffimg,0,0,null);
    88. }else {
    89. String over = "游戏结束";
    90. g.setColor(Color.red);
    91. g.setFont(new Font("微软雅黑",1,60));
    92. g.drawString(over,180,250);
    93. String reset = "Space Reset Game";
    94. g.drawString(reset,25,350);
    95. }
    96. }
    97. //按键
    98. public void add(KeyEvent e){
    99. switch (e.getKeyCode()){
    100. case KeyEvent.VK_UP:
    101. bird.fly(1);
    102. break;
    103. case KeyEvent.VK_SPACE:
    104. if (bird.life == false) {
    105. restart();
    106. }
    107. break;
    108. }
    109. }
    110. //抬键
    111. public void minu(KeyEvent e){
    112. switch (e.getKeyCode()){
    113. case KeyEvent.VK_UP:
    114. bird.fly(5);
    115. break;
    116. }
    117. }
    118. /**
    119. * 重置游戏
    120. */
    121. public void restart(){
    122. gameBarrierLayer.restant();
    123. bird.restartDraw();
    124. }
    125. }

    GameFrontGround 类

    1. package main;
    2. import util.Constant;
    3. import util.GameUtil;
    4. import java.awt.*;
    5. import java.awt.image.BufferedImage;
    6. import java.util.ArrayList;
    7. import java.util.List;
    8. import java.util.Random;
    9. /**
    10. * 游戏的前景类
    11. */
    12. public class GameFrontGround {
    13. //云彩的个数
    14. private static final int CLOUND_COUNT = 2;
    15. //存放云彩的容器
    16. private List clouds;
    17. //云彩的飞行速度
    18. private static final int CLOUNG_SPEED = 1;
    19. //使用到图片资源
    20. private BufferedImage[] img;
    21. //用于产生随机数
    22. private Random random;
    23. // 构造器初始化数据
    24. public GameFrontGround() {
    25. clouds = new ArrayList<>();
    26. img = new BufferedImage[CLOUND_COUNT];
    27. //容器中添加云彩的图片
    28. for (int i = 0; i < CLOUND_COUNT; i++) {
    29. img[i] = GameUtil.loadBufferedImage("img/cloud0" + i + ".png");
    30. }
    31. random = new Random();
    32. }
    33. //绘制云彩
    34. public void draw(Graphics g) {
    35. logic();
    36. for (int i = 0; i < clouds.size(); i++) {
    37. clouds.get(i).draw(g);
    38. }
    39. }
    40. /**
    41. * 用于云彩的个数控制
    42. */
    43. private void logic() {
    44. if ((int) (500 * Math.random()) < 5) {
    45. Cloud cloud = new Cloud(img[random.nextInt(CLOUND_COUNT)], CLOUNG_SPEED, 600, random.nextInt(150));
    46. clouds.add(cloud);
    47. }
    48. for (int i = 0; i < clouds.size(); i++) {
    49. Cloud cloud = clouds.get(i);
    50. if (cloud.isOutFrame()){
    51. clouds.remove(i);
    52. i--;
    53. System.out.println("移除了"+cloud);
    54. }
    55. }
    56. }
    57. }

    GameTime 类

    1. package main;
    2. /**
    3. * 游戏计时器
    4. */
    5. public class GameTime {
    6. //开始
    7. private long beginTime;
    8. //结束
    9. private long endTime;
    10. //时间差
    11. private long differ;
    12. public GameTime(){}
    13. public void begin(){
    14. beginTime = System.currentTimeMillis();
    15. }
    16. public long differ(){
    17. endTime = System.currentTimeMillis();
    18. return differ=(endTime-beginTime)/1000;
    19. }
    20. }

    untl

    Constant 类

    1. package util;
    2. import java.awt.*;
    3. /**
    4. * @author
    5. * @create
    6. */
    7. public class Constant {
    8. //窗口的大小
    9. public static final int FRAM_WIDTH= 600;
    10. public static final int FRAM_HEIGNT= 500;
    11. //窗口标题
    12. public static final String FRAM_TITLE= "飞翔的小鸟";
    13. //窗口的初始化位置
    14. public static final int FRAM_X= 200;
    15. public static final int FRAM_Y= 200;
    16. //背景图片
    17. public static final String BK_IMG_OATH = "img/bird_bk.png";
    18. //背景色
    19. public static final Color BK_COLOR = new Color(0x4BC4CF);
    20. //小鸟图片资源
    21. public static final String[] BIRD_IMG={
    22. "img/bird_normal.png","img/bird_up.png","img/bird_down.png"
    23. };
    24. //障碍物图片资源
    25. public static final String[] BARRIER_IMG_PATH ={
    26. "img/barrier.png","img/barrier_up.png","img/barrier_down.png"};
    27. }

    GameUtil 类

    1. package util;
    2. import javax.imageio.ImageIO;
    3. import java.awt.image.BufferedImage;
    4. import java.io.FileInputStream;
    5. import java.io.IOException;
    6. /**
    7. * 游戏工具类
    8. */
    9. public class GameUtil {
    10. /**
    11. *该方法传入一个图片路径 读取图片
    12. */
    13. public static BufferedImage loadBufferedImage(String ImgPath){
    14. try {
    15. return ImageIO.read(new FileInputStream(ImgPath));
    16. } catch (IOException e) {
    17. e.printStackTrace();
    18. }
    19. return null;
    20. }
    21. }

  • 相关阅读:
    【微机原理笔记】第 5 章 - 存储器系统与接口
    【C++】类和对象 — 日期类的实现 运算符重载 初始化列表 友元(下篇)
    【面试干货】数据库乐观锁,悲观锁的区别,怎么实现
    数据治理-数据资产估值
    用VS Code搞Qt6:编译源代码与基本配置
    【重装系统SSH连不上】 WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!
    如何通过大带宽优化美国站群服务器的全球访问速度?
    20、商品微服务-web层实现
    计算机毕业设计django基于python商品比价平台(源码+系统+mysql数据库+Lw文档)
    一张图读懂TuGraph Analytics开源技术架构
  • 原文地址:https://blog.csdn.net/2301_76549057/article/details/134521905