Java實(shí)現(xiàn)升級(jí)版布谷鳥闖關(guān)游戲的示例代碼
前言
《布谷鳥闖關(guān)-升級(jí)版》是一個(gè)基于java的布谷鳥闖關(guān)游戲,鼠標(biāo)左鍵點(diǎn)擊控制鳥的位置穿過管道間的縫隙,需要做碰撞檢測,監(jiān)聽鍵盤事件,背景圖片的切換,障礙物管道產(chǎn)生時(shí)y軸上需要隨機(jī)位置。
主要設(shè)計(jì)
1.設(shè)計(jì)游戲界面,用swing實(shí)現(xiàn)
2.設(shè)計(jì)背景
3.設(shè)計(jì)移動(dòng)墻
4.設(shè)計(jì)布谷鳥
5.設(shè)計(jì)障礙物
6.設(shè)計(jì)背景音樂和音效
7.新增用戶賬號(hào)注冊登錄功能
8.引用mysql數(shù)據(jù)庫,管理用戶賬號(hào)密碼和儲(chǔ)存排行榜等信息
- ? 需要提前創(chuàng)建好數(shù)據(jù)庫"game",字符集選“utf8mb4”
- ? 然后執(zhí)行mysql表結(jié)構(gòu)和初始化數(shù)據(jù)腳本
- ? 修改代碼里的DBUtils的參數(shù)值
9.新增游戲商城模塊
10.新增排行榜模塊
功能截圖
用戶注冊:

游戲歡迎界面:

游戲開始界面:

鼠標(biāo)左鍵點(diǎn)擊控制鳥的位置穿過管道間的縫隙

代碼實(shí)現(xiàn)
游戲啟動(dòng)類
//開始界面
public class frame extends JFrame {
private JPanel pane;
public static frame frame;
public frame(){
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(240,150);
ImageIcon backGround = new ImageIcon("images/frame.png");
JLabel bkgImage = new JLabel(backGround);
bkgImage.setSize(240,150);
bkgImage.setLocation(0,0);
JPanel bkgPanel = (JPanel) this.getContentPane();
bkgPanel.setOpaque(false);
//注冊按鈕
JButton button_regist =new JButton("注冊");
button_regist.setBounds(30,40, 60,30);
bkgPanel.add(button_regist);
button_regist.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
new frame_regist().setVisible(true);
}
});
//登錄按鈕
JButton button_log=new JButton("登錄");
button_log.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
new frame_log(frame).setVisible(true);
}
});
button_log.setBounds(130,40, 60, 30);
bkgPanel.add(button_log);
this.getContentPane().add(bkgImage);
}
//彈出顯示傳入String的提示框
public static void frame_warning(String warning) {
JFrame frame=new JFrame();
JPanel pane=new JPanel();
frame.setBounds(540, 360, 300, 150);
frame.setContentPane(pane);
pane.setBorder(new EmptyBorder(5, 5, 5, 5));
pane.setLayout(null);
JLabel label_warning=new JLabel(warning);
label_warning.setBounds(50,20,150,50);
pane.add(label_warning);
JButton button_yes = new JButton("確定");
button_yes.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
frame.setVisible(false);
}
});
button_yes.setBounds(80, 80, 93, 23);
pane.add(button_yes);
frame.setVisible(true);
}
public static void main(String[] args){
frame =new frame();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
/**
*
*/
}核心類
import static java.lang.Thread.sleep;
public class GameplayingFrame extends JFrame{
private static final long serialVersionUID = 1L;
user_inf user;
MainFrame mainframe;
List<rank_information> rfs;
Graphics g=this.getGraphics();
GameplayingFrame(user_inf user,List<rank_information> rfs,MainFrame mainframe)
{
this.mainframe=mainframe;
this.rfs=rfs;
this.user=user;
this.setTitle("Fly Bird");
this.setSize(Constant.Frame_Width,Constant.Frame_Height);
this.setResizable(false);
this.setLocationRelativeTo(null);
this.setVisible(true);
Scene stage = new Scene(user);
Graphics g = this.getGraphics();
ImageIcon backGround = new ImageIcon("images/bg_day.png");
JLabel bkgImage = new JLabel(backGround);
bkgImage.setSize(288,512);
bkgImage.setLocation(0,0);
JPanel bkgPanel = (JPanel) this.getContentPane();
bkgPanel.setOpaque(false);
this.getContentPane().add(bkgImage);
//為界面加入鼠標(biāo)的響應(yīng)事件
this.addMouseListener(new MouseListener(){
@Override
public void mouseReleased(MouseEvent e) {}
@Override
public void mousePressed(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseClicked(MouseEvent e) {
stage.speed_y=-175;
}
});
JLabel scoreText = new JLabel();
Font font = new Font(Font.SERIF, Font.ITALIC, 30);
scoreText.setFont(font);
scoreText.setText(String.valueOf(0));
this.setLayout(null);
scoreText.setBounds(129,0,30,30);
this.add(scoreText);
//添加一個(gè)線程對(duì)象,用于重復(fù)繪圖,來節(jié)約主線程的資源,使游戲運(yùn)行更加流暢
new Thread(new playingThread(stage,g,this,user,rfs,mainframe)).start();
}
}
/*
* 場景類,包含了游戲界面的物品以及游戲?qū)崿F(xiàn)的算法
*/
class Scene{
//后綴_x/_y表示橫/縱坐標(biāo),窗口左上角為原點(diǎn)
public final int bird_x = 88;
//重力加速度
public final int G = 300;
public double bird_y;
public double birdRadius;
//速度——正值為向下,負(fù)值為向上
public int speed_x;
public int speed_y;
private Ground land= new Ground(GameUtil.toBufferedImage(GameUtil.getImage("ioo/land.png")));
BufferedImage back= GameUtil.toBufferedImage(GameUtil.getImage("ioo/bg_day.png")) ;
Barrier barrier;
Barrier barrier1;
Ground ground;
FlyingBird bird=null;
Scene(user_inf user){
bird_y = 200;
bird=new FlyingBird(bird_x,bird_y,user);
birdRadius = 22;
speed_x = 3;
speed_y = 0;
ground = new Ground(GameUtil.toBufferedImage(GameUtil.getImage("ioo/land.png")));
barrier= new Barrier(GameUtil.toBufferedImage(GameUtil.getImage("ioo/pipe_down1.png")));
barrier1= new Barrier(GameUtil.toBufferedImage(GameUtil.getImage("ioo/pipe_down.png")));
}
//返回true是游戲已經(jīng)結(jié)束,返回false表示游戲繼續(xù)進(jìn)行
boolean ifGameOver(){
//碰到地面
if(bird_y + birdRadius > 512 - ground.getHeight()){
System.out.println("hit ground");
return true;
}
//碰到頂
if(bird_y - birdRadius < 0){
System.out.println("hit sky");
return true;
}
//未過左邊界時(shí)
if(bird_x + birdRadius <= barrier.location_x){
return false;
}
//接近左邊界時(shí)
if(bird_x + birdRadius > barrier.location_x && bird_x < barrier.location_x){
if(bird_y < barrier.topHeight || bird_y + birdRadius*0.7 > 512 - barrier.bottomHeight){
System.out.println("hit left edge");
return true;
}
return false;
}
//通過管道時(shí)
if(bird_x >= barrier.location_x && bird_x < barrier.location_x + barrier.width){
boolean y1 = bird_y + birdRadius > 512 - barrier.bottomHeight;
boolean y2 = bird_y <barrier.topHeight;
if(y1 || y2){
System.out.println("hit inside");
}
return y1 || y2;
}
//已通過管道
if(bird_x >= barrier.location_x + barrier.width){
return false;
}
return false;
}
//ifGameOver=false時(shí)才執(zhí)行
boolean ifGetScore(){
return bird_x + birdRadius > barrier.location_x;
}
//第二次之后的繪圖
public void drawItems(Graphics g){
//鳥
bird.draw(g);
//下障礙物
g.drawImage(barrier.img, barrier.location_x, 512 - barrier.bottomHeight,33,barrier.bottomHeight, null);
//上障礙物
g.drawImage(barrier1.img,barrier.location_x, 0,33,barrier.topHeight, null);
//地面
g.drawImage(ground.getBufferedImage(),0,512-30, null);
ground.checkGround();
barrier.checkBarrier();
}
//更新各個(gè)物體的位置(每秒30次)
public void itemMove() {
//計(jì)算鳥的速度
speed_y += G*0.033;
//鳥最大的向下速度為220
if(speed_y > 220){
speed_y = 220;
}
//計(jì)算鳥的縱坐標(biāo)
bird_y += speed_y*0.033;
bird.y=bird_y;
//計(jì)算障礙物和地面的橫坐標(biāo)
barrier.location_x -= speed_x;
ground.setX(ground.getX()-speed_x);
}
//變速方法,根據(jù)分?jǐn)?shù)調(diào)整速度
public void shift(int score){
if(score < 1) {
speed_x = 3;
}
else if (score < 100){
speed_x = 4;
}
else if (score < 200){
speed_x = 5;
}
else if (score < 300){
speed_x = 6;
}
else if (score < 400){
speed_x = 7;
}
else if (score < 500){
speed_x = 8;
}
else speed_x = 9;
}
}線程類用于重復(fù)繪圖
public class playingThread implements Runnable {
Scene stage;
private Image iBuffer;
private Graphics gBuffer;
List<rank_information> rfs;
user_inf user;
MainFrame mainframe;
Graphics g ;
GameplayingFrame playingframe ;
public static boolean flag = true;//控制計(jì)分板的變量
public int score = 0;//分?jǐn)?shù)
playingThread(Scene stage,Graphics g,GameplayingFrame playingframe ,user_inf user,List<rank_information> rfs,MainFrame mainframe)
{
this.mainframe=mainframe;
this.rfs=rfs;
this.user=user;
this.stage=stage;
this.g=g;
this.playingframe=playingframe;
}
@Override
public void run() {
while (!stage.ifGameOver()){
stage.drawItems(g);//繪畫
stage.itemMove();//物體移動(dòng)
//33ms刷新一次
try {
sleep(33);
} catch (InterruptedException e) {
e.printStackTrace();
}
if(stage.ifGetScore()){
score++;
}
stage.shift(score);
//playingframe.update(g);
//清除上一次繪畫結(jié)果
//雙緩沖方法清除上一次繪畫結(jié)果
if (iBuffer == null){
iBuffer = playingframe.createImage(playingframe.getSize().width,playingframe.getSize().height);
gBuffer = iBuffer.getGraphics();
}
playingframe.paint(gBuffer);
g.drawImage(iBuffer,0,0,null);
// stage.drawItems(g);//再繪畫
}
user.setCoin(user.getCoin()+score);
new user_dao().update(user);
playingframe.setVisible(false);
rank_information rf=new rank_information();
rf.setScore(score);
rf.setName(user.getUser_name());
rfs.add(rf);
new rank_dao().update_rank(rfs);
new resultFrame(score,user,rfs,mainframe);
System.out.println("game over");
}
}總結(jié)
通過此次的《布谷鳥闖關(guān)-升級(jí)版》實(shí)現(xiàn),讓我對(duì)JAVA的相關(guān)知識(shí)有了進(jìn)一步的了解,對(duì)java這門語言也有了比以前更深刻的認(rèn)識(shí)。
java的一些基本語法,比如數(shù)據(jù)類型、運(yùn)算符、程序流程控制和數(shù)組等,理解更加透徹。java最核心的核心就是面向?qū)ο笏枷?,?duì)于這一個(gè)概念,終于悟到了一些。
以上就是Java實(shí)現(xiàn)升級(jí)版布谷鳥闖關(guān)游戲的示例代碼的詳細(xì)內(nèi)容,更多關(guān)于Java布谷鳥闖關(guān)游戲的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Java用三元運(yùn)算符判斷奇數(shù)和偶數(shù)的簡單實(shí)現(xiàn)
這篇文章主要介紹了Java用三元運(yùn)算符判斷奇數(shù)和偶數(shù)的簡單實(shí)現(xiàn),需要的朋友可以參考下2014-02-02
SpringBoot利用Redis解決海量重復(fù)提交問題
本文主要介紹了SpringBoot利用Redis解決海量重復(fù)提交問題,介紹了三種常見的解決方案,包括使用Redis計(jì)數(shù)器,使用Redis分布式鎖和使用Redis發(fā)布/訂閱機(jī)制,感興趣的可以了解一下2024-03-03
Spring Boot Admin 進(jìn)行項(xiàng)目監(jiān)控管理的方法
Spring Boot Admin是一個(gè)開源社區(qū)項(xiàng)目,用于管理和監(jiān)控SpringBoot應(yīng)用程序。 這篇文章主要介紹了 Spring Boot Admin 進(jìn)行項(xiàng)目監(jiān)控管理的方法,需要的朋友可以參考下2020-07-07
Java方法參數(shù)傳遞如何實(shí)現(xiàn)
這篇文章主要介紹了Java方法參數(shù)傳遞如何實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-05-05
簡單快速對(duì)@RequestParam聲明的參數(shù)作校驗(yàn)操作
這篇文章主要介紹了簡單快速對(duì)@RequestParam聲明的參數(shù)作校驗(yàn)操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08
spring boot自定義配置時(shí)在yml文件輸入有提示問題及解決方案
自定義一個(gè)配置類,然后在yml文件具體配置值時(shí),一般不會(huì)有提示,今天小編給大家分享spring boot自定義配置時(shí)在yml文件輸入有提示問題,感興趣的朋友一起看看吧2023-10-10
java基于jcifs.smb實(shí)現(xiàn)遠(yuǎn)程發(fā)送文件到服務(wù)器
這篇文章主要介紹了java基于jcifs.smb實(shí)現(xiàn)遠(yuǎn)程發(fā)送文件到服務(wù)器,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-01-01
java基于控制臺(tái)的學(xué)生學(xué)籍管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了java基于控制臺(tái)的學(xué)生學(xué)籍管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-07-07

