最近在学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 && xcol.y-col.gap/2+size/2 && 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=distance2-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();
}