initial commit

This commit is contained in:
Mahdi Dibaiee
2018-04-16 16:07:09 +04:30
commit f2e93fa79b
30 changed files with 1068 additions and 0 deletions

11
core/build.gradle Normal file
View File

@ -0,0 +1,11 @@
apply plugin: "java"
sourceCompatibility = 1.6
[compileJava, compileTestJava]*.options*.encoding = 'UTF-8'
sourceSets.main.java.srcDirs = [ "src/" ]
eclipse.project {
name = appName + "-core"
}

View File

@ -0,0 +1,28 @@
package com.mdibaiee.supersnake;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.physics.box2d.Shape;
import com.badlogic.gdx.utils.Array;
public class Ball extends Point {
private Color color = Colors.ball;
public int radius = 15;
public int size = radius * 2;
public Ball(float x, float y) {
super(x, y);
}
public void draw(ShapeRenderer shapeRenderer) {
shapeRenderer.begin(ShapeRenderer.ShapeType.Filled);
shapeRenderer.setColor(color);
shapeRenderer.circle(x, y, radius);
shapeRenderer.end();
}
}

View File

@ -0,0 +1,9 @@
package com.mdibaiee.supersnake;
import com.badlogic.gdx.graphics.Color;
public class Colors {
static public Color background = new Color(0.20f, 0.28f, 0.37f, 1);
static public Color snake = new Color(0, 0.9f, 0.7f, 1);
static public Color ball = new Color(1, 0.4f, 0, 1);
}

View File

@ -0,0 +1,16 @@
package com.mdibaiee.supersnake;
public class DirectedPoint extends Point {
public Direction direction;
public DirectedPoint(float x, float y, Direction direction) {
super(x, y);
this.direction = direction;
}
public void setDirection(Direction direction) {
if (Direction.oppositeDirections(this.direction, direction)) return;
this.direction = direction;
}
}

View File

@ -0,0 +1,15 @@
package com.mdibaiee.supersnake;
public enum Direction {
Left, Right,
Up, Down;
static public boolean oppositeDirections(Direction a, Direction b) {
if ((a == Left && b == Right) || (a == Right && b == Left)) return true;
if ((a == Up && b == Down) || (a == Down && b == Up)) return true;
return false;
};
};

View File

@ -0,0 +1,44 @@
package com.mdibaiee.supersnake;
import com.badlogic.gdx.Gdx;
public class Point {
public float x;
public float y;
private int WIDTH = Gdx.graphics.getBackBufferWidth();
private int HEIGHT = Gdx.graphics.getBackBufferHeight();
public Point(float x, float y) {
this.x = x;
this.y = y;
}
public boolean move(float x, float y) {
this.x = x;
this.y = y;
if (this.x > WIDTH) {
this.x -= WIDTH;
return true;
}
if (this.y > HEIGHT) {
this.y -= HEIGHT;
return true;
}
if (this.x < 0) {
this.x += WIDTH;
return true;
}
if (this.y < 0) {
this.y += HEIGHT;
return true;
}
return false;
}
public double distance(Point p) {
return Math.sqrt(Math.pow(this.x - p.x, 2) + Math.pow(this.y - p.y, 2));
}
}

View File

@ -0,0 +1,130 @@
package com.mdibaiee.supersnake;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.physics.box2d.Shape;
import com.badlogic.gdx.utils.Array;
class Tail extends DirectedPoint {
public int length;
public boolean breaking;
public Tail(float x, float y, Direction direction, int length) {
super(x, y, direction);
this.length = length;
}
public Tail(float x, float y, Direction direction, boolean breaking) {
super(x, y, direction);
this.breaking = breaking;
}
}
public class Snake extends DirectedPoint {
private Color color = Colors.snake;
public int size = 10;
private Array<Tail> tail = new Array<Tail>();
public int speed = 2;
public int lives = 3;
public Snake(float x, float y, int length) {
super(x, y, Direction.Left);
tail.insert(0, new Tail(x + length, y, direction, length));
}
private void draw_line(ShapeRenderer shapeRenderer, float x0, float y0, float x1, float y1) {
shapeRenderer.begin(ShapeRenderer.ShapeType.Filled);
shapeRenderer.setColor(color);
shapeRenderer.rectLine(x0, y0, x1, y1, size);
shapeRenderer.end();
}
public void draw(ShapeRenderer shapeRenderer) {
draw_line(shapeRenderer, this.x, this.y, tail.first().x, tail.first().y);
for (int i = 0; i < tail.size - 1; i++) {
Tail t = tail.get(i);
if (t.breaking) continue;
Tail n = tail.get(i + 1);
draw_line(shapeRenderer, t.x, t.y, n.x, n.y);
}
}
public boolean isDead() {
return lives < 1;
}
@Override
public void setDirection(Direction direction) {
if (Direction.oppositeDirections(this.direction, direction)) return;
if (this.direction == direction) return;
tail.insert(0, new Tail(x, y, direction, 0));
this.direction = direction;
}
private int cx(Direction direction) {
if (direction == Direction.Left) return -1;
if (direction == Direction.Right) return 1;
return 0;
}
private int cy(Direction direction) {
if (direction == Direction.Up) return 1;
if (direction == Direction.Down) return -1;
return 0;
}
private Direction getNewDirection(DirectedPoint point, DirectedPoint last) {
if (point.direction == last.direction) return point.direction;
float expectedX = point.x + cx(last.direction) * size;
float expectedY = point.y + cy(last.direction) * size;
if (Math.abs(last.x - expectedX) <= speed &&
Math.abs(last.y - expectedY) <= speed) {
return last.direction;
}
return point.direction;
}
public void move() {
float ox = x;
float oy = y;
boolean cycled = this.move(x + cx(direction) * speed,
y + cy(direction) * speed);
if (cycled) {
tail.insert(0, new Tail(ox, oy, direction, 0));
tail.insert(0, new Tail(x, y, direction, true));
}
Tail first = tail.first();
first.length += speed;
Tail last = tail.peek();
last.move(last.x + cx(last.direction) * speed, last.y + cy(last.direction) * speed);
last.length -= speed;
if (last.length <= 0) {
tail.removeIndex(tail.size - 1);
}
}
public void addTail() {
Tail last = tail.peek();
last.length += 5;
last.x -= cx(last.direction) * 5;
last.y -= cy(last.direction) * 5;
speed += 1;
}
}

View File

@ -0,0 +1,96 @@
package com.mdibaiee.supersnake;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.mdibaiee.supersnake.Colors;
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture;
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.graphics.g2d.BitmapFont;
import com.badlogic.gdx.utils.Array;
public class SuperSnake extends ApplicationAdapter {
public int WIDTH = 800;
public int HEIGHT = 480;
int points = 0;
SpriteBatch batch;
ShapeRenderer shapeRenderer;
Texture img;
Snake snake;
BitmapFont font;
private Array<Ball> balls = new Array<Ball>();
@Override
public void create () {
batch = new SpriteBatch();
font = new BitmapFont();
WIDTH = Gdx.graphics.getBackBufferWidth();
HEIGHT = Gdx.graphics.getBackBufferHeight();
shapeRenderer = new ShapeRenderer();
snake = new Snake(WIDTH / 2, HEIGHT / 2, 50);
}
@Override
public void render () {
Gdx.gl.glClearColor(Colors.background.r, Colors.background.g, Colors.background.b, Colors.background.a);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
if(Gdx.input.justTouched()) {
float x = Gdx.input.getX();
float y = Gdx.input.getY();
if (snake.direction == Direction.Left || snake.direction == Direction.Right) {
if (y < HEIGHT / 2) {
snake.setDirection(Direction.Up);
} else {
snake.setDirection(Direction.Down);
}
} else {
if (x < WIDTH / 2) {
snake.setDirection(Direction.Left);
} else {
snake.setDirection(Direction.Right);
}
}
}
if (balls.size < 1) {
balls.add(new Ball((float) Math.random() * WIDTH, (float) Math.random() * HEIGHT));
}
snake.draw(shapeRenderer);
snake.move();
for(Ball b: balls) {
b.draw(shapeRenderer);
}
if (snake.distance(balls.first()) < balls.first().size) {
balls.removeIndex(0);
points += 1;
snake.addTail();
}
batch.begin();
font.draw(batch, Integer.toString(points), 10, 25);
batch.end();
}
@Override
public void dispose () {
batch.dispose();
img.dispose();
font.dispose();
shapeRenderer.dispose();
}
}