我使用LibGDX和netBeans在java中创建了我的第一个游戏。它非常简单,所以我可以改进很多东西。我想实现一个字符选择屏幕,但我不知道如何制作可点击的图像。我将发布我的整个代码,因为我不确定什么是相关的。
package com.stars.game;
import java.util.Iterator;
import javax.swing.*;
import java.awt.event.AWTEventListener;
import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.audio.Music;
import com.badlogic.gdx.audio.Sound;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.utils.Array;
import com.badlogic.gdx.utils.TimeUtils;
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.scenes.scene2d.ui.ImageButton;
public class Stars extends ApplicationAdapter {
private OrthographicCamera camera;
long starFly = 1;
long horsePower = 0;
long musicPlay = 0;
long starCount = 0;
long fakeFishCount = 0;
long jellyLives = 3;
long horseLives = 3;
int shouldJump = 0;
int horseShouldJump = 0;
int win = 0;
int start = 1;
private Texture starImage;
private Texture shellImage;
private Texture blueStarImage;
private Texture jellyImage;
private Texture seaHImage;
private Texture fishImage;
private Texture loseImage;
private Texture shockImage;
int fishRate = 2000000000;
private Sound dingSound;
private Sound backround;
private Sound wompSound;
private Sound jumpSound;
private Sound popSound;
private Sound fishHitSound;
private Sound shellSound;
private Sound shootSound;
private Rectangle jellyfish;
private Rectangle seahorse;
private Rectangle mouseSquare;
private Array<Rectangle> stars;
private Array<Rectangle> shocks;
private Array<Rectangle> shells;
private Array<Rectangle> blueStars;
private Array<Rectangle> fishs;
private Array<Rectangle> fakeFishs;
private long lastStarTime;
private long lastShellTime;
private long lastShockTime;
private long lastFishTime;
private long lastFakeFishTime;
private long lastBlueStarTime;
private long jumpTime;
private long horseJumpTime;
private long musicTime;
private long musicTime2;
float maxJumpHeight;
SpriteBatch batch;
Texture img;
ShapeRenderer shapeRenderer;
BitmapFont font;
@Override
public void create () {
font = new BitmapFont();
starImage = new Texture(Gdx.files.internal("star.png"));
shellImage = new Texture(Gdx.files.internal("seashell.png"));
blueStarImage = new Texture(Gdx.files.internal("blueStar.png"));
dingSound = Gdx.audio.newSound((Gdx.files.internal("charlieDing.mp3")));
shellSound = Gdx.audio.newSound((Gdx.files.internal("shellSound.mp3")));
shootSound = Gdx.audio.newSound((Gdx.files.internal("zoop.mp3")));
wompSound = Gdx.audio.newSound((Gdx.files.internal("womp.mp3")));
popSound = Gdx.audio.newSound((Gdx.files.internal("pop.mp3")));
jumpSound = Gdx.audio.newSound((Gdx.files.internal("sashaJump.mp3")));
fishHitSound = Gdx.audio.newSound((Gdx.files.internal("fishHit.mp3")));
backround = Gdx.audio.newSound((Gdx.files.internal("sashaBackround.mp3")));
fishImage = new Texture(Gdx.files.internal("fish.png"));
shockImage = new Texture(Gdx.files.internal("lightning.png"));
jellyImage = new Texture(Gdx.files.internal("jellyfish.png"));
seaHImage = new Texture(Gdx.files.internal("seahorse.png"));
loseImage = new Texture(Gdx.files.internal("youLose.png"));
//Sets textures
//Sets sounds
musicTime = TimeUtils.nanoTime();
backround.play();
//Makes music loop and makes it play at the start
camera = new OrthographicCamera();
//ortho = 2d
camera.setToOrtho(false, 800,480);
//800 by 480 pixel camera, false/true whether camera points down
batch = new SpriteBatch();
//draws the screen
mouseSquare = new Rectangle();
mouseSquare.x = Gdx.input.getX();
mouseSquare.y = Gdx.input.getY();
mouseSquare.width = 10;
mouseSquare.height = 10;
jellyfish = new Rectangle();
jellyfish.x = 800 / 2 - 64 /2;
//center screen half camera width, half of bucket rectangle width
jellyfish.y = 63;
//height off the ground
jellyfish.width = 60;
jellyfish.height = 100;
seahorse = new Rectangle();
seahorse.x = 800 / 2 - 64 /2;
//center screen half camera width, half of bucket rectangle width
seahorse.y = 63;
//height off the ground
seahorse.width = 60;
seahorse.height = 110;
//dimensions of bucket rectangle
starFly = 1;
stars = new Array<Rectangle>();
shells = new Array<Rectangle>();
blueStars = new Array<Rectangle>();
fishs = new Array<Rectangle>();
shocks = new Array<Rectangle>();
fakeFishs = new Array<Rectangle>();
//creates array value for array of raindrops
spawnStar();
spawnBlueStar();
spawnFish();
spawnShell();
//calls spawnRainadrop function below
}
private void spawnShell(){
if(starFly == 1){
Rectangle shell = new Rectangle();
//creates raindrop
shell.y = MathUtils.random(80, 480 - 50);
//makes random x location between 800 and 64
shell.x = -8500;
//sets height off the ground
//dimensions
shell.width = 50;
shell.height = 50;
shells.add(shell);
//adds the raindrop to the array
lastShellTime = TimeUtils.nanoTime();
if(jellyLives == 0 && horseLives == 0){
shell.y = MathUtils.random(80, 480 - 50);
//makes random x location between 800 and 64
shell.x = -8500;
}
}
//records the amount of time since last raindrop
}
private void spawnShock(){
if(starFly == 1 && TimeUtils.nanoTime() - lastShockTime > 2100000000 ){
Rectangle shock = new Rectangle();
//creates raindrop
shock.y = jellyfish.y + 30;
//makes random x location between 800 and 64
shock.x = jellyfish.x;
//sets height off the ground
//dimensions
shootSound.play();
shock.width = 50;
shock.height = 70;
shocks.add(shock);
lastShockTime = TimeUtils.nanoTime();
//adds the raindrop to the array
}
//records the amount of time since last raindrop
}
private void spawnStar(){
if(starFly == 1){
Rectangle star = new Rectangle();
//creates raindrop
star.y = MathUtils.random(80, 480 - 50);
//makes random x location between 800 and 64
star.x = -50;
//sets height off the ground
//dimensions
star.width = 50;
star.height = 50;
stars.add★;
//adds the raindrop to the array
lastStarTime = TimeUtils.nanoTime();
}
//records the amount of time since last raindrop
}
private void spawnBlueStar(){
if(starFly == 1 && starCount >= 50){
Rectangle blueStar = new Rectangle();
//creates raindrop
blueStar.y = MathUtils.random(80, 480 - 50);
//makes random x location between 800 and 64
blueStar.x = -50;
//sets height off the ground
//dimensions
blueStar.width = 50;
blueStar.height = 50;
blueStars.add(blueStar);
//adds the raindrop to the array
lastBlueStarTime = TimeUtils.nanoTime();
}
//records the amount of time since last raindrop
}
private void spawnFish(){
if(starFly == 1){
Rectangle fish = new Rectangle();
//creates raindrop
fish.y = MathUtils.random(80, 480 - 50);
//makes random x location between 800 and 64
fish.x = 800;
//sets height off the ground
//dimensions
fish.width = 50;
fish.height = 50;
fishs.add<"(((<3enter code here;
//adds the raindrop to the array
lastFishTime = TimeUtils.nanoTime();
}
}
/* private void spawnFakeFish(){
if(fakeFishCount < 1){
Rectangle fakeFish = new Rectangle();
//creates raindrop
fakeFish.y = MathUtils.random(80, 480 - 50);
//makes random x location between 800 and 64
fakeFish.x = 800;
//sets height off the ground
//dimensions
fakeFish.width = 50;
fakeFish.height = 50;
fakeFishs.add(fakeFish);
//adds the raindrop to the array
lastFakeFishTime = TimeUtils.nanoTime();
}
}
*/
//records the amount of time since last raindrop
private void reset(){
if(jellyLives == 0 && horseLives == 0){
win = 0;
fishRate = 2000000000;
starCount = 0;
starFly = 1;
jellyLives = 3;
horseLives = 3;
jellyfish.y = 63;
seahorse.y = 63;
}
}
@Override
public void render() {
Gdx.gl.glClearColor(0,0,0.2f,1);
//backround color RGB from 0-1 transparency at end
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
//clears entire screen
camera.update();
//refreshes camera with new render
batch.setProjectionMatrix(camera.combined);
//tells spritebatch to render in the coordinate system
//specified by camera
batch.begin();
if(TimeUtils.nanoTime() - musicTime > 1870000000 && musicPlay == 0 && jellyLives > 0 ||TimeUtils.nanoTime() - musicTime > 1870000000 && musicPlay == 0 && horseLives > 0){
musicPlay = 1;
musicTime2 = TimeUtils.nanoTime();
}
if(TimeUtils.nanoTime() - musicTime2 > 1870000000 && musicPlay == 1 && jellyLives > 0 || TimeUtils.nanoTime() - musicTime2 > 1870000000 && musicPlay == 1 && horseLives > 0){
backround.play();
musicPlay = 0;
musicTime = TimeUtils.nanoTime();
}
if(start == 1){
batch.draw(seaHImage, Gdx.input.getX(), Gdx.input.getY());
}
if(win == 1){
batch.draw(loseImage, 0 - 25 , 0);
font.draw(batch, "Click to try again", 380 - 25, 180 - 20);
font.draw(batch, "Score: " + starCount, 380 - 25, 180 - 40);
}
batch.draw(jellyImage, jellyfish.x , jellyfish.y);
batch.draw(seaHImage, seahorse.x - 20 , seahorse.y);
if(win == 0){
font.draw(batch, "Stars Caught: " + starCount, 30, 30);
font.draw(batch, "Jellyfish Lives: " + jellyLives, 800 - 170, 30);
font.draw(batch, "Seahorse Lives: " + horseLives, 800 - 170, 50);
}
for(Rectangle star: stars) {
batch.draw(starImage, star.x , star.y);
}
for(Rectangle shock: shocks) {
batch.draw(shockImage, shock.x , shock.y + 10);
}
for(Rectangle shell: shells) {
batch.draw(shellImage, shell.x , shell.y);
}
for(Rectangle blueStar: blueStars) {
batch.draw(blueStarImage, blueStar.x , blueStar.y);
}
for(Rectangle fish: fishs) {
batch.draw(fishImage, fish.x , fish.y);
}
for(Rectangle fakeFish: fakeFishs) {
batch.draw(fishImage, fakeFish.x , fakeFish.y);
}
batch.end();
/*
if(Gdx.input.isKeyPressed(Keys.E)){
spawnStar();
}
if(Gdx.input.isKeyPressed(Keys.S)){
horseLives = 0;
}
if(Gdx.input.isKeyPressed(Keys.Z)){
if(jellyLives == 0){
jellyfish.y = 63;
}
jellyLives++;
}
if(Gdx.input.isKeyPressed(Keys.X)){
if(horseLives == 0){
seahorse.y = 63;
}
horseLives++;
}
if(Gdx.input.isKeyPressed(Keys.Q)){
spawnFish();
}
*/
if(Gdx.input.isKeyPressed(Keys.A)) jellyfish.x -= 600 * Gdx.graphics.getDeltaTime();
if(Gdx.input.isKeyPressed(Keys.LEFT)) seahorse.x -= 1200 * Gdx.graphics.getDeltaTime();
if(Gdx.input.isKeyPressed(Keys.D)) jellyfish.x += 600 * Gdx.graphics.getDeltaTime();
if(Gdx.input.isKeyPressed(Keys.R)) {
jellyLives = 0;
horseLives = 0;
reset();
}
if(Gdx.input.isKeyPressed(Keys.SPACE)) {
spawnShock();
}
if(Gdx.input.isKeyPressed(Keys.RIGHT)) seahorse.x += 1200 * Gdx.graphics.getDeltaTime();
if(Gdx.input.isKeyPressed(Keys.DOWN) && seahorse.y > 63) {
horseShouldJump = 0;
horsePower = 1;
}
if(seahorse.y > 63 && horsePower == 1){
seahorse.y -= 1200 * Gdx.graphics.getDeltaTime();
}
if(seahorse.y <= 63){
horsePower = 0;
}
//if(Gdx.input.isKeyPressed(Keys.UP)) player.y += 600 * Gdx.graphics.getDeltaTime();
// if(Gdx.input.isKeyPressed(Keys.DOWN)) player.y -= 600 * Gdx.graphics.getDeltaTime();
if((Gdx.input.isKeyPressed(Keys.W) && shouldJump == 0 && jellyfish.y < 800) ) {
jumpTime = TimeUtils.nanoTime();
jumpSound.play();
shouldJump = 1;
}
if((Gdx.input.isKeyPressed(Keys.UP) && horseShouldJump == 0 && seahorse.y < 800)){
horseJumpTime = TimeUtils.nanoTime();
jumpSound.play();
horseShouldJump = 1;
}
if(Gdx.input.isButtonPressed(Input.Buttons.LEFT)){
reset();
}
if(jellyfish.x < 0) {
jellyfish.x = 0;
}
//stops player from moving off screen left
if(jellyfish.x > 800 - 80) {
jellyfish.x = 800 - 80;
}
if(seahorse.x < 0) {
seahorse.x = 0;
}
//stops player from moving off screen left
if(seahorse.x > 800 - 60) {
seahorse.x = 800 - 60;
}
if(shouldJump == 1){
jellyfish.y += 12 - (jellyfish.y/30);
}
if(TimeUtils.nanoTime() - jumpTime > 1860000000 && shouldJump == 1|| TimeUtils.nanoTime() - jumpTime > 1860000000 && shouldJump == 2 ){
maxJumpHeight = jellyfish.y;
jellyfish.y -= 12 - (jellyfish.y/30);
shouldJump = 2;
}
if(jellyfish.y <= 63 && shouldJump == 2){
jumpTime = 0;
shouldJump = 0;
maxJumpHeight = 0;
}
if(horseShouldJump == 1){
seahorse.y += 12 - (seahorse.y/30);
}
if(TimeUtils.nanoTime() - horseJumpTime > 1860000000 && horseShouldJump == 1|| TimeUtils.nanoTime() - horseJumpTime > 1860000000 && horseShouldJump == 2 ){
maxJumpHeight = jellyfish.y;
seahorse.y -= 12 - (seahorse.y/30);
horseShouldJump = 2;
}
if(seahorse.y <= 63 && horseShouldJump == 2){
horseJumpTime = 0;
horseShouldJump = 0;
maxJumpHeight = 0;
}
if(TimeUtils.nanoTime() - lastStarTime > 1000000000/2) spawnStar();
//calls spawnRaindrop function if more than 1000000000 nanoseconds have gone by
Iterator<Rectangle> iter = stars.iterator();
while(iter.hasNext()) {
Rectangle star = iter.next();
star.x += 200 * Gdx.graphics.getDeltaTime();
if(star.x > 800) iter.remove();
if(star.overlaps(jellyfish) || star.overlaps(seahorse)) {
starCount++;
dingSound.play();
iter.remove();
}
//plays sound and removes raindrop if it gets the bucket or hits the bottom of the screen
}
//calls spawnRaindrop function if more than 1000000000 nanoseconds have gone by
//hi
Iterator<Rectangle> iter6 = shells.iterator();
while(iter6.hasNext()) {
Rectangle shell = iter6.next();
shell.x += 200 * Gdx.graphics.getDeltaTime();
if(shell.x > 800){
shell.x = -8500;
shell.y = MathUtils.random(80, 480 - 50);
}
if(shell.overlaps(jellyfish)) {
if(horseLives > 0){
jellyLives++;
}
if(horseLives == 0){
horseLives++;
seahorse.y = 63;
}
shellSound.play();
shell.y = MathUtils.random(80, 480 - 50);
shell.x = -8500;
}
if(shell.overlaps(seahorse)) {
if(jellyLives > 0){
horseLives++;
}
if(jellyLives == 0){
jellyLives++;
jellyfish.y = 63;
}
shellSound.play();
shell.y = MathUtils.random(80, 480 - 50);
shell.x = -8500;
}
//plays sound and removes raindrop if it gets the bucket or hits the bottom of the screen
}
if(TimeUtils.nanoTime() - lastBlueStarTime > 2147483000/2) spawnBlueStar();
//calls spawnRaindrop function if more than 1000000000 nanoseconds have gone by
Iterator<Rectangle> iter3 = blueStars.iterator();
while(iter3.hasNext()) {
Rectangle blueStar = iter3.next();
blueStar.x += 200 * Gdx.graphics.getDeltaTime();
if(blueStar.x > 800) iter3.remove();
if(blueStar.overlaps(jellyfish) || blueStar.overlaps(seahorse)) {
starCount += 3;
dingSound.play();
iter3.remove();
}
//plays sound and removes raindrop if it gets the bucket or hits the bottom of the screen
}
if(TimeUtils.nanoTime() - lastFakeFishTime > 1){
if(fakeFishCount < 2000){
/* spawnFakeFish(); */
}
}
//calls spawnRaindrop function if more than 1000000000 nanoseconds have gone by
Iterator<Rectangle> iter5 = fakeFishs.iterator();
while(iter5.hasNext()) {
Rectangle fakeFish = iter5.next();
fakeFish.x -= 200 * Gdx.graphics.getDeltaTime();
if(fakeFish.x < -80){
fakeFishCount++;
iter5.remove();
}
/*if(fish.overlaps(jellyfish)) {
jellyfish.y = 9000000;
starFly = 0;
win = 1;
wompSound.play();
iter5.remove();
}*/
//plays sound and removes raindrop if it gets the bucket or hits the bottom of the screen
}
if(starCount > 50){
fishRate = 1300000000;
}
if (jellyLives == 0){
jellyfish.y = 9000000;
}
if (horseLives == 0){
seahorse.y = 9000000;
}
if(horseLives == 0 && jellyLives == 0){
win = 1;
starFly = 0;
backround.pause();
dingSound.pause();
popSound.pause();
}
if(TimeUtils.nanoTime() - lastFishTime > fishRate) {
spawnFish();
//calls spawnRaindrop function if more than 1000000000 nanoseconds have gone by
}
Iterator<Rectangle> iter2 = fishs.iterator();
while(iter2.hasNext()) {
Rectangle fish = iter2.next();
fish.x -= 200 * Gdx.graphics.getDeltaTime();
if(fish.x < -800) iter2.remove();
if(fish.overlaps(jellyfish)) {
jellyLives--;
popSound.play();
if(jellyLives == 0){
wompSound.play();
}
iter2.remove();
}
if(fish.overlaps(seahorse)){
horseLives--;
popSound.play();
if(horseLives == 0){
wompSound.play();
}
iter2.remove();
}
Iterator<Rectangle> iter7 = shocks.iterator();
while(iter7.hasNext()) {
Rectangle shock = iter7.next();
shock.y += 4;
if(shock.y > 520) iter7.remove();
if(fish.overlaps(shock)) {
fishHitSound.play();
iter7.remove();
iter2.remove();
}
//plays sound and removes raindrop if it gets the bucket or hits the bottom of the screen
}
}
//plays sound and removes raindrop if it gets the bucket or hits the bottom of the screen
}
}
答案 0 :(得分:0)
我认为最好的选择是使用com.badlogic.gdx.scenes.scene2d.ui包类。示例如下:
public class ScreenMainMenu implements Screen{
private Stage stage;
private Skin skin;
public ScreenMainMenu(StartScreen startScreen){
}
@Override
public void show() {
stage = new Stage(Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), false);
Gdx.input.setInputProcessor(stage);
skin = new Skin(Gdx.files.internal("data/skins/skin.json"));
buildMenu();
}
@Override
public void hide() {
stage = null;
skin = null;
}
@Override
public void render(float delta) {
Gdx.gl.glClearColor(0,0,0.2f,1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
stage.act();
stage.draw();
}
private void buildMenu(){
Table menuTable = new Table(skin);
menuTable.setPosition(0, 0);
menuTable.setSize(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
menuTable.align(Align.top);
stage.addActor(menuTable);
Texture texture = new Texture(Gdx.files.internal("data/ubuntulogo.png"));
Image image = new Image(texture);
//first option
image.addListener(new ClickListener(){
@Override
public void clicked(InputEvent event, float x, float y) {
//your action on click
}
});
menuTable.add(image);
//second option
ImageButton imageButton = new ImageButton(image.getDrawable());
imageButton.addListener(new ClickListener(){
@Override
public void clicked(InputEvent event, float x, float y) {
//your action on click
}
});
menuTable.add(imageButton);
//third option
Button button = new Button(skin);
button.add(image);
button.addListener(new ClickListener(){
@Override
public void clicked(InputEvent event, float x, float y) {
//your action on click
}
});
menuTable.add(button);
}
@Override
public void resize(int width, int height) {
}
@Override
public void pause() {
}
@Override
public void resume() {
}
@Override
public void dispose() {
}
}
根据libgdx的版本,可能会有一些小的差异,尤其是Stage构造函数可能会有所不同。