Flappy Bird java 源代码

Flappy Bird java 源代码


2024年5月4日发(作者:)

package mcnk;

import ;

import ;

import cs;

import cs2D;

import dapter;

import vent;

import istener;

import edImage;

import ption;

import ;

import O;

import ;

import ;

import layer;

import tream;

/**

* FlappyBird

* @author mcnk

* @version 1.0

*/

public class FlappyBird {

public static void main(String[] args) throws Exception {

JFrame frame = new JFrame("我是一只小小小小鸟~!");

World world = new World();

(world);

e(432,644+30);

ible(true);

//快捷键: 代码快速提示 alt + /

izable(false);//是否重设大小

ationRelativeTo(null);//设置位置

aultCloseOperation(_ON_CLOSE);//关闭操作

();

}

}

/**

* 游戏界面(鸟活动的世界)

* @author mcnk

* 快捷键: alt + ↑(↓) 移动代码

* JPanel: 面板

* 快捷键: ctrl + shift + o 导入(引入)包

*/

class World extends JPanel{

BufferedImage background;//图像缓存区,保存图片

Ground ground;

Column column1;

Column column2;

Bird bird;

boolean gameover;

BufferedImage gameoverImage;

BufferedImage startedImage;

int score;

boolean start;

public World() throws IOException{

background = (getClass().getResource(""));

gameoverImage = (getClass().getResource(""));

startedImage = (getClass().getResource(""));

start();

}

public void start() throws IOException{

ground = new Ground();

column1 = new Column(1);

column2 = new Column(2);

bird = new Bird();

gameover = false;

score = 0;

start = false;

}

public void paint(Graphics g){

age(background,0,0,null);

age(,column1.x - /2, column1.y

/2,null);

age(,column2.x - /2, column2.y

/2,null);

age(,ground.x,ground.y,null);

or();

Font font = new Font(_SERIF,,30);

t(font);

-

-

ring(score+"",20,40);

Graphics2D g2d = (Graphics2D)g;

(-, bird.x, bird.y);//旋转坐标系

age(,bird.x - /2, bird.y - /2, null);

(, bird.x, bird.y);

if(gameover){//绘制结束背景

age(gameoverImage,0,0,null);

}

if(!start){//绘制开始背景

age(startedImage,0,0,null);

}

}

public void action() throws Exception{

//定义鼠标监听器(匿名内部类)

MouseListener l = new MouseAdapter(){

public void mousePressed(MouseEvent e) {

if(gameover){

try {

start();

} catch (IOException e1) {

tackTrace();

}

}else{

start = true;

();

}

}

};

addMouseListener(l);//绑定监听器

//添加背景音乐

new Thread(){

public void run(){

AudioStream as;

try {

as = new AudioStream(getClass().getResourceAsStream(""));

(as);

} catch (IOException e) {

tackTrace();

}

}

}.start();

while(true){

if(!gameover){

if(start){

();

();

();

}

();

();

if(bird.x == column1.x || bird.x == column2.x){

score++;

}

}

if((ground) || (column1) || (column2)){

gameover = true;

}

repaint();//尽快重新调用paint()绘图

(1000/100);

}

}

}

/**

* 地面

* @author mcnk

*

*/

class Ground{

BufferedImage image;

int x;

int y;

public Ground() throws IOException{

}

/**

image = (getClass().getResource(""));

x = 0;

y = 500;

}

//地面移动

public void step(){

x--;

if(x <= -108){

x = 0;

}

}

* 柱子

* @author mcnk

*

*/

class Column{

BufferedImage image;

int x; //中心点X坐标

int y; //中心点Y坐标

int width;

int height;

int gap; //柱子缝隙

int distance;//柱子与柱子之间间距

Random random = new Random();

public Column(int num) throws IOException{

image = (getClass().getResource(""));

width = th();

height = ght();

gap = 144;

distance = 245;

x = (num -1) * distance + (432+width/2);

y = t(250) + 132;//[132, 132+279]

}

public void step(){

x--;

if(x <= -width/2){

x = 2 * distance - width/2;

y = t(250) + 132;

}

}

}

/**

* 小鸟

* @author mcnk

*

*/

class Bird{

BufferedImage[] images;//保存鸟的所有活动状态

BufferedImage image;//保存当前鸟的状态

int x;

int y;

int width;

int height;

int size;

double g;//重力加速度

double h;//经过时间t秒后的垂直位移

double s;//经过时间t秒后的水平位移

double t;//时间

double speed;//速度(初始速度:V0/经过时间t秒后的速度:Vt)

double alpha;//倾角

int index;//当前图片的下标

public Bird() throws IOException{

images = new BufferedImage[8];

for(int i=0; i<8; i++){// 8 ->

images[i] = (getClass().getResource(i+".png"));

}

image = images[0];

width = th();

height = ght();

x = 132;

y = 280;

size = 40;

g = 4;

h = 0;

s = 0;

speed = 20;

t = 0.25;

alpha = 0;

index = 0;

}

public void step(){

double v0 = speed;

h = v0*t - 0.5*g*t*t;

double vt = v0 - g*t;

s = v0 * t;

y = y - (int)h;

speed = vt;

alpha = (h/8);

}

//鸟翅膀挥动

public void fly(){

index++;

image = images[(index/8)%8];

//0 1 2 3 4 5 6 7 8 9 10

///8

//0 0 0 0 0 0 0 0 1 1 1 1 1 ...

//%8

//0 0 0 0 0 0 0 0 1 1 1 1 1

}

public void flappy(){

speed = 20;

}

//鸟撞击地面检查

public boolean hit(Ground ground){

boolean hit = (y + size/2 >= ground.y);

if(hit){

alpha = -/2;

}

return hit;

}

//鸟撞击到柱子的检查

public boolean hit(Column column){

int x1 = column.x - /2 - size/2;

int x2 = column.x + /2 + size/2;

int y1 = column.y - /2 + size/2;

int y2 = column.y + /2 - size/2;

if(x > x1 && x < x2){

if(y > y1 && y < y2){//除去柱子的间隙

}

return false;

}

return true;

}

return false;

}


发布者:admin,转转请注明出处:http://www.yc00.com/web/1714817250a2521731.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信