Tron light bikes project

edited January 2016 in How To...

Hi, I am considering make a simple 2D game based on this video https://m.youtube.com/watch?v=-3ODe9mqoDE

However I can.t think of how to implement the situation when some bike crosses the border. I mean is there a getPixelColour function? Or am I supposed to use a huuuuuge array against which I will be checking the coordinates of the bike?

Tagged:

Answers

  • There are two ways to get destroyed

    1) Hit the border of the game area
    2) Hit any path created by a light bike

    If the game area is the same size as the screen you can simply test the bike position against the display (x < 0 or x >= width or y < 0 or y >= height then the bike has left the game area).

    If smaller than the display you can use use get() to get the pixel colour and based on that decide what action to do.

    You could use a huge array but I wouldn't recommend it.

  • Hmmm. But to make the border stay i will have to stop using background. However, if I do this then thre bikes will trail shapes as well...

  • If you want to use background you would have to take into account the trail of each bike. I would do this by having three (same number as the bikes) arrays to store each point at which the movement direction changes. By doing that, after you use background you could plot the trails of the bikes using a simple for loop.

  • i will have to stop using background

    Not true you simply draw the entire display every frame e.g.

    void setup(){
      size(400,400);
    }
    
    void draw(){
      background(128,0,0); // red border
      fill(64); // cycle arena area
      noStroke();
      rect(20,20,360,360);
    }
    
  • OK just had a ah-ha moment, are you talking about storing the bike paths in an array?

  • Yes, it seems my wording confused you. Sorry. I was thinking of using an array to store the bike paths.

  • edited January 2016

    It is not very clear how you intend to change the direction of each bike. But given that you have stored the position where each bike has changed its direction, here is a code to redesign the background and trails.

    PVector[] redBike = new PVector[100];
    PVector[] greenBike = new PVector[100];
    PVector[] blueBike = new PVector[100];
    
    int rn=0, gn=0, bn=0;
    
    void setup(){
      size();
    }
    
    void draw(){
      background(0);  //i guess you want a black background
    
      for (int i=0; i<=max(rn, gn, bn)-1; i++){
        if (i<=rn-1){
          stroke(255,0,0);
          line(redBike[i].x, redBike[i].y, redBike[i].x, redBike[i].y);
        }
        if (i<=gn-1){
          stroke(0,255,0);
          line(greenBike[i].x, greenBike[i].y, greenBike[i].x, greenBike[i].y);
        }
        if (i<=bn-1){
          stroke(0,0,255);
          line(blueBike[i].x, blueBike[i].y, blueBike[i].x, blueBike[i].y);
        }
      }
    
      //where rn, gn, bn is the number of times each bike has changed direction
    }
    
  • I would use an ArrayList of PVectors for the bike path. A new point will be added for every turn.

  • Have you used classes before?

  • Yes, i thought of putting all these arrayLists into bike classes.

  • I would recommend that. The Bike class should be autonomous which will make it easier to have multiple bikes at the same time.

  • Answer ✓

    isn't that just snake game without the food? ;-)

    code not by me

    final color col = color(57, 185, 247);
    final color foodColor = color(240, 109, 227);
    
    float speed;
    
    int cx, cy;
    
    int moveX = 0;
    int moveY = 0;
    int snakeX = 0;
    int snakeY = 0;
    int foodX = -1;
    int foodY = -1;
    
    boolean check = true;
    int []snakesX;
    int []snakesY;
    int snakeSize = 1;
    int windowSize = 200;
    boolean gameOver = false;
    boolean paused = false;
    PFont Font; 
    
    int dir=DOWN;
    
    void setup() {
      size(200, 200);
    
      Font = createFont("Times New Roman", 20);
    
      background(0);
      speed = 50;
      speed=speed/frameRate;
      snakesX = new int[100];
      snakesY = new int[100];
    
      cx = width/2;
      cy = height/2;
    
      snakeX = cx-5;
      snakeY = cy-5;
      foodX = -1;
      foodY = -1;
      gameOver = false;
      check = true;
      snakeSize =1;
    }
    
    void draw() {
      if (speed%10 == 0) {
        background(0);
        runGame();
      }
      speed++;
    }
    
    // ---------------------------------------
    
    void reset() {
      snakeX = cx-5;
      snakeY = cy-5;
      gameOver = false;
      paused   = false;
      check = true;
      snakeSize =1;
      moveY = 0;
      moveX = 0;
    }
    
    void runGame() {
      if (gameOver == false) {
        keyPressed1();
        drawfood();
        drawSnake();
        fill(255);
        textAlign (LEFT);
        textSize(11);
        text("score: "+(snakeSize-1)*10, 10, 22);
        if (!paused) {
          snakeMove();
        } else {
          textSize(12);
          text("paused.", 12, height-20);
        }
        ateFood();
        checkHitSelf();
      } else {
        keyPressed1();
        String modelString = "game over";
        textAlign (CENTER);
        textFont(Font);
        textSize(30);
        text(modelString, 100, 100 );
        textSize(16);
        text("score: "+(snakeSize-1)*10, 100, 130 );
      }
    }
    
    void checkHitSelf() {
      for (int i = 1; i < snakeSize; i++) {
        if (snakeX == snakesX[i] && snakeY== snakesY[i]) {
          gameOver = true;
        }
      }
    }
    
    void ateFood() {
      if (foodX == snakeX && foodY == snakeY) {
        check = true;
        snakeSize++;
      }
    }
    
    void drawfood() {
      fill(foodColor);
      while (check) {
        int x = (int)random(1, windowSize/10);
        int y =  (int)random(1, windowSize/10);
        foodX = 5+x*10;
        foodY = 5+y*10;
    
        for (int i = 0; i < snakeSize; i++) {
          if (x == snakesX[i] && y == snakesY[i]) {
            check = true;
            i = snakeSize;
          } else {
            check = false;
          }
        }
      }
      rect(foodX-5, foodY-5, 10, 10);
    }
    
    void drawSnake() {
      fill(col);
    
      for (int i = 0; i < snakeSize; i++) {
        int X = snakesX[i];
        int Y = snakesY[i];
        rect(X-5, Y-5, 10, 10);
      }
      if (!paused) { 
        for (int i = snakeSize; i > 0; i--) {
          snakesX[i] = snakesX[i-1];
          snakesY[i] = snakesY[i-1];
        }
      }
    }
    
    void snakeMove() {
      snakeX += moveX;
      snakeY += moveY;
      if (snakeX > windowSize-5 || snakeX < 5||snakeY > windowSize-5||snakeY < 5) {
        gameOver = true;
      }
      snakesX[0] = snakeX;
      snakesY[0] = snakeY;
    }
    
    void keyPressed1() {
    
      int key1=keyCode;
    
      if (key1 == UP) {  
        if (snakesY[1] != snakesY[0]-10) {
          moveY = -10; 
          moveX = 0;
        }
      } else if (key1 == DOWN) {  
        if (snakesY[1] != snakesY[0]+10) {
          moveY = 10; 
          moveX = 0;
        }
      } else if (key1 == LEFT) { 
        if (snakesX[1] != snakesX[0]-10) {
          moveX = -10; 
          moveY = 0;
        }
      } else if (key1 == RIGHT) { 
        if (snakesX[1] != snakesX[0]+10) {
          moveX = 10; 
          moveY = 0;
        }
      } else if (key1 == 'R' || key1 == 'r') {
        reset();
      } else if (key1 == 'P' || key1 == 'p') {
        paused=!paused;
      }
    }
    
  • edited January 2016

    People you have to stop this:) I want to write the code by myself)) Now i will be too tempted to look at the provided ones.

    Only joking))

    As to the question, well it IS a snake, but the snake that I have on my Tetris has a limited number of positions - pixels, it means the maximum length is limited by the resolution of the screen.

    PC screens are very detailed, so the array might grow very quickly. In the code above, the coder works around this problem by drawing the tail as a number of squares. "Pixelizing" so to say.

    Also, I was thinking of not limiting the player to the screen. It means the array will grow even quicker, and I was afraid it would eat much memory.

    I might be wrong though.

  • Answer ✓

    It is not the same as the snake game because the snake length is fixed even though it increases with eating. The light cycle trail never decreses until the bike is destroyed and then the whole cycle trail dissappears. The data structures for the two scenarios would be very different.

    If you want the cycle arena bigger than the screen and have the ability to scroll and zoom then you should avoid using the pixel colour to represent type of data e.g. bike, trail, boundary walls etc. I would avoid using a 2D array to specify the arena as it is unwieldy.

Sign In or Register to comment.