在路上

 找回密码
 立即注册
在路上 站点首页 学习 查看内容

Java实现的一个打飞机的小游戏

2016-12-20 13:15| 发布者: zhangjf| 查看: 470| 评论: 0

摘要: Bullet.java package GameSubstance;import java.awt.Graphics;import java.awt.Image;import java.awt.Toolkit;import Tools.GameBox;import View.StartFrame;public class Bullet{ public static final in ...
Bullet.java
  1. package GameSubstance;
  2. import java.awt.Graphics;
  3. import java.awt.Image;
  4. import java.awt.Toolkit;
  5. import Tools.GameBox;
  6. import View.StartFrame;
  7. public class Bullet{
  8. public static final int BULLET_SPEED=15;
  9. private int x,y;
  10. private boolean live=true;
  11. Image bullet=GameBox.bullet;
  12. public Bullet(int x,int y){
  13. this.x=x;
  14. this.y=y;
  15. }
  16. public void draw(Graphics g){
  17. if(!live){
  18. return;
  19. }
  20. g.drawImage(bullet,x,y,GameBox.BULLET_WIDTH,GameBox.BULLET_HEIGHT,null);
  21. y=y-BULLET_SPEED;
  22. if(y<-30){
  23. live=false;
  24. }
  25. }
  26. public boolean isLive() {
  27. return live;
  28. }
  29. public void setLive(boolean live) {
  30. this.live = live;
  31. }
  32. public int getX() {
  33. return x;
  34. }
  35. public int getY() {
  36. return y;
  37. }
  38. }
复制代码
EnemyBullet.java
  1. package GameSubstance;
  2. import java.awt.Graphics;
  3. import java.awt.Image;
  4. import java.awt.Toolkit;
  5. import Tools.GameBox;
  6. public class EnemyBullet {
  7. private int x,y;
  8. private int bulletspeed=15;
  9. private boolean live=true;
  10. Image enemyplanebullet=GameBox.enemybullet;
  11. public EnemyBullet(int x,int y){
  12. this.x=x;
  13. this.y=y;
  14. }
  15. public void draw(Graphics g){
  16. if(!live){
  17. return;
  18. }
  19. y=y+bulletspeed;
  20. g.drawImage(enemyplanebullet,x,y,GameBox.ENEMYBULLET_WIDTH,GameBox.ENEMYBULLET_HEIGHT,null);
  21. if(y>GameBox.GAME_HEIGHT+GameBox.ENEMYBULLET_HEIGHT){
  22. live=false;
  23. }
  24. }
  25. public boolean isLive() {
  26. return live;
  27. }
  28. public void setLive(boolean live) {
  29. this.live = live;
  30. }
  31. public int getX() {
  32. return x;
  33. }
  34. public int getY() {
  35. return y;
  36. }
  37. }
复制代码
EnemyPlane.java
  1. package GameSubstance;
  2. import java.awt.Graphics;
  3. import java.awt.Image;
  4. import java.awt.Toolkit;
  5. import java.util.ArrayList;
  6. import java.util.List;
  7. import java.util.Random;
  8. import Tools.GameBox;
  9. import View.StartFrame;
  10. public class EnemyPlane {
  11. StartFrame sf;
  12. private int xpeed=8;
  13. private int yspeed=5;
  14. private boolean live=true;
  15. private boolean fired=true;
  16. private int x,y=0;
  17. private static int i=0;
  18. private String dir;
  19. Image enemy;
  20. Random rand=new Random();
  21. public EnemyPlane(StartFrame sf){
  22. this.sf=sf;
  23. if(i==0){
  24. enemy=GameBox.enemy1;
  25. i=1;
  26. }else{
  27. enemy=GameBox.enemy2;
  28. i=0;
  29. }
  30. x=rand.nextInt(GameBox.GAME_WIDTH);
  31. if(x%2==0){
  32. dir="left";
  33. }else{
  34. dir="right";
  35. }
  36. if(x<0){
  37. x=0;
  38. }
  39. if(x+GameBox.ENEMY_WIDTH>GameBox.GAME_WIDTH){
  40. x=GameBox.GAME_WIDTH-GameBox.ENEMY_WIDTH;
  41. }
  42. }
  43. public void draw(Graphics g){
  44. if(live){
  45. g.drawImage(enemy,x,y,GameBox.ENEMY_WIDTH,GameBox.ENEMY_HEIGHT,null);
  46. move();
  47. }
  48. }
  49. private void move(){
  50. y=y+xpeed;
  51. int i=rand.nextInt(5)+2;
  52. if(dir=="left"){
  53. x=x-i;
  54. }else{
  55. x=x+i;
  56. }
  57. if(y>GameBox.GAME_HEIGHT+GameBox.ENEMY_HEIGHT){
  58. live=false;
  59. }
  60. if(x<0){
  61. x=0;
  62. dir="right";
  63. }
  64. if(x+GameBox.ENEMY_WIDTH>GameBox.GAME_WIDTH){
  65. x=GameBox.GAME_WIDTH-GameBox.ENEMY_WIDTH;
  66. dir="left";
  67. }
  68. }
  69. public int getX() {
  70. return x;
  71. }
  72. public int getY() {
  73. return y;
  74. }
  75. public boolean isLive() {
  76. return live;
  77. }
  78. public void setLive(boolean live) {
  79. this.live = live;
  80. }
  81. public int getXpeed() {
  82. return xpeed;
  83. }
  84. public void setXpeed(int xpeed) {
  85. this.xpeed = xpeed;
  86. }
  87. public int getYspeed() {
  88. return yspeed;
  89. }
  90. public void setYspeed(int yspeed) {
  91. this.yspeed = yspeed;
  92. }
  93. public boolean isFired() {
  94. return fired;
  95. }
  96. public void setFired(boolean fired) {
  97. this.fired = fired;
  98. }
  99. }
复制代码
Explode.java ~ 724B
  1. package GameSubstance;
  2. import java.awt.Graphics;
  3. import java.awt.Image;
  4. import java.awt.Toolkit;
  5. import Tools.GameBox;
  6. public class Explode {
  7. private int x,y;
  8. private boolean live=true;
  9. private int count;
  10. Image Explode1=GameBox.Explode1;
  11. public Explode(int x,int y){
  12. this.x=x;
  13. this.y=y;
  14. }
  15. public void draw(Graphics g){
  16. if(!live){
  17. return;
  18. }
  19. g.drawImage(Explode1,x,y,GameBox.EXPLODE1_WIDTH,GameBox.EXPLODE1_HEIGHT,null);
  20. if(count==8){
  21. live=false;
  22. }
  23. }
  24. public boolean isLive() {
  25. return live;
  26. }
  27. public void setLive(boolean live) {
  28. this.live = live;
  29. }
  30. public int getCount() {
  31. return count;
  32. }
  33. public void setCount(int count) {
  34. this.count = count;
  35. }
  36. }
复制代码
[文件] MyPlane.java ~ 2KB
  1. package GameSubstance;
  2. import Tools.GameBox;
  3. import View.StartFrame;
  4. import java.awt.Graphics;
  5. import java.awt.Image;
  6. import java.awt.Toolkit;
  7. import java.awt.event.KeyEvent;
  8. import javax.swing.ImageIcon;
  9. public class MyPlane {
  10. private int x,y;
  11. private static Image plane;
  12. private Bullet bullet;
  13. private boolean dl=false,du=false,dr=false,dd=false;
  14. public static final int MYPLANE_XSPEED=10;
  15. public static final int MYPLANE_YSPEED=10;
  16. private boolean live=true;
  17. public MyPlane(int x,int y){
  18. this.x=x;
  19. this.y=y;
  20. }
  21. public void draw(Graphics g){
  22. if(!live){
  23. return;
  24. }
  25. g.drawImage(GameBox.plane,x,y,GameBox.PLANE_WIDTH,GameBox.PLANE_HEIGHT,null);
  26. move();
  27. }
  28. public void keyPressed(KeyEvent e) {
  29. int key=e.getKeyCode();
  30. switch(key){
  31. case KeyEvent.VK_UP:
  32. du=true;
  33. break;
  34. case KeyEvent.VK_DOWN:
  35. dd=true;
  36. break;
  37. case KeyEvent.VK_LEFT:
  38. dl=true;
  39. break;
  40. case KeyEvent.VK_RIGHT:
  41. dr=true;
  42. break;
  43. }
  44. }
  45. public void keyReleased(KeyEvent e) {
  46. int key=e.getKeyCode();
  47. switch(key){
  48. case KeyEvent.VK_UP:
  49. du=false;
  50. break;
  51. case KeyEvent.VK_DOWN:
  52. dd=false;
  53. break;
  54. case KeyEvent.VK_LEFT:
  55. dl=false;
  56. break;
  57. case KeyEvent.VK_RIGHT:
  58. dr=false;
  59. }
  60. }
  61. private void move(){
  62. if(du&&!dl&&!dd&&!dr){
  63. y=y-MYPLANE_YSPEED;//上
  64. }else if(dd&&!du&&!dl&&!dr){
  65. y=y+MYPLANE_YSPEED;//下
  66. }else if(!dd&&!du&&dl&&!dr){
  67. x=x-MYPLANE_XSPEED;//左
  68. }else if(!dd&&!du&&!dl&&dr){
  69. x=x+MYPLANE_XSPEED;//右
  70. }else if(!dd&&du&&dl&&!dr){
  71. y=y-MYPLANE_YSPEED;//左上
  72. x=x-MYPLANE_XSPEED;
  73. }else if(du&&!dl&&!dd&&dr){
  74. y=y-MYPLANE_YSPEED;//右上
  75. x=x+MYPLANE_XSPEED;
  76. }else if(dd&&!du&&dl&&!dr){
  77. x=x-MYPLANE_XSPEED;//左下
  78. y=y+MYPLANE_YSPEED;
  79. }else if(dd&&!du&&!dl&&dr){
  80. x=x+MYPLANE_XSPEED;//右下
  81. y=y+MYPLANE_YSPEED;
  82. }
  83. if(x+GameBox.PLANE_WIDTH>GameBox.GAME_WIDTH){
  84. x=GameBox.GAME_WIDTH-GameBox.PLANE_WIDTH;
  85. }
  86. if(x<0){
  87. x=0;
  88. }
  89. if(y+GameBox.PLANE_HEIGHT*2>GameBox.GAME_HEIGHT){
  90. y=GameBox.GAME_HEIGHT-GameBox.PLANE_HEIGHT*2;
  91. }
  92. if(y<30){
  93. y=30;
  94. }
  95. }
  96. public int getX() {
  97. return x;
  98. }
  99. public int getY() {
  100. return y;
  101. }
  102. public boolean isLive() {
  103. return live;
  104. }
  105. public void setLive(boolean live) {
  106. this.live = live;
  107. }
  108. }
复制代码
GameBox.java
  1. package Tools;
  2. import java.awt.Font;
  3. import java.awt.Image;
  4. import java.awt.Toolkit;
  5. import java.util.ArrayList;
  6. import java.util.List;
  7. import GameSubstance.EnemyBullet;
  8. import GameSubstance.Explode;
  9. public class GameBox {
  10. public static int GAME_WIDTH=800;
  11. public static int GAME_HEIGHT=600;
  12. public static Font f1=new Font("oúì?",3,20);
  13. public static Toolkit tk = Toolkit.getDefaultToolkit();
  14. public static Image backgroundimage=tk.createImage("images\±3?°.jpg");
  15. public static Image plane=tk.createImage("images\?ò·?·é?ú.gif");
  16. public static int PLANE_WIDTH=30;
  17. public static int PLANE_HEIGHT=30;
  18. public static Image Explode1=tk.createImage("images/±??¨.gif");
  19. public static int EXPLODE1_WIDTH=30;
  20. public static int EXPLODE1_HEIGHT=30;
  21. public static Image bullet=tk.createImage("images\×óμˉ.gif");
  22. public static int BULLET_WIDTH=18;
  23. public static int BULLET_HEIGHT=35;
  24. public static Image enemy1=tk.createImage("images\μD?ú1.gif");
  25. public static Image enemy2=tk.createImage("images\μD?ú2.gif");
  26. public static int ENEMY_WIDTH=30;
  27. public static int ENEMY_HEIGHT=30;
  28. public static Image enemybullet=tk.createImage("images\·??-μ?×óμˉ.gif");
  29. public static int ENEMYBULLET_WIDTH=15;
  30. public static int ENEMYBULLET_HEIGHT=30;
  31. }
复制代码
StartFrame.java
  1. package View;
  2. import GameSubstance.*;
  3. import Tools.*;
  4. import java.awt.Color;
  5. import java.awt.Font;
  6. import java.awt.Graphics;
  7. import java.awt.Image;
  8. import java.awt.Toolkit;
  9. import java.awt.event.KeyAdapter;
  10. import java.awt.event.KeyEvent;
  11. import java.awt.image.ImageObserver;
  12. import java.util.ArrayList;
  13. import java.util.List;
  14. import java.util.Random;
  15. import javax.swing.ImageIcon;
  16. import javax.swing.JFrame;
  17. import javax.swing.JPanel;
  18. public class StartFrame extends JFrame implements Runnable{
  19. //游戏组件
  20. Image bk=GameBox.backgroundimage;
  21. MyPlane myplane;
  22. List<EnemyPlane> enemyplanelist=new ArrayList<EnemyPlane>();
  23. private int count=0;
  24. private float sourse=0;
  25. List<Bullet> bulletlist=new ArrayList<Bullet>();
  26. List<Explode> explodelist=new ArrayList<Explode>();
  27. List<EnemyBullet> enemybulletlist=new ArrayList<EnemyBullet>();
  28. JPanel jp;
  29. public static void main(String[] args) {
  30. new Thread(new StartFrame()).start();
  31. }
  32. public StartFrame(){
  33. this.setSize(GameBox.GAME_WIDTH,GameBox.GAME_HEIGHT);
  34. //窗口居中
  35. int width=Toolkit.getDefaultToolkit().getScreenSize().width;
  36. int height=Toolkit.getDefaultToolkit().getScreenSize().height;
  37. setLocation(width/2-GameBox.GAME_WIDTH/2, height/2-GameBox.GAME_HEIGHT/2);
  38. setResizable(false);
  39. myplane=new MyPlane(GameBox.GAME_WIDTH/2,GameBox.GAME_HEIGHT-GameBox.PLANE_HEIGHT*2);
  40. new Thread(new AddEnemy()).start();
  41. this.addKeyListener(new KeyMonitor());
  42. this.add(new GameJPanel());
  43. setTitle("打飞机小游戏");
  44. this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
  45. setVisible(true);
  46. }
  47. @Override
  48. public void run() {
  49. while(true){
  50. repaint();
  51. Random rand=new Random();
  52. try {
  53. Thread.sleep(50);
  54. } catch (InterruptedException e) {
  55. e.printStackTrace();
  56. }
  57. }
  58. }
  59. private class KeyMonitor extends KeyAdapter {
  60. public void keyReleased(KeyEvent e) {
  61. if(e.getKeyCode()==KeyEvent.VK_F2){
  62. myplane.setLive(true);
  63. }
  64. myplane.keyReleased(e);
  65. }
  66. public void keyPressed(KeyEvent e) {
  67. myplane.keyPressed(e);
  68. }
  69. }
  70. private class AddEnemy implements Runnable{
  71. @Override
  72. public void run() {
  73. while(true){
  74. try {
  75. Thread.sleep(500);
  76. addEnemyPlane();
  77. } catch (InterruptedException e) {
  78. e.printStackTrace();
  79. }
  80. }
  81. }
  82. private void addEnemyPlane(){
  83. EnemyPlane enemyplane=new EnemyPlane(StartFrame.this);
  84. enemybulletlist.add(new EnemyBullet(enemyplane.getX(),enemyplane.getY()));
  85. enemyplanelist.add(enemyplane);
  86. }
  87. }
  88. private class GameJPanel extends JPanel{
  89. public void paint(Graphics g){
  90. super.paint(g);
  91. g.drawImage(bk,0,0,GameBox.GAME_WIDTH,GameBox.GAME_HEIGHT,null);
  92. myplane.draw(g);
  93. if (count==0){
  94. if(myplane.isLive()){
  95. createBullet();
  96. }else{
  97. for(int i=0;i<bulletlist.size();i++){
  98. bulletlist.remove(i);
  99. }
  100. }
  101. }
  102. count++;
  103. if(count==2){
  104. count=0;
  105. }
  106. for(int i=0;i<bulletlist.size();i++){
  107. Bullet bullet=bulletlist.get(i);
  108. bullet.draw(g);
  109. clearBullet();
  110. }
  111. for(int i=0;i<enemyplanelist.size();i++){
  112. EnemyPlane enemyplane=enemyplanelist.get(i);
  113. enemyplane.draw(g);
  114. clearEnemyplane();
  115. Random rand=new Random();
  116. if(rand.nextInt(100)>93&&enemyplane.isLive()){
  117. enemybulletlist.add(new EnemyBullet(enemyplane.getX(),enemyplane.getY()));
  118. }
  119. //如果敌机面对我方飞机时发射一枚子弹
  120. if(enemyplane.getX()>=myplane.getX()&&enemyplane.getX()<=myplane.getX()+GameBox.PLANE_WIDTH&&enemyplane.isFired()&&enemyplane.isLive()){
  121. enemybulletlist.add(new EnemyBullet(enemyplane.getX(),enemyplane.getY()));
  122. enemyplane.setFired(false);
  123. }
  124. }
  125. for(int i=0;i<enemybulletlist.size();i++){
  126. EnemyBullet eb=enemybulletlist.get(i);
  127. eb.draw(g);
  128. clearEnemyBullet();
  129. }
  130. hitPlane();//攻击敌机
  131. impactMyplane();//敌机与我方飞机发生碰撞
  132. if(enemybulletlist.size()>=1)hitMyplane();//
  133. for(int i=0;i<explodelist.size();i++){
  134. Explode exp=explodelist.get(i);
  135. exp.draw(g);
  136. exp.setCount(exp.getCount()+1);
  137. clearExplode();
  138. }
  139. g.setColor(Color.RED);
  140. // g.setFont(GameBox.f1);//设置字体之后加载窗口会卡2秒钟
  141. if(sourse!=0){
  142. g.drawString("您的得分:"+(int)sourse+"0", 50, 50);
  143. }else{
  144. g.drawString("您的得分:0", 50, 50);
  145. }
  146. }
  147. private void clearEnemyBullet() {
  148. for(int i=0;i<enemybulletlist.size();i++){
  149. if(!(enemybulletlist.get(i).isLive())){
  150. enemybulletlist.remove(i);
  151. }
  152. }
  153. }
  154. private void createBullet(){
  155. Bullet bullet=new Bullet((myplane.getX()+5),(myplane.getY()-30));
  156. bulletlist.add(bullet);
  157. }
  158. private void clearBullet(){
  159. for(int i=0;i<bulletlist.size();i++){
  160. if(!(bulletlist.get(i).isLive())){
  161. bulletlist.remove(i);
  162. }
  163. }
  164. }
  165. private void clearEnemyplane() {
  166. for(int i=0;i<enemyplanelist.size();i++){
  167. if(!(enemyplanelist.get(i).isLive())){
  168. enemyplanelist.remove(i);
  169. }
  170. }
  171. }
  172. private void clearExplode() {
  173. for(int i=0;i<explodelist.size();i++){
  174. if(!(explodelist.get(i).isLive())){
  175. explodelist.remove(i);
  176. }
  177. }
  178. }
  179. private void hitPlane(){
  180. int bx,ex,by,ey;
  181. for(int i=0;i<bulletlist.size();i++){
  182. Bullet bullet=bulletlist.get(i);
  183. bx=bullet.getX();
  184. by=bullet.getY();
  185. for(int j=0;j<enemyplanelist.size();j++){
  186. EnemyPlane enemyplane=enemyplanelist.get(j);
  187. ex=enemyplane.getX();
  188. ey=enemyplane.getY();
  189. if(bx>=ex&&bx<=ex+GameBox.ENEMY_WIDTH&&by>=ey&&by<=ey+GameBox.ENEMY_HEIGHT&&enemyplane.isLive()&&bullet.isLive()){
  190. bullet.setLive(false);
  191. enemyplane.setLive(false);
  192. explodelist.add(new Explode(bx,by));
  193. sourse=sourse+1.168320194f;//防止CE恶意修改分数(我的QQ号码)
  194. }
  195. }
  196. }
  197. }
  198. private void impactMyplane(){
  199. int mx,my,ex,ey;
  200. mx=myplane.getX();
  201. my=myplane.getY();
  202. for(int i=0;i<enemyplanelist.size();i++){
  203. EnemyPlane enemyplane=enemyplanelist.get(i);
  204. ex=enemyplane.getX();
  205. ey=enemyplane.getY();
  206. if(mx>=ex&&mx<=ex+GameBox.ENEMY_WIDTH&&my>=ey&&my<=ey+GameBox.ENEMY_HEIGHT&&myplane.isLive()&&enemyplane.isLive()){
  207. myplane.setLive(false);
  208. enemyplane.setLive(false);
  209. explodelist.add(new Explode(mx,my));
  210. explodelist.add(new Explode(ex,ey));
  211. }
  212. }
  213. }
  214. private void hitMyplane() {
  215. int mx,my,ex,ey;
  216. mx=myplane.getX();
  217. my=myplane.getY();
  218. for(int i=0;i<enemyplanelist.size();i++){
  219. EnemyBullet eb=enemybulletlist.get(i);
  220. ex=eb.getX();
  221. ey=eb.getY();
  222. if(ex+GameBox.ENEMYBULLET_WIDTH>=mx&&ex+GameBox.ENEMYBULLET_WIDTH<=mx+GameBox.PLANE_WIDTH&&ey+GameBox.ENEMYBULLET_HEIGHT>=my&&ey+GameBox.ENEMYBULLET_HEIGHT<=my+GameBox.PLANE_HEIGHT&&myplane.isLive()&&eb.isLive()){
  223. myplane.setLive(false);
  224. eb.setLive(false);
  225. explodelist.add(new Explode(mx,my));
  226. }
  227. }
  228. }
  229. }
  230. }
复制代码
爆炸.gif 背景.jpg [图片] 敌机1.gif [图片] 敌机2.gif [图片] 愤怒的子弹.gif [图片] 子弹.gif

最新评论

小黑屋|在路上 ( 蜀ICP备15035742号-1 

;

GMT+8, 2025-7-8 12:05

Copyright 2015-2025 djqfx

返回顶部