aide, libgdx写一个爱心小游戏

aide, libgdx写一个爱心小游戏

介绍

手机写游戏太麻烦,所以代码没有注释。
运行效果见
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;

	@Override
	public 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(){
				@Override
				public void changed(ChangeEvent event, Actor actor) {
					// TODO Auto-generated method stub
					Gdx.app.exit();
				}
			});
		button1.addListener(new ChangeListener(){
				@Override
				public void changed(ChangeEvent event, Actor actor) {
					// TODO Auto-generated method stub
					resume();
					/*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();

	}

	@Override
	public 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;
	    }else
			for(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;}
			}


	}

	@Override
	public void dispose()
	{

	}

	@Override
	public void resize(int width, int height)
	{
	}

	@Override
	public void pause()
	{
	}

	@Override
	public 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(){
				@Override
				public boolean touchDown(InputEvent event,float x,float y,int pointer,int button){
					down=true;
					setVisible(false);
					return true;
				}
			});
	}

	@Override
	public void draw(Batch batch, float parentAlpha)
	{
		// TODO: Implement this method
		super.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());

	}

	@Override
	public void act(float delta)
	{
		// TODO: Implement this method
		super.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;
}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值