200字范文,内容丰富有趣,生活中的好帮手!
200字范文 > 编程小白自学Java开发FlyBird小游戏

编程小白自学Java开发FlyBird小游戏

时间:2020-06-28 10:44:46

相关推荐

编程小白自学Java开发FlyBird小游戏

本人在读大学生一枚,在上个月自学了半个月的Java,现在寒假放假有空闲时间,用三天肝爆出一款之前爆火的小游戏,FlyBird,用的是IDEA开发环境。

参考了B站代码帝国的教学思路,在面向对象和构造方法中花费了不少功夫。废话不多说,以下是代码实现(素材和游戏演示视频在代码后面):

package Game; //游戏入口主类import java.io.IOException;public class GameBegin {public static void main(String[] dd) throws IOException{GameFrame frame= new GameFrame();frame.setVisible(true);GamePanel panel=new GamePanel();frame.add(panel);}}

package Game;//小鸟类import java.awt.image.BufferedImage;import java.util.ArrayList;import java.util.List;public class Bird {BufferedImage img;int x;int y;int w;int h;//小鸟飞行轨迹double v0;double t;double s;double g;List<BufferedImage> list;public Bird(){img=Tools.getImg("../img/bluebird0.png");w=img.getWidth();h=img.getHeight();x=50;y=150;list=new ArrayList<BufferedImage>();for(int i=0;i<4;i++){list.add(Tools.getImg("../img/bluebird"+i+".png"));}v0=4;//初始化速度t=0.21;//时间s=0;//距离g=0.8;//重力}//鸟的翅膀动起来int index=0;public void fly(){if(index>=4){index=0;}img=list.get(index);index++;}//小鸟的落体运动public void move(){s=v0*t;y=(int)(y-s);double v2=v0-g*t;v0=v2;}//小鸟向上运动public void up(){v0=12;}//小鸟与顶部碰撞或地面发生碰撞public boolean hittop(){if(y<=0||y>=559)return true;return false;}//小鸟和柱子之间发生碰撞public boolean hit(Column column){if(x>=column.x-w&&x<=column.x+column.w&&y>=column.y-h+1){return true;}return false;}}

package Game;import java.awt.image.BufferedImage;import java.io.IOException;import java.util.Random;import javax.imageio.ImageIO;//柱子类public class Column {BufferedImage img;int x;int y;int w;int h;int distance;Random ran=new Random();public Column(int i){img=Tools.getImg("../img/pipe1.png");h=img.getHeight();w=img.getWidth()/2;distance=245;x=300;if(i==1){x=300;}else if(i==2){x=300+distance;}y=ran.nextInt(601);while(true){if(y>200){break;}elsey=ran.nextInt(601);}}public void move() {if(x<=-50){x=418;y=ran.nextInt(601);while(true){if(y>200){break;}elsey=ran.nextInt(601);}}x--;}}

package Game;//游戏窗口类import java.io.IOException;import javax.imageio.ImageIO;import javax.swing.JFrame;public class GameFrame extends JFrame{public GameFrame(){setTitle("FlyBird");setSize(418,700);setLocationRelativeTo(null);setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);try {setIconImage(ImageIO.read(this.getClass().getResource("../img/bird0.png")));} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}

package Game;import java.awt.image.BufferedImage;import java.io.IOException;import javax.imageio.ImageIO;//游戏结束类public class GameOver {int x;//X sizeint y;//Y sizeBufferedImage img;int w;//W sizeint h;//H sizepublic GameOver(){img=Tools.getImg("../img/gameover.png");h=img.getHeight();w=img.getWidth();x=200;y=200;}//地面移动的方法public void move(){if(x<=-(w-402)){x=0;}x--;}}

package Game;//游戏面板类import java.awt.Color;import java.awt.Font;import java.awt.Graphics;import java.awt.Image;import java.awt.event.MouseAdapter;import java.awt.event.MouseEvent;import java.awt.image.BufferedImage;import java.io.IOException;import javax.imageio.ImageIO;import javax.swing.JPanel;public class GamePanel extends JPanel{BufferedImage bg;Ground ground;//声明一个用于存放地面图片的变量。Column column;//声明一个用于存放柱子图片的变量。Column column2;//声明一个用于存放d第二根柱子图片的变量。Bird bird;boolean start;boolean gameOver;int score;int best;public GamePanel(){//声明背景颜色setBackground(Color.white);//设置背景图片bg=Tools.getImg("../img/yourname.jpg");//初始化地面对象ground=new Ground();//初始化柱子对象column=new Column(1);//初始化第二根柱子对象column2=new Column(2);//初始化小鸟对象bird=new Bird();//初始化结束画面//初始化鼠标监听器MouseAdapter adapter=new MouseAdapter(){@Overridepublic void mouseClicked(MouseEvent e) {// TODO Auto-generated method stubsuper.mouseClicked(e);if(start==false){start=true;start();}else if(gameOver){start=false;gameOver=false;bg=Tools.getImg("../img/yourname.jpg");//初始化地面对象ground=new Ground();//初始化柱子对象column=new Column(1);//初始化第二根柱子对象column2=new Column(2);//初始化小鸟对象bird=new Bird();//初始化结束画面score=0;repaint();}else {bird.up();}}};this.addMouseListener(adapter);}//开始页面public void start(){MyThread mt=new MyThread();Thread t=new Thread(mt);t.start();}@Overridepublic void paint(Graphics g) {// TODO Auto-generated method stubsuper.paint(g);g.drawImage(bg,0,0,null);g.drawImage(column.img,column.x,column.y,null);g.drawImage(column2.img,column2.x,column2.y,null);g.drawImage(ground.img,ground.x,ground.y,null);g.drawImage(bird.img,bird.x,bird.y,null);if(start==false)g.drawImage(Tools.getImg("../img/start3.png"),150,230,null);if(gameOver){g.drawImage(Tools.getImg("../img/gameover3.png"),90,200,null);Font b=new Font("宋体",Font.BOLD,30);g.setFont(b);g.setColor(Color.white);if(score>best){best=score;}g.drawString("最高纪录:"+best, 102, 300);}Font f=new Font("宋体",Font.BOLD,20);g.setFont(f);g.setColor(Color.LIGHT_GRAY);g.drawString("分数:"+score, 15, 30);}class MyThread implements Runnable{@Overridepublic void run() {// TODO Auto-generated method stubwhile(true){ground.move();column.move();column2.move();bird.fly();bird.move();boolean bool=bird.hittop();boolean bool1=bird.hit(column);boolean bool2=bird.hit(column2);if(bool){gameOver=true;return;}if(bool1){gameOver=true;return;}if(bool2){gameOver=true;return;}if(bird.x==column.x+column.w||bird.x==column2.x+column2.w)score++;try {Thread.sleep(4);repaint();} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();} }}}}

package Game;import java.awt.image.BufferedImage;import java.io.IOException;import javax.imageio.ImageIO;//地面类public class Ground {int x;//X sizeint y;//Y sizeBufferedImage img;int w;//W sizeint h;//H sizepublic Ground(){img=Tools.getImg("../img/land.png");h=img.getHeight();w=img.getWidth();x=0;y=578;}//地面移动的方法public void move(){if(x<=-(w-401)){x=0;}x--;}}

package Game;//工具类import java.awt.image.BufferedImage;import java.io.IOException;import javax.imageio.ImageIO;public class Tools {public static BufferedImage getImg(String path){BufferedImage img = null;try {img=ImageIO.read(Tools.class.getResource(path));} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}return img;}}

以下是游戏体验视频的链接和素材图片:

游戏视频已上传B站:https://b23.tv/Eetb

游戏素材:

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。