Blame 15.c

c0abb6
c0abb6
#include <stdio.h></stdio.h>
c0abb6
#include <helianthus.h></helianthus.h>
c0abb6
c0abb6
c0abb6
#define CELLSIZE 128
c0abb6
c0abb6
c0abb6
Group edges;
c0abb6
Group group;
c0abb6
Animation frame;
c0abb6
Sprite selected;
c0abb6
double dx, dy;
c0abb6
c0abb6
c0abb6
void generate() {
c0abb6
  int snake[16];
c0abb6
  for(int i = 0; i < 16; ++i)
c0abb6
    snake[i] = i;
c0abb6
c0abb6
  for(int i = 0; i < 128; ++i) {
c0abb6
    int j = randomNumber(0, 15);
c0abb6
    int k = randomNumber(0, 15);
c0abb6
    int s = snake[j];
c0abb6
    snake[j] = snake[k];
c0abb6
    snake[k] = s;
c0abb6
  }
c0abb6
c0abb6
  int count = 0;
c0abb6
  for(int i = 0; i < 16; ++i)
c0abb6
    for(int j = i+1; j < 16; ++j)
c0abb6
      if (snake[i] && snake[j] && snake[i] < snake[j])
c0abb6
        ++count;
c0abb6
c0abb6
  if (count % 2) {
c0abb6
    for(int i = 0; i < 15; ++i) {
c0abb6
      if (snake[i] && snake[i+1]) {
c0abb6
        int s = snake[i];
c0abb6
        snake[i] = snake[i+1];
c0abb6
        snake[i+1] = s;
c0abb6
        break;
c0abb6
      }
c0abb6
    }
c0abb6
  }
c0abb6
c0abb6
  for(int i = 0; i < 16; ++i) {
c0abb6
    int y = i/4;
c0abb6
    int x = i%4;
c0abb6
    if (y%2) x = 3 - x;
c0abb6
    if (snake[i])
c0abb6
      spriteSetXY(groupGet(group, snake[i] - 1), x + 0.5, y + 0.5);
c0abb6
  }
c0abb6
}
c0abb6
c0abb6
c0abb6
void init() {
c0abb6
  edges = createEdgesGroupEx(0, 0, 4, 4, 1, 100);
c0abb6
  groupSetVisibleEach(edges, FALSE);
c0abb6
c0abb6
  frame = createAnimation("data/sprite/15/frame.png");
c0abb6
c0abb6
  group = createGroup();
c0abb6
  for(int i = 0; i < 15; ++i) {
c0abb6
    char filename[256] = {};
c0abb6
    sprintf(filename, "data/sprite/15/%d.png", i+1);
348218
    Sprite sprite = createSpriteEx(0, 0, 1, 1, createAnimation(filename));
c0abb6
    spriteSetColliderRectangle(sprite, 0, 0, 0, 1, 1, 0.1);
c0abb6
    groupAdd(group, sprite);
c0abb6
  }
c0abb6
c0abb6
  generate();
c0abb6
}
c0abb6
c0abb6
c0abb6
void draw() {
c0abb6
  saveState();
c0abb6
  zoom(CELLSIZE);
c0abb6
c0abb6
  noStroke();
c0abb6
  rectTextured(frame, 0, 0, 6, 6);
c0abb6
c0abb6
  translate(1, 1);
c0abb6
c0abb6
  if (mouseDown("left")) {
ef1455
    double mx = mouseTransformedX();
ef1455
    double my = mouseTransformedY();
c0abb6
    if (!selected) {
c0abb6
      selected = groupSpriteByPoint(group, mx, my, TRUE);
c0abb6
      if (selected) {
c0abb6
        dx = spriteGetX(selected) - mx;
c0abb6
        dy = spriteGetY(selected) - my;
c0abb6
      }
c0abb6
    }
c0abb6
c0abb6
    if (selected) {
c0abb6
      double d = 0.1;
c0abb6
      double x0 = spriteGetX(selected);
c0abb6
      double y0 = spriteGetY(selected);
c0abb6
      double x1 = mx + dx;
c0abb6
      double y1 = my + dy;
c0abb6
      if (x1 < x0 - d) x1 = x0 - d;
c0abb6
      if (x1 > x0 + d) x1 = x0 + d;
c0abb6
      if (y1 < y0 - d) y1 = y0 - d;
c0abb6
      if (y1 > y0 + d) y1 = y0 + d;
c0abb6
      spriteSetXY(selected, x1, y1);
c0abb6
    }
c0abb6
  } else {
c0abb6
    selected = NULL;
c0abb6
  }
c0abb6
c0abb6
  if (mouseWentDown("right") || keyWentDown("space"))
c0abb6
    generate();
c0abb6
c0abb6
  groupCollideBetween(group);
c0abb6
  groupCollideGroup(edges, group);
c0abb6
  drawSprites();
c0abb6
c0abb6
  restoreState();
c0abb6
}
c0abb6
c0abb6
c0abb6
int main() {
ef1455
  windowSetTitle("15");
ef1455
  windowSetInit(&init);
ef1455
  windowSetDraw(&draw);
ef1455
  windowSetSize(6*CELLSIZE, 6*CELLSIZE);
ef1455
  windowSetVariableFrameRate();
ef1455
  windowRun();
c0abb6
  return 0;
c0abb6
}