当前位置: 首页 > news >正文

JAVA小游戏“飞翔的小鸟”

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

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

再创建一个包  来存储照片

如下:

package game;
import java.awt.*;
import javax.swing.*;
import javax.imageio.ImageIO;public class Bird {Image image;int x,y;int width,height;int size;double g;double t;double v0;double speed;double s;double alpha;//֡Image[] images;int index;public Bird() throws Exception{image=new ImageIcon("source/0.png").getImage();width = image.getWidth(null);height = image.getHeight(null);x=132;y=280;size=40;g=4;v0=20;t=0.25;speed=v0;s=0;alpha=0;images=new Image[8];for(int i=0;i<8;i++){images[i]=new ImageIcon("source/"+i+".png").getImage();}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=Math.PI/2;}return hit;}public boolean hit(Column column){if(x>column.x-column.width/2-size/2&&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;}
}
package game;import javax.imageio.ImageIO;
import java.util.*;import javax.swing.*;import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import javax.imageio.*;
import java.awt.*;public class BirdGame extends JPanel {Image background;Image startImage;Image overImage;Ground ground;//����Column column1,column2;Bird bird;int score;int state;//״̬//״̬����public static final int START=0;public static final int RUNNING=1;public static final int GAME_OVER=2;public BirdGame() throws Exception{background = new ImageIcon("./source/bg.png").getImage();startImage = new ImageIcon("./source/start.png").getImage();overImage=new ImageIcon("./source/gameover.png").getImage();//״̬ground=new Ground();column1=new Column(1);column2=new Column(2);bird=new Bird();score=0;state=0;}public void paint(Graphics g){g.drawImage(background, 0, 0,null);g.drawImage(ground.image, ground.x, ground.y, 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);Graphics2D g2=(Graphics2D) g;g2.rotate(-bird.alpha,bird.x,bird.y);g.drawImage(bird.image,bird.x-bird.width/2,bird.y-bird.height/2,null);g2.rotate(bird.alpha,bird.x,bird.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 START:g.drawImage(startImage, 0, 0, null);break;case GAME_OVER:g.drawImage(overImage, 0, 0, null);break;}}public void action() throws Exception{MouseListener l=new MouseAdapter(){public void mousePressed(MouseEvent e){try {switch(state) {case START://״̬state=RUNNING;break;case RUNNING:bird.flappy();break;case GAME_OVER:column1=new Column(1);column2=new Column(2);bird=new Bird();score=0;state=START;break;}}catch (Exception ex){ex.printStackTrace();}}};addMouseListener(l);while(true){switch(state){case START:bird.fly();ground.step();break;case RUNNING:ground.step();column1.step();column2.step();bird.fly();bird.step();score++;//if(bird.hit(ground)||bird.hit(column1)||bird.hit(column2)){state=GAME_OVER;}break;}	Thread.sleep(1000/60);repaint();}}public static void main(String[] args) throws Exception{JFrame frame=new JFrame();BirdGame game=new BirdGame();frame.add(game);frame.setSize(440,670);frame.setLocationRelativeTo(null);frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frame.setVisible(true);game.action();}}
package game;import java.util.*;
import java.awt.*;import javax.imageio.ImageIO;
import javax.swing.*;public class Column {Image image;int x,y;int width,height;int gap;int distance;Random random =new Random();public Column(int n) throws Exception{image=new ImageIcon("source/column.png").getImage();width=image.getWidth(null);height=image.getHeight(null);gap=144;distance=245;x=550+(n-1)*distance;y=random.nextInt(218)+132;}public void step(){x-=4;if(x<= -width/2){x=distance*2-width/2;y=random.nextInt(218);}}
}
package game;import javax.swing.*;
import java.awt.*;public class Ground {Image image;int x,y;int width,height;public Ground() throws Exception{image =new ImageIcon("source/ground.png").getImage();width=image.getWidth(null);height=image.getHeight(null);x=0;y=500;}public void step(){x-=4;if(x<=-109){x=0;}}}

http://www.lryc.cn/news/241693.html

相关文章:

  • 1410. HTML 实体解析器 --力扣 --JAVA
  • 【开源】基于Vue.js的海南旅游景点推荐系统的设计和实现
  • 机器学习【01】相关环境的安装
  • 微服务实战系列之签名Sign
  • 家用小型洗衣机哪款性价比高?口碑最好迷你洗衣机排行榜
  • 企业远程访问业务系统:对比MPLS专线,贝锐蒲公英为何更优优势?
  • Maven项目下详细的SSM整合流程
  • Linux 设置文件开启数量限制
  • 详解Java中的异常体系机构(throw,throws,try catch,finally)
  • 【jvm】虚拟机之堆
  • 二百零五、Flume——数据流监控工具Ganglia单机版安装以及使用Ganglia监控Flume任务的数据流(附流程截图)
  • 解决Resolving Android Dependencies问题
  • 深度学习技术前沿:探索与挑战
  • 2023-11-24--oracle--实验--[Merge 语句]
  • 2023亚太杯C题30页高质量word成品论文+五小问py代码+数据集+运行结果图
  • 【docker】安装redis和mysql生产实战
  • 【nlp】3.1 Transformer背景介绍及架构
  • 喜报!美格智能连续五年荣获物联网100强企业
  • 『Confetti 喜庆散花插件の使用』
  • Axios 通过a标签下载文件 跨域下载
  • VR全景展示,“超前点播”打开娱乐行业线上营销门户
  • Mac安装配置typescript及在VSCode上运行ts
  • 举个栗子!Tableau 技巧(260):文本表中省市县数据的灵活逐级下钻「方法二」
  • 软著项目推荐 深度学习 opencv python 实现中国交通标志识别
  • 【Java基础】Java导Excel攻略
  • maven打包可执行jar含依赖lib
  • 虾皮知虾数据分析软件:优化您的电商经营
  • uniapp开发小程序-如何判断小程序是在手机端还是pc端打开
  • 7-tcp 三次握手和四次挥手、osi七层协议,哪七层,每层有哪些?tcp和udp的区别?udp用在哪里了?
  • DELPHI开发APP回忆录二安卓与pc端路径的选择