package com.qiku.bard;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.IOException;
/**
* 游戏启动类
* 通过extends关键字,继承JPanel画板类==> 于是Birdgame就具备了画板类的功能
*/
public class Birdgame extends JPanel {
static BufferedImage bg= null;//背景图片
static BufferedImage ground_image =null;//地面图片
static BufferedImage bird_image =null;//小鸟背景
static BufferedImage column_image =null;//柱子背景
//静态代码块 一般用于加载静态资源(视频,音频,图片等)
static {
//将本地的图片bg,png读取到 程序中的bg
try {
bg = ImageIO.read(Birdgame.class.getResourceAsStream("bg.png"));
ground_image = ImageIO.read(Birdgame.class.getResourceAsStream("ground.png"));
bird_image = ImageIO.read(Birdgame.class.getResourceAsStream("0.png"));
column_image = ImageIO.read(Birdgame.class.getResourceAsStream("column.png"));
} catch (IOException e) {
e.printStackTrace();
}
}
Ground ground;//声明地面
bird Bird;//声明小鸟
Column Colum1;//声明柱子1
Column Colum2;//声明柱子2
//BirdGame的构造方式
public Birdgame(){
ground = new Ground();//创建地面对象
Bird = new bird();//创建小鸟对象
Colum1 = new Column();//创建柱子对象1
Colum2 = new Column();//创建柱子对象2
Colum2.x = Colum1.x + Colum1.distance;//柱子2的坐标=柱子1的坐标的基础上加上244(保持水平间距)
}
/**
* 用于在画板上绘制内容的方法:想在画板上显示什么,在这个方法里面写就行了
* @param g 画笔
*/
@Override
public void paint(Graphics g) {
g.drawImage(bg,0,0,null);
g.drawImage(column_image,Colum1.x,Colum1.y,null);
g.drawImage(column_image,Colum2.x,Colum2.y,null);
//g.setColor(Color.BLACK);//将画笔颜色设置为黑色;
//g.fillRect(0,0,432,644);
g.drawImage(ground_image,ground.x,ground.y,null);//话地面
g.drawImage(Bird.image,Bird.x,Bird.y,null);
}
//游戏控制流程的方法
public void action()throws Exception{
//给当前对象(画板)添加鼠标单击事件
this.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
//小鸟往上飞
Bird.up();
}
});
//死循环:{}内部的代码 会一直反复的执行
while (true){
ground.step();//地面一直一直移动
Colum1.step();
Colum2.step();
Bird.fly();
Bird.down();
// System.out.println(ground.x);
repaint();//重画 即重新执行 paint方法
Thread.sleep(10);
}
}
//main方法 —程序的入口(即:有main方法,程序才能运行)
public static void main(String[] args) throws Exception{
//创建画框对象(即:从Java提供的资源中 获取JFrame)
JFrame frame = new JFrame();
Birdgame game = new Birdgame();//创建画板对象
frame.setSize(432,644);//设置宽高
frame.setLocationRelativeTo(null);//居中显示
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设置关闭窗口的同时使程序结束
frame.setVisible(true); //设置可见性
frame.add(game);//将画板放在画框上
//调用action
game.action();
}
}
package com.qiku.bard;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.IOException;
public class bird {
int x ;//小鸟坐标
int y ;
int width ; //小鸟宽高
int height ;
BufferedImage image ;//小鸟当前状态图片
BufferedImage []images ;//小鸟所有状态的图片
public bird(){
images = new BufferedImage[8] ;//初始化数组容量 该数组能一下保存8张图片
for(int i = 0 ; i < images.length;i ++){
try {
images[i] = ImageIO.read(bird.class.getResourceAsStream( i+".png"));
} catch (IOException e) {
e.printStackTrace();
}
}
image = Birdgame.bird_image;
width = image.getWidth();
height = image.getHeight();
x = 200;
y = 200;
}
//小鸟飞翔的方法
int index = 0 ;
public void fly(){
image = images[index % images.length];
index ++;
}
int g = 6 ;//重力加速度
double t = 0.18 ;//下落时间
double v = 0 ;//初速度
double h = 0 ;//下落距离
//小鸟下落一次
public void down(){
h = v * t + g * t * t / 2 ;//具体下落的距离
v = v + g * t ;//末速度=当前速度+重力加速度*时间
y= y + (int)h ;
}
public void up(){
//给一个负方向的初速度即可
v = -30 ;
}
}
package com.qiku.bard;
/**
* 柱子
*/
import java.awt.image.BufferedImage;
public class Column {
int x ;
int y ;
int width ;
int height ;
BufferedImage image ;
int gap ;//上下柱子之间的间隙
int distance ;//左右柱子之间的间距
int min = -(1200 / 2 - 144 / 2);
int max = 644 - 146 - 144 / 2 - 1200 / 2;
public Column(){
image = Birdgame.column_image;
width = image.getWidth();
height = image.getHeight();
y = (int)( Math.random() * (max - min)+min);
x = 432 ;
gap = 144;
distance= 244;
}
public void step(){
x = x -1 ;
if(x <= - width){
x = 432;
//重新随机纵坐标
y = (int)(Math.random() *(max - min) + min);
}
}
}
package com.qiku.bard;
import java.awt.image.BufferedImage;
/**
* 地面类
*/
public class Ground {
int x ;//地面坐标
int y ;
int width ; //地面宽高
int height ;
BufferedImage image ;//地面图片
public Ground(){
image = Birdgame.ground_image ;
x = 0 ;
y = Birdgame.bg.getHeight()-image.getHeight() ;
width = image.getWidth();
height = image.getHeight();
}
/**
* 地面走一步的方法
*/
public void step(){
x = x - 1;
if(x <= 432 - width ){
x = 0 ;
}
}
}
简单易懂的小游戏(不来试试吗?)
最新推荐文章于 2024-07-15 22:27:12 发布