游戏玩法:通过鼠标点击使小鸟上下移动穿过柱子并完成得分,小鸟碰到柱子或掉落到地面上都会结束游戏。
(游戏内图片)
文章来源地址https://www.toymoban.com/news/detail-528047.html
下面是实现这个游戏的代码:
Brid类:
package bird;
import org.omg.CORBA.IMP_LIMIT;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
public class Brid {
int x,y;
int width,height;
int size;//鸟的大小,用于检测碰撞
BufferedImage image;
BufferedImage[] images;
int index;
double g; //重力加速度
double t; //两次位置的时间间隔
double v0; //初始上抛的速度
double speed; //当前上抛的速度
double s; //经过时间t以后的位移
double alpha; //鸟的倾角,单位是弧度
public Brid() throws Exception {
image = ImageIO.read(getClass().getResource("0.png"));
width = image.getWidth();
height = image.getHeight();
x = 132;
y = 280;
size = 40;
g = 4;
v0 = 20;
t = 0.25;
speed = v0;
s = 0;
alpha = 0;
images = new BufferedImage[8];
for (int i = 0; i < 8; i++) {
images[i] = ImageIO.read(getClass().getResource(i + ".png"));
}
index=0;
}
public void fly(){
index++;
image=images[(index/12)%8];
}
public void step(){
double v0=speed;
s=v0*t+g*t*t/2; //计算上抛运动的位移;
y=y-(int)s;//计算鸟的位置坐标
double v =v0-g*t;//计算下次的速度
speed=v;
alpha=Math.atan(s/8);
}
public void flappy(){
speed=v0; //重新设置初始速度,重新向上飞
}
public boolean hit( Ground ground){
boolean hit=y+size/2>ground.y;
if(hit){
y= ground.y-size/2;
alpha=-3.1415926/2;
}
return hit;
}
public boolean hit(Column column) {
if (x > column.x - column.width / 2 - size / 2 && x < column.x + column.width / 2 + size / 2) {
if (y > column.y - column.gap / 2 + size / 2 && y < column.y + column.gap / 2 - size / 2) {
return false;
}
return true;
}
return false;
}
}
BridGame类:
package bird;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
public class BridGame extends JPanel {
Brid brid;
Column column1,column2;
Ground ground;
BufferedImage background;
int score;
// boolean gameOver;
//游戏状态
int state;
public static final int START=0;
public static final int RUNNING=1;
public static final int GAME_OVER=2;
BufferedImage gameOverImage;
BufferedImage startImage;
public BridGame() throws Exception{
//gameOver=false;
state=START;
gameOverImage =ImageIO.read(getClass().getResource("gameover.png"));
startImage=ImageIO.read(getClass().getResource("start.png"));
brid=new Brid();
column1=new Column(1);
column2=new Column(2);
ground=new Ground();
background= ImageIO.read(getClass().getResource("bg.png"));
score=0;
}
@Override
public void paint(Graphics g) {
super.paint(g);
g.drawImage(background,0,0,null);
g.drawImage(column1.image,column1.x-column1.width/2,column1.y-column1.height/2,null);
g.drawImage(column2.image,column2.x-column2.width/2,column2.y-column2.height/2,null);
g.drawImage(ground.image,ground.x,ground.y,null);
Graphics2D g2=(Graphics2D) g;
g2.rotate(-brid.alpha,brid.x,brid.y);
g.drawImage(brid.image,brid.x-brid.width/2,brid.y-brid.width/2,null);
g2.rotate(brid.alpha,brid.x,brid.y);
Font f=new Font(Font.SANS_SERIF,Font.BOLD,40);
g.setFont(f);
g.drawString(""+score,40,60);
g.setColor(Color.white);
g.drawString(""+score,40-3,60-3);
switch (state){
case GAME_OVER:
g.drawImage(gameOverImage,0,0,null);
break;
case START:
g.drawImage(startImage,0,0,null);
break;
}
}
public void action() throws Exception{ //让地面动起来
MouseListener l=new MouseAdapter(){
@Override
public void mousePressed(MouseEvent e) { //鼠标按下
// brid.flappy(); //鸟向上飞
try {
switch (state) {
case GAME_OVER:
column1 = new Column(1);
column2 = new Column(2);
brid = new Brid();
score = 0;
state = START;
break;
case START:
state = RUNNING;
case RUNNING:
brid.flappy();
}
}catch (Exception e2){
}
}
};
addMouseListener(l);
while(true){
switch (state){
case START:
brid.fly();
ground.step();
break;
case RUNNING:
ground.step();
column1.step();
column2.step();
brid.step();
brid.fly();
if(brid.x==column1.x||brid.x==column2.x){
score++;
}
if(brid.hit(ground)||brid.hit(column1)||brid.hit(column2)){
state=GAME_OVER;
}
break;
}
repaint();
Thread.sleep(1000/30);
}
}
public static void main(String[] args) throws Exception {
JFrame frame=new JFrame();
BridGame game=new BridGame();
frame.add(game);
frame.setSize(440,670);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
game.action();
}
}
Column类:
package bird;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.font.LineMetrics;
import java.awt.image.BufferedImage;
import java.util.Random;
//柱子
public class Column {
int x,y;
int width,height;
int gap;
int distance;
BufferedImage image;
Random random=new Random();
public Column(int n) throws Exception{
image= ImageIO.read(getClass().getResource("column.png"));
width=image.getWidth();
height =image.getHeight();
gap=144;
distance=245;
x=550+(n-1)*distance;
y=random.nextInt(218)+132;
}
public void step(){
x--;
if(x==-width/2){
x=distance*2-width/2;
y=random.nextInt(218)+132;
}
}
}
Ground类:
package bird;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
public class Ground {
int x,y;
int width,height;
BufferedImage image;
public Ground() throws Exception{
image= ImageIO.read(getClass().getResource("ground.png"));
width=image.getWidth();
height =image.getHeight();
x=0;
y=500;
}
//添加方法让地面移动
public void step(){
x--;
if(x==-109){
x=0;
}
}
}
文章来源:https://www.toymoban.com/news/detail-528047.html
到了这里,关于【Java实现小游戏】飞翔的小鸟(源码)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!