欢迎光临
我们一直在努力

【Java实现小游戏】飞翔的小鸟(源码)

游戏玩法:通过鼠标点击使小鸟上下移动穿过柱子并完成得分,小鸟碰到柱子或掉落到地面上都会结束游戏。

(游戏内图片)

【Java实现小游戏】飞翔的小鸟(源码)

【Java实现小游戏】飞翔的小鸟(源码)

下面是实现这个游戏的代码:

【Java实现小游戏】飞翔的小鸟(源码)

brid类:

package bird; import org.omg.CORBA.IMP_LIMIT; import javax.imageio.ImageIO; import java.awt.image.BufferedImage; public class Brid { int x,y; int width,height; int size;//鸟的大小,用于检测碰撞 BufferedImage image; BufferedImage[] images; int index; double g; //重力加速度 double t; //两次位置的时间间隔 double v0; //初始上抛的速度 double speed; //当前上抛的速度 double s; //经过时间t以后的位移 double alpha; //鸟的倾角,单位是弧度 public Brid() throws Exception { image = ImageIO.read(getClass().getResource("0.png")); width = image.getWidth(); height = image.getHeight(); x = 132; y = 280; size = 40; g = 4; v0 = 20; t = 0.25; speed = v0; s = 0; alpha = 0; images = new BufferedImage[8]; for (int i = 0; i < 8; i++) { images[i] = ImageIO.read(getClass().getResource(i + ".png")); } index=0; } public void fly(){ index++; image=images[(index/12)%8]; } public void step(){ double v0=speed; s=v0*t+g*t*t/2; //计算上抛运动的位移; y=y-(int)s;//计算鸟的位置坐标 double v =v0-g*t;//计算下次的速度 speed=v; alpha=Math.atan(s/8); } public void flappy(){ speed=v0; //重新设置初始速度,重新向上飞 } public boolean hit( Ground ground){ boolean hit=y+size/2>ground.y; if(hit){ y= ground.y-size/2; alpha=-3.1415926/2; } return hit; } public boolean hit(Column column) { if (x > column.x - column.width / 2 - size / 2 &amp;& x < column.x + column.width / 2 + size / 2) { if (y > column.y - column.gap / 2 + size / 2 && y < column.y + column.gap / 2 - size / 2) { return false; } return true; } return false; } } 

BridGame类:

package bird; import javax.imageio.ImageIO; import javax.swing.*; import java.awt.*; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.awt.image.BufferedImage; public class BridGame extends JPanel { Brid brid; Column column1,column2; Ground ground; BufferedImage background; int score; // boolean gameOver; //游戏状态 int state; public static final int START=0; public static final int RUNNING=1; public static final int GAME_OVER=2; BufferedImage gameOverImage; BufferedImage startImage; public BridGame() throws Exception{ //gameOver=false; state=START; gameOverImage =ImageIO.read(getClass().getResource("gameover.png")); startImage=ImageIO.read(getClass().getResource("start.png")); brid=new Brid(); column1=new Column(1); column2=new Column(2); ground=new Ground(); background= ImageIO.read(getClass().getResource("bg.png")); score=0; } @Override public void paint(Graphics g) { super.paint(g); g.drawImage(background,0,0,null); g.drawImage(column1.image,column1.x-column1.width/2,column1.y-column1.height/2,null); g.drawImage(column2.image,column2.x-column2.width/2,column2.y-column2.height/2,null); g.drawImage(ground.image,ground.x,ground.y,null); Graphics2D g2=(Graphics2D) g; g2.rotate(-brid.alpha,brid.x,brid.y); g.drawImage(brid.image,brid.x-brid.width/2,brid.y-brid.width/2,null); g2.rotate(brid.alpha,brid.x,brid.y); Font f=new Font(Font.SANS_SERIF,Font.BOLD,40); g.setFont(f); g.drawString(""+score,40,60); g.setColor(Color.white); g.drawString(""+score,40-3,60-3); switch (state){ case GAME_OVER: g.drawImage(gameOverImage,0,0,null); break; case START: g.drawImage(startImage,0,0,null); break; } } public void action() throws Exception{ //让地面动起来 MouseListener l=new MouseAdapter(){ @Override public void mousePressed(MouseEvent e) { //鼠标按下 // brid.flappy(); //鸟向上飞 try { switch (state) { case GAME_OVER: column1 = new Column(1); column2 = new Column(2); brid = new Brid(); score = 0; state = START; break; case START: state = RUNNING; case RUNNING: brid.flappy(); } }catch (Exception e2){ } } }; addMouseListener(l); while(true){ switch (state){ case START: brid.fly(); ground.step(); break; case RUNNING: ground.step(); column1.step(); column2.step(); brid.step(); brid.fly(); if(brid.x==column1.x||brid.x==column2.x){ score++; } if(brid.hit(ground)||brid.hit(column1)||brid.hit(column2)){ state=GAME_OVER; } break; } repaint(); Thread.sleep(1000/30); } } public static void main(String[] args) throws Exception { JFrame frame=new JFrame(); BridGame game=new BridGame(); frame.add(game); frame.setSize(440,670); frame.setLocationRelativeTo(null); frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); game.action(); } } 

Column类:

package bird; import javax.imageio.ImageIO; import java.awt.*; import java.awt.font.LineMetrics; import java.awt.image.BufferedImage; import java.util.Random; //柱子 public class Column { int x,y; int width,height; int gap; int distance; BufferedImage image; Random random=new Random(); public Column(int n) throws Exception{ image= ImageIO.read(getClass().getResource("column.png")); width=image.getWidth(); height =image.getHeight(); gap=144; distance=245; x=550+(n-1)*distance; y=random.nextInt(218)+132; } public void step(){ x--; if(x==-width/2){ x=distance*2-width/2; y=random.nextInt(218)+132; } } } 

Ground类:

package bird; import javax.imageio.ImageIO; import java.awt.image.BufferedImage; public class Ground { int x,y; int width,height; BufferedImage image; public Ground() throws Exception{ image= ImageIO.read(getClass().getResource("ground.png")); width=image.getWidth(); height =image.getHeight(); x=0; y=500; } //添加方法让地面移动 public void step(){ x--; if(x==-109){ x=0; } } } 

  • 海报
海报图正在生成中...
赞(0) 打赏
声明:
1、本博客不从事任何主机及服务器租赁业务,不参与任何交易,也绝非中介。博客内容仅记录博主个人感兴趣的服务器测评结果及一些服务器相关的优惠活动,信息均摘自网络或来自服务商主动提供;所以对本博客提及的内容不作直接、间接、法定、约定的保证,博客内容也不具备任何参考价值及引导作用,访问者需自行甄别。
2、访问本博客请务必遵守有关互联网的相关法律、规定与规则;不能利用本博客所提及的内容从事任何违法、违规操作;否则造成的一切后果由访问者自行承担。
3、未成年人及不能独立承担法律责任的个人及群体请勿访问本博客。
4、一旦您访问本博客,即表示您已经知晓并接受了以上声明通告。
文章名称:《【Java实现小游戏】飞翔的小鸟(源码)》
文章链接:https://www.456zj.com/37257.html
本站资源仅供个人学习交流,请于下载后24小时内删除,不允许用于商业用途,否则法律问题自行承担。

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址