java写了一个简单的飞翔的小鸟小游戏

最近在学java 只是入了个们,所以写了一个飞行的小鸟小游戏,不多说,直接上代码
Birt.java

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

public class Birt
{
double g;//重力加速度
double t;//运动时间
double v0;//初始上跑速度
double speed;//当前速度
double s;

double a;//角度


int index=0;//当前图片序号
int size=40;
int x,y;//鸟的位置,这个位置是鸟的中心位置
double angle;//飞行角度 


BufferedImage image;//鸟当前图片
BufferedImage images[];//鸟的动画帧图片


public Birt()throws IOException
{
	this.g=4;
	this.t=0.25;
	this.v0=20;
	
	
	
	x=132;y=280;
	images=new BufferedImage[8];
	for(int i=0;i<8;i++)
	{
		images[i]=ImageIO.read(new File(i+".png"));
	}
	image=images[0];
	
	
}
public void fly() 
{//切换动画帧,扇翅膀
	index++;
    image = images[(index/8)%8];
}

public void step()//飞行一步,下降v=v0-gt  s=v0*t-0.5*g*t*t
{
	double v1=speed;//本次运行速度
	double v2=v1-g*t;//飞行一段时间速度
	speed=v2;//作为下次运算的初始速度
	s=v1*t-0.5*g*t*t;
	y=y-(int)s;
	
    a=-Math.atan(s/8);
}
public void flappy()//飞扬
{
	speed=v0;
}




public boolean pass(Coloumn coloumn1, Coloumn coloumn2) {
	return coloumn1.x==x||coloumn2.x==x;
}
public boolean hit(Coloumn coloumn1, Coloumn coloumn2, Ground ground) {
	  //碰到地面
	if(y-size/2 >= ground.y){
		return true;
	}
	//碰到柱子
	return hit(coloumn1) || hit(coloumn2);
}
public boolean hit(Coloumn col)
{
	//如果鸟碰到柱子: 鸟的中心点x坐标在 柱子宽度 + 鸟的一半
			if( x>col.x-col.width/2-size/2 && x<col.x+col.width/2+size/2){
				if(y>col.y-col.gap/2+size/2 && y<col.y+col.gap/2-size/2 ){
					return false;
				}
				return true;
			}
			return false;
}
public synchronized void paint(Graphics g) {//一种锁的机制
	Graphics2D g2=(Graphics2D)g;
    g2.rotate(a,this.x,this.y);
	
	int x=this.x-image.getWidth()/2;
	int y=this.y-image.getHeight()/2;
	g2.drawImage(image, x, y,null);
	g2.rotate(-a,this.x,this.y);
}

}
Coloumn.java

import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Random;

import javax.imageio.ImageIO;

public class Coloumn {
BufferedImage image;//贴图
int x,y;//作为柱子的中心点
int width;
int height;
int gap=144;
int distance=245;
Random random=new Random();
public Coloumn(int n) {
try {
image=ImageIO.read(new File(“column.png”));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
width=image.getWidth();
height=image.getHeight();
x=distancen+432+80;
y=random.nextInt(120)+220;
}
public void step()
{
x–;
if(x<=-width/2)
{
x=distance
2-width/2;
y=random.nextInt(120)+220;
}

}
public void paint(Graphics g)
{
	g.drawImage(image, x-width/2, y-height/2,null);
}

}
Ground.java

import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

public class Ground {//地面
BufferedImage image;
int x,y;
public Ground()
{
y=500;
x=0;
try {
image=ImageIO.read(new File(“ground.png”));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void step()
{
x–;
if(x<=-111)
x=0;
}
public void paint(Graphics e)
{
e.drawImage(image, x, y,null);
}

}
Sky.java

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Sky extends JPanel{
Birt birt;//天空上有一只鸟
Ground ground;//地面
Coloumn coloumn1;//柱子
Coloumn coloumn2;

BufferedImage background;//背景图片
BufferedImage gameOverImg;//结束图片
BufferedImage startImg;//开始图片

//状态量
boolean start;
boolean hameOver;
int score;
public Sky() throws IOException
{
	
	
		try
	    {
			background=ImageIO.read(new File("bg.png"));
			gameOverImg=ImageIO.read(new File("gameover.png"));
			startImg=ImageIO.read(new File("start.png"));
		
	    }
	   catch(IOException e)
	   {
		   e.printStackTrace();
	   }
		start();
}

public void  start()//开始游戏的方法,初始化数据
{
	try
	{   start=false;
	    hameOver=false;
	    score=0;
		
		birt=new Birt();//鸟
		ground=new Ground();//地面
		coloumn1=new Coloumn(1);//柱子
		coloumn2=new Coloumn(2);
	} 
	catch (IOException e) 
	{
		e.printStackTrace();
	}
}
public void action()throws Exception//启动游戏
{
	this.addMouseListener(new MouseAdapter() {
		public void mousePressed(MouseEvent e) {
			if(hameOver){
				start();
				return;
			}
			start=true;
			birt.flappy();
		}
	});
	requestFocus();//请求标点
	
	
	while(true)
	{
		if(start&&!hameOver)
		{
			birt.step();//鸟下落
			coloumn1.step();//柱子移动
		    coloumn2.step();
		    if(birt.pass(coloumn1,coloumn2))//鸟通过柱子
		    {
		    	score++;
		    }
		    if(birt.hit(coloumn1,coloumn2,ground))//判读鸟是否撞击
		    {
		    	start=false;
		    	hameOver=true;
		    }
		}
		
		
		if(!hameOver)//如果碰到柱子,定格
		{
			birt.fly();//鸟扇翅膀
		}
		
		ground.step();//地面移动
		
		repaint();//重组指面板
		Thread.sleep(10);
	}
	
}


public void paint(Graphics g)
{
	super.paint(g);
	
	
	
	g.drawImage(background,0,0,null);
	birt.paint(g);
	coloumn1.paint(g);
	coloumn2.paint(g);
	ground.paint(g);
	//绘制分数
	
	Font font=g.getFont();
	font=new Font(font.getName(),Font.BOLD,35);
	g.setFont(font);
	
	g.setColor(Color.WHITE);
	g.drawString("SCORE:"+score, 30, 60);
	if(hameOver)
	{
		g.drawImage(gameOverImg, 0, 0, null);
		return;
	}
	if(!start)
	{
		g.drawImage(startImg, 0, 0,null);
	}
	
	
}




public static void main(String args[])throws Exception
{
	JFrame frame=new JFrame("飞扬的小鸟");
	Sky sky=new Sky();
	frame.add(sky);
	frame.setSize(432+8, 644+30);
	frame.setResizable(false);
	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	frame.setLocationRelativeTo(null);//居中
	frame.setVisible(true);
	sky.action();
}

}
下面是运行图片
这是主界面

这是图片1 接下来按顺序
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

好的,以下是一个简单Java代码示例,实现了一个飞翔小鸟小游戏: ```java import java.util.Scanner; public class FlyingBirdGame { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int height = 10; int velocity = 0; int gravity = 2; int obstacleDistance = 20; int obstacleHeight = 5; int score = 0; System.out.println("欢迎来到飞翔小鸟游戏!"); while (true) { // 输出当前状态 System.out.println("当前高度:" + height); System.out.println("当前速度:" + velocity); System.out.println("当前得分:" + score); // 判断小鸟是否碰到障碍物 if (obstacleDistance == 0 && height <= obstacleHeight) { System.out.println("游戏结束,小鸟撞到了障碍物!"); break; } // 判断小鸟是否落地 if (height <= 0) { System.out.println("游戏结束,小鸟落地了!"); break; } // 让玩家输入操作(向上飞或不飞) System.out.println("请操作小鸟(0表示不飞,1表示向上飞):"); int operation = scanner.nextInt(); if (operation == 1) { velocity -= 5; } // 计算新的高度和速度 height += velocity; velocity += gravity; // 计算障碍物的距离和高度 obstacleDistance--; if (obstacleDistance < 0) { obstacleDistance = 20; obstacleHeight = (int) (Math.random() * 10) + 1; System.out.println("出现新的障碍物,高度为:" + obstacleHeight); } // 计算得分 if (obstacleDistance == 0) { score++; System.out.println("得分加1,当前得分:" + score); } } } } ``` 这个小游戏的规则很简单:玩家需要控制小鸟上下飞行,避开障碍物并尽可能多地得分。小鸟每次向上飞行,速度会减少5,向下飞行时速度会增加2。障碍物每隔一段时间出现,玩家需要在障碍物到来之前及时进行操作。如果小鸟撞到了障碍物或落地了,游戏结束。如果成功躲过障碍物,得分加1。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值