java--飞翔的小鸟
2023-12-13 15:38:04
游戏玩法:通过鼠标点击使小鸟上下移动穿过柱子并完成得分,小鸟碰到柱子或掉落到地面上都会结束游戏。
游戏内图片
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.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://blog.csdn.net/ywl20041008/article/details/134872258
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!