Flying Birds' blog 4th

2021年07月17日 99 字


关于实践周的第一个项目选择了制作Flying Birds小游戏。接下来是地面类和柱子类详细参数的设置。

地面类初始化

1
2
3
4
5
6
7
8
public Ground() throws Exception
{
image =new ImageIcon("source/ground.png").getImage();//原图片
width=image.getWidth(null);//使用 img.getWidth(null); 图片加载到内存中,可获图片真实宽度
height=image.getHeight(null);//使用 img.getHeight(null); 图片加载到内存中,可获图片真实高度
x=0;
y=500;
}

设计Gound类里默认读取图片资源中的ground的图片利用image.getWidth(null) image.getHeight(null)读取图片的高宽。
x y来控制图片放置的位置。

Ground类左移

1
2
3
4
5
6
7
8
9
//左移
public void step()
{
x-=4;
if(x<=-109)
{
x=0;
}
}

同样名为step类意味小鸟移动其实是用地面的图像左移的动画来实现的。

柱子类初始化

1
2
3
4
5
6
7
8
9
10
public Column(int n) throws Exception
{
image=new ImageIcon("source/column.png").getImage();
width=image.getWidth(null);
height=image.getHeight(null);
gap=144;
distance=245;
x=550+(n-1)*distance;
y=random.nextInt(218)+132;
}

同理可以默认读取图片资源中的clolumn的图片利用image.getWidth(null) image.getHeight(null)读取图片的高宽。
利用random类使游戏过程中柱子不断刷新后的纵坐标不同,所以柱子出现的高矮就会不同甚至没有上柱子。
gap是柱子之间缝隙(上下),由于贴图原因实际上gap应该为定制,如果缩小gap可能在间隙也会发生碰撞,如果扩大gap虽然确实可以增加通过的间隙,但是部分
间隙和柱子重叠,显示的是鸟经过了柱子但是没有发生碰撞。
由于柱子类出现X可以有distance控制,所以改变distance实际上是可以改变柱子出现的间隔距离(左右)。可以通过此处来改变游戏难度。

柱子类移动

1
2
3
4
5
6
7
8
9
public void step()
{
x-=4;
if(x<= -width/2)
{
x=distance*2-width/2;
y=random.nextInt(218);
}
}

柱子随地面按step函数移动。

Welcome back AM1ng!

本文作者: AM1ngkk
本文链接: https://am1ngkk.github.io/2021/07/17/blog4/