package com.lemon.flappybird;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
/**
* 初始世界,包含背景图片,开始图片
* @author D
*
*/
public class World extends JPanel{
BufferedImage backgroud;//背景图片
BufferedImage startImg,startImg2;//开始图片
Column column1;Column column2;boolean start;
Bird bird;
Ground ground;//地板
boolean gameover;
int score=1;; int game=1;;int score_ji;int game_ji;
public World() throws IOException{//在构造方法中对图片进行加载
backgroud = ImageIO.read(getClass().getResource("bg.png"));
startImg = ImageIO.read(getClass().getResource("start.png"));
startImg2 = ImageIO.read(getClass().getResource("start2.png"));
ground =new Ground();
column1=new Column(388);//柱子开始出现位置
column2=new Column(560);
bird =new Bird(120,225);
}
public void paint(Graphics g) {//绘制背景图片
if (score%3==0&&score!=score_ji) {
score_ji=score;
game++;
}
g.drawImage(backgroud, 0, 0, null);
if (start==true) {
column1.paint(g);
column2.paint(g);
bird.paint(g);
}
g.setFont(new Font(Font.MONOSPACED, Font.BOLD, 20));
g.setColor(Color.WHITE);
g.drawString("关卡:"+game, 30, 20);g.drawString("分数:"+score, 30, 50);
if (gameover) {
try {
BufferedImage end =ImageIO.read(getClass().getResource("gameover.png"));
g.drawImage(end, 38, 210, null);
return;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}if (start==false) {
g.drawImage(startImg, 88, 250, null);
g.drawImage(startImg2, 46, 180, null);
}
ground.paint(g);//绘制地面
}
public void action() throws InterruptedException {//定义一个方法,调用Ground类中的step()方法,让x--,看起来像动
addMouseListener(new MouseListener() {
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent e) {
if (gameover==true) {//游戏结束,初始化世界
try {
score =0;
if (game!=1) {
game--;
}
ground =new Ground();
column1=new Column(388);//柱子开始出现位置
column2=new Column(560);
bird =new Bird(120,225);
start = false;
gameover =false;
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
start =true;
bird.flyappy();
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
}
});
while (true) {
ground.step();
if (start==true) {//游戏开始后绘画
column1.step();
column2.step();
bird.step();
if (bird.hit(column1,column2,ground)) {
start=false;
gameover = true;
}
if (bird.pass(column1, column2)) {
score++;
}
}
repaint();//重绘
Thread.sleep(1000/game*2/60);//每1/60重绘一次
}
}
public static void main(String[] args) throws IOException, InterruptedException {
JFrame frame =new JFrame("FlappyBird游戏");//创建窗口
World world= new World();//创建面板
frame.setSize(288, 512);//对应背景图片大小
frame.setResizable(false);//设置不能调大小
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设置关闭时结束游戏
frame.setLocationRelativeTo(null);//让窗口出现在屏幕最中心
frame.add(world);
frame.setVisible(true);
world.action();//加载让地板重绘的方法
}
}
局中棋
- 粉丝: 1
- 资源: 1
最新资源
- 光纤到户及通信基础设施报装申请表.docx
- 踝关节功能丧失程度评定表.docx
- 环保设施投资估算表.docx
- 既有建筑物通信报装申请表.docx
- 既有建筑物通信报装现场查勘报告.docx
- 监督机构检查记录表.docx
- 肩关节功能丧失程度评定表.docx
- 大学生创新创业训练计划大创项目的全流程指南
- 简易低风险工业厂房通信报装申请表.docx
- 建设工程消防验收各阶段意见回复表.docx
- 建设工程消防验收模拟验收意见表.docx
- 建设工程消防验收图纸核查意见表.docx
- 建设工程消防验收现场指导意见表.docx
- 建筑工程竣工验收消防设计质量检查报告(表格填写模板).docx
- 建筑工程消防查验意见和结论.docx
- 建筑工程消防施工竣工报告(表格填写模板).docx
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈