Skip to content

Instantly share code, notes, and snippets.

@housemeow
Created October 18, 2013 12:44
Show Gist options
  • Select an option

  • Save housemeow/7040976 to your computer and use it in GitHub Desktop.

Select an option

Save housemeow/7040976 to your computer and use it in GitHub Desktop.
package com.me.mygdxgame;
import java.util.ArrayList;
import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.Texture.TextureFilter;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.graphics.g2d.TextureAtlas.AtlasRegion;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.Vector3;
public class MyGdxGame implements ApplicationListener {
private OrthographicCamera camera;
private SpriteBatch batch;
private Sprite sprite;
private ArrayList<Sprite> spriteList = new ArrayList<Sprite>();
private int topLeft = 0;
private int top = 0;
private int topRight = 0;
private int left = 0;
private int center = 0;
private int right = 0;
private int bottomLeft = 0;
private int bottom = 0;
private int bottomRight = 0;
@Override
public void create() {
camera = new OrthographicCamera(Gdx.graphics.getWidth(),
Gdx.graphics.getHeight());
batch = new SpriteBatch();
TextureAtlas atlas;
atlas = new TextureAtlas(Gdx.files.internal("data/tictactoe.pack"));
AtlasRegion region = atlas.findRegion("board");
sprite = new Sprite(region);
sprite.setSize(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
sprite.setPosition(-sprite.getWidth() / 2, -sprite.getHeight() / 2);
// float w = Gdx.graphics.getWidth();
// float h = Gdx.graphics.getHeight();
//
// camera = new OrthographicCamera(1, h/w);
// batch = new SpriteBatch();
//
// texture = new Texture(Gdx.files.internal("data/libgdx.png"));
// texture.setFilter(TextureFilter.Linear, TextureFilter.Linear);
//
// TextureRegion region = new TextureRegion(texture, 0, 0, 512, 275);
//
// sprite = new Sprite(region);
// sprite.setSize(0.9f, 0.9f * sprite.getHeight() / sprite.getWidth());
// sprite.setOrigin(sprite.getWidth()/2, sprite.getHeight()/2);
// sprite.setPosition(-sprite.getWidth()/2, -sprite.getHeight()/2);
}
@Override
public void dispose() {
batch.dispose();
// texture.dispose();
}
@Override
public void render() {
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
batch.setProjectionMatrix(camera.combined);
batch.begin();
sprite.draw(batch);
for (Sprite spriteItem : spriteList) {
spriteItem.draw(batch);
}
batch.end();
if (Gdx.input.isTouched()) {
Gdx.app.log("x", Gdx.input.getX() + "");
Gdx.app.log("y", Gdx.input.getY() + "");
Vector3 touchPos = new Vector3();
touchPos.set(Gdx.input.getX(), Gdx.input.getY(), 0);
camera.unproject(touchPos);
Gdx.app.log("touchx", touchPos.x + "");
Gdx.app.log("touchy", touchPos.y + "");
if (canPut(Gdx.input.getX(), Gdx.input.getY())) {
TextureAtlas atlas;
atlas = new TextureAtlas(
Gdx.files.internal("data/tictactoe.pack"));
AtlasRegion region = atlas.findRegion("o");
Sprite oSprite = new Sprite(region);
oSprite.setPosition(touchPos.x, touchPos.y);
spriteList.add(oSprite);
}
}
}
boolean canPut(float x, float y) {
if (25 < x && x <= 120) {
// x左邊
if (130 < y && y <= 225) {
// y上方
if (topLeft == 0) {
// 沒有放過旗子
topLeft = 1;
return true;
}
} else if (225 < y && y <= 320) {
// y中間
if (left == 0) {
// 沒放過旗子
left = 1;
return true;
}
} else if (320 < y && y <= 415) {
// y下方
if (bottomLeft == 0) {
bottomLeft = 1;
return true;
}
}
} else if (120 < x && x <= 215) {
// x正中間
if (130 < y && y <= 225) {
// y上方
if (top == 0) {
// 沒有放過旗子
top = 1;
return true;
}
} else if (225 < y && y <= 320) {
// y中間
if (center == 0) {
// 沒放過旗子
center = 1;
return true;
}
} else if (320 < y && y <= 415) {
// y下方
if (bottom == 0) {
bottom = 1;
return true;
}
}
} else if (215<x && x <= 310) {
// x右邊
if (130 < y && y <= 225) {
// y上方
if (topRight == 0) {
// 沒有放過旗子
topRight = 1;
return true;
}
} else if (225 < y && y <= 320) {
// y中間
if (right == 0) {
// 沒放過旗子
right = 1;
return true;
}
} else if (320 < y && y <= 415) {
// y下方
if (bottomRight == 0) {
bottomRight = 1;
return true;
}
}
}
return false;
}
@Override
public void resize(int width, int height) {
}
@Override
public void pause() {
}
@Override
public void resume() {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment