<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom">
	<channel>
      <title>Tagged with #snake - Processing 2.x and 3.x Forum</title>
      <link>https://forum.processing.org/two/discussions/tagged/feed.rss?Tag=%23snake</link>
      <pubDate>Sun, 08 Aug 2021 18:13:37 +0000</pubDate>
         <description>Tagged with #snake - Processing 2.x and 3.x Forum</description>
   <language>en-CA</language>
   <atom:link href="/two/discussions/tagged%23snake/feed.rss" rel="self" type="application/rss+xml" />
   <item>
      <title>While value in array</title>
      <link>https://forum.processing.org/two/discussion/27986/while-value-in-array</link>
      <pubDate>Wed, 16 May 2018 21:47:16 +0000</pubDate>
      <dc:creator>h314</dc:creator>
      <guid isPermaLink="false">27986@/two/discussions</guid>
      <description><![CDATA[<p>For a snake game, I have the food teleport to a random spot on the screen when you eat it, but it's possible for the food to end up on your tail, and not on an empty space. Basically, I sort of need some way to check if the food's x is in the arrayList for the snake's x, and the smae with y, and if so, to execute an action until that is not true.</p>
]]></description>
   </item>
   <item>
      <title>initialize array</title>
      <link>https://forum.processing.org/two/discussion/26818/initialize-array</link>
      <pubDate>Tue, 13 Mar 2018 22:30:21 +0000</pubDate>
      <dc:creator>Cys</dc:creator>
      <guid isPermaLink="false">26818@/two/discussions</guid>
      <description><![CDATA[<p>Hey Guys,
I started my first little project using processing, which turned out to become a snake game (Code below).
I finished the head and the moving mechanisms. Now ive come to that point, where i have to define the X and Y positions of each part of the body. I created 2 arrays (tailx[] and tail[]) where i wanna save the locations. Ive also created an algorithm to initialize the position of tailx[n] based on tailx[n - 1], which should move on till n &gt; 0 is false. Then i defined tailx[0] as current X position of the head. After that the Head moves and gets a new X position.
But it wont work, instead it says Array index out of bounds. The error is somewhere inside the tail() function from line 127 to line 143.
Thankful for every answer.</p>

<pre><code>float gridsize = 20;
float speed = 0.05;
int window = 900;
int score = 0;
int n = score;
float snakex = (int((window/ gridsize)/ 2)) * gridsize;
float snakey = (int((window/ gridsize)/ 2)) * gridsize;
float accelx = 0;
float accely = 0;
float gridnumber = window / gridsize;
float pointa = random(2, gridnumber - 3);
float pointb = random(2, gridnumber - 3);
float pointx = int(pointa) * gridsize;
float pointy = int(pointb) * gridsize;




void setup(){
 size(900, 900); 
 frameRate(100);
}

void draw(){
 background(10);
 run();
}



void food(){
  if(snakex == pointx){
   if(snakey == pointy){
     pointa = random(2, gridnumber - 3);
     pointb = random(2, gridnumber - 3);
     score++;
     println(score);
   }
 }
  pointx = int(pointa) * gridsize;
  pointy = int(pointb) * gridsize;
}




void move(){
 if(keyPressed){
   if(keyCode == UP){
     if(snakex % gridsize == 0){
       if(accely != speed){
         accelx = 0;
         accely = -speed;
       }

     }

   }
   if(keyCode == DOWN){
     if(snakex % gridsize == 0){
       if(accely != -speed){
         accelx = 0;
         accely = speed;
       }
     }
   }
   if(keyCode == RIGHT){
     if(snakey % gridsize == 0){
       if(accelx != -speed){
         accelx = speed;
         accely = 0;
       }
     }
   }
   if(keyCode == LEFT){
     if(snakey % gridsize == 0){
       if(accelx != speed){
         accelx = -speed;
         accely = 0;
       }
     }
   }
 }
}




 void bordercheck(){
   if(snakex &gt; height - gridsize){
     snakex = (int((window/ gridsize)/ 2)) * gridsize;
     snakey = (int((window/ gridsize)/ 2)) * gridsize;
     accelx = 0;
     accely = 0;
     score = 0;
     println("---");
   }
    if(snakey &gt; width - gridsize){
     snakex = (int((window/ gridsize)/ 2)) * gridsize;
     snakey = (int((window/ gridsize)/ 2)) * gridsize;
     accelx = 0;
     accely = 0;
     score = 0;
     println("---");
   }
    if(snakex &lt; 0){
     snakex = (int((window/ gridsize)/ 2)) * gridsize;
     snakey = (int((window/ gridsize)/ 2)) * gridsize;
     accelx = 0;
     accely = 0;
     score = 0;
     println("---");
   }
    if(snakey &lt; 0){
     snakex = (int((window/ gridsize)/ 2)) * gridsize;
     snakey = (int((window/ gridsize)/ 2)) * gridsize;
     accelx = 0;
     accely = 0;
     score = 0;
     println("---");
   }
 }




 void tail(){
   n = score;
   float[] tailx = new float[n + 1];
   float[] taily = new float[n + 1];
   while(n &gt; 0){
     tailx[n] = tailx[n - 1];
     taily[n] = taily[n - 1];
     fill(255);
     rect(tailx[n], taily[n], gridsize, gridsize);
     n--;
   }
   tailx[0] = snakex;
   taily[0] = snakey;
  fill(255);
  rect(tailx[0], taily[0], gridsize, gridsize);

 }


 void design(){
   fill(255,0,0); 
   rect(pointx,pointy,gridsize,gridsize);
   fill(0,255,0);
   rect(snakex,snakey,gridsize,gridsize);
 }



  void run(){
   food();
   tail();
   move();
   snakex = snakex + accelx * gridsize;
   snakey = snakey + accely * gridsize;
   bordercheck();

   design();
  }
</code></pre>
]]></description>
   </item>
   <item>
      <title>Snake Game Controls not working</title>
      <link>https://forum.processing.org/two/discussion/25868/snake-game-controls-not-working</link>
      <pubDate>Mon, 08 Jan 2018 11:33:31 +0000</pubDate>
      <dc:creator>benfa8</dc:creator>
      <guid isPermaLink="false">25868@/two/discussions</guid>
      <description><![CDATA[<p>I am currently coding a basic snake game in processing and I want the snake not to move in the opposite direction it is currently moving.</p>

<p>The snake just stops whenever I press the opposite direction but i want the game to ignore the keypress.</p>

<p><code>if (keyCode == DOWN &amp;&amp; !direction.equals("up")){
    if (frameCount % framespeed == 0){
    ypos += speed; 
    }
    direction = "down";
  }
  if (keyCode == UP &amp;&amp; !direction.equals("down")){
    if (frameCount % framespeed == 0){
    ypos -= speed;
    }
    direction = "up";
  }
  if (keyCode == RIGHT &amp;&amp; !direction.equals("left")){
    if (frameCount % framespeed == 0){
    xpos += speed; 
    }
    direction = "right";
  }
  if (keyCode == LEFT &amp;&amp; !direction.equals("right")){
    if (frameCount % framespeed == 0){
    xpos -= speed; 
    }
    direction = "left";
  }</code></p>
]]></description>
   </item>
   <item>
      <title>How to record and run subsequently the input of a "broken keyboard"</title>
      <link>https://forum.processing.org/two/discussion/25457/how-to-record-and-run-subsequently-the-input-of-a-broken-keyboard</link>
      <pubDate>Fri, 08 Dec 2017 15:15:56 +0000</pubDate>
      <dc:creator>Eleza</dc:creator>
      <guid isPermaLink="false">25457@/two/discussions</guid>
      <description><![CDATA[<p>Hello guys! :)
I inserted "keys broken" moments in my code. I need to record the keyboard input of those "broken-moments" and run them subsequently faster once the keyboard starts working again, in order to let the user realize he has regained control of the system. How can I do that? Thanks in advance!!</p>
]]></description>
   </item>
   <item>
      <title>Snake teleport problem</title>
      <link>https://forum.processing.org/two/discussion/25339/snake-teleport-problem</link>
      <pubDate>Sat, 02 Dec 2017 18:51:07 +0000</pubDate>
      <dc:creator>Eleza</dc:creator>
      <guid isPermaLink="false">25339@/two/discussions</guid>
      <description><![CDATA[<p>Hello guys!
I have troubles with this code of Snake. I was trying to teleport the snake to the other side and actually, the code I wrote mostly works (line 114). The problem is once the snake goes through the wall and is teleported to the other side, it doesn't eat the food anymore. I'm a beginner so I don't have any clue of why. Any suggestion? :(
Thanks in advance!</p>

<pre lang="processing">
    color col=color(0);
    color foodColor = color(0,255,255);
    float speed = 100;
    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 = 600;
    boolean gameOver = false;
    PFont Font = createFont("Arial",20, true);

    void setup(){
      size(int(windowSize), int(windowSize),P3D); 
      background(255);
      noStroke();
      speed = 100;
      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%5 == 0){
        background(255);
        runGame();}
      speed++;
    }
    
    void reset(){
      snakeX = cx-5;
      snakeY = cy-5;
      gameOver = false;
      check = true;
      snakeSize =1; 
      moveY = 0;
      moveX = 0;
    }
    void runGame(){
      if(gameOver== false){
        drawfood();
        drawSnake();
        snakeMove();
        ateFood();
        checkHitSelf();
      }else{
          String modelString = "game over";
          textAlign (CENTER);
          textFont(Font);
          text(modelString,100,100,40);}
    }
    
    void checkHitSelf(){
       for(int i = 1; i &lt; snakeSize; i++){
           if(snakeX == snakesX[i] &amp;&amp; snakeY== snakesY[i]){
              gameOver = true;}
       }  
    }

    void ateFood(){
      if(foodX == snakeX &amp;&amp; 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 &lt; snakeSize; i++){
           if(x == snakesX[i] &amp;&amp; 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 &lt; snakeSize; i++) {
        int X = snakesX[i];
        int Y = snakesY[i];
        rect(X-5,Y-5,10,10);
      }
       for(int i = snakeSize; i &gt; 0; i--){
        snakesX[i] = snakesX[i-1];
        snakesY[i] = snakesY[i-1];
      }
    }
    
    void snakeMove(){
      snakeX += moveX;
      snakeY += moveY;
    
    if (snakeX &gt; windowSize){snakeX = 0;}              
    if (snakeX &lt; 0){snakeX = windowSize;}      
    if (snakeY &lt; 0){snakeY = windowSize;}        
    if (snakeY &gt; windowSize){snakeY = 0;}      
        
      snakesX[0] = snakeX;
      snakesY[0] = snakeY;
    }
     
    void keyPressed() { 
      if(keyCode == UP){  
        if(snakesY[1] != snakesY[0]-10){moveY = -10; moveX = 0;}}
      if(keyCode == DOWN){
        if(snakesY[1] != snakesY[0]+10){moveY = 10; moveX = 0;}}
      if(keyCode == LEFT){
        if(snakesX[1] != snakesX[0]-10){moveX = -10; moveY = 0;}}
      if(keyCode == RIGHT){
        if(snakesX[1] != snakesX[0]+10){moveX = 10; moveY = 0;}}
      if(keyCode == 'R') {reset();}
    }

</pre>
]]></description>
   </item>
   <item>
      <title>Save coordinates in array doesn't work?</title>
      <link>https://forum.processing.org/two/discussion/25303/save-coordinates-in-array-doesn-t-work</link>
      <pubDate>Thu, 30 Nov 2017 14:47:41 +0000</pubDate>
      <dc:creator>LuigiPlayIt</dc:creator>
      <guid isPermaLink="false">25303@/two/discussions</guid>
      <description><![CDATA[<p>Hello,</p>

<p>I'm quite new to programming and we have to create the game Snake with
processing for School. But the snake is always 2 parts long no matter how big 
the snake size variable is. I hope you can help me out. (ignore the german names :P)</p>

<pre><code>//Variables
int swidth = 20;  //snake width
int sheight = 20; //snake height
int xpos = 200; //Snake position
int ypos = 200;
int dir = 0; //direction: 0 = up, 1 = right, 2 = down, 3 = left
int slength = 5; //snake length

//Arrays
int[] xSchlange = new int[50]; //X Position Snake
int[] ySchlange = new int[50]; //Y Position Snake

void setup(){
   size(400,400);
   frameRate(8);
   background(255);
   stroke(0);
   zeichneGitter(); //draw grid
   xSchlange[0] = xpos;
   ySchlange[0] = ypos;
}

void draw(){
   zeichneGitter();
   steuerung();
   schlangeZeichnen();
   fill(0); 
}
void zeichneGitter(){ //draw grid
   background(255);
   stroke(0);
   for(int i = 0; i &lt; 20; i++){ //X lines 
      line(0, i*20, 400, i*20);  
   }
   for(int i = 0; i &lt; 20; i++){ //Y lines
      line(i*20, 0, i*20, 400);  
   } 
}

void steuerung(){ //controls
   if(dir == 0){
       ySchlange[0] = ySchlange[0] - 20;
   }else if(dir == 1){
       xSchlange[0] = xSchlange[0] + 20;
   }else if(dir == 2){
       ySchlange[0] = ySchlange[0] + 20;
   }else if(dir == 3){
       xSchlange[0] = xSchlange[0] - 20;
   } 
}

void schlangeZeichnen(){ //draw snake
    for(int i = 0; i &lt;= slength; i++){
      rect(xSchlange[i], ySchlange[i], swidth, sheight);
    }
    for(int i = 0; i &lt;= slength; i++){
       xSchlange[i + 1] = xSchlange[i];
       ySchlange[i + 1] = ySchlange[i];
    }
} 

void keyPressed(){
   if(key == 'W' || key == 'w'){  //WASD controls
      dir = 0; 
   }else if(key == 'S' || key == 's'){
      dir = 2;
   }else if(key == 'A' || key == 'a'){
      dir = 3; 
   }else if(key == 'D' || key == 'd'){
      dir = 1; 
   }
}
</code></pre>
]]></description>
   </item>
   <item>
      <title>How to ignore all other keys and use only arrows in a game</title>
      <link>https://forum.processing.org/two/discussion/24953/how-to-ignore-all-other-keys-and-use-only-arrows-in-a-game</link>
      <pubDate>Fri, 10 Nov 2017 19:01:54 +0000</pubDate>
      <dc:creator>shideception</dc:creator>
      <guid isPermaLink="false">24953@/two/discussions</guid>
      <description><![CDATA[<p>I'll make a snake game and when I press any other key in keyboard the circle stops, I wan't to deactivate all other keys and allow only arrows to move the snake.</p>

<pre><code>int x, y;
void setup()
{
  size(500,500);
}

void draw()
{

if (keyCode==UP)
  {
    y-=2;

  }

  if (keyCode==DOWN)
  {
    y+=2;
  }

  if (keyCode==LEFT)
  {
    x-=2;

  }

  if (keyCode==RIGHT)
  {
    x+=2;
  }

  ellipse(x,y,30,30);

}
</code></pre>
]]></description>
   </item>
   <item>
      <title>rect() not creating rectangles</title>
      <link>https://forum.processing.org/two/discussion/24029/rect-not-creating-rectangles</link>
      <pubDate>Mon, 04 Sep 2017 23:47:03 +0000</pubDate>
      <dc:creator>Zodiac</dc:creator>
      <guid isPermaLink="false">24029@/two/discussions</guid>
      <description><![CDATA[<p>hi everyone, i've been creating code for an assignment for school but i'm having trouble. my goal is to re-create the old game snake. i've got the automatic movement handled but everytime i eat a piece of food, the rect() doesn't create a rectangle at all. all i want is to able to see the rectangle, i should be able to make it move with the head myself but any help is greatly appreciated (:</p>

<p>code:</p>

<pre><code>PVector head;
int headSize = 10;
int foodX = int( random(50));
int foodY = int( random(50));
int foodXX = round(foodX);
int foodYY = round(foodY);
int foodXXX = foodXX * 10;
int foodYYY = foodYY * 10;
int snakeSpeed = 10;
int bodypos = 10;
char direction;

//setup
void setup() {
  size(500, 500);
  frameRate(60);
  head = new PVector(250, 250);
  direction = 'w';
}

//drawing
void draw() {
  background(255);
  rect(head.x, head.y, 10, 10);
  rect(foodXXX, foodYYY, 10, 10);
  if (keyPressed &amp;&amp; frameCount % 1 == 0) 
    direction = key;
  move();
  if (head.x == foodXXX) {
    if (head.y == foodYYY) {
      foodX = int( random(50));
      foodY = int( random(50));
      foodXX = round(foodX);
      foodYY = round(foodY);
      foodXXX = foodXX * 10;
      foodYYY = foodYY * 10;
    }
  }
  if (head.x == foodXXX) {
    if (head.y == foodYYY) {
      if (direction == 'w') {
        delay(100);
        rect(head.x, head.y - bodypos, 10, 10);
        bodypos = bodypos + 10;
      }
      else if (direction == 'a') {
        delay(100);
        rect(head.x - bodypos, head.y, 10, 10);
        bodypos = bodypos + 10;
      }
      else if (direction == 's') {
        delay(100);
        rect(head.x, head.y + bodypos, 10, 10);
        bodypos = bodypos + 10;
      }
      else if (direction == 'd') {
        delay(100);
        rect(head.x + bodypos, head.y, 10, 10);
        bodypos = bodypos + 10;
      }
    }
  }
}

void move() {
  switch(direction) {

  case 'w' :
    head = new PVector(head.x, head.y - snakeSpeed);
    delay(100);
    break;
  case 'a' :
    head = new PVector(head.x - snakeSpeed, head.y);
    delay(100);
    break;
  case 's' :
    head = new PVector(head.x, head.y + snakeSpeed);
    delay(100);
    break;
  case 'd' :
    head = new PVector(head.x + snakeSpeed, head.y);
    delay(100);
    break;
  }
  delay(10);
}
</code></pre>
]]></description>
   </item>
   <item>
      <title>Automatic movement for snake</title>
      <link>https://forum.processing.org/two/discussion/23910/automatic-movement-for-snake</link>
      <pubDate>Thu, 24 Aug 2017 04:25:31 +0000</pubDate>
      <dc:creator>Zodiac</dc:creator>
      <guid isPermaLink="false">23910@/two/discussions</guid>
      <description><![CDATA[<p>I'm recreating the old game snake but I have no clue how to have it so that the snake will move on its own (except when you change it's direction obviously). I only need help with the movement, other than that i only have the basic code.</p>
]]></description>
   </item>
   <item>
      <title>Can't put "Game Over" when I lose the game</title>
      <link>https://forum.processing.org/two/discussion/21851/can-t-put-game-over-when-i-lose-the-game</link>
      <pubDate>Thu, 06 Apr 2017 09:47:00 +0000</pubDate>
      <dc:creator>TimEdits</dc:creator>
      <guid isPermaLink="false">21851@/two/discussions</guid>
      <description><![CDATA[<p>I tried to have a text pop up that said "Game Over" but it keeps giving me an error.</p>

<p>Error: (IndexOutOfBoundsException; Index; 1 ,Size: 1)</p>

<p>I would also LOVE to add a score board (with names and high scores) if someone's kind enough to help me through that.</p>

<pre>
import processing.video.*;
Movie intro;
Movie bg;

import ddf.minim.*;
import ddf.minim.effects.*;
Minim minim;
AudioPlayer transition1;
AudioPlayer music1;

ArrayList x = new ArrayList(), y = new ArrayList();
int w = 50, h = 50, bs = 20, dir = 2, computerx = 12, computery = 10;

int stage;
int[] dx = {0, 0, 1, -1}, dy = {1, -1, 0, 0}; 
boolean gameover = false;
int score;

void setup() {
  stage = 1;
  size( 1000, 1000, P3D);
  //size(w * bs, h * bs, P3D);
  minim = new Minim(this);
  intro = new Movie(this, "I_LUV_U.mp4");

  intro.loop();
  

  bg = new Movie(this, "background.mp4");
  frameRate(30);
  x.add(5);
  y.add(5);
}

void movieEvent(Movie m) {
  m.read();
}

void keyPressed() {
  if (keyCode == ENTER) {
    transition1 = minim.loadFile("transition1.mp3", 2048);
    transition1.play();
    music1 = minim.loadFile("song2_01.mp3", 2048);
    music1.loop();
    //////////////////////////////////////
  } else if (keyPressed == true) {
    int newdir = key=='s' ? 0 : (key=='w' ? 1 : (key=='d' ? 2 : (key=='a' ? 3 : -1)));
    //////////////////////////////////////
    if (newdir != -1 &amp;&amp; (x.size() &lt;= 1 || !(x.get(1) ==x.get(0) + dx[newdir] &amp;&amp; y.get (1) == y.get(0) + dy[newdir]))) dir = newdir;
  }
}

void draw() {
  image(intro, 0, 0);
  fill(255);
  textSize(15);
  text("Press ENTER to begin!", 10, 990);
  if (keyCode == ENTER) {
    stage = 2;
  }
  //////////////////////////////////////
  if (stage == 2) {
    intro.stop();
    image(bg, 0, 0);
    bg.play();
    for (int i = 0; i &lt; w; i++) line (i *bs, 0, i *bs, height);
    for (int i = 0; i &lt; h; i++) line (0, i *bs, width, i *bs);
    for (int i = 0; i &lt; x.size(); i++) {
      fill(255);
      rect(x.get(i)*bs, y.get(i)*bs, bs, bs);
    }
    //////////////////////////////////////
    if (!gameover) {
      fill(100, 150, 250);
      rect(computerx*bs, computery*bs, bs, bs);
      //////////////////////////////////////
      if (frameCount%2==0) {
        x.add(0, x.get(0)+ dx[dir]);
        y.add(0, y.get(0)+ dy[dir]);
        //////////////////////////////////////
        if (x.get(0) &lt; 0 || y.get(0) &lt; 0 || x.get(0) &gt;= w || y.get(0) &gt;= h) gameover = true;
        for (int i = 1; i &lt; x.size(); i++) if (x.get(0) == x.get(i) &amp;&amp;  y.get(0) == y.get(i)) gameover = true;
        if (x.get(0)==computerx &amp;&amp; y.get(0)==computery) {
          computerx= (int)random(0, w);
          computery= (int)random(0, h);
          //////////////////////////////////////
        } else {
          x.remove(x.size()-1);
          y.remove(y.size()-1);
        }
        //////////////////////////////////////
      }
    } else {
      fill(0);
      textSize(100);
      textAlign(CENTER);
      text("GAME OVER", width/2, height/2);
      if (keyPressed&amp;&amp;key==' ')
        x.clear();
      y.clear();
      x.add(5);
      y.add(5);
      gameover = false;
    }
    if (x.get(0)==computerx &amp;&amp; y.get(0)==computery) {
      computerx = (int)random(0, w);
      computery = (int)random(0, h);
    }
  }
}
</pre>

<p>Thank you!</p>
]]></description>
   </item>
   <item>
      <title>Can you help with my snake game ?</title>
      <link>https://forum.processing.org/two/discussion/21572/can-you-help-with-my-snake-game</link>
      <pubDate>Fri, 24 Mar 2017 09:16:04 +0000</pubDate>
      <dc:creator>Its_Stunz</dc:creator>
      <guid isPermaLink="false">21572@/two/discussions</guid>
      <description><![CDATA[<p>I am new to processing and I'm making a snake game but I don't know where to start or go about getting the snake the eat the food and create the food in the first place. If you have an suggestions or code to offer it would be very helpful thank you.</p>

<pre><code>final int titleScreen = 1;
final int game = 2;
final int gameOver = 3;
int rectX = 250;
int rectY = 250;
int screen;
int xspeed;
int yspeed;



void setup() {
  size(500, 500);
  background(255);
  smooth();

  screen = titleScreen;
  rectX = 250;
  rectY = 250;          
  xspeed=0;     
  yspeed=0;
}

void draw() {

  switch(screen) {

  case titleScreen:
    titleScreen();
    break;

  case game:
    gameScreen();
    break;

  case gameOver:
    gameOver();
    break;
  }
} 


void titleScreen() {
  background(0);
  smooth();

  fill(255);
  textSize(48);
  rect(135, 355, 227, 60);
  fill(0);
  textAlign(CENTER, TOP);
  text("Play Now!", 250, 355);

  fill(random(0, 255), random(0, 255), random(0, 255));
  textSize(93);
  text("SNAKE!", 250, 115);


  if (mousePressed == true) {
    if ((mouseX &gt; 135)&amp;&amp;(mouseX &lt; 362))
      if ((mouseY &gt; 355)&amp;&amp;(mouseY &lt; 413))
        screen=2;
  }
}

void gameScreen() {

  background(255);

  rect(0, 0, 5, 495);//Left
  rect(0, 0, 495, 5);//Top
  rect(0, 495, 500, 10);//Bottom
  rect(495, 0, 10, 500);//right

  rect(rectX, rectY, 10, 10);

  rectX = rectX +xspeed; 
  rectY = rectY +yspeed;






  if (( rectX == 0)||( rectX &gt; 495)) {
    screen = 3;
  }
  if (( rectY == 0)||( rectY &gt; 495)) {
    screen = 3;
  }
}
void reset() {

  rectX = 250;
  rectY = 250;          
  xspeed=0;     
  yspeed=0;
}
void gameOver() {
  background(0);
  fill(255);
  textSize(48);
  text(" Game over", 250, 150);
  rect(90, 400, 320, 45);
  textSize(32);
  fill(0);
  text(" Press To Play Again", 250, 400);
  if (mousePressed) {  
    reset();
    screen=1;
  }
}

void mousePressed() {
  print("x: " + mouseX + " y: "+ mouseY +"\n");
}
void keyPressed() { 

  if (keyCode==UP || key == 'w') 
  { 
    xspeed=0; 
    yspeed=-2;
  } else if (keyCode==DOWN || key =='s') 
  { 
    xspeed=0; 
    yspeed=2;
  } else if (keyCode==LEFT || key=='a') 
  { 
    xspeed=-2; 
    yspeed=0;
  } else if (keyCode==RIGHT || key =='d') 
  { 
    xspeed=2; 
    yspeed=0;
  }
} 
</code></pre>
]]></description>
   </item>
   <item>
      <title>Make an object disappear upon collision</title>
      <link>https://forum.processing.org/two/discussion/20886/make-an-object-disappear-upon-collision</link>
      <pubDate>Mon, 20 Feb 2017 00:53:23 +0000</pubDate>
      <dc:creator>Akkadian</dc:creator>
      <guid isPermaLink="false">20886@/two/discussions</guid>
      <description><![CDATA[<p>Hello friends:</p>

<p>I'm a starter (I really mean starter, I have little or no clue about programming at all), and our teacher assigned us this homework in which we basically have to make 2 snakes (more like 2 independent squares) to go move around using wasd and the arrow keys and eat coins that are generated randomly. I have pretty much all I want except the part where the coins disappear.</p>

<p>So the question is: How do I make the coins disappear when one of the snakes collide with them?</p>

<p>Additional info:
-I know the snakes don't move fluidly and since we are beginners the teacher told us it was okay.
-There are some things in Spanish because it's my native language but I hope you won't have issues with it.</p>

<p>Here's my code until now:</p>

<pre><code>Snake1 serpiente1;
Snake2 serpiente2;

Bol uno;
Bol dos;
Bol tres;
Bol cuatro;
Bol cinco;
Bol seis;
Bol siete;
Bol ocho;
Bol nueve;
Bol diez;


void setup(){
  size(600,600);
  serpiente1=new Snake1();
  serpiente2=new Snake2();

  uno= new Bol ();
  dos= new Bol ();
  tres= new Bol ();
  cuatro= new Bol ();
  cinco= new Bol ();
  seis= new Bol ();
  siete= new Bol ();
  ocho= new Bol ();
  nueve= new Bol ();
  diez= new Bol ();

}

  void draw (){
    background(255);``
    serpiente1.moverSerpiente();
    serpiente1.dibujaSerpiente(); 
    serpiente2.moverSerpiente();
    serpiente2.dibujaSerpiente();
    uno.dibujaMoneda();
    dos.dibujaMoneda();
    tres.dibujaMoneda();
    cuatro.dibujaMoneda();
    cinco.dibujaMoneda();
    seis.dibujaMoneda();
    siete.dibujaMoneda();
    ocho.dibujaMoneda();
    nueve.dibujaMoneda();
    diez.dibujaMoneda();


}

class Snake1{
float posx;
float posy;
float ancho;
float alto;
color c;
//constructor
Snake1 (){
  posx=30;
  posy=30;
  ancho=25;
  alto=25;
  c=color(random(255),random(255),random(255));
}

void moverSerpiente(){
  if (keyCode == UP){
    posy=posy-3;
    if(posy&lt;0)
    posy=600;
  }
  else if (keyCode == DOWN){
    posy=posy+3;
    if(posy&gt;600)
    posy=0;
  }
  else if (keyCode == RIGHT){
    posx=posx+3;
    if(posx&gt;600)
    posx=0;
  }
  else if (keyCode == LEFT){
    posx=posx-3;
    if(posx&lt;0)
    posx=600;
  }
}

  void dibujaSerpiente(){
    fill(c);
    rect(posx, posy, ancho, alto);
  }
}
class Snake2{
float posx;
float posy;
float ancho;
float alto;
color c;

//constructor
Snake2 (){
  posx=550;
  posy=550;
  ancho=25;
  alto=25;
  c=color(random(255),random(255),random(255));
}

void moverSerpiente(){
  if (key == 'w'){
    posy=posy-3;
    if(posy&lt;0)
    posy=600;
  }
  else if (key == 's'){
    posy=posy+3;
     if(posy&gt;600)
    posy=0;
  }
  else if (key == 'd'){
    posx=posx+3;
    if(posx&gt;600)
    posx=0;
  }
  else if (key == 'a'){
    posx=posx-3;
     if(posx&lt;0)
    posx=600;
  }
}

  void dibujaSerpiente(){
    fill(c);
    rect(posx, posy, ancho, alto);
  }
}

class Bol{
  float posx;
  float posy;
  float ancho;
  float alto;

  Bol(){
    posx=random(570);
    posy=random(570);
    ancho=15;
    alto=15;
  }

  void dibujaMoneda(){
    fill(250,250,0);
    ellipse(posx,posy,ancho,alto);
  }
}
</code></pre>

<p>I hope you guys can help me out! 
Thanks.</p>
]]></description>
   </item>
   <item>
      <title>I am new to processing, and I am trying to use the easiest way to code the game snake.</title>
      <link>https://forum.processing.org/two/discussion/19660/i-am-new-to-processing-and-i-am-trying-to-use-the-easiest-way-to-code-the-game-snake</link>
      <pubDate>Sun, 11 Dec 2016 14:56:59 +0000</pubDate>
      <dc:creator>tomFang2000</dc:creator>
      <guid isPermaLink="false">19660@/two/discussions</guid>
      <description><![CDATA[<p>I have done the part which controls the snake to move, but I am still trying to figure out a way to duplicate the snake's tail after eating the apple. Here below is my code. Looking out for any suggestion and inspiration.  Thanks a lot.</p>

<pre><code>int x;
int y;
int a;
int b;
int a2;
int b2;
int aa,bb;
float dis;
int xpos=200;
int ypos=300;
int applex=int(random(1, 20))*20;
int appley=int(random(1, 30))*20;




void setup() {
  size(600, 600);
  smooth();


}

void draw() {
  background(225);
  //drawlines();
  drawsnake();
  eatfood();
  drawfood();

}

void drawlines() {
  fill(0);
  stroke(0);
  x+=20;
  y+=20;
  if (x&lt;=600) {
    line(x, 0, x, 600);
  }
  if (y&lt;=600) {
    line(0, y, 600, y);
  }
}

void keyPressed() {
  if (keyCode == LEFT) {
    a=-5;
    b=0;
    a2=28;
    b2=0;
  } else if (keyCode == RIGHT) {
    a=5;
    b=0;
    a2=-28;
    b2=0;
  } else  if (keyCode == UP) {
    a=0;
    b=-5;
    b2=28;
    a2=0;
  } else if (keyCode == DOWN) {
    a=0;
    b=5;
    b2=-28;
    a2=0;
  }
}

void drawsnake() {
  fill(0);
  rect(xpos, ypos, 20, 20);
  xpos+=a;
  if (xpos&gt;600) {
    xpos=0;
  }
  if (xpos&lt;0) {
    xpos=600;
  }
  ypos+=b;
  if (ypos&gt;600) {
    ypos=0;
  }
  if (ypos&lt;0) {
    ypos=600;
  }
}

void drawfood() {
  fill(0, 225, 225);
  rect(applex, appley, 20, 20);
}

void eatfood(){
  dis=dist(xpos,ypos,applex,appley);
  if (dis&lt;15){
applex=int(random(1, 20))*20;
appley=int(random(1, 30))*20;
clonesnake();
  }
}

void clonesnake(){

  fill(0);
  rect(xpos+a2, ypos+b2, 20, 20);



}
</code></pre>
]]></description>
   </item>
   <item>
      <title>new to processing, trying to use the easiest way to code the game snake, looking for some help here.</title>
      <link>https://forum.processing.org/two/discussion/19663/new-to-processing-trying-to-use-the-easiest-way-to-code-the-game-snake-looking-for-some-help-here</link>
      <pubDate>Sun, 11 Dec 2016 15:59:04 +0000</pubDate>
      <dc:creator>tomFang2000</dc:creator>
      <guid isPermaLink="false">19663@/two/discussions</guid>
      <description><![CDATA[<p><img src="" alt="" />I have done the part which controls the snake to move, but I am still trying to figure out a way to duplicate the snake's tail after eating the apple. Here below is my code. Looking out for any suggestion and inspiration. Thanks a lot.</p>

<p>here is my code:</p>

<pre><code>int x;
int y;
int a;
int b;
int a2;
int b2;
int aa,bb;
float dis;
int xpos=200;
int ypos=300;
int applex=int(random(1, 20))*20;
int appley=int(random(1, 30))*20;




void setup() {
  size(600, 600);
  smooth();


}

void draw() {
  background(225);
  //drawlines();
  drawsnake();
  eatfood();
  drawfood();

}

void drawlines() {
  fill(0);
  stroke(0);
  x+=20;
  y+=20;
  if (x&lt;=600) {
    line(x, 0, x, 600);
  }
  if (y&lt;=600) {
    line(0, y, 600, y);
  }
}

void keyPressed() {
  if (keyCode == LEFT) {
    a=-5;
    b=0;
    a2=28;
    b2=0;
  } else if (keyCode == RIGHT) {
    a=5;
    b=0;
    a2=-28;
    b2=0;
  } else  if (keyCode == UP) {
    a=0;
    b=-5;
    b2=28;
    a2=0;
  } else if (keyCode == DOWN) {
    a=0;
    b=5;
    b2=-28;
    a2=0;
  }
}

void drawsnake() {
  fill(0);
  rect(xpos, ypos, 20, 20);
  xpos+=a;
  if (xpos&gt;600) {
    xpos=0;
  }
  if (xpos&lt;0) {
    xpos=600;
  }
  ypos+=b;
  if (ypos&gt;600) {
    ypos=0;
  }
  if (ypos&lt;0) {
    ypos=600;
  }
}

void drawfood() {
  fill(0, 225, 225);
  rect(applex, appley, 20, 20);
}

void eatfood(){
  dis=dist(xpos,ypos,applex,appley);
  if (dis&lt;15){
applex=int(random(1, 20))*20;
appley=int(random(1, 30))*20;
clonesnake();
  }
}

void clonesnake(){

  fill(0);
  rect(xpos+a2, ypos+b2, 20, 20);



}
</code></pre>
]]></description>
   </item>
   <item>
      <title>How to program a "character-snake"?</title>
      <link>https://forum.processing.org/two/discussion/19634/how-to-program-a-character-snake</link>
      <pubDate>Sat, 10 Dec 2016 09:56:03 +0000</pubDate>
      <dc:creator>doeroe</dc:creator>
      <guid isPermaLink="false">19634@/two/discussions</guid>
      <description><![CDATA[<p>Hi there,</p>

<p>I'm getting started with processing and searching for a way to build a "character-snake", starting on the bottom in the right edge, moving to the left edge, jumping one row up, moving further to the right edge and so on.</p>

<p>The characters from my string "chars" are pushed in the charArray. The snake should become endless, so that "5" is followed by "3", is followed by ".", is followed by "5" ... and every single character runs the whole line to the top of the canvas.</p>

<p>My questions are:
- how to transform the commented out code lines at the end of my code to a short working code? 
At this moment, the distance between the characters are right, but as soon as the first "5" reaches the left edge, all characters referring to this "5" jump in the next line.</p>

<ul>
<li>this is my first code - do you have any suggestions to improve it :)</li>
</ul>

<p>Thanks a lot!</p>

<p>Here is my code:</p>

<pre><code>var tS, chars, x, y, charCount, speed;

function setup() {
  createCanvas(400,400);
  frameRate(50);
  x = width;
  y = height;
  speed = 4;
  bg = "53.56859";
  lg = "10.033459999999991";
  chars = bg + " " + lg + " ";
  charCount = 0; 

  charArray = [];
  widthArray = [];
}

function draw() {

  // called functions
  generalSettings();
  fillArrays();
  loopChars();
  move();
  jump();
  letters();
}

function generalSettings() {
  background(255);
  // text
  textFont("Menlo");
  tS = 12;
  textSize(tS);
  fill(255, 0, 0);
}

function fillArrays() {
  // save char in charArray and charWidth in widthArray
  for (i = 0; i &lt; chars.length; i++) {
    charArray.push(chars.charAt(i));
    widthArray.push(textWidth(charArray[i]));
  }
}

function loopChars() {
  // looping the gps-position
  charCount = (charCount + 1) % chars.length;
}

function move() {
  x = x - speed;   
}  

function jump() {
  // reverse speed by reaching an edge
  if((x &lt; 0) || (x &gt; width)){
    speed = speed * -1;
    y = y - tS;
  }
}

function letters() {
  text(charArray[0], x, y);
}



/*  // correct distance
  text(charArray[0], x , y);
  text(charArray[1], x + textWidth(charArray[0]), y);
  text(charArray[2], x + textWidth(charArray[0]) + textWidth(charArray[1]), y);
  text(charArray[3], x + textWidth(charArray[0]) + textWidth(charArray[1]) + textWidth(charArray[2]), y);
  text(charArray[4], x + textWidth(charArray[0]) + textWidth(charArray[1]) + textWidth(charArray[2]) + textWidth(charArray[3]), y);
*/  
</code></pre>
]]></description>
   </item>
   <item>
      <title>I need help on my programming game. I only have ten minutes or i will fail.  This is my code.</title>
      <link>https://forum.processing.org/two/discussion/19586/i-need-help-on-my-programming-game-i-only-have-ten-minutes-or-i-will-fail-this-is-my-code</link>
      <pubDate>Wed, 07 Dec 2016 19:03:55 +0000</pubDate>
      <dc:creator>JJJ7878</dc:creator>
      <guid isPermaLink="false">19586@/two/discussions</guid>
      <description><![CDATA[<p>I only have a problem with my dot.
`Snake mySnake;// My snake.
Dot newDot;// Dot that the Snake gets.</p>

<p>int hitCount = 0; // Counter for how many times the dot has been eaten
int Snakespeed=7;// Sets speed for Snake.</p>

<p>void setup(){
  size(500,500);
  //Creates new Snake and new Dot.
  mySnake= new Snake();
  newDot= new Dot(); 
}</p>

<p>void draw(){
  background(0,255,0);
  text (hitCount, width - 50, height - 20);// place scoe keeper in bottom right corner
  fill(0);
  newDot.display();// Draws dot randomly on screen
  mySnake.display();// Snake starts in the middle of the screen
  mySnake.move();// Allows me to move the Snake.</p>

<p>// Checks if the Snake gets the dot or not.
     if (rectangle_collision (mySnake.xpos, mySnake.ypos, mySnake.sizeW, mySnake.sizeH, 
  newDot.xpos, newDot.ypos, newDot.sizeW, newDot.sizeH)) {</p>

<p>hitCount= hitCount+1;// Increases hit counter by 1.
   if (hitCount == 10) {
      hitCount = 0;
      newDot.kill();</p>

<p>}
 else{
   newDot= new Dot(); //Redraws Dot somewhere random.
}
{</p>

<p>if (mySnake.xpos + mySnake.sizeW/2 &gt; width) {
    mySnake.xpos = mySnake.xpos - Snakespeed;
  } 
  else if (mySnake.xpos - mySnake.sizeW/2 &lt; 0) {
    mySnake.xpos = mySnake.xpos + Snakespeed;
  } 
  else if (mySnake.ypos + mySnake.sizeH/2 &gt; height) {
    mySnake.ypos = mySnake.ypos - Snakespeed;
  } 
  else if (mySnake.ypos - mySnake.sizeH/2 &lt; 0) {
    mySnake.ypos = mySnake.ypos + Snakespeed;
  }
  }</p>

<p>// set up of boolean to determine collision
boolean rectangle_collision(float SnakeX, float SnakeY, float SnakeW, float SnakeH, 
float DotX, float DotY, float DotW,float DotH)
// if it's true it returns
{
  return (DotX + DotW/2  SnakeX - SnakeW/2 &amp;&amp; // and if the left side of the Dot is farther right than the left side of the Snake
  DotY +DotW/2 &lt; SnakeY + SnakeW/2 &amp;&amp; // and if the bottom of the Dot is higher than the bottom of the Snake
  DotY - DotW/2 &gt; SnakeY - SnakeW/2);  // and if the top of the Dot is lower than the top of the Snake</p>

<p>}
//  Makes Snake move.
void keyPressed() {
  // Only works if i is true.
  int k = keyCode;
 if (k == UP)     mySnake.up    = true; // sets up movement to true if up is pressed
  else if (k == DOWN)   mySnake.down  = true; // sets down movement to true if down is pressed
  else if (k == LEFT)   mySnake.left  = true; // sets left movement to true if left is pressed
  else if (k == RIGHT)  mySnake.right = true; // sets right movement to true if right is pressed
}
void keyReleased() {
  int k = keyCode;
  //Code for if it is false
  if      (k == UP)     mySnake.up    = false;
  else if (k == DOWN)   mySnake.down  = false;
  else if (k == LEFT)   mySnake.left  = false;
  else if (k == RIGHT)  mySnake.right = false;
}</p>

<p>class Snake {
  color c; // sets color of Snake/ C also stores colors so you dont have to add the RG everytime
  float xpos; // xpos of Snake
  float ypos; // ypos of Snake
  int sizeW = 10; // width of Snake
  int sizeH = 90; // height of Snake
  boolean up, down, left, right; // booleans for Snake movement</p>

<p>// Snake Class</p>

<p>Snake() {
    c= color(0,128,0); // Color is green. 
    xpos = width/2; // Snake is centered at the start of game.
    ypos = width/2;
  }</p>

<pre><code>void display() {
rectMode(CENTER); // position determined by the center point of Snake
stroke(0); // black outline to Snake
fill(c); // Fill is the light gray
rect(xpos, ypos, sizeW, sizeH); // draw the Snake in the center with dimensions 50x50
</code></pre>

<p>}
// Uses the boolean I used on top 
//since the snake moves in any direction it can move diagonally
   void move() {
    if (up)    ypos -= Snakespeed;
    if (down)  ypos += Snakespeed;
    if (left)  xpos -= Snakespeed;
    if (right) xpos += Snakespeed;
  }</p>

<p>class Dot {
color c; // color of dot.
  float xpos; // xpos of dot
  float ypos; // ypos of dot
  int sizeW = 10; // width of dot
  int sizeH = 10; // height of dot</p>

<p>Dot() {
    c = color(255,0,0); // color is red
    xpos = int(random(width - sizeW/2)); // xposition of dot
    ypos = int(random(height - sizeH/2)); // yposition of dot
  }
  void display() {
    rectMode(CENTER);// Changes location of where it is drawn
    stroke(0);// Oulines the dot
    fill(c);
    rect(xpos, ypos, sizeW, sizeH);// Draws it
  }
  //Allows the grape to go somewhere else when snake ges it.
  void teleport() {
    xpos = int(random(width));
    ypos = int(random(height));
  }
  void kill(){
    xpos= 1000;
    ypos= 1000;
}}
}`</p>
]]></description>
   </item>
   <item>
      <title>Sometimes my character stops collecting the "food"</title>
      <link>https://forum.processing.org/two/discussion/19407/sometimes-my-character-stops-collecting-the-food</link>
      <pubDate>Tue, 29 Nov 2016 23:22:18 +0000</pubDate>
      <dc:creator>lbrez16</dc:creator>
      <guid isPermaLink="false">19407@/two/discussions</guid>
      <description><![CDATA[<p>Hello everyone!</p>

<p>I'm making a snake game where an individual icon collecting "food" but sometimes the icon wont pick up the food and sometimes it will and then will stop at some random point. Is there an obvious part of my code causing this that im missing? Also, I cannot for the life of me figure out how to get the game to have a game over and restart setting when the icon hits the wall. Also, how can I call the "Welcome" screen into the main code to pop up before the game? I'm sorry for all these questions. I've been working on this for 10+ hours and I'm not seeing everything clearly anymore.</p>

<p>_________________________Main Body__________________________</p>

<pre><code>    import ddf.minim.*;
    import ddf.minim.signals.*;
    import ddf.minim.analysis.*;
    import ddf.minim.effects.*;

Minim minim;
AudioPlayer player;

Logo B;
Welcome W;
int grid = 35;
int Time;
PImage BH;
PImage SC;
PImage Rink;


PVector Cup;


void setup() {

  size(800, 422);
  B = new Logo();
  W= new Welcome();
  frameRate(8);
  pickLocation();
  BH = loadImage("BH.png");
  SC = loadImage("SC.png");
  Rink = loadImage("Rink.jpg");

  minim = new Minim( this );

  player = minim.loadFile("CD.mp3");
  player.play();
}

void pickLocation() {
  int across = width/grid;
  int down = height/grid;
  Cup = new PVector(floor(random(across)), floor(random(down)));
  Cup.mult(grid);
}

void draw() {
  background(Rink);


  if (B.capture(Cup)) {
    pickLocation();
  }
  B.death();
  B.update();
  B.show();


  image(SC, Cup.x, Cup.y, grid, grid);
}

void keyPressed() {

  if (keyCode == UP) {
    B.dir(0, -1);
  } else if (keyCode == DOWN) {
    B.dir(0, 1);
  } else if (keyCode == RIGHT) {
    B.dir(1, 0);
  } else if (keyCode == LEFT) {
    B.dir(-1, 0);
  }
}
</code></pre>

<p>_________________Logo__________________</p>

<pre><code>class Logo {
  float x = 0;
  float y = 0;
  float xspeed = 1;
  float yspeed = 0;
  int total = 0;
  boolean endgame=false;
  ArrayList&lt;PVector&gt; hawk = new ArrayList&lt;PVector&gt;();

  Logo() {
  }

  boolean capture(PVector pos) {
    float d = dist(x, y, pos.x, pos.y);
    if (d &lt; 1) {
      return true;
    } else {
      return false;
    }
  }

  void dir(float x, float y) {
    xspeed = x;
    yspeed = y;
  }

  void death() {
    for (int i = 0; i &lt; hawk.size(); i++) {
      PVector pos = hawk.get(i);
      float d = dist(x, y, pos.x, pos.y);
      if (d &lt; 1) {
        text("starting over",100,100);
        total = 0;
        hawk.clear();
      }
    }
  }

  void update() {

    x = x + xspeed*grid;
    y = y + yspeed*grid;

    x = constrain(x, 0, width-grid);
    y = constrain(y, 0, height-grid);
  }

  void show() {
    image(BH, x, y, grid, grid);
  }
}
</code></pre>

<p>____________________Welcome______________________</p>

<pre><code>class Welcome {
  PFont Silom;
  boolean buttonpressed;
  int buttonA, buttonB, buttonC, buttonD;

  Welcome()
  {
    Silom = loadFont("Silom.vlw");

    buttonpressed = false;
    buttonC = 215;
    buttonD = 100;
    buttonA = (width-510);
    buttonB = (height-410);


    textFont(Silom, 70);
    if (buttonpressed) {
      println("Begin");
    } else {
      fill(255);
      rect(buttonA, buttonB, buttonC, buttonD);
      fill (0);
      text("START", buttonA+5, buttonB+buttonD-25);
    }
    {     

      if (buttonpressed) {
        fill(255, 255, 255, 0);
      } else {
        textFont(Silom, 20);
        text("Press the arrow keys to move around", 210, 235);
        textFont(Silom, 60);
        text("Catch the cup!", 180, 200);
      }
    }
  }

  void mousePressed() {
    if (mouseX&gt;buttonA &amp;&amp; mouseX&lt;buttonA+buttonC &amp;&amp; mouseY&gt;buttonB &amp;&amp; mouseY&lt;buttonB+buttonC)
      buttonpressed = true;
  }
}
</code></pre>
]]></description>
   </item>
   <item>
      <title>Fix Capture Bug and Game Over Screen. (Runnable Code!)</title>
      <link>https://forum.processing.org/two/discussion/19459/fix-capture-bug-and-game-over-screen-runnable-code</link>
      <pubDate>Thu, 01 Dec 2016 21:55:18 +0000</pubDate>
      <dc:creator>lbrez16</dc:creator>
      <guid isPermaLink="false">19459@/two/discussions</guid>
      <description><![CDATA[<p>``Hello everyone!</p>

<p>So I have been making a game similar to the traditional snake game. The difference being that it is on individual shape catching the food rather than it growing. However, after awhile the icon wont pickup the food anymore and I cannot figure out why. I also cant figure out how to create a game over box when the icon hits the wall. Any help would be greatly appreciated! I adjusted the code to not use the images so it could be run by anyone willing to help. Thank you in advance!</p>

<p>_________Main Body____________</p>

<pre><code>import ddf.minim.*;
import ddf.minim.signals.*;
import ddf.minim.analysis.*;
import ddf.minim.effects.*;

Minim minim;
AudioPlayer player;

Logo B;
int grid = 35;
int Time;
//PImage BH;
//PImage SC;
//PImage Rink;
PFont Silom;

PVector Cup;


void setup() {

  size(600, 631);
  B = new Logo();
  frameRate(8);
  pickLocation();
  //BH = loadImage("BH.png");
  //SC = loadImage("SC.png");
  //Rink = loadImage("Rink.jpg");
  Silom = loadFont("Silom.vlw");


  minim = new Minim( this );

  player = minim.loadFile("CD.mp3");
  player.play();
}

void pickLocation() {
  int across = width/grid;
  int down = height/grid;
  Cup = new PVector(floor(random(across)), floor(random(down)));
  Cup.mult(grid);
}

void draw() {
  background(155);


  if (B.capture(Cup)) {
    pickLocation();
  }
  B.death();
  B.update();
  B.show();

  //image(SC, Cup.x, Cup.y, grid, grid);
  rect(Cup.x, Cup.y, grid, grid);
  fill (0);
  textFont(Silom, 20);
  text("Press the arrow keys", 10, 30);
  text("to move around", 40, 50);
}

void keyPressed() {

  if (keyCode == UP) {
    B.dir(0, -1);
  } else if (keyCode == DOWN) {
    B.dir(0, 1);
  } else if (keyCode == RIGHT) {
    B.dir(1, 0);
  } else if (keyCode == LEFT) {
    B.dir(-1, 0);
  }
  else if (keyCode == ' ');

}
</code></pre>

<p>__________Logo_____________</p>

<pre><code>    class Logo {
      float x = 0;
      float y = 0;
      float xspeed = 1;
      float yspeed = 0;
      int total = 0;
      boolean endgame=false;
      ArrayList&lt;PVector&gt; hawk = new ArrayList&lt;PVector&gt;();

      Logo() {
      }

      boolean capture(PVector pos) {
        float d = dist(x, y, pos.x, pos.y);
        if (d &lt; 1) {
          return true;
        } else {
          return false;
        }
      }

      void dir(float x, float y) {
        xspeed = x;
        yspeed = y;
      }

      void death() {
        for (int i = 0; i &lt; hawk.size(); i++) {
          PVector pos = hawk.get(i);
          float d = dist(x, y, pos.x, pos.y);
          if (d &lt; 1) {
            text("starting over",100,100);
            total = 0;
            hawk.clear();
          }
        }
      }

      void update() {

        x = x + xspeed*grid;
        y = y + yspeed*grid;

        x = constrain(x, 0, width-grid);
        y = constrain(y, 0, height-grid);
      }

      void show() {
        //image(BH, x, y, grid, grid);
        rect(x, y, grid, grid);
        fill(255, 150, 20);
      }
    }
</code></pre>
]]></description>
   </item>
   <item>
      <title>How To Create "Game Over"</title>
      <link>https://forum.processing.org/two/discussion/19380/how-to-create-game-over</link>
      <pubDate>Tue, 29 Nov 2016 04:46:13 +0000</pubDate>
      <dc:creator>lbrez16</dc:creator>
      <guid isPermaLink="false">19380@/two/discussions</guid>
      <description><![CDATA[<p>Hello! So I am creating a game similar to a snake game but with one individual shape moving instead of getting bigger. The issue with my code that I'm having is that when the icon hits the wall I can't figure out how to get it to be a full on Game Over where you have to press a button to restart the game. Below I have posted my code so any help that can be given is greatly appreciated!!</p>

<pre><code>Logo l;
int grid = 35;
PImage BH;
PImage SC;
PImage Rink;

PVector Cup;


void setup() {

  size(800, 422);
  l = new Logo();
  frameRate(8);
  pickLocation();
  BH = loadImage("BH.png");
  SC = loadImage("SC.png");
  Rink = loadImage("Rink.jpg");

}

void pickLocation() {
  int across = width/grid;
  int down = height/grid;
  Cup = new PVector(floor(random(across)), floor(random(down)));
  Cup.mult(grid);
}

void draw() {
  background(Rink);

  if (l.capture(Cup)) {
    pickLocation();
  }
  l.death();
  l.update();
  l.show();

  image(SC,Cup.x, Cup.y, grid, grid);
}

void keyPressed() {
  if (keyCode == UP) {
    l.dir(0, -1);
  } else if (keyCode == DOWN) {
    l.dir(0, 1);
  } else if (keyCode == RIGHT) {
    l.dir(1, 0);
  } else if (keyCode == LEFT) {
    l.dir(-1, 0);
  }
}
</code></pre>

<hr />

<p>Logo.pde</p>

<pre><code>class Logo {
  float x = 0;
  float y = 0;
  float xspeed = 1;
  float yspeed = 0;
  int total = 0;
  ArrayList&lt;PVector&gt; tail = new ArrayList&lt;PVector&gt;();

  Logo() {
  }

  boolean capture(PVector pos) {
    float d = dist(x, y, pos.x, pos.y);
    if (d &lt; 1) {
      return true;
    } else {
      return false;
    }
  }

  void dir(float x, float y) {
    xspeed = x;
    yspeed = y;
  }

  void death() {
    for (int i = 0; i &lt; tail.size(); i++) {
      PVector pos = tail.get(i);
      float d = dist(x, y, pos.x, pos.y);
      if (d &lt; 1) {
        println("starting over");
        total = 0;
        tail.clear();
      }
    }
  }

  void update() {
    if (total &gt; 0) {
      if (total == tail.size() &amp;&amp; !tail.isEmpty()) {
        tail.remove(0);
      }
      tail.add(new PVector(x, y));
    }

    x = x + xspeed*grid;
    y = y + yspeed*grid;

    x = constrain(x, 0, width-grid);
    y = constrain(y, 0, height-grid);
  }

  void show() {
    fill(255);
    for (PVector v : tail) {
      rect(v.x, v.y, grid, grid);
    }
    image(BH, x, y, grid, grid);
  }
}
</code></pre>
]]></description>
   </item>
   <item>
      <title>Creating a Game!</title>
      <link>https://forum.processing.org/two/discussion/19323/creating-a-game</link>
      <pubDate>Sun, 27 Nov 2016 04:50:57 +0000</pubDate>
      <dc:creator>lbrez16</dc:creator>
      <guid isPermaLink="false">19323@/two/discussions</guid>
      <description><![CDATA[<p>Hello everyone! I would really appreciate any help that could be given with this project.</p>

<p>So for my final in a few weeks, I am creating a game in Processing. It is a spin on the snake game where instead of the traditional snake and red square I want to make it where I will have two png images in their place. Specifically, I will be having the Blackhawks logo chasing the Stanley Cup! I began my code with a welcome screen with instructions that I've gotten to work perfectly. My problem is figuring out how to use the reference code (the one I will post below) to adapt it for the two images. Another issue I have is that I'm unsure how to incorporate the game to the welcome screen code. Would I just add it at the end or would I need to use ArrayList?</p>

<p>Thank you again in advance for any help you can give!
Below is the reference code that I'm using for the game aspect. (Credit to OpenProcessing - Ian151)</p>

<pre><code> int angle=0;
int snakesize=5;
int time=0;
int[] headx= new int[2500];
int[] heady= new int[2500];
int applex=(round(random(47))+1)*8;
int appley=(round(random(47))+1)*8;
boolean redo=true;
boolean stopgame=false;
void setup()
{
  restart();
  size(400,400);
  textAlign(CENTER);
}
void draw()
{
  if (stopgame)
  {
    //do nothing because of game over (stop playing)
  }
  else
  {
    //draw stationary stuff
  time+=1;
  fill(255,0,0);
  stroke(0);
  rect(applex,appley,8,8);
  fill(0);
  stroke(0);
  rect(0,0,width,8);
  rect(0,height-8,width,8);
  rect(0,0,8,height);
  rect(width-8,0,8,height);
  //my modulating time by 5, we create artificial frames each 5 frames
  //(otherwise the game would go WAY too fast!)
  if ((time % 5)==0)
  {
    travel();
    display();
    checkdead();
  }
  }
}
//controls:
void keyPressed()
{
  if (key == CODED)
  {
    if (keyCode == UP &amp;&amp; angle!=270 &amp;&amp; (heady[1]-8)!=heady[2])
    {
      angle=90;
    }
    if (keyCode == DOWN &amp;&amp; angle!=90 &amp;&amp; (heady[1]+8)!=heady[2])
    {
      angle=270;
    }if (keyCode == LEFT &amp;&amp; angle!=0 &amp;&amp; (headx[1]-8)!=headx[2])
    {
      angle=180;
    }if (keyCode == RIGHT &amp;&amp; angle!=180 &amp;&amp; (headx[1]+8)!=headx[2])
    {
      angle=0;
    }
    if (keyCode == SHIFT)
    {
      //restart the game by pressing shift
      restart();
    }
  }
}
void travel()
{
  for(int i=snakesize;i&gt;0;i--)
  {
    if (i!=1)
    {
      //shift all the coordinates back one array
      headx[i]=headx[i-1];
      heady[i]=heady[i-1];
    }
    else
    {
      //move the new spot for the head of the snake, which is
      //always at headx[1] and heady[1].
      switch(angle)
      {
        case 0:
        headx[1]+=8;
        break;
        case 90:
        heady[1]-=8;
        break;
        case 180:
        headx[1]-=8;
        break;
        case 270:
        heady[1]+=8;
        break;
      }
    }
  }

}
void display()
{
  //is the head of the snake eating the apple?
  if (headx[1]==applex &amp;&amp; heady[1]==appley)
  {
    //grow and spawn the apple somewhere away from the snake
    //(currently some of the code below might not be working, but the game still works.)
    snakesize+=round(random(3)+1);
    redo=true;
    while(redo)
    {
      applex=(round(random(47))+1)*8;
      appley=(round(random(47))+1)*8;
      for(int i=1;i&lt;snakesize;i++)
      {

        if (applex==headx[i] &amp;&amp; appley==heady[i])
        {
          redo=true;
        }
        else
        {
          redo=false;
          i=1000;
        }
      }
    }
  }
  //draw the new head of the snake...
  stroke(sinecolor(1),sinecolor(0),sinecolor(.5));
  fill(0);
  rect(headx[1],heady[1],8,8);
  //...then erase the back end of the snake.
  fill(255);
  rect(headx[snakesize],heady[snakesize],8,8);

}
void checkdead()
{
  for(int i=2;i&lt;=snakesize;i++)
  {
    //is the head of the snake occupying the same spot as any of the snake chunks?
    if (headx[1]==headx[i] &amp;&amp; heady[1]==heady[i])
    {
      fill(255);
      rect(125,125,160,100);
      fill(0);
      text("GAME OVER",200,150);
      text("Score:  "+str(snakesize-1)+" units long",200,175);
      text("To restart, press Shift.",200,200);
      stopgame=true;
    }
    //is the head of the snake hitting the walls?
    if (headx[1]&gt;=(width-8) || heady[1]&gt;=(height-8) || headx[1]&lt;=0 || heady[1]&lt;=0)
    {
      fill(255);
      rect(125,125,160,100);
      fill(0);
      text("GAME OVER",200,150);
      text("Score:  "+str(snakesize-1)+" units long",200,175);
      text("To restart, press Shift.",200,200);
      stopgame=true;
    }
  }
}
void restart()
{
  //by pressing shift, all of the main variables reset to their defaults.
  background(255);
  headx[1]=200;
  heady[1]=200;
  for(int i=2;i&lt;1000;i++)
  {
    headx[i]=0;
    heady[i]=0;
  }
  stopgame=false;
  applex=(round(random(47))+1)*8;
  appley=(round(random(47))+1)*8;
  snakesize=5;
  time=0;
  angle=0;
  redo=true;
}
float sinecolor(float percent)
{
  float slime=(sin(radians((((time +(255*percent)) % 255)/255)*360)))*255;
  return slime;
}
</code></pre>
]]></description>
   </item>
   <item>
      <title>How can I make this sound play every time the snake eats a square?</title>
      <link>https://forum.processing.org/two/discussion/19159/how-can-i-make-this-sound-play-every-time-the-snake-eats-a-square</link>
      <pubDate>Sat, 19 Nov 2016 12:54:42 +0000</pubDate>
      <dc:creator>daniel00101</dc:creator>
      <guid isPermaLink="false">19159@/two/discussions</guid>
      <description><![CDATA[<p>So i'm makin' a snake game as a project, and fortunately i'm almost there. The last thing I need is to make a sound every time the snake eats one of the random squares, but as it is right now, it only plays the sound the first time. How can I fix this?</p>

<p>Another thing I don't know how to do is to make a restart button for the game over screen. How can I make a function that makes the game start all over again as if you closed the window and run the code over again?</p>

<p>Project file here, and make sure to have the minim audio extension installed!</p>

<p><a href="http://www.filedropper.com/snake_2" target="_blank" rel="nofollow">http://www.filedropper.com/snake_2</a></p>
]]></description>
   </item>
   <item>
      <title>Snake game questions</title>
      <link>https://forum.processing.org/two/discussion/16409/snake-game-questions</link>
      <pubDate>Tue, 03 May 2016 17:50:38 +0000</pubDate>
      <dc:creator>drg2477</dc:creator>
      <guid isPermaLink="false">16409@/two/discussions</guid>
      <description><![CDATA[<p>What to do next?</p>

<pre><code>int snakeX=370;
int snakeY=370;
int foodX=int(random(1,780));
int foodY=int(random(1,780));
int score=1;
boolean up=false;
boolean down=false;
boolean right=false;
boolean left=false;
void setup(){
  size(800,800);
}
void draw(){
  fill(0);
  stroke(255);
  rect(-1,-1,801,801);
  rect(snakeX,snakeY,29,29);
  fill(255);
  stroke(255);
  rect(foodX,foodY,19,19);
  fill(0);
  if (up==true &amp;&amp; down==false &amp;&amp; right==false &amp;&amp; left==false){
    snakeY-=1;
  }
  if (down==true &amp;&amp; up==false &amp;&amp; right==false &amp;&amp; left==false){
    snakeY+=1;
  }
  if (right==true &amp;&amp; up==false &amp;&amp; down==false &amp;&amp; left==false){
    snakeX+=1;
  }
  if (left==true &amp;&amp; up==false &amp;&amp; down==false &amp;&amp; right==false){
    snakeX-=1;
  }
  if (up==true){
    if (snakeX&lt;foodX+20 &amp;&amp; snakeX+30&gt;foodX+20 &amp;&amp; snakeY==foodY+20 || snakeX&lt;foodX &amp;&amp; snakeX+30&gt;foodX &amp;&amp; snakeY==foodY+20){
      foodX=int(random(1,780));
      foodY=int(random(1,780));
      score+=1;
    }
    if (down==true){
    if (snakeX&lt;foodX+20 &amp;&amp; snakeX+30&gt;foodX+20 &amp;&amp; snakeY+30==foodY || snakeX&lt;foodX &amp;&amp; snakeX+30&gt;foodX &amp;&amp; snakeY+30==foodY){
      foodX=int(random(1,780));
      foodY=int(random(1,780));
      score+=1;
    }
  }
  if(up==true){
  rect(snakeX,snakeY,30,score*30);
  }
  if(down==true){
  rect(score*snakeX,snakeY,30,30);
  }
  }
}
void keyPressed()
{
  if  (key == CODED)
  {
    if (keyCode == UP)
    {
      if(down==false){
      left=false;
      right=false;
      up=true;
      }
    }
    if (keyCode == DOWN)
    {
      if(up==false){
      up=false;
      left=false;
      right=false;
      down=true;
      }
    }
        if (keyCode == RIGHT)
    {
      if(left==false){
      up=false;
      down=false;
      left=false;
      right=true;
      }
    }
    if (keyCode == LEFT)
    {
      if(right==false){
      up=false;
      down=false;
      right=false;
      left=true;
      }
    }
  }
}
</code></pre>

<p>Ok, sorry, just realized it didn't paste the whole code, here it is.</p>
]]></description>
   </item>
   <item>
      <title>I cannot for the life of me get this snake game to run right, can anyone help?</title>
      <link>https://forum.processing.org/two/discussion/16498/i-cannot-for-the-life-of-me-get-this-snake-game-to-run-right-can-anyone-help</link>
      <pubDate>Sun, 08 May 2016 19:47:22 +0000</pubDate>
      <dc:creator>S_Knighter</dc:creator>
      <guid isPermaLink="false">16498@/two/discussions</guid>
      <description><![CDATA[<p>Problem solved, please delete thread.</p>
]]></description>
   </item>
   <item>
      <title>I need help making my game restart ?</title>
      <link>https://forum.processing.org/two/discussion/16303/i-need-help-making-my-game-restart</link>
      <pubDate>Wed, 27 Apr 2016 23:14:22 +0000</pubDate>
      <dc:creator>theserdzo</dc:creator>
      <guid isPermaLink="false">16303@/two/discussions</guid>
      <description><![CDATA[<p><em>Can someone help me, i need to make it restart after the game has ended, basically draw the game from scratch every time i press a button(in my case Shift).
Also any help on making enemies randomly appear for  like 4-5 seconds and then disapear would be greatly appriciated.
-Cheers</em></p>

<p>THE.CODE</p>

<hr />

<pre><code>//add power ups,random enemies spawn,confusion(reverse controls), border
snake sn;
food food1;
enemy enemy1;
int score;
PImage bkg;
PImage img;
PImage imgg;
boolean redo=true;
boolean stopgame=false;

void setup(){
    size(700, 500);
    frameRate(15);   
    sn = new snake();
    food1 = new food();
    enemy1 = new enemy();
    rectMode(CENTER);
    textAlign(CENTER, CENTER);
    bkg = loadImage("bkg.jpg");
    img = loadImage("gover.jpg");
    score =0;
    reset();
  }

void draw(){
  if(stopgame){    
    size(700, 500);
    background(img);
    noFill();
    rect(350, 390, 200, 100, 10);
    fill(0);
    textSize(18);
    text("Snake Lenght: "+sn.len+" ",350,378);
    textSize(15);
    text("To exit, press Esc.",350,420);
    return;
}
   else{
     playGame();
 }     
}

void playGame() {
    score = sn.len;
    stopgame=false; 
    background(bkg);
    drawScoreboard();
    sn.move();
    sn.display();
    food1.display();
    enemy1.display();

 if( dist(food1.xpos, food1.ypos, sn.xpos.get(0), sn.ypos.get(0)) &lt; sn.slen ){
    food1.reset();
    sn.addLink();
    }

 if( dist(enemy1.xpos, enemy1.ypos, sn.xpos.get(0), sn.ypos.get(0)) &lt; sn.slen ){
    enemy1.reset();
    sn.removeLink();

 }
}  

void reset() {
  playGame();
}

void drawScoreboard(){
  fill(250, 0, 250);
  textSize(25);
  text( "", width/2, 80);
  fill(250, 0, 250);
   textSize(20);
  text( "", width/2, 140);

  stroke(140, 140, 80);
  fill(255, 0 ,255);
  rect(0, 0, 0, 10);
  fill(18, 20, 67);
  textSize(25);
  text( "Score: " + (sn.len-1), 350, 255);
}

void keyPressed(){
  if(key == CODED){
    if(keyCode == LEFT &amp;&amp; sn.dir!= "right"){
      sn.dir = "left";
    }
    if(keyCode == RIGHT &amp;&amp; sn.dir!= "left"){
      sn.dir = "right";
    }
    if(keyCode == UP &amp;&amp; sn.dir!= "down"){
      sn.dir = "up";
    }
    if(keyCode == DOWN &amp;&amp; sn.dir!= "up"){
      sn.dir = "down";
    }
    if (keyCode == SHIFT) {
    playGame();
    }
    if (keyCode == ESC) {
    exit();
    } 

  }
}

class food{
  float xpos, ypos;
    food(){
    xpos = random(10, width - 10);
    ypos = random(10, height - 10);
  }

void display(){

   fill(154,  205  ,50);
   ellipse(xpos, ypos,21,21);
 }

void reset(){
    xpos = random(51, width - 51);
    ypos = random(51, height - 51);
 }   
}

class enemy{
  float xpos, ypos;
    enemy(){
    xpos = random(10, width - 10);
    ypos = random(10, height - 10);
  }

void display(){   
   fill(219,112,147);
   ellipse(xpos, ypos,21,21);
 }

void reset(){
    xpos = random(51, width - 51);
    ypos = random(51, height - 51);
 }   
}

class snake{
  int len;
  int len0;
  float slen;
  String dir; 
  ArrayList &lt;Float&gt; xpos, ypos;

  snake(){
    len = 1;
    slen = 20.5;
    xpos = new ArrayList();
    ypos = new ArrayList();
    xpos.add( random(width) );
    ypos.add( random(height) );
  }

void move(){
   for(int i = len - 1; i &gt; 0; i = i -1 ){
    xpos.set(i, xpos.get(i - 1));
    ypos.set(i, ypos.get(i - 1));  
   } 
   if(dir == "left"){
     xpos.set(0, xpos.get(0) - slen);
   }
   if(dir == "right"){
     xpos.set(0, xpos.get(0) + slen);
   }

   if(dir == "up"){
     ypos.set(0, ypos.get(0) - slen);

   }

   if(dir == "down"){
     ypos.set(0, ypos.get(0) + slen);
   }
   xpos.set(0, (xpos.get(0) + width) % width);
   ypos.set(0, (ypos.get(0) + height) % height);

 // check if hit itself and if so cut off the tail
    if( checkHit() == true){
   stopgame=true;

    }
  }

void display(){
    for(int i = 0; i &lt;len; i++){
      stroke(179, 140, 198);
      fill(99 , 184 , 255, map(i-1, 0, len-1, 250, 50));
      ellipse(xpos.get(i), ypos.get(i), slen, slen);
    }  
  }


void addLink(){
    xpos.add( xpos.get(len-1) + slen);
    ypos.add( ypos.get(len-1) + slen);
    len++;
  }

void removeLink(){
    xpos.remove( xpos.get(len-1) + slen);
    ypos.remove( ypos.get(len-1) + slen);
    len--;
    if (sn.len == 0){
    stopgame=true;
    }    
  }
   boolean checkHit(){
    for(int i = 1; i &lt; len; i++){
     if( dist(xpos.get(0), ypos.get(0), xpos.get(i), ypos.get(i)) &lt; slen){
       return true;
     }
    } 
    return false;
   } 
}
</code></pre>
]]></description>
   </item>
   <item>
      <title>having a trail from my snake</title>
      <link>https://forum.processing.org/two/discussion/10635/having-a-trail-from-my-snake</link>
      <pubDate>Mon, 04 May 2015 15:20:03 +0000</pubDate>
      <dc:creator>thirdtimesa</dc:creator>
      <guid isPermaLink="false">10635@/two/discussions</guid>
      <description><![CDATA[<p>Hi, i'm creating a slightly different version of snake where everytime the food is eaten, the speed increases. However, i would like to Jazz it up a bit and add a trail that fades coming from the snake. I've tried adding a 4th number to my rect so that it would be transparent and fade but that doesn't work. Any suggestions? Here is my code. Thanks</p>

<pre><code>Snake snake;

int headx;
int heady;
int size = 20;
int speed = 10;
char direction;
boolean foodEaten;
int eatx;
int eaty;
int snakesize = 1;
int fps = 5;
PImage background;
int highScore;

void setup() {
  size(1900, 1000);
  rectMode(CENTER);
  smooth();
  foodEaten = false;
  eatx=(round(random(90))+1)*10;
  eaty=(round(random(70))+1)*10;
  snake = new Snake();
  background = loadImage("Tron background.jpg");
}

void draw() {
  background(background);
  frameRate(fps);
  displayFood();
  snake.display();
  checkCollision();
  drawScoreboard();
}

void drawScoreboard() {
  //draw scoreboard
  fill(#FFB2B2);
  textSize(30);
  text("Score: " + highScore, 800, 400);
  fill(#FFB2B2);
  textSize(40);
  text("High Score:" + highScore, 800, 500);
}


void displayFood() {
  fill(#E3D250);
  ellipse(eatx, eaty, size, size);
}

void checkCollision() {
  if (snake.headx == eatx &amp;&amp; snake.heady == eaty) {
    eatx=(round(random(90))+1)*10;
    eaty=(round(random(60))+1)*10;
    foodEaten = true;
    fps += 5;
  } 
  else {
    foodEaten = false;
  }
}

void keyPressed() {
  if (key == 'w') { //UP
    direction = 'w';
  } 
  else if (key == 'a') { //LEFT
    direction = 'a';
  } 
  else if (key == 's') { //DOWN
    direction = 's';
  } 
  else if (key == 'd') { //RIGHT
    direction = 'd';
  }
}

class Snake {

  int headx;
  int heady;
  int snakesize;

  Snake() {
    headx = width/2;
    heady = height/2;
    snakesize = 1;
  }

  void display() {
    fill(#87D9E3);
    noStroke();
    rect(headx, heady, size, size);
    checkMove();
    borders();
    checkCollision();
  }

  void moveUp() {
    heady -=speed;
  }
  void moveLeft() {
    headx -=speed;
  }
  void moveDown() {
    heady +=speed;
  }
  void moveRight() {
    headx +=speed;
  }

  void checkMove() {
    if (direction == 'w') {  // Move up
      snake.moveUp();
    } else if (direction == 'a') { // Move left
      snake.moveLeft();
    } else if (direction == 's') { // Move down
      snake.moveDown();
    } else if (direction == 'd') { // Move right
      snake.moveRight();
    }
  }

  void borders() {
    if (headx &gt; width) {
      //headx=0;
      exit();
    } else if (headx &lt; 0) {
      //headx = width;
      exit();
    } else if (heady &gt; height) {
      //heady = 0;
      exit();
    } else if (heady &lt; 0) {
      //heady = height;
      exit();
    }
  }
}
</code></pre>
]]></description>
   </item>
   </channel>
</rss>