aide, libgdx写一个爱心小游戏

  • 介绍
  • 代码文件
    • main
    • MyHartActor
    • mySkin

介绍

手机写游戏太麻烦,所以代码没有注释。
运行效果见
aide手机写游戏libgdx入门

代码文件

main

package com.mycompany.mygame;import com.badlogic.gdx.*;
import com.badlogic.gdx.graphics.*;
import com.badlogic.gdx.graphics.g2d.*;
import com.badlogic.gdx.scenes.scene2d.*;
import com.badlogic.gdx.scenes.scene2d.ui.*;
import com.badlogic.gdx.scenes.scene2d.utils.*;
import javax.xml.transform.*;
import android.text.method.*;
import com.badlogic.gdx.audio.*;public class MyGdxGame implements ApplicationListener
{Texture texture,texture1;SpriteBatch batch;Stage stage;Label label1,label2;mySkin skin;Dialog dialog;Label label;TextButton button1,button2;Music music;Texture[] texturewz;MyHartActor[] hartactor;//int xx,yy;int[] t={150,150,150,150,150,150,150,150,150,150,150,150};//int t1=150;int i,times=6,love=0,n=12,b=-1;boolean a=false;@Overridepublic void create(){//xx=Gdx.graphics.getWidth();yy=Gdx.graphics.getHeight();texture = new Texture(Gdx.files.internal("mybg1.jpg"));batch = new SpriteBatch();stage=new Stage();skin=new mySkin(800,500,Color.RED,Color.PINK);label1=new Label("times: "+times,skin,"aaa");label1.setPosition(20,Gdx.graphics.getHeight()-200);label2=new Label("love:   "+love,skin,"aaa");label2.setPosition(20,Gdx.graphics.getHeight()-300);stage.addActor(label1);stage.addActor(label2);Gdx.input.setInputProcessor(stage);texturewz=new Texture[n];texturewz[0]=new Texture(Gdx.files.internal("an1.png"));texturewz[1]=new Texture(Gdx.files.internal("an2.png"));texturewz[2]=new Texture(Gdx.files.internal("ba3.png"));texturewz[3]=new Texture(Gdx.files.internal("an3.png"));texturewz[4]=new Texture(Gdx.files.internal("ba1.png"));texturewz[5]=new Texture(Gdx.files.internal("ba2.png"));texturewz[6]=new Texture(Gdx.files.internal("ba4.png"));texturewz[7]=new Texture(Gdx.files.internal("an4.png"));texturewz[8]=new Texture(Gdx.files.internal("bb1.png"));texturewz[9]=new Texture(Gdx.files.internal("bb2.png"));texturewz[10]=new Texture(Gdx.files.internal("bb3.png"));texturewz[11]=new Texture(Gdx.files.internal("bb4.png"));hartactor=new MyHartActor[n];hartactor[0]=new MyHartActor(Color.RED);hartactor[1]=new MyHartActor(Color.YELLOW);hartactor[2]=new MyHartActor(Color.LIGHT_GRAY);hartactor[3]=new MyHartActor(Color.PINK);hartactor[4]=new MyHartActor(Color.NAVY);hartactor[5]=new MyHartActor(Color.GREEN);hartactor[6]=new MyHartActor(Color.MAGENTA);hartactor[7]=new MyHartActor(Color.BLUE);hartactor[8]=new MyHartActor(Color.ORANGE);hartactor[9]=new MyHartActor(Color.BLACK);hartactor[10]=new MyHartActor(Color.WHITE);hartactor[11]=new MyHartActor(Color.CYAN);for(i=0;i<n;i++)stage.addActor(hartactor[i]);dialog=new Dialog("GAME OVER",skin);dialog.setBounds(Gdx.graphics.getWidth()/2-400,Gdx.graphics.getHeight()/2-250,800,500);dialog.setTitleAlignment(Align.top);//label=new Label("I love you.",skin);//dialog.addActor(label);//dialog.text("I love you.");button1=new TextButton("again",skin);button1.setBounds(80,40,200,100);dialog.addActor(button1);button2=new TextButton("exit",skin);button2.setBounds(520,40,200,100);dialog.addActor(button2);button2.addListener(new ChangeListener(){@Overridepublic void changed(ChangeEvent event, Actor actor) {// TODO Auto-generated method stubGdx.app.exit();}});button1.addListener(new ChangeListener(){@Overridepublic void changed(ChangeEvent event, Actor actor) {// TODO Auto-generated method stubresume();/*create();times=5;love=0;for(i=0;i<n;i++) t[i]=150;label1.setText("times: "+times);label2.setText("love:   "+love);*/}});//stage.addActor(dialog);music=Gdx.audio.newMusic(Gdx.files.internal("snow.mp3"));music.setLooping(true);music.play();}@Overridepublic void render(){        Gdx.gl.glClearColor(1, 1, 1, 1);Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);batch.begin();batch.draw(texture, 0, 0);//batch.draw(texture1,0,0);batch.end();stage.act();stage.draw();if(a){switch(love){case 4:case 3:dialog.text("I will love you and cherish you, \nfor better or worse, \nfor poorer and richer.");break;case 2:case 1:dialog.text("You can do nothing, for me, \nbut I love you.");break;case 0:dialog.text("Best wishes for your happiness. \nGod bless you.");break;case -1:case -2:dialog.text("Our lives were determined by \nthe opportunity, \neven if we miss the person.");break;case -3:case -4:dialog.text("If you do not cherish, \nwe will part soon, \nand  I will never see you again.");break;}dialog.show(stage);a=false;}elsefor(i=0;i<n;i++){if(hartactor[i].getend()){times--;t[i]=t[i]-1;hartactor[i].down=false;switch(i){case 0:case 1:case 7:case 3: love++;break;case 2:case 4:case 6:case 5: love--;break;}label1.setText("times: "+times);label2.setText("love:   "+love);if(times==0){b=i;for(int j=0;j<n;j++)hartactor[j].clearListeners();//dialog.show(stage);}}if(t[i]>0&&t[i]<150){batch.begin();batch.draw(texturewz[i],200,1400);batch.end();t[i]=t[i]-1;}else if(times==0&&t[b]==0){ a=true;times=-1;}}}@Overridepublic void dispose(){}@Overridepublic void resize(int width, int height){}@Overridepublic void pause(){}@Overridepublic void resume(){music.dispose();switch(love){case 4:case 3: music=Gdx.audio.newMusic(Gdx.files.internal("01.mp3"));break;case 2:case 1: music=Gdx.audio.newMusic(Gdx.files.internal("02.mp3"));break;case 0: music=Gdx.audio.newMusic(Gdx.files.internal("03.mp3"));break;case -1:case -2: music=Gdx.audio.newMusic(Gdx.files.internal("04.mp3"));break;case -3:case -4: music=Gdx.audio.newMusic(Gdx.files.internal("05.mp3"));break;}times=6;love=0;for(i=0;i<n;i++) t[i]=150;label1.setText("times: "+times);label2.setText("love:   "+love);stage.clear();hartactor=new MyHartActor[n];hartactor[0]=new MyHartActor(Color.RED);hartactor[1]=new MyHartActor(Color.YELLOW);hartactor[2]=new MyHartActor(Color.LIGHT_GRAY);hartactor[3]=new MyHartActor(Color.PINK);hartactor[4]=new MyHartActor(Color.NAVY);hartactor[5]=new MyHartActor(Color.GREEN);hartactor[6]=new MyHartActor(Color.MAGENTA);hartactor[7]=new MyHartActor(Color.BLUE);hartactor[8]=new MyHartActor(Color.ORANGE);hartactor[9]=new MyHartActor(Color.BLACK);hartactor[10]=new MyHartActor(Color.WHITE);hartactor[11]=new MyHartActor(Color.CYAN);for(i=0;i<n;i++)stage.addActor(hartactor[i]);stage.addActor(label1);stage.addActor(label2);//dialog.clear();dialog=new Dialog("GAME OVER",skin);dialog.setBounds(Gdx.graphics.getWidth()/2-400,Gdx.graphics.getHeight()/2-250,800,500);dialog.setTitleAlignment(Align.top);dialog.addActor(button1);dialog.addActor(button2);music.setLooping(true);music.play();}
}

MyHartActor

package com.mycompany.mygame;
import com.badlogic.gdx.scenes.scene2d.*;
import com.badlogic.gdx.graphics.*;
import com.badlogic.gdx.graphics.g2d.*;
import java.util.*;
import com.badlogic.gdx.*;public class MyHartActor extends Actor
{Pixmap pixmap;Color color;TextureRegion region;float a1,a2;float xx,yy,xt,yt;boolean down=false;public MyHartActor(Color color){this.color=color;region=new TextureRegion(new Texture(GetHart(50,color)));xt=this.region.getRegionWidth();yt=this.region.getRegionHeight();setSize(xt,yt);Random r=new Random();a1=r.nextFloat()*10;a2=r.nextFloat()*10;xx=Gdx.graphics.getWidth();yy=Gdx.graphics.getHeight();addListener(new InputListener(){@Overridepublic boolean touchDown(InputEvent event,float x,float y,int pointer,int button){down=true;setVisible(false);return true;}});}@Overridepublic void draw(Batch batch, float parentAlpha){// TODO: Implement this methodsuper.draw(batch, parentAlpha);//batch.draw(region,getX(),getY(),getOriginX(),getOriginY(),getWidth(),getHeight(),getScaleX(),getScaleY(),getRotation());batch.draw(region,getX(),getY(),getOriginX(),getOriginY(),getWidth(),getHeight(),getScaleX(),getScaleY(),getRotation());}@Overridepublic void act(float delta){// TODO: Implement this methodsuper.act(delta);if(getX()<0||getX()+xt>xx) a1=-a1;if(getY()<0||getY()+yt>yy) a2=-a2;moveBy(a1,a2);}public Pixmap GetHart(int radius,Color color){Pixmap pixmap=new Pixmap(radius*2,radius*2,Pixmap.Format.RGBA8888);pixmap.setColor(color);pixmap.fillCircle(radius/2,radius,radius/2);pixmap.fillCircle(radius*3/2,radius,radius/2);pixmap.fillTriangle(radius,2*radius,radius/5,radius*7/5,radius*9/5,radius*7/5);pixmap.fillTriangle(radius,2*radius,0,radius,radius*2,radius);return pixmap;}public boolean getend(){return down;}public void dispose(){pixmap.dispose();region.getTexture().dispose();}}

mySkin

package com.mycompany.mygame2;
import com.badlogic.gdx.scenes.scene2d.ui.;
import com.badlogic.gdx.graphics.
;
import com.badlogic.gdx.graphics.g2d.*;

public class mySkin extends Skin
{
Pixmap pixmap,pixmap2;
int width,height;
Color color1,color2;
BitmapFont font;
public mySkin(int width,int height,Color color1,Color color2){
this.width=width;this.height=height;
this.color1=color1;this.color2=color2;
font=new BitmapFont();
font.scale(3);
pixmap=getFrame();
add(“frame”,new Texture(pixmap));
pixmap2=new Pixmap(1,1,Pixmap.Format.RGBA8888);
pixmap.setColor(Color.LIGHT_GRAY);
pixmap2.fill();
add(“light_gray”,new Texture(pixmap2));
pixmap.setColor(Color.GRAY);
pixmap2.fill();
add(“gray”,new Texture(pixmap2));
add(“default”,new Window.WindowStyle(font,Color.BLACK,getDrawable(“frame”)));
add(“default”,new Button.ButtonStyle(getDrawable(“light_gray”),getDrawable(“gray”),null));
font.setColor(Color.BLACK);
add(“default”,new TextButton.TextButtonStyle(getDrawable(“light_gray”),getDrawable(“gray”),null,font));
BitmapFont font2=new BitmapFont();
font2.scale(2);
add(“default”,new Label.LabelStyle(font2,Color.WHITE));
add(“aaa”,new Label.LabelStyle(font,Color.BLACK));
}
public Pixmap getFrame(){
Pixmap pixmap=new Pixmap(width,height,Pixmap.Format.RGBA8888);
pixmap.setColor(color1);
pixmap.fillRectangle(0,height/4,width,height/2);
pixmap.fillRectangle(height/4,0,width-height/2,height);
pixmap.fillCircle(height/4,height/4,height/4);
pixmap.fillCircle(height/4,height3/4,height/4);
pixmap.fillCircle(width-height/4,height/4,height/4);
pixmap.fillCircle(width-height/4,height
3/4,height/4);

 int a=20,b=a/2;pixmap.setColor(color2);pixmap.fillRectangle(height/a,height/4+height/a,width-height/b,height/2-height/b);pixmap.fillRectangle(height/4+height/a,height/a,width-height/2-height/b,height-height/b);pixmap.fillCircle(height/4,height/4,height/4-height/a);pixmap.fillCircle(height/4,height*3/4,height/4-height/a);pixmap.fillCircle(width-height/4,height/4,height/4-height/a);pixmap.fillCircle(width-height/4,height*3/4,height/4-height/a);pixmap.setColor(1,1,1,0.5f);pixmap.fillCircle(height/8,height/8,height/8);pixmap.fillCircle(height/8,height/4,height/12);return pixmap;
}

}

aide, libgdx写一个爱心小游戏相关推荐

  1. 教你前端如何用js写一个跑酷小游戏

    在线体验地址:http://summer.pkec.net/ 源码地址:https://gitee.com/ihope_top/juejin-summer 前言 不知不觉夏天又到了,提到夏天你们能想到 ...

  2. ChatGPT实现用C语言写一个扫雷小游戏

    前几天我们利用 ChatGPT实现用C语言写一个学生成绩管理系统 其过程用时不到30秒,速度惊人 今天又让ChatGPT用C语言写了一个扫雷小游戏,它的回答是:抱歉,我是AI语言模型,无法编写程序. ...

  3. ES6 手写一个“辨色”小游戏

    1. 前言 依稀记得几年前朋友圈流行的辨色小游戏,找出颜色不同的矩形.前些天突发奇想,打算自己手写一个类似的游戏,话不多说,先上 Demo . --项目源码 本实例基于 ES6 实现,并兼容 ie9及 ...

  4. 使用C语言写一个扫雷小游戏

    前言 相信扫雷游戏小伙伴们肯定都玩过吧,学习了C语言中的数组.函数等基础内容之后就可以自己写一个简易的扫雷小游戏了,今天就我写扫雷小游戏的过程及思路写一篇博客,希望大家看完我的博客能有所收获. 软件及 ...

  5. html5翻卡片游戏,用 JavaScript 写一个卡片小游戏

    小游戏使用了HTML5,CSS3和JavaScript的基本的技术. 将讨论数据属性.定位.透视.转换.flexbox.事件处理.超时和三元组. 你不需要在编程方面有太多的知识和经验就能看懂,不过还是 ...

  6. python俄罗斯方块小游戏实验报告,童年的记忆——如何用python写一个俄罗斯方块小游戏!...

    谈到记忆里的小游戏,俄罗斯方块是大家一定会想到的一款游戏,自己写出来的应该玩起来更有感觉,然后就写了一个俄罗斯方块的游戏 给大家分享一下这个游戏的源码 先用python创建一个py文件 定义这次程序所 ...

  7. 用R写一个迷宫小游戏

    R实现迷宫小游戏 效果图 缘起 R的图形API DFS函数生成迷宫 数据结构 代码解析 后话 附录:完整代码 效果图 缘起 刚装了Ubuntu系统,发现里面有自带的扫雷等小游戏.最近又疯狂使用R,忽然 ...

  8. Python入门如何给自己写一个文字小游戏?一只小白的Python游戏

    有朋友看了我b站投稿的小游戏视频表示感兴趣,于是在这里贴一下源代码. 视频演示的b站传送门:https://www.bilibili.com/video/av64680988#reply2016223 ...

  9. Python写一个迷宫小游戏

    相关文件 关注小编,私信小编领取源码和其他小游戏的代码哟~~ 开发环境 Python版本:3.6.4 相关模块: pygame模块: 以及一些Python自带的模块. 原码.安装包(点击领取即可) 原 ...

  10. 用 JavaScript 写一个卡片小游戏

    小游戏使用了HTML5,CSS3和JavaScript的基本的技术. 将讨论数据属性.定位.透视.转换.flexbox.事件处理.超时和三元组. 你不需要在编程方面有太多的知识和经验就能看懂,不过还是 ...

最新文章

  1. 记录安装oracle的那些事(二)之双系统安装
  2. Docker1.12.1之swarm集群搭建与使用
  3. JavaScript 32位整型无符号操作
  4. Maxim and Biology
  5. 起一卦,还是那个破事。还是大凶。
  6. 用 PS 调整服务器时间
  7. “数据资产化探索”专题
  8. Oracle expdp impdp导出导入命令及数据库备份
  9. cut最后几位 shell_Linux Shell之cut命令
  10. CVPR2019目标检测论文汇总
  11. 密钥管理系统研究(一)
  12. 「深度」VR家装开局完美,但适度挤掉一点水分或许更利于它的发展
  13. oracle 取流水号
  14. 【头歌】共享单车之数据存储
  15. 软考中级过了就是中级职称吗?
  16. 2012 款斯柯达明锐车行驶中发动机偶尔自动熄火
  17. 前端-可视化创建vue项目
  18. Object和Objects
  19. 中国公民身份证编号规则
  20. 浅谈ERP实施应用的流程步骤

热门文章

  1. JNI/NDK入门指南之JavaVM和JNIEnv
  2. 翻译软件安装linux,一天学一招---安装翻译软件星际译王
  3. fluidsim元件库下载_FluidSIM下载
  4. 【数据艺术科技1】基于pyhon的高维数据可视化。(1、2维)
  5. Ectouch修改虚拟销售数量的方法
  6. Learning Instagram 学习Instagram Lynda课程中文字幕
  7. 象棋人机对弈java_象棋人机对弈程序的思想
  8. hustoj 配置java_HUSTOJ增加其他语言出现RuntimeError解决办法
  9. ENSP配置 实例六 OSPF配置
  10. html5如何直接源码,html5源码可以直接使用吗