Skip to content

Commit

Permalink
Turtle is back
Browse files Browse the repository at this point in the history
  • Loading branch information
pmudry committed May 30, 2016
1 parent 199c600 commit fb0f589
Showing 1 changed file with 96 additions and 0 deletions.
96 changes: 96 additions & 0 deletions gdx2d-demoDesktop/src/ch/hevs/gdx2d/demos/simple/DemoTurtle.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package ch.hevs.gdx2d.demos.simple;

import com.badlogic.gdx.graphics.Color;

import ch.hevs.gdx2d.components.graphics.Turtle;
import ch.hevs.gdx2d.desktop.PortableApplication;
import ch.hevs.gdx2d.lib.GdxGraphics;

/**
* A very simple demonstration on how to display something animated with the
* turtle paradigm
*
* @author Pierre-André Mudry (mui)
* @version 1.0
*/
public class DemoTurtle extends PortableApplication {
Turtle t;

/**
* Draws the snowflake
* @param length Length of the side of the flake
* @param level The complexity level of the recursion
*/
void drawFlake(double length, int level) {
drawSegment(length, level);
t.turn(120);
drawSegment(length, level);
t.turn(120);
drawSegment(length, level);
t.turn(120);
}

/**
* Draws a side of the flake
* @param length length of the segment
* @param level complexity level for the recursion
*/
void drawSegment(double length, int level) {
//Recursion shall end somewhere
if (level == 0) {
t.forward(length);
return;
}

double currentLevel = length / 3.0;
drawSegment(currentLevel, level - 1);
t.turn(-60);
drawSegment(currentLevel, level - 1);
t.turn(120);
drawSegment(currentLevel, level - 1);
t.turn(-60);
drawSegment(currentLevel, level - 1);
}

@Override
public void onInit() {
setTitle("Moving turtle demo, mui 2016");
}

int flakeSize = 2, animDirection = 1;

@Override
public void onGraphicRender(GdxGraphics g) {
if (t == null)
t = new Turtle(g, g.getScreenWidth(), g.getScreenHeight());

// Clears the screen
g.clear(Color.DARK_GRAY);

// Go to the top of the screen
t.changeColor(Color.WHITE);

/**
* Locate the turtle at the bottom of the screen
*/
t.penDown();
t.setAngle(60);
t.jump((float) (g.getScreenWidth() * 0.5), (float) (g.getScreenHeight() * 0.25));

// Displays the snowflake
drawFlake(20 + flakeSize * 2, 4);

// Animate size
flakeSize += animDirection;

if (flakeSize == 100 || flakeSize == 2)
animDirection *= -1;

g.drawFPS();
g.drawSchoolLogo();
}

public static void main(String[] args) {
new DemoTurtle();
}
}

0 comments on commit fb0f589

Please sign in to comment.