Java小游戏飞翔的小鸟

第一步是创建项目 项目名自拟

第二步创建个包名 来规范class

再创建一个包  来存储照片

如下:

  700c544959154eb29fef1e5223c791aa.png

 

代码如下:

 
  1. package game;

  2. import java.awt.*;

  3. import javax.swing.*;

  4. import javax.imageio.ImageIO;

  5.  
  6. public class Bird {

  7.  
  8. Image image;

  9. int x,y;

  10. int width,height;

  11. int size;

  12.  
  13. double g;

  14.  
  15. double t;

  16.  
  17. double v0;

  18.  
  19. double speed;

  20.  
  21. double s;

  22.  
  23. double alpha;

  24.  
  25. //֡

  26. Image[] images;

  27.  
  28. int index;

  29.  
  30. public Bird() throws Exception

  31. {

  32. image=new ImageIcon("source/0.png").getImage();

  33. width = image.getWidth(null);

  34. height = image.getHeight(null);

  35. x=132;

  36. y=280;

  37. size=40;

  38.  
  39. g=4;

  40. v0=20;

  41. t=0.25;

  42. speed=v0;

  43. s=0;

  44.  
  45. alpha=0;

  46.  
  47.  
  48. images=new Image[8];

  49.  
  50. for(int i=0;i<8;i++)

  51. {

  52. images[i]=new ImageIcon("source/"+i+".png").getImage();

  53. }

  54. index=0;

  55.  
  56. }

  57.  
  58.  
  59. public void fly()

  60. {

  61. index++;

  62. image=images[(index/12)%8];

  63. }

  64.  
  65.  
  66. public void step()

  67. {

  68. double v0=speed;

  69.  
  70. s=v0*t+g*t*t/2;

  71.  
  72. y=y-(int)s;

  73.  
  74. double v=v0-g*t;

  75. speed =v;

  76.  
  77. alpha=Math.atan(s/8);

  78.  
  79. }

  80.  
  81.  
  82. public void flappy()

  83. {

  84.  
  85. speed=v0;

  86. }

  87.  
  88.  
  89. public boolean hit(Ground ground)

  90. {

  91. boolean hit =y+size/2>ground.y;

  92. if(hit)

  93. {

  94. y=ground.y-size/2;

  95. alpha=Math.PI/2;

  96. }

  97. return hit;

  98. }

  99.  
  100.  
  101. public boolean hit(Column column)

  102. {

  103.  
  104. if(x>column.x-column.width/2-size/2&&x

  105. {

  106. if(y>column.y-column.gap/2+size/2&&y

  107. return true;

  108. }

  109. return false;

  110. }

  111. }

 

 
  1. package game;

  2.  
  3. import javax.imageio.ImageIO;

  4. import java.util.*;

  5.  
  6. import javax.swing.*;

  7.  
  8. import java.awt.Graphics;

  9. import java.awt.event.MouseAdapter;

  10. import java.awt.event.*;

  11. import java.awt.image.BufferedImage;

  12. import javax.imageio.*;

  13. import java.awt.*;

  14.  
  15. public class BirdGame extends JPanel {

  16.  
  17.  
  18. Image background;

  19. Image startImage;

  20. Image overImage;

  21. Ground ground;//����

  22. Column column1,column2;

  23. Bird bird;

  24. int score;

  25. int state;//״̬

  26. //״̬����

  27. public static final int START=0;

  28. public static final int RUNNING=1;

  29. public static final int GAME_OVER=2;

  30.  
  31. public BirdGame() throws Exception

  32. {

  33. background = new ImageIcon("./source/bg.png").getImage();

  34. startImage = new ImageIcon("./source/start.png").getImage();

  35. overImage=new ImageIcon("./source/gameover.png").getImage();

  36. //״̬

  37. ground=new Ground();

  38. column1=new Column(1);

  39. column2=new Column(2);

  40. bird=new Bird();

  41. score=0;

  42. state=0;

  43. }

  44.  
  45. public void paint(Graphics g)

  46. {

  47.  
  48. g.drawImage(background, 0, 0,null);

  49.  
  50. g.drawImage(ground.image, ground.x, ground.y, null);

  51.  
  52. g.drawImage(column1.image,column1.x-column1.width/2,column1.y-column1.height/2,null);

  53. g.drawImage(column2.image,column2.x-column2.width/2,column2.y-column2.height/2,null);

  54.  
  55. Graphics2D g2=(Graphics2D) g;

  56. g2.rotate(-bird.alpha,bird.x,bird.y);

  57. g.drawImage(bird.image,bird.x-bird.width/2,bird.y-bird.height/2,null);

  58. g2.rotate(bird.alpha,bird.x,bird.y);

  59.  
  60. Font f=new Font(Font.SANS_SERIF,Font.BOLD,40);

  61. g.setFont(f);

  62. g.drawString(""+score, 40, 60);

  63. g.setColor(Color.WHITE);

  64. g.drawString(""+score,40-3, 60-3);

  65.  
  66. switch(state)

  67. {

  68. case START:

  69. g.drawImage(startImage, 0, 0, null);

  70. break;

  71. case GAME_OVER:

  72. g.drawImage(overImage, 0, 0, null);

  73. break;

  74. }

  75. }

  76.  
  77. public void action() throws Exception

  78. {

  79.  
  80. MouseListener l=new MouseAdapter()

  81. {

  82. public void mousePressed(MouseEvent e)

  83. {

  84. try {

  85. switch(state) {

  86. case START:

  87. //״̬

  88. state=RUNNING;

  89. break;

  90. case RUNNING:

  91.  
  92. bird.flappy();

  93. break;

  94. case GAME_OVER:

  95.  
  96. column1=new Column(1);

  97. column2=new Column(2);

  98. bird=new Bird();

  99. score=0;

  100. state=START;

  101. break;

  102. }

  103. }

  104. catch (Exception ex)

  105. {

  106. ex.printStackTrace();

  107. }

  108. }

  109.  
  110. };

  111. addMouseListener(l);

  112. while(true)

  113. {

  114. switch(state)

  115. {

  116. case START:

  117. bird.fly();

  118. ground.step();

  119. break;

  120. case RUNNING:

  121. ground.step();

  122. column1.step();

  123. column2.step();

  124. bird.fly();

  125. bird.step();

  126.  
  127. score++;

  128. //

  129. if(bird.hit(ground)||bird.hit(column1)||bird.hit(column2))

  130. {

  131. state=GAME_OVER;

  132. }

  133. break;

  134. }

  135.  
  136. Thread.sleep(1000/60);

  137. repaint();

  138. }

  139. }

  140.  
  141.  
  142.  
  143. public static void main(String[] args) throws Exception

  144. {

  145.  
  146. JFrame frame=new JFrame();

  147. BirdGame game=new BirdGame();

  148. frame.add(game);

  149. frame.setSize(440,670);

  150. frame.setLocationRelativeTo(null);

  151. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

  152. frame.setVisible(true);

  153. game.action();

  154. }

  155.  
  156. }

 
  1. package game;

  2.  
  3. import java.util.*;

  4. import java.awt.*;

  5.  
  6. import javax.imageio.ImageIO;

  7. import javax.swing.*;

  8.  
  9. public class Column {

  10.  
  11. Image image;

  12.  
  13. int x,y;

  14. int width,height;

  15.  
  16. int gap;

  17.  
  18. int distance;

  19. Random random =new Random();

  20.  
  21.  
  22.  
  23. public Column(int n) throws Exception

  24. {

  25. image=new ImageIcon("source/column.png").getImage();

  26. width=image.getWidth(null);

  27. height=image.getHeight(null);

  28. gap=144;

  29. distance=245;

  30. x=550+(n-1)*distance;

  31. y=random.nextInt(218)+132;

  32. }

  33.  
  34. public void step()

  35. {

  36. x-=4;

  37. if(x<= -width/2)

  38. {

  39. x=distance*2-width/2;

  40. y=random.nextInt(218);

  41. }

  42. }

  43. }

 

 

 
  1. package game;

  2.  
  3. import javax.swing.*;

  4. import java.awt.*;

  5.  
  6. public class Ground {

  7.  
  8. Image image;

  9.  
  10. int x,y;

  11.  
  12. int width,height;

  13.  
  14.  
  15. public Ground() throws Exception

  16. {

  17. image =new ImageIcon("source/ground.png").getImage();

  18. width=image.getWidth(null);

  19. height=image.getHeight(null);

  20. x=0;

  21. y=500;

  22. }

  23.  
  24.  
  25. public void step()

  26. {

  27. x-=4;

  28. if(x<=-109)

  29. {

  30. x=0;

  31. }

  32. }

  33.  
  34.  
  35. }

运行结果如下所示:

776a30b8160940fb928870925045da67.png

 e844b5c8b42e41248f68089c439a7945.png493dc9a0c9af4bf2887d04e9d58b7183.png

 

你可能感兴趣的:(java)