diff --git a/android/assets/blue.png b/android/assets/blue.png new file mode 100644 index 0000000..881c7bd Binary files /dev/null and b/android/assets/blue.png differ diff --git a/android/assets/green.png b/android/assets/green.png new file mode 100644 index 0000000..10819e0 Binary files /dev/null and b/android/assets/green.png differ diff --git a/android/assets/skull.png b/android/assets/skull.png new file mode 100644 index 0000000..f5b2a93 Binary files /dev/null and b/android/assets/skull.png differ diff --git a/android/assets/spiral.png b/android/assets/spiral.png new file mode 100644 index 0000000..0725950 Binary files /dev/null and b/android/assets/spiral.png differ diff --git a/android/assets/sprites.svg b/android/assets/sprites.svg new file mode 100644 index 0000000..dae1e26 --- /dev/null +++ b/android/assets/sprites.svg @@ -0,0 +1,142 @@ + + + + + + + + + + + + + image/svg+xml + + + + + + + + + + + + + + + + diff --git a/android/assets/star.png b/android/assets/star.png new file mode 100644 index 0000000..966cefe Binary files /dev/null and b/android/assets/star.png differ diff --git a/core/build.gradle b/core/build.gradle index 03cd1be..e06ac13 100644 --- a/core/build.gradle +++ b/core/build.gradle @@ -1,6 +1,7 @@ apply plugin: "java" -sourceCompatibility = 1.6 +targetCompatibility = 1.8 +sourceCompatibility = 1.8 [compileJava, compileTestJava]*.options*.encoding = 'UTF-8' sourceSets.main.java.srcDirs = [ "src/" ] diff --git a/core/src/com/mdibaiee/supersnake/Ball.java b/core/src/com/mdibaiee/supersnake/Ball.java deleted file mode 100644 index eda35e1..0000000 --- a/core/src/com/mdibaiee/supersnake/Ball.java +++ /dev/null @@ -1,28 +0,0 @@ -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(); - } -} diff --git a/core/src/com/mdibaiee/supersnake/Colors.java b/core/src/com/mdibaiee/supersnake/Colors.java index b81a446..da696dd 100644 --- a/core/src/com/mdibaiee/supersnake/Colors.java +++ b/core/src/com/mdibaiee/supersnake/Colors.java @@ -6,4 +6,5 @@ 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); + static public Color red = new Color(1, 0.34f, 0, 1); } diff --git a/core/src/com/mdibaiee/supersnake/Magic.java b/core/src/com/mdibaiee/supersnake/Magic.java new file mode 100644 index 0000000..dfd3e87 --- /dev/null +++ b/core/src/com/mdibaiee/supersnake/Magic.java @@ -0,0 +1,75 @@ +package com.mdibaiee.supersnake; + +import com.badlogic.gdx.Gdx; +import com.badlogic.gdx.graphics.g2d.SpriteBatch; +import com.badlogic.gdx.graphics.Texture; +import com.badlogic.gdx.graphics.glutils.ShapeRenderer; + +public abstract class Magic extends SizedPoint { + SpriteBatch batch; + private Texture image; + public boolean active = false; + public int frames = 0; + public int drawn = 0; + public int lifetime = 20 * 60; + + public int srcX = 0; + public int srcY = 0; + + public int srcWidth = 0; + public int srcHeight = 0; + + public float rotation = 0; + public float scale = 1; + + private float drawn_width; + private float drawn_height; + + public Magic(float x, float y, String image_url) { + super(x, y, 40); + + image = new Texture(Gdx.files.internal(image_url)); + batch = new SpriteBatch(); + + srcWidth = image.getWidth(); + srcHeight = image.getHeight(); + float ratio = (float) srcHeight / (float) srcWidth; + + drawn_width = size; + drawn_height = size * ratio; + size = Math.max(size, size * ratio); + } + + public boolean draw() { + drawn += 1; + float width = image.getWidth(); + float height = image.getHeight(); + + batch.begin(); + + batch.draw(image, x, y, + size / 2, size / 2, + drawn_width, drawn_height, + scale, scale, + rotation, + srcX, srcY, + srcWidth, srcHeight, + false, false); + + batch.end(); + + lifetime -= 1; + + if (lifetime <= 0) { + return true; + } + + return false; + } + + abstract public void action(Snake snake); + + // this method will be called after action at each iteration until it returns true + // when it does return true ,the object is disposed + abstract public boolean iter(); +} diff --git a/core/src/com/mdibaiee/supersnake/Magics/Growth.java b/core/src/com/mdibaiee/supersnake/Magics/Growth.java new file mode 100644 index 0000000..ac432bd --- /dev/null +++ b/core/src/com/mdibaiee/supersnake/Magics/Growth.java @@ -0,0 +1,36 @@ +package com.mdibaiee.supersnake.Magics; + +import com.mdibaiee.supersnake.Magic; +import com.mdibaiee.supersnake.Snake; + +public class Growth extends Magic { + private Snake snake; + private int seconds = 30; + + public Growth(float x, float y) { + super(x, y, "blue.png"); + } + + public void action(Snake snake) { + this.snake = snake; + snake.size += 10; + } + + public boolean draw() { + double cycle = Math.sin(((drawn * 6) % 360) * Math.PI / 360); + scale = (float) (1 + cycle * 0.3); + + return super.draw(); + } + + public boolean iter() { + frames++; + + if (frames > seconds * 60) { + snake.size -= 10; + return true; + } + + return false; + } +} diff --git a/core/src/com/mdibaiee/supersnake/Magics/Skull.java b/core/src/com/mdibaiee/supersnake/Magics/Skull.java new file mode 100644 index 0000000..1a94bcb --- /dev/null +++ b/core/src/com/mdibaiee/supersnake/Magics/Skull.java @@ -0,0 +1,30 @@ +package com.mdibaiee.supersnake.Magics; + +import com.mdibaiee.supersnake.Colors; +import com.mdibaiee.supersnake.Magic; +import com.mdibaiee.supersnake.Snake; + +public class Skull extends Magic { + private Snake snake; + private int seconds = 15; + + public Skull(float x, float y) { + super(x, y, "skull.png"); + } + + public void action(Snake snake) { + this.snake = snake; + snake.color = Colors.red; + } + + public boolean iter() { + frames++; + + if (frames > seconds * 60) { + snake.color = Colors.snake; + return true; + } + + return false; + } +} diff --git a/core/src/com/mdibaiee/supersnake/Magics/SpeedBoost.java b/core/src/com/mdibaiee/supersnake/Magics/SpeedBoost.java new file mode 100644 index 0000000..93153d2 --- /dev/null +++ b/core/src/com/mdibaiee/supersnake/Magics/SpeedBoost.java @@ -0,0 +1,44 @@ +package com.mdibaiee.supersnake.Magics; + +import com.mdibaiee.supersnake.Magic; +import com.mdibaiee.supersnake.Snake; + +public class SpeedBoost extends Magic { + private Snake snake; + private int seconds = 30; + + private float original_x; + private float original_y; + + public SpeedBoost(float x, float y) { + super(x, y, "green.png"); + original_x = x; + original_y = y; + } + + public void action(Snake snake) { + this.snake = snake; + snake.speed += 5; + } + + public boolean draw() { + double rx = Math.random() * 5; + double ry = Math.random() * 5; + + x = (float) (original_x + rx); + y = (float) (original_y + ry); + + return super.draw(); + } + + public boolean iter() { + frames++; + + if (frames > seconds * 60) { + snake.speed -= 5; + return true; + } + + return false; + } +} diff --git a/core/src/com/mdibaiee/supersnake/Magics/StarPoint.java b/core/src/com/mdibaiee/supersnake/Magics/StarPoint.java new file mode 100644 index 0000000..1a2ef22 --- /dev/null +++ b/core/src/com/mdibaiee/supersnake/Magics/StarPoint.java @@ -0,0 +1,27 @@ +package com.mdibaiee.supersnake.Magics; + + +import com.badlogic.gdx.Gdx; +import com.mdibaiee.supersnake.Magic; +import com.mdibaiee.supersnake.Snake; + +public class StarPoint extends Magic { + public StarPoint(float x, float y) { + super(x, y, "star.png"); + } + + public boolean draw() { + rotation = drawn % 360; + + return super.draw(); + } + + public void action(Snake snake) { + snake.point++; + snake.addTail(); + } + + public boolean iter() { + return true; + } +} diff --git a/core/src/com/mdibaiee/supersnake/Point.java b/core/src/com/mdibaiee/supersnake/Point.java index ccc6ee8..05d6137 100644 --- a/core/src/com/mdibaiee/supersnake/Point.java +++ b/core/src/com/mdibaiee/supersnake/Point.java @@ -39,6 +39,6 @@ public class Point { } public double distance(Point p) { - return Math.sqrt(Math.pow(this.x - p.x, 2) + Math.pow(this.y - p.y, 2)); + return Math.sqrt(Math.pow(x - p.x, 2) + Math.pow(y - p.y, 2)); } } diff --git a/core/src/com/mdibaiee/supersnake/SizedPoint.java b/core/src/com/mdibaiee/supersnake/SizedPoint.java new file mode 100644 index 0000000..6bf726f --- /dev/null +++ b/core/src/com/mdibaiee/supersnake/SizedPoint.java @@ -0,0 +1,27 @@ +package com.mdibaiee.supersnake; + +public class SizedPoint extends Point { + public float size; + + public SizedPoint(float x, float y, float size) { + super(x, y); + + this.size = size; + } + + public float centerX() { + return x + size / 2; + } + public float centerY() { + return y + size / 2; + } + + public double distance(SizedPoint other) { + return Math.sqrt(Math.pow(centerX() - other.centerX(), 2) + + Math.pow(centerY() - other.centerY(), 2)); + } + + public boolean collides(SizedPoint other) { + return distance(other) < (size / 2 + other.size / 2); + } +} diff --git a/core/src/com/mdibaiee/supersnake/Snake.java b/core/src/com/mdibaiee/supersnake/Snake.java index def99ca..6fcb5e1 100644 --- a/core/src/com/mdibaiee/supersnake/Snake.java +++ b/core/src/com/mdibaiee/supersnake/Snake.java @@ -10,10 +10,10 @@ import com.badlogic.gdx.physics.box2d.Shape; import com.badlogic.gdx.utils.Array; class Tail extends DirectedPoint { - public int length; + public float length; public boolean breaking; - public Tail(float x, float y, Direction direction, int length) { + public Tail(float x, float y, Direction direction, float length) { super(x, y, direction); this.length = length; } @@ -25,12 +25,13 @@ class Tail extends DirectedPoint { } public class Snake extends DirectedPoint { - private Color color = Colors.snake; + public Color color = Colors.snake; public int size = 10; + public int point = 0; private Array tail = new Array(); - public int speed = 2; + public float speed = 2; public int lives = 3; public Snake(float x, float y, int length) { @@ -82,26 +83,14 @@ public class Snake extends DirectedPoint { 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); + Gdx.app.log("Snake", "(" + ox + ", " + oy + ") > " + "(" + x + ", " + y + ")"); + if (cycled) { tail.insert(0, new Tail(ox, oy, direction, 0)); tail.insert(0, new Tail(x, y, direction, true)); @@ -114,8 +103,10 @@ public class Snake extends DirectedPoint { last.move(last.x + cx(last.direction) * speed, last.y + cy(last.direction) * speed); last.length -= speed; - if (last.length <= 0) { + while (tail.peek().length <= 0) { + float diff = tail.peek().length; tail.removeIndex(tail.size - 1); + tail.peek().length += diff; } } @@ -125,6 +116,11 @@ public class Snake extends DirectedPoint { last.x -= cx(last.direction) * 5; last.y -= cy(last.direction) * 5; - speed += 1; + speed += 0.5; + } + + public boolean head_collision(SizedPoint p) { + SizedPoint s = new SizedPoint(x - size / 2, y - size / 2, size); + return s.collides(p); } } diff --git a/core/src/com/mdibaiee/supersnake/SuperSnake.java b/core/src/com/mdibaiee/supersnake/SuperSnake.java index 02dc597..b56ad3e 100644 --- a/core/src/com/mdibaiee/supersnake/SuperSnake.java +++ b/core/src/com/mdibaiee/supersnake/SuperSnake.java @@ -1,7 +1,5 @@ package com.mdibaiee.supersnake; - -import com.badlogic.gdx.graphics.OrthographicCamera; -import com.mdibaiee.supersnake.Colors; +import com.mdibaiee.supersnake.Magics.*; import com.badlogic.gdx.ApplicationAdapter; import com.badlogic.gdx.Gdx; @@ -9,7 +7,6 @@ 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; @@ -17,15 +14,13 @@ 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 balls = new Array(); + private Array magics = new Array(); @Override public void create () { @@ -35,6 +30,8 @@ public class SuperSnake extends ApplicationAdapter { WIDTH = Gdx.graphics.getBackBufferWidth(); HEIGHT = Gdx.graphics.getBackBufferHeight(); + Gdx.app.log("Snake", "VIEWPORT " + WIDTH + ", " + HEIGHT); + shapeRenderer = new ShapeRenderer(); snake = new Snake(WIDTH / 2, HEIGHT / 2, 50); @@ -64,25 +61,53 @@ public class SuperSnake extends ApplicationAdapter { } } - if (balls.size < 1) { - balls.add(new Ball((float) Math.random() * WIDTH, (float) Math.random() * HEIGHT)); + int drawn_magics = 0; + + for (Magic m: magics) { + if (!m.active) drawn_magics++; + } + + if (drawn_magics < 3) { + double r = Math.random() * 100; + float mx = (float) Math.random() * WIDTH; + float my = (float) Math.random() * HEIGHT; + + Magic newMagic; + if (r < 70) { + newMagic = new StarPoint(mx, my); + } else if (r < 80) { + newMagic = new SpeedBoost(mx, my); + } else if (r < 90) { + newMagic = new Growth(mx, my); + } else { + newMagic = new Skull(mx, my); + } + + newMagic = new SpeedBoost(mx, my); + + magics.add(newMagic); } 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(); + for(Magic m: magics) { + if (m.active) { + if (m.iter()) { + magics.removeValue(m, true); + } + } else { + if (m.draw()) { + magics.removeValue(m, true); + } else if (snake.head_collision(m)) { + m.action(snake); + m.active = true; + } + } } batch.begin(); - font.draw(batch, Integer.toString(points), 10, 25); + font.draw(batch, Integer.toString(snake.point), 10, 25); batch.end(); }