Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int[][] grid;
- int row;
- int col;
- int dir = 0;
- final int DIR_UP = 0;
- final int DIR_RIGHT = 1;
- final int DIR_DOWN = 2;
- final int DIR_LEFT = 3;
- void setup()
- {
- size(500, 500);
- background(255);
- grid = new int[width][height];
- col = width / 2;
- row = height / 2;
- }
- void draw()
- {
- go();
- }
- void go()
- {
- int pix = col + row * width;
- int state = grid[row][col];
- loadPixels();
- if(state == 0)
- {
- turn(1);
- grid[row][col] = 1;
- pixels[pix] = color(255);
- }
- else
- {
- turn(-1);
- grid[row][col] = 0;
- pixels[pix] = color(0);
- }
- updatePixels();
- move();
- }
- void turn(int rotation)
- {
- dir += rotation;
- if(dir < 0)
- {
- dir = 3;
- }
- else if(dir > 3)
- {
- dir = 0;
- }
- // Does not work, can return negative values
- //dir = (dir + rotation) % 4;
- }
- void move()
- {
- switch(dir)
- {
- case DIR_UP:
- row--;
- case DIR_RIGHT:
- col++;
- case DIR_LEFT:
- col--;
- case DIR_DOWN:
- row++;
- }
- if(col < 0)
- col = width - 1;
- else if(col >= width)
- col = 0;
- if(row < 0)
- row = height - 1;
- else if(row >= height)
- row = 0;
- }
Add Comment
Please, Sign In to add comment