日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

第一个libgdx程序--仿别踩白块

發(fā)布時間:2025/4/16 编程问答 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 第一个libgdx程序--仿别踩白块 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

為什么80%的碼農都做不了架構師?>>> ??

? ? 學libgdx有一段時間了,有一個星期了吧,邊做邊學,選的是別踩白塊。

? ? libgdx總體思想可以用下面這張圖來表示


? ??

? ? 總體可分為兩層吧,入口不算,Screen+Actor. 這個很好理解啊,你去看話劇表演,得有屏幕吧,屏幕上面有英雄,有壞蛋,還有道具吧,
?所以,簡單來說暫且就看作兩層。


? ? 下面看看具體項目:


? ? ?首先跑起來是這樣的:
???
??????????????????? ??????????? ?
?

?????代碼是這樣的:


? ? ?1. 入口類 FirstGame
? ? ? ?
? ? ?既然是入口,主要做一些初始化操作

public class FirstGame extends Game {private MenuScreen menuScreen;@Overridepublic void create(){Const.WIDTH = Gdx.graphics.getWidth();Const.HEIGHT = Gdx.graphics.getHeight();menuScreen = new MenuScreen(this);setScreen(menuScreen);}@Overridepublic void render(){Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);super.render();}}


? ? ?2. 游戲選擇界面 MenuScreen

? ? ?這里真正開始libgdx部分了,主要是圖片按鈕TextButton控件的使用及兩個動畫特效的實現。根據界面顯示,用了6個主菜單TextButton和3個子菜單TextButton,代碼如下:

public class MenuScreen implements Screen {private TextButtonStyle whiteStyle, blackStyle;/** 6個主菜單 */private TextButton classicBtn, arcadeBtn, zenBtn, speedBtn, relayBtn, moreBtn;/** 3個子菜單 */private TextButton mode1Btn, mode2Btn, mode3Btn;private Texture texture;private Stage stage;/** 這里用集合來存放所有點擊過的菜單的位置,用來得到哪一個需要收縮,哪一個需要彈出 */private List<Integer> idList;/** 是否播放動畫 */private boolean isrun;private FirstGame game;public MenuScreen(FirstGame game){this.game = game;}@Overridepublic void render(float delta){/** 收縮菜單與彈出菜單動畫特效。這里開始邏輯有點亂,耐心看看吧,一時間沒想到更好的辦法,唉 ==||*/if (isrun){int len = idList.size();if (len > 0){if (len > 1){if (idList.get(len - 2) % 2 == 1){if (inx > -240){modeBtnMoveIn();}} else{if (inx < 480){modeBtnMoveIn();}}}if (idList.get(len - 1) % 2 == 1){if (outx < 0){modeBtnMoveOut();} else{isrun = false;}} else{if (outx > 240){modeBtnMoveOut();} else{isrun = false;}}}}Gdx.gl.glClearColor(1, 1, 1, 1);Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);stage.draw();}@Overridepublic void resize(int width, int height){}@Overridepublic void show(){stage = new Stage(new StretchViewport(Const.WIDTH, Const.HEIGHT));initBtnStyle();initBtn();Gdx.input.setInputProcessor(stage);idList = new ArrayList<Integer>();}/** 初始化按鈕背景顏色 */private void initBtnStyle(){texture = new Texture(Gdx.files.internal("data/whitebg.png"));Sprite sprite = new Sprite(texture);SpriteDrawable whiteSpriteDrawable = new SpriteDrawable(sprite);whiteStyle = new TextButtonStyle();whiteStyle.down = whiteSpriteDrawable;whiteStyle.up = whiteSpriteDrawable;whiteStyle.font = new BitmapFont();whiteStyle.fontColor = Color.BLACK;texture = new Texture(Gdx.files.internal("data/black.png"));sprite = new Sprite(texture);SpriteDrawable blackSpriteDrawable = new SpriteDrawable(sprite);blackStyle = new TextButtonStyle();blackStyle.down = blackSpriteDrawable;blackStyle.up = blackSpriteDrawable;blackStyle.font = new BitmapFont();blackStyle.fontColor = Color.WHITE;}/** 實例化按鈕 */private void initBtn(){classicBtn = new TextButton("Classic", whiteStyle);classicBtn.setPosition(0, Const.HEIGHT * 2 / 3);classicBtn.setSize(Const.WIDTH / 2, Const.HEIGHT / 3);classicBtn.addListener(new InputListener(){@Overridepublic boolean touchDown(InputEvent event, float x, float y, int pointer, int button){initmodeBtn("25", "50", "Discontinuous", 1);idList.add(1);setInitX();isrun = true;return super.touchDown(event, x, y, pointer, button);}});arcadeBtn = new TextButton("Arcade", blackStyle);arcadeBtn.setPosition(Const.WIDTH / 2, Const.HEIGHT * 2 / 3);arcadeBtn.setSize(Const.WIDTH / 2, Const.HEIGHT / 3);arcadeBtn.addListener(new InputListener(){@Overridepublic boolean touchDown(InputEvent event, float x, float y, int pointer, int button){initmodeBtn("Normal", "Faster", "Retrograde", 0);idList.add(2);setInitX();isrun = true;return super.touchDown(event, x, y, pointer, button);}});zenBtn = new TextButton("Zen", blackStyle);zenBtn.setPosition(0, Const.HEIGHT / 3);zenBtn.setSize(Const.WIDTH / 2, Const.HEIGHT / 3);zenBtn.addListener(new InputListener(){@Overridepublic boolean touchDown(InputEvent event, float x, float y, int pointer, int button){initmodeBtn("15\"", "30\"", "Discontinuous", 1);idList.add(3);setInitX();isrun = true;return super.touchDown(event, x, y, pointer, button);}});speedBtn = new TextButton("Speed", whiteStyle);speedBtn.setPosition(Const.WIDTH / 2, Const.HEIGHT / 3);speedBtn.setSize(Const.WIDTH / 2, Const.HEIGHT / 3);speedBtn.addListener(new InputListener(){@Overridepublic boolean touchDown(InputEvent event, float x, float y, int pointer, int button){initmodeBtn("Slow", "Normal", "Retrograde", 0);idList.add(4);setInitX();isrun = true;return super.touchDown(event, x, y, pointer, button);}});relayBtn = new TextButton("Relay", whiteStyle);relayBtn.setPosition(0, 0);relayBtn.setSize(Const.WIDTH / 2, Const.HEIGHT / 3);relayBtn.addListener(new InputListener(){@Overridepublic boolean touchDown(InputEvent event, float x, float y, int pointer, int button){initmodeBtn("8\"", "10\"", "12\"", 1);idList.add(5);setInitX();isrun = true;return super.touchDown(event, x, y, pointer, button);}});moreBtn = new TextButton("More", blackStyle);moreBtn.setPosition(Const.WIDTH / 2, 0);moreBtn.setSize(Const.WIDTH / 2, Const.HEIGHT / 3);moreBtn.addListener(new InputListener(){@Overridepublic boolean touchDown(InputEvent event, float x, float y, int pointer, int button){initmodeBtn("1", "2", "3", 0);idList.add(6);setInitX();isrun = true;return super.touchDown(event, x, y, pointer, button);}});stage.addActor(classicBtn);stage.addActor(arcadeBtn);stage.addActor(zenBtn);stage.addActor(speedBtn);stage.addActor(relayBtn);stage.addActor(moreBtn);/** 在這里實例化并注冊監(jiān)聽事件,不能去到initmodeBtn再做操作 */mode1Btn = new TextButton("", whiteStyle);mode2Btn = new TextButton("", blackStyle);mode3Btn = new TextButton("", whiteStyle);mode1Btn.addListener(new InputListener(){@Overridepublic boolean touchDown(InputEvent event, float x, float y, int pointer, int button){setScreen(1);return super.touchDown(event, x, y, pointer, button);}});mode2Btn.addListener(new InputListener(){@Overridepublic boolean touchDown(InputEvent event, float x, float y, int pointer, int button){setScreen(2);return super.touchDown(event, x, y, pointer, button);}});mode3Btn.addListener(new InputListener(){@Overridepublic boolean touchDown(InputEvent event, float x, float y, int pointer, int button){setScreen(3);return super.touchDown(event, x, y, pointer, button);}});}/** 頁面跳轉 */private void setScreen(int mode){switch (idList.get(idList.size() - 1)){case 1:switch (mode){case 1:game.setScreen(new GameScreen(game, Const.GameModeOne.CLASSIC, Const.GameModeTwo.MODE1));break;case 2:game.setScreen(new GameScreen(game, Const.GameModeOne.CLASSIC, Const.GameModeTwo.MODE2));break;case 3:game.setScreen(new GameScreen(game, Const.GameModeOne.CLASSIC, Const.GameModeTwo.MODE3));break;default:break;}break;case 2:switch (mode){case 1:game.setScreen(new GameScreen(game, Const.GameModeOne.ARCADE, Const.GameModeTwo.MODE1));break;case 2:game.setScreen(new GameScreen(game, Const.GameModeOne.ARCADE, Const.GameModeTwo.MODE2));break;case 3:game.setScreen(new GameScreen(game, Const.GameModeOne.ARCADE, Const.GameModeTwo.MODE3));break;default:break;}break;case 3:switch (mode){case 1:game.setScreen(new GameScreen(game, Const.GameModeOne.ZEN, Const.GameModeTwo.MODE1));break;case 2:game.setScreen(new GameScreen(game, Const.GameModeOne.ZEN, Const.GameModeTwo.MODE2));break;case 3:game.setScreen(new GameScreen(game, Const.GameModeOne.ZEN, Const.GameModeTwo.MODE3));break;default:break;}break;case 4:switch (mode){case 1:game.setScreen(new GameScreen(game, Const.GameModeOne.SPEED, Const.GameModeTwo.MODE1));break;case 2:game.setScreen(new GameScreen(game, Const.GameModeOne.SPEED, Const.GameModeTwo.MODE2));break;case 3:game.setScreen(new GameScreen(game, Const.GameModeOne.SPEED, Const.GameModeTwo.MODE3));break;default:break;}break;case 5:switch (mode){case 1:game.setScreen(new GameScreen(game, Const.GameModeOne.RELAY, Const.GameModeTwo.MODE1));break;case 2:game.setScreen(new GameScreen(game, Const.GameModeOne.RELAY, Const.GameModeTwo.MODE2));break;case 3:game.setScreen(new GameScreen(game, Const.GameModeOne.RELAY, Const.GameModeTwo.MODE3));break;default:break;}break;case 6:break;default:break;}}/** 初始化不同游戲模式按鈕 */private void initmodeBtn(String s1, String s2, String s3, int flag){mode1Btn.setText(s1);mode2Btn.setText(s2);mode3Btn.setText(s3);/** 背景色分了兩種情況 */if (flag == 1){mode1Btn.setStyle(whiteStyle);mode2Btn.setStyle(blackStyle);mode3Btn.setStyle(whiteStyle);} else{mode1Btn.setStyle(blackStyle);mode2Btn.setStyle(whiteStyle);mode3Btn.setStyle(blackStyle);}stage.addActor(mode1Btn);stage.addActor(mode2Btn);stage.addActor(mode3Btn);}/** 收縮菜單的x坐標需要平移到哪里為止,彈出菜單的x坐標需要平移到哪里為止 */private float inx, outx;/** 這兩個值分別用來計算彈出菜單的高度和y坐標 */private float moveh, moveYEnd;/** 按鈕收縮動畫效果 */private void modeBtnMoveIn(){int len = idList.size();if (len > 1){switch (idList.get(len - 2)){case 1:moveYEnd = 800 * 2 / 3;moveh = 800 / 3;inx -= 20;mode1Btn.setPosition(inx, moveYEnd + moveh * 2 / 3);mode1Btn.setSize(Const.WIDTH / 2, moveh / 3);mode2Btn.setPosition(inx, moveYEnd + moveh / 3);mode2Btn.setSize(Const.WIDTH / 2, moveh / 3);mode3Btn.setPosition(inx, moveYEnd);mode3Btn.setSize(Const.WIDTH / 2, moveh / 3);break;case 2:moveYEnd = 800 * 2 / 3;moveh = 800 / 3;inx += 20;mode1Btn.setPosition(inx, moveYEnd + moveh * 2 / 3);mode1Btn.setSize(Const.WIDTH / 2, moveh / 3);mode2Btn.setPosition(inx, moveYEnd + moveh / 3);mode2Btn.setSize(Const.WIDTH / 2, moveh / 3);mode3Btn.setPosition(inx, moveYEnd);mode3Btn.setSize(Const.WIDTH / 2, moveh / 3);break;case 3:moveYEnd = 800 / 3;moveh = 800 / 3;inx -= 20;mode1Btn.setPosition(inx, moveYEnd + moveh * 2 / 3);mode1Btn.setSize(Const.WIDTH / 2, moveh / 3);mode2Btn.setPosition(inx, moveYEnd + moveh / 3);mode2Btn.setSize(Const.WIDTH / 2, moveh / 3);mode3Btn.setPosition(inx, moveYEnd);mode3Btn.setSize(Const.WIDTH / 2, moveh / 3);break;case 4:moveYEnd = 800 / 3;moveh = 800 / 3;inx += 20;mode1Btn.setPosition(inx, moveYEnd + moveh * 2 / 3);mode1Btn.setSize(Const.WIDTH / 2, moveh / 3);mode2Btn.setPosition(inx, moveYEnd + moveh / 3);mode2Btn.setSize(Const.WIDTH / 2, moveh / 3);mode3Btn.setPosition(inx, moveYEnd);mode3Btn.setSize(Const.WIDTH / 2, moveh / 3);break;case 5:moveYEnd = 0;moveh = 800 / 3;inx -= 20;mode1Btn.setPosition(inx, moveYEnd + moveh * 2 / 3);mode1Btn.setSize(Const.WIDTH / 2, moveh / 3);mode2Btn.setPosition(inx, moveYEnd + moveh / 3);mode2Btn.setSize(Const.WIDTH / 2, moveh / 3);mode3Btn.setPosition(inx, moveYEnd);mode3Btn.setSize(Const.WIDTH / 2, moveh / 3);break;case 6:moveYEnd = 0;moveh = 800 / 3;inx += 20;mode1Btn.setPosition(inx, moveYEnd + moveh * 2 / 3);mode1Btn.setSize(Const.WIDTH / 2, moveh / 3);mode2Btn.setPosition(inx, moveYEnd + moveh / 3);mode2Btn.setSize(Const.WIDTH / 2, moveh / 3);mode3Btn.setPosition(inx, moveYEnd);mode3Btn.setSize(Const.WIDTH / 2, moveh / 3);break;default:break;}}}/** 按鈕彈出動畫效果 */private void modeBtnMoveOut(){switch (idList.get(idList.size() - 1)){case 1:moveYEnd = 800 * 2 / 3;moveh = 800 / 3;outx += 15;mode1Btn.setPosition(outx, moveYEnd + moveh * 2 / 3);mode1Btn.setSize(Const.WIDTH / 2, moveh / 3);mode2Btn.setPosition(outx, moveYEnd + moveh / 3);mode2Btn.setSize(Const.WIDTH / 2, moveh / 3);mode3Btn.setPosition(outx, moveYEnd);mode3Btn.setSize(Const.WIDTH / 2, moveh / 3);break;case 2:moveYEnd = 800 * 2 / 3;moveh = 800 / 3;outx -= 15;mode1Btn.setPosition(outx, moveYEnd + moveh * 2 / 3);mode1Btn.setSize(Const.WIDTH / 2, moveh / 3);mode2Btn.setPosition(outx, moveYEnd + moveh / 3);mode2Btn.setSize(Const.WIDTH / 2, moveh / 3);mode3Btn.setPosition(outx, moveYEnd);mode3Btn.setSize(Const.WIDTH / 2, moveh / 3);break;case 3:moveYEnd = 800 / 3;moveh = 800 / 3;outx += 15;mode1Btn.setPosition(outx, moveYEnd + moveh * 2 / 3);mode1Btn.setSize(Const.WIDTH / 2, moveh / 3);mode2Btn.setPosition(outx, moveYEnd + moveh / 3);mode2Btn.setSize(Const.WIDTH / 2, moveh / 3);mode3Btn.setPosition(outx, moveYEnd);mode3Btn.setSize(Const.WIDTH / 2, moveh / 3);break;case 4:moveYEnd = 800 / 3;moveh = 800 / 3;outx -= 15;mode1Btn.setPosition(outx, moveYEnd + moveh * 2 / 3);mode1Btn.setSize(Const.WIDTH / 2, moveh / 3);mode2Btn.setPosition(outx, moveYEnd + moveh / 3);mode2Btn.setSize(Const.WIDTH / 2, moveh / 3);mode3Btn.setPosition(outx, moveYEnd);mode3Btn.setSize(Const.WIDTH / 2, moveh / 3);break;case 5:moveYEnd = 0;moveh = 800 / 3;outx += 15;mode1Btn.setPosition(outx, moveYEnd + moveh * 2 / 3);mode1Btn.setSize(Const.WIDTH / 2, moveh / 3);mode2Btn.setPosition(outx, moveYEnd + moveh / 3);mode2Btn.setSize(Const.WIDTH / 2, moveh / 3);mode3Btn.setPosition(outx, moveYEnd);mode3Btn.setSize(Const.WIDTH / 2, moveh / 3);break;case 6:moveYEnd = 0;moveh = 800 / 3;outx -= 15;mode1Btn.setPosition(outx, moveYEnd + moveh * 2 / 3);mode1Btn.setSize(Const.WIDTH / 2, moveh / 3);mode2Btn.setPosition(outx, moveYEnd + moveh / 3);mode2Btn.setSize(Const.WIDTH / 2, moveh / 3);mode3Btn.setPosition(outx, moveYEnd);mode3Btn.setSize(Const.WIDTH / 2, moveh / 3);break;}}/** 獲得收縮菜單和彈出菜單平移的目標位置 */private void setInitX(){int len = idList.size();if (len > 1){if (idList.get(len - 2) % 2 == 1){inx = 0;} else{inx = 240;}}if (idList.get(len - 1) % 2 == 1){outx = -240;} else{outx = 480;}}@Overridepublic void hide(){}@Overridepublic void pause(){}@Overridepublic void resume(){}@Overridepublic void dispose(){}}



? ? ?如果看上面代碼毫無壓力的話(是指基本用法方面,本人代碼比較亂++|| ),libgdx的基本用法差不多了。

? ? ?3. 演員類 BlockActor
? ? ?
? ? ?參考OOP里一切皆對象的說法。libgdx里所有用戶能看到能動的東西基本都能作為演員,類似你去看話劇,舞臺上能動的是不是基本都是演員(可能不是很恰當,初學的話暫時先這么理解吧)。演員有自己的屬性,事件,動作等,可以在這里定義。

public class BlockActor extends Actor {private GameScreen screen;public ImageButton mImageButton;private TextureRegionDrawable startRegionDrawable, blackRegionDrawable, whiteRegionDrawable, greyRegionDrawable, redRegionDrawable;private TextureRegion startRegion, blackRegion, whiteRegion, greyRegion, redRegion;private Texture startTexture, blackTexture, whiteTexture, greyTexture, redTexture;public BlockActor(GameScreen screen, BlockColor blockColor, float x, float y, int nameIndex){this.screen = screen;startTexture = new Texture(Gdx.files.internal("data/start.png"));startRegion = new TextureRegion(startTexture);startRegionDrawable = new TextureRegionDrawable(startRegion);blackTexture = new Texture(Gdx.files.internal("data/black.png"));whiteTexture = new Texture(Gdx.files.internal("data/white.png"));greyTexture = new Texture(Gdx.files.internal("data/grey.png"));redTexture = new Texture(Gdx.files.internal("data/red.png"));whiteRegion = new TextureRegion(whiteTexture, 120, 200);whiteRegionDrawable = new TextureRegionDrawable(whiteRegion);redRegion = new TextureRegion(redTexture, 120, 200);redRegionDrawable = new TextureRegionDrawable(redRegion);blackRegion = new TextureRegion(blackTexture, 120, 200);blackRegionDrawable = new TextureRegionDrawable(blackRegion);greyRegion = new TextureRegion(greyTexture, 120, 200);greyRegionDrawable = new TextureRegionDrawable(greyRegion);if (nameIndex == 1 && blockColor == Const.BlockColor.BLACKBLOCK){mImageButton = new ImageButton(startRegionDrawable, greyRegionDrawable);mImageButton.setName(nameIndex + "");} else if (blockColor == Const.BlockColor.BLACKBLOCK){mImageButton = new ImageButton(blackRegionDrawable, greyRegionDrawable);mImageButton.setName(nameIndex + "");} else{mImageButton = new ImageButton(whiteRegionDrawable, redRegionDrawable);mImageButton.setName("-1");}mImageButton.setSize(120, 200);mImageButton.setPosition(x, y);mImageButton.addListener(new InputListener(){@Overridepublic boolean touchDown(InputEvent event, float x, float y, int pointer, int button){return true;}@Overridepublic void touchUp(InputEvent event, float x, float y, int pointer, int button){int position = Integer.parseInt(mImageButton.getName() + "");if (position == 1){BlockActor.this.screen.prerowpostion = position;BlockActor.this.screen.isStart = true;BlockActor.this.screen.blockState = Const.BlockState.MOVEING;} else{if (BlockActor.this.screen.gameModeOne == Const.GameModeOne.CLASSIC && position == Const.BLOCKCOUNT){BlockActor.this.screen.isSuc = true;BlockActor.this.screen.blockState = Const.BlockState.IDLE;BlockActor.this.screen.game.setScreen(new DeadScreen(BlockActor.this.screen));} else{if (position == BlockActor.this.screen.prerowpostion + 1){BlockActor.this.screen.prerowpostion = position;BlockActor.this.screen.blockState = Const.BlockState.MOVEING;} else{BlockActor.this.screen.blockState = Const.BlockState.IDLE;BlockActor.this.screen.game.setScreen(new DeadScreen(BlockActor.this.screen));}}}super.touchUp(event, x, y, pointer, button);}});}@Overridepublic void draw(Batch batch, float parentAlpha){super.draw(batch, parentAlpha);}}




? ? 我的博客其他文章列表
???
?http://my.oschina.net/helu

轉載于:https://my.oschina.net/helu/blog/291297

總結

以上是生活随笔為你收集整理的第一个libgdx程序--仿别踩白块的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。