Advertisement
Guest User

Snake Game in Processing

a guest
Oct 27th, 2019
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 10.40 KB | None | 0 0
  1. //Snake Game in Processing 3.5.3 (www.processing.org)
  2.  
  3. import processing.sound.*;
  4. import processing.serial.*;
  5.  
  6. Serial joyStick;
  7. int joyStickCommand=RIGHT;
  8. boolean joyStickEnabled=false;
  9.  
  10. int pixW = 50;
  11. ArrayList<bodypart> snake;
  12. float trem = 0;
  13. int keycode = RIGHT;
  14. boolean press = false;
  15. int prevcode = 0;
  16. int foodX;
  17. int foodY;
  18. float root = 2;
  19. int menu = 0;
  20. long sndtimer = 0;
  21.  
  22. int ease = 2000;
  23. boolean start = false;
  24. boolean pause = false;
  25.  
  26. TriOsc tri, tri2;
  27. SawOsc saw;
  28. boolean sound = true;
  29.  
  30. PFont font;
  31.  
  32. void foodCoordinates(int recursion)
  33. {
  34.   foodX = int(random(width/pixW));
  35.   foodY = int(random(height/pixW));
  36.   for (bodypart part : snake)
  37.   {
  38.     if (part.m_x == foodX && part.m_y == foodY)
  39.     {
  40.       if (recursion != 0) foodCoordinates(recursion - 1);
  41.       break;
  42.     }
  43.   }
  44. }
  45.  
  46. void init()
  47. {
  48.   snake.clear();
  49.   snake.add(new bodypart(0, int(height/2/pixW)));
  50.   snake.add(new bodypart(1, int(height/2/pixW)));
  51.   snake.add(new bodypart(2, int(height/2/pixW)));
  52.   snake.add(new bodypart(3, int(height/2/pixW)));
  53. }
  54.  
  55. void stopsnd()
  56. {
  57.   tri.stop();
  58.   tri2.stop();
  59.   saw.stop();
  60. }
  61.  
  62. void reinit()
  63. {
  64.   snake = new ArrayList<bodypart>();
  65.   init();
  66.   foodCoordinates(1000);
  67.   root = 2;
  68. }
  69.  
  70. void setup()
  71. {
  72.   if (Serial.list().length > 1) joyStick = new Serial(this, Serial.list()[1], 38400);
  73.  
  74.   font = createFont("Courier New", 50);
  75.   textFont(font);
  76.   fullScreen();
  77.   noCursor();
  78.   background(0);
  79.   textSize(300);
  80.   fill(0, 255, 0);
  81.   textAlign(CENTER, CENTER);
  82.   text("SNAKE", width/2, height/6.5);
  83.   textSize(50);
  84.   text("1. EASY\n2. MEDIUM\n3. HARD\n4. HARDCORE\nJ - TOGGLE EXTERNAL JOYSTICK\nS - TOGGLE SOUND\nE - EXIT", width/2, 2*height/3);
  85.  
  86.   snake = new ArrayList<bodypart>();
  87.  
  88.   init();
  89.  
  90.   tri = new TriOsc(this);
  91.   tri2 = new TriOsc(this);
  92.   saw = new SawOsc(this);
  93.  
  94.   tri.freq(440);
  95.   tri2.freq(880);
  96.   saw.freq(50);
  97.  
  98.   foodX = int(random(width/pixW));
  99.   foodY = int(random(height/pixW));
  100.  
  101.   sndtimer = 100;
  102.  
  103.   pause = false;
  104.   stroke(0);
  105. }
  106.  
  107. void draw()
  108. {
  109.   if (start)
  110.   {
  111.     if (!joyStickEnabled)
  112.     {
  113.       if (keyCode > 36 && keyCode < 41)
  114.       {
  115.         if (abs(keyCode-prevcode) != 2) keycode = keyCode;
  116.         if (keyPressed)
  117.         {
  118.           if (!press)
  119.             if (sound) {
  120.               stopsnd();
  121.               tri.play();
  122.               sndtimer = millis() + 100;
  123.             }
  124.           press = true;
  125.         } else {
  126.           press = false;
  127.         }
  128.       }
  129.     } else {
  130.       if (abs(joyStickCommand-prevcode) != 2) keycode = joyStickCommand;
  131.     }
  132.  
  133.     if (keyPressed && (key == 'p' || key == 'P'))
  134.  
  135.       if (pause)
  136.       {
  137.         background(0);
  138.         textSize(300);
  139.         text("PAUSE", width/2, height/2);
  140.       }
  141.  
  142.     if (!pause && trem > (trem = millis()%(ease/(root+2))))
  143.     {
  144.       bodypart test = new bodypart(0, 0);
  145.       prevcode = keycode;
  146.       switch(keycode)
  147.       {
  148.       case UP:
  149.         test = snake.get(snake.size()-1).up();
  150.         break;
  151.       case DOWN:
  152.         test = snake.get(snake.size()-1).down();
  153.         break;
  154.       case LEFT:
  155.         test = snake.get(snake.size()-1).left();
  156.         break;
  157.       case RIGHT:
  158.         test = snake.get(snake.size()-1).right();
  159.         break;
  160.       default:
  161.         break;
  162.       }
  163.       if (test.m_x < 0 || test.m_x >= width/pixW || test.m_y < 0 || test.m_y >= height/pixW)
  164.       {
  165.         menu = 1;
  166.         start = false;
  167.         test = new bodypart(0, 0);
  168.         if (sound) {
  169.           stopsnd();
  170.           saw.play();
  171.           sndtimer = millis() + 200;
  172.         }
  173.       } else
  174.       {
  175.         boolean crash = false;
  176.         for (bodypart part : snake)
  177.         {
  178.           if (part.m_x == test.m_x && part.m_y == test.m_y)
  179.           {
  180.             crash = true;
  181.             break;
  182.           }
  183.         }
  184.         if (!crash)
  185.         {
  186.           snake.add(test);
  187.           if (test.m_x == foodX && test.m_y == foodY)
  188.           {
  189.             foodCoordinates(1000);
  190.             root = sqrt(snake.size());
  191.             if (sound)
  192.             {
  193.               stopsnd();
  194.               tri2.play();
  195.               sndtimer = millis() + 100;
  196.             }
  197.           } else {
  198.             snake.remove(0);
  199.           }
  200.         } else {
  201.           menu = 1;
  202.           start = false;
  203.           if (sound) {
  204.             stopsnd();
  205.             saw.play();
  206.             sndtimer = millis() + 100;
  207.           }
  208.         }
  209.       }
  210.     }
  211.     background(0);
  212.     fill(255, 0, 0);
  213.     rect(foodX*pixW, foodY*pixW, pixW, pixW);
  214.     fill(0, 255, 0);
  215.     for (bodypart part : snake)
  216.     {
  217.       part.update();
  218.     }
  219.   } else
  220.   {
  221.     switch(menu)
  222.     {
  223.     case 0:
  224.       if (!keyPressed) press=false;
  225.       else
  226.         switch(key)
  227.       {
  228.       case '1':
  229.         ease = 2000;
  230.         start = true;
  231.         if (sound) {
  232.           stopsnd();
  233.           tri.play();
  234.           sndtimer = millis() + 100;
  235.         }
  236.         break;
  237.       case '2':
  238.         ease = 1000;
  239.         start = true;
  240.         if (sound) {
  241.           stopsnd();
  242.           tri.play();
  243.           sndtimer = millis() + 100;
  244.         }
  245.         break;
  246.       case '3':
  247.         ease = 600;
  248.         start = true;
  249.         if (sound) {
  250.           stopsnd();
  251.           tri.play();
  252.           sndtimer = millis() + 100;
  253.         }
  254.         break;
  255.       case '4':
  256.         ease = 300;
  257.         start = true;
  258.         if (sound) {
  259.           stopsnd();
  260.           tri.play();
  261.           sndtimer = millis() + 100;
  262.         }
  263.         break;
  264.       case 'J':
  265.       case 'j':
  266.         if (keyPressed)
  267.         {
  268.           if (!press)
  269.           {
  270.             joyStickEnabled = !joyStickEnabled;
  271.             stopsnd();
  272.             (joyStickEnabled ? tri2 : tri).play();
  273.             sndtimer = millis() + 100;
  274.           }
  275.           press = true;
  276.         } else {
  277.           press = false;
  278.         }
  279.         break;
  280.       case 'S':
  281.       case 's':
  282.         if (keyPressed)
  283.         {
  284.           if (!press)
  285.           {
  286.             sound = !sound;
  287.             stopsnd();
  288.             (sound ? tri2 : tri).play();
  289.             sndtimer = millis() + 100;
  290.           }
  291.           press = true;
  292.         }
  293.         break;
  294.       case 'E':
  295.       case 'e':
  296.         exit();
  297.       default:
  298.         break;
  299.       }
  300.       break;
  301.     case 1:
  302.       keycode = RIGHT;
  303.       background(0);
  304.       textSize(150);
  305.       fill(255, 0, 0);
  306.       text("GAME OVER", width/2, height/4);
  307.       textSize(35);
  308.       text("YOUR SCORE: " + (snake.size()-4) + "\n\n\n\n\n\n\n", width/2, height/1.5);
  309.       fill(0, 255, 0);
  310.       text("\n1. EASY\n2. MEDIUM\n3. HARD\n4. HARDCORE\nJ - TOGGLE EXTERNAL JOYSTICK\nS - TOGGLE SOUND\nE - EXIT", width/2, height/1.5);
  311.       if (!keyPressed) press=false;
  312.       else
  313.         switch(key)
  314.       {
  315.       case '1':
  316.         ease = 2000;
  317.         start = true;
  318.         if (sound) {
  319.           stopsnd();
  320.           tri.play();
  321.           sndtimer = millis() + 100;
  322.         }
  323.         reinit();
  324.         break;
  325.       case '2':
  326.         ease = 1000;
  327.         start = true;
  328.         if (sound) {
  329.           stopsnd();
  330.           tri.play();
  331.           sndtimer = millis() + 100;
  332.         }
  333.         reinit();
  334.         break;
  335.       case '3':
  336.         ease = 600;
  337.         start = true;
  338.         if (sound) {
  339.           stopsnd();
  340.           tri.play();
  341.           sndtimer = millis() + 100;
  342.         }
  343.         reinit();
  344.         break;
  345.       case '4':
  346.         ease = 300;
  347.         start = true;
  348.         if (sound) {
  349.           stopsnd();
  350.           tri.play();
  351.           sndtimer = millis() + 100;
  352.         }
  353.         reinit();
  354.         break;
  355.       case 'J':
  356.       case 'j':
  357.         if (keyPressed)
  358.         {
  359.           if (!press)
  360.           {
  361.             joyStickEnabled = !joyStickEnabled;
  362.             stopsnd();
  363.             (joyStickEnabled ? tri2 : tri).play();
  364.             sndtimer = millis() + 100;
  365.           }
  366.           press = true;
  367.         } else {
  368.           press = false;
  369.         }
  370.         break;
  371.       case 'S':
  372.       case 's':
  373.         if (keyPressed)
  374.         {
  375.           if (!press)
  376.           {
  377.             sound = !sound;
  378.             stopsnd();
  379.             (sound ? tri2 : tri).play();
  380.             sndtimer = millis() + 100;
  381.           }
  382.           press = true;
  383.         }
  384.         break;
  385.       case 'E':
  386.       case 'e':
  387.         exit();
  388.       default:
  389.         break;
  390.       }
  391.       break;
  392.     default:
  393.       break;
  394.     }
  395.   }
  396.   if (sndtimer < millis()) stopsnd();
  397. }
  398.  
  399. class bodypart
  400. {
  401.   public int m_x, m_y;
  402.   bodypart(int x, int y)
  403.   {
  404.     m_x = x;
  405.     m_y = y;
  406.   }
  407.  
  408.   void update()
  409.   {
  410.     rect(m_x*pixW, m_y*pixW, pixW, pixW);
  411.   }
  412.  
  413.   bodypart up()
  414.   {
  415.     return new bodypart(m_x, m_y-1);
  416.   }
  417.  
  418.   bodypart down()
  419.   {
  420.     return new bodypart(m_x, m_y+1);
  421.   }
  422.   bodypart left()
  423.   {
  424.     return new bodypart(m_x-1, m_y);
  425.   }
  426.   bodypart right()
  427.   {
  428.     return new bodypart(m_x+1, m_y);
  429.   }
  430. }
  431.  
  432. float vx, vy, but;
  433. boolean pausebuf = true;
  434. void serialEvent (Serial joyStick) {
  435.  
  436.   String inString = joyStick.readStringUntil('\n');
  437.   if (inString != null) {
  438.  
  439.     inString = trim(inString);
  440.     int inputs[] = int(split(inString, ','));
  441.     if (inputs.length == 3) {
  442.       vy = inputs[0];
  443.       vx = inputs[1];
  444.       but = inputs[2];
  445.       if (start)
  446.       {
  447.         if (but == 0)
  448.         {
  449.           if (pausebuf) pause = !pause;
  450.           pausebuf = false;
  451.         } else {
  452.           pausebuf = true;
  453.         }
  454.         if (vy < 400)
  455.         {
  456.           if (joyStickCommand != DOWN && sound)
  457.           {
  458.             stopsnd();
  459.             tri.play();
  460.             sndtimer = millis() + 100;
  461.           }
  462.           joyStickCommand = DOWN;
  463.         }
  464.         if (vy > 1024-400)
  465.         {
  466.           if (joyStickCommand != UP && sound)
  467.           {
  468.             stopsnd();
  469.             tri.play();
  470.             sndtimer = millis() + 100;
  471.           }
  472.           joyStickCommand = UP;
  473.         }
  474.         if (vx < 400)
  475.         {
  476.           if (joyStickCommand != LEFT && sound)
  477.           {
  478.             stopsnd();
  479.             tri.play();
  480.             sndtimer = millis() + 100;
  481.           }
  482.           joyStickCommand = LEFT;
  483.         }
  484.         if (vx > 1024-400)
  485.         {
  486.           if (joyStickCommand != RIGHT && sound)
  487.           {
  488.             stopsnd();
  489.             tri.play();
  490.             sndtimer = millis() + 100;
  491.           }
  492.           joyStickCommand = RIGHT;
  493.         }
  494.       }
  495.     }
  496.   }
  497. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement