Libgdx打地鼠一

package com.xuefei.mygame;

import com.badlogic.gdx.backends.lwjgl.LwjglApplication;
import com.badlogic.gdx.backends.lwjgl.LwjglApplicationConfiguration;

public class Main {
	public static void main(String[] args) {
		LwjglApplicationConfiguration cfg = new LwjglApplicationConfiguration();
		cfg.title = "MyGame";
		cfg.useGL20 = true;
		cfg.width = 800;
		cfg.height =480;
		
		new LwjglApplication(new MyGame(), cfg);
	}
}


package com.xuefei.mygame;

import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.ui.ImageButton;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.Label.LabelStyle;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
import com.badlogic.gdx.scenes.scene2d.utils.SpriteDrawable;

public class MyGame implements ApplicationListener {
	private Stage stage;
	private BitmapFont bitmapFont;
	private Label label;
	private TextureAtlas back;
	private TextureAtlas pack;
	private Image background;
	private Image game_name;
	
	private Texture splash_image;
	private ImageButton ButtonSplashImage;
	private Texture button_new;
	private Texture button_new_on;
	private ImageButton buttonNew;
	private Texture button_help;
	private Texture button_help_on;
	private ImageButton buttonHelp;
	private Texture button_exit;
	private Texture button_exit_on;
	private ImageButton buttonExit;
	
	@Override
	public void create() {		
		float w = Gdx.graphics.getWidth();
		float h = Gdx.graphics.getHeight();
		
		float scaleX = w/800;
		float scaleY = h/480;
		
		stage = new Stage(w,h);
		
		back = new TextureAtlas("back/back.pack");
		background = new Image(back.findRegion("background"));
		background.setName("background");
		background.setPosition(0, 0);
		background.setScale(scaleX, scaleY);
		
		pack = new TextureAtlas("pack/pack.pack");
		game_name = new Image(pack.findRegion("game_name"));
		game_name.setName("game_name");
		game_name.setPosition(110*scaleX, 200*scaleY);
		
		button_new = new Texture(Gdx.files.internal("image/button_new.png"));
		button_new_on = new Texture(Gdx.files.internal("image/button_new_on.png"));
		buttonNew = new ImageButton(new SpriteDrawable(new Sprite(button_new)),new SpriteDrawable(new Sprite(button_new_on)));
		buttonNew.setPosition(500*scaleX, 300*scaleY);
		buttonNew.addListener(new ClickListener(){
			public void clicked(InputEvent event,float x,float y){ 
				 stage.clear(); 
				 stage.addActor(background);
				 stage.addActor(label);
            }  
		});
		
		
		button_help = new Texture(Gdx.files.internal("image/button_help.png"));
		button_help_on = new Texture(Gdx.files.internal("image/button_help_on.png"));
		buttonHelp = new ImageButton(new SpriteDrawable(new Sprite(button_help)),new SpriteDrawable(new Sprite(button_help_on)));
		buttonHelp.setPosition(500*scaleX, 200*scaleY);
		buttonHelp.addListener(new ClickListener(){
			public void clicked(InputEvent event,float x,float y){
				
            }  
		});
		
		
		button_exit = new Texture(Gdx.files.internal("image/button_exit.png"));
		button_exit_on = new Texture(Gdx.files.internal("image/button_exit_on.png"));
		buttonExit = new ImageButton(new SpriteDrawable(new Sprite(button_exit)),new SpriteDrawable(new Sprite(button_exit_on)));
		buttonExit.setPosition(500*scaleX, 100*scaleY);
		buttonExit.addListener(new ClickListener(){
			public void clicked(InputEvent event,float x,float y){  
                 
            }  
		});
		
		
		
		bitmapFont = new BitmapFont(Gdx.files.internal("font/font.fnt"),Gdx.files.internal("font/font.png"),false);
		LabelStyle labelStyle = new LabelStyle(bitmapFont, Color.BLACK); 
		label = new Label("FPS:",labelStyle);
		label.setName("fpsLabel");
		label.setX(0);
		label.setY(0);
		
		splash_image = new Texture(Gdx.files.internal("image/splash_image.png"));
		ButtonSplashImage = new ImageButton(new SpriteDrawable(new Sprite(splash_image)),new SpriteDrawable(new Sprite(splash_image)));
		ButtonSplashImage.setPosition(0, 0);
		ButtonSplashImage.addListener(new ClickListener(){
			public void clicked(InputEvent event,float x,float y){ 
				stage.clear();
				stage.addActor(background);
				stage.addActor(game_name);
				stage.addActor(buttonNew);
				stage.addActor(buttonHelp);
				stage.addActor(buttonExit);
				stage.addActor(label);
           } 
		});
		
		
		
		stage.addActor(ButtonSplashImage);
		
		
		Gdx.input.setInputProcessor(stage);
	}

	@Override
	public void dispose() {
		stage.dispose();
	}

	@Override
	public void render() {		
		Gdx.gl.glClearColor(1, 1, 1, 1);
		Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
		label.setText("FPS:"+Gdx.graphics.getFramesPerSecond());
		label.setX(0);
		stage.act();
		stage.draw();
		
	}

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

	@Override
	public void pause() {
	}

	@Override
	public void resume() {
	}
}


Libgdx打地鼠一_第1张图片


Libgdx打地鼠一_第2张图片


新手代码其烂无比轻喷,点击这里下载

你可能感兴趣的:(game,libgdx)