HISE Logo Forum
    • Categories
    • Register
    • Login

    There's a snake in my flute!

    Scheduled Pinned Locked Moved Scripting
    5 Posts 4 Posters 1.2k Views
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • d.healeyD
      d.healey
      last edited by d.healey

      alt text

      Content.makeFrontInterface(700, 500);
      
      const var CANVAS_SIZE = 40
      const var TILE_SIZE = 10;
      reg lastKey = 0;
      reg score = 0;
      
      const var drawCanvas = function(g)
      {	
      	g.fillAll(0xFFFFFFFF);
      	
      	g.setColour(0xFF000000);
      	//Canvas is drawn as a grid of tiles
      	for (i = 0; i < CANVAS_SIZE*TILE_SIZE; i++)
      	{
      		for (j = 0; j < CANVAS_SIZE*TILE_SIZE; j++)
      		{
      			g.fillRect([i*TILE_SIZE, j*TILE_SIZE, TILE_SIZE, TILE_SIZE]);
      		}
      	}
      }
      
      const var drawBoard = function(g)
      {
      	reg segment;
      	
      	g.setColour(0xFF00FF00);
      	
      	for (i = 0; i < path.getLength(); i++)
      	{
      		segment = path.getElementAt(i);
      		
      		g.fillRect([segment.x*TILE_SIZE, segment.y*TILE_SIZE, TILE_SIZE, TILE_SIZE]);
      	}
      	
      	//Draw food
      	g.setColour(0xFFFF0000);
      	g.fillRect([food.getX()*TILE_SIZE, food.getY()*TILE_SIZE, TILE_SIZE, TILE_SIZE]);
      }
      
      const var background = Content.addPanel("background", 0, 0)
      background.set("width", CANVAS_SIZE*TILE_SIZE+20);
      background.set("height", CANVAS_SIZE*TILE_SIZE+20);
      background.setPaintRoutine(function(g){g.fillAll(0xFFFFFFFF);});
      
      const var canvas = Content.addPanel("canvas", 10, 10);
      canvas.set("width", CANVAS_SIZE*TILE_SIZE);
      canvas.set("height", CANVAS_SIZE*TILE_SIZE);
      canvas.setPaintRoutine(drawCanvas);
      
      //Snake and food drawn on another panel over canvas - this means the canvas doesn't have to be redrawn for each update
      const var board = Content.addPanel("board", 0, 0);
      board.set("width", CANVAS_SIZE*TILE_SIZE);
      board.set("height", CANVAS_SIZE*TILE_SIZE);
      board.set("parentComponent", "canvas");
      board.setPaintRoutine(drawBoard);
      
      const var btnStart = Content.addButton("btnStart", 450, 0);
      btnStart.set("text", "Start/Pause");
      
      const var lblScore = Content.addLabel("lblScore", 450, 50);
      lblScore.set("width", 150);
      lblScore.set("height", 50);
      lblScore.set("text", "Points: 0");
      lblScore.set("textColour", 0xFF000000);
      lblScore.set("fontSize", 24);
      lblScore.set("alignment", "left");
      
      const var knbSpeed = Content.addKnob("knbSpeed", 450, 100);
      knbSpeed.set("text", "Speed");
      knbSpeed.setRange(1, 5, 1);
      
      const var gameLoop = Engine.createTimerObject();
      
      inline function checkDirection()
      {
      	switch(lastKey)
      	{		
      		case controls.N: if (path.getDirection() != controls.S) path.setDirection(controls.N); break;
      		case controls.S: if (path.getDirection() != controls.N) path.setDirection(controls.S); break;
      		case controls.E: if (path.getDirection() != controls.W) path.setDirection(controls.E); break;
      		case controls.W: if (path.getDirection() != controls.E) path.setDirection(controls.W); break;
      	}
      
      	switch(path.getDirection())
      	{
      		case controls.N: path.moveHead(0, -1); break; //Keep moving n
      		case controls.S: path.moveHead(0, 1); break; //Keep moving s
      		case controls.E: path.moveHead(1, 0); break; //Keep moving e
      		case controls.W: path.moveHead(-1, 0); break; //Keep moving w
      	}
      }
      
      //Check for a collision with the edge of the canvas
      inline function checkBoundary()
      {
      	reg head = path.getHead();
      
      	if (head.x < 0 || head.x+1 > canvas.get("width")/TILE_SIZE || head.y < 0 || head.y+1 > canvas.get("height")/TILE_SIZE)
      	{
      		return true;
      	}
      	return false;
      }
      
      //Check for a collision with food
      inline function checkFood()
      {
      	reg head = path.getHead();
      	
      	if (head.x == food.getX() && head.y == food.getY())
      	{
      		return true;
      	}
      	return false;		
      }
      
      //Check if the snake has eaten itself
      inline function checkCannibalism()
      {
      	reg head = path.getHead();
      	reg segment;
      	
      	for (i = 1; i < path.getLength(); i++) //Start at 1 to skip head
      	{
      		segment = path.getElementAt(i);
      		if (head.x == segment.x && head.y == segment.y)
      		{
      			return true;
      		}
      	}
      	return false;
      }
      
      gameLoop.callback = function()
      {			
      	if (checkBoundary() == true || checkCannibalism() == true)
      	{
      		gameLoop.stopTimer();
      		path.reset();
      		lastKey = 0;
      		btnStart.setValue(0);
      		lblScore.set("text", "***GAME OVER***");
      	}
      	else if (checkFood() == true)
      	{
      		food.changePosition();
      		path.grow();
      		lblScore.set("text", "Points: " + ++score);
      	}
      	
      	path.ripple();	
      	checkDirection();
      	board.repaint();
      };
      
      namespace controls
      {
      	const var N = 60;
      	const var S = 62;
      	const var E = 65;
      	const var W = 64;
      }
      
      namespace food 
      {
      	reg posX;
      	reg posY;
      	
      	inline function changePosition()
      	{
      		posX = Math.randInt(0, CANVAS_SIZE-1);
      		posY = Math.randInt(0, CANVAS_SIZE-1);
      	}
      	
      	inline function getX()
      	{
      		return posX;
      	}
      	
      	inline function getY()
      	{
      		return posY;
      	}
      }
      
      namespace path
      {
      	reg posX = [];
      	reg posY = [];
      	reg direction;
      
      	inline function reset()
      	{
      		posX = [CANVAS_SIZE/2];
      		posY = [CANVAS_SIZE/2];
      		direction = 0;
      	}
      	
      	function grow()
      	{
      		posX.push(0);
      		posY.push(0);
      	}
      	
      	inline function ripple()
      	{
      		for (i = posX.length-1; i > 0; i--)
      		{
      			posX[i] = posX[i-1];
      			posY[i] = posY[i-1];
      		}
      	}
      	   
      	inline function getHead()
      	{
      		return {"x":posX[0], "y":posY[0]};
      	}
      		
      	inline function moveHead(x, y)
      	{
      		posX[0] = posX[0]+x;
      		posY[0] = posY[0]+y;
      	}
      	
      	inline function getElementAt(i)
      	{
      		return {"x":posX[i], "y":posY[i]};
      	}
      	
      	inline function getLength()
      	{
      		return posX.length;
      	}
      	
      	inline function getDirection()
      	{
      		return direction;
      	}
      	
      	inline function setDirection(d)
      	{
      		direction = d;
      	}
      }function onNoteOn()
      {
      	if ([controls.N, controls.S, controls.E, controls.W].contains(Message.getNoteNumber()))
      	{
      		lastKey = Message.getNoteNumber();	
      	}	
      }
      function onNoteOff()
      {
      	
      }
      function onController()
      {
      	
      }
      function onTimer()
      {
      	
      }
      function onControl(number, value)
      {
      	if (number == btnStart) 
      	{
      		if (value == 0)
      		{
      			gameLoop.stopTimer();
      		}
      		else 
      		{
      			path.setDirection(0);
      			food.changePosition();
      			lblScore.set("text", "Points: 0");
      			gameLoop.startTimer(50*(6-knbSpeed.getValue()));
      		}
      	}
      }
      

      Libre Wave - Freedom respecting instruments and effects
      My Patreon - HISE tutorials
      YouTube Channel - Public HISE tutorials

      1 Reply Last reply Reply Quote 3
      • Dark BoubouD
        Dark Boubou
        last edited by

        omg bravo D.Healey!

        1 Reply Last reply Reply Quote 1
        • Christoph HartC
          Christoph Hart
          last edited by

          Great! I need to dust off the Pong script then we can make a HISE Retro Games collection.

          1 Reply Last reply Reply Quote 3
          • d.healeyD
            d.healey
            last edited by

            Just had a reason to fix this for the latest HISE, so here's the snippet (this is not my finest work and there is much room for improvement!)

            HiseSnippet 2597.3ocsYztaaibjJIL4j5kCIEGJ5O25ebUN9CQ4ZmqHt4Z7GxWMhsiPTtj3ZXDrhbkzFSQRPtx1p476P+UeB56QA5STeCtNytbIWJQonXfSvFf678L6ryLbY63PWVRRXrUkpuYbDypxWa2YbfXvdCn7.qC22px2XeLMQvhIJP6NNhljv7rpT4t+HBnR06YI+8+9q6R8oAtrbPVVuMj6xNhOjKxg19Euj66e.0i8F9PCp27EG5FFrWne3HvdtqsiUD08BZe1ITjr6XaU49s73hv3NBpfk.zran23NCBuJPQ+a4I7t9LbQSqNffTfs1a.22qs1WSrrpbu14d9cUd92ZeL2imAOOB7HIBRNGlwfJ2YdlTyu.SphgIcOkI8X6Ntw7HQNFzd9M1GF.aH8nPn1zTTzZcmG86r2KDnHPr9P5ErChgEYbT+6cbVkrkiyxaWqFDtSDjKovl6Nm71c57gNG92aQdNYSGCTu4viZoQzzY6Zwr9DeHk3krw.jT.Itg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K60E6qamB6G42xnUk6U7Rpu+hcI0tp2k0fvvfCC3hWEwRWePnuGd4y3ySek1VouLLd41o6F.oB4Ua+MoWscGetGK1hCBols1Orjw+7uLvidg0Oc39TAUKIPnfhhXwBN5OU1mcI2kotz7p16yRt.1jrpXmc+IfOqz6C0WoNduNR09014W2l00.SUPUNV+fb5cPp2+eWQYLpgxMgXsHp4qrUS1qTQMsJpYph+4TpPCAc6Lc7fYoiGXKG4XddwrUwD23jgEaMst0aepqQJc6SeRRo++aEC8i4zRKN+RjrRnWxNLnsrmJ98KXATHID+VKGwnwAHHSMWqnWKudoTEqO+Lgh2vzw+WVS53ajBQYaO1dhwLsxu6HjZfveAoVeSQHreepDxteHX+2FuenBgrGtHY7EsbOPnIhw3Qx6ai2ij0PtmmeVcCiu51+IQvhRMnruD2vheXtgzqyW9suHYTud7qkeQsdb+gIBvrNbHTq1pxu09mfRYdrdzQ9B7cZBrfdOWv7dSnjdUr5A1Ji1Z5OY0irONzajOUT7KngepvTDn4a9YqvOMU.3ViMs3ufOqlyb+rZKpI9X613srTtMdmRrQ3L4uF1X5Gi7g1s50CZejaf2y9f2ea+xieF0qt.49vL6wXZgMzptCj06x.sG.0VRvTx6f04Tqczmp6vB7jK9E3WJxl35JoHapQZMj5FG9A8DEPv6qjP.aJP90cqZeLtlzTW6211YcGLsm+AWWz8WCZwTNOabK34OcK3YyaAOacK34o2Bd99aAO+44xCVHdmQhvgpiC.f1sj8fqTokrRsLKz5+CXEpsEM
            

            Peek 2021-10-19 17-08.gif

            Libre Wave - Freedom respecting instruments and effects
            My Patreon - HISE tutorials
            YouTube Channel - Public HISE tutorials

            ustkU 1 Reply Last reply Reply Quote 6
            • ustkU
              ustk @d.healey
              last edited by ustk

              @d-healey I made one about a year ago, I'll look if I can find it back :)
              It was part of a game bundle I planned to release for iOS but it'll probably never see the daylight...
              It was using the MIDI keyboard but now keys are handled in panels I'll try an update :)

              Can't help pressing F5 in the forum...

              1 Reply Last reply Reply Quote 1
              • First post
                Last post

              37

              Online

              1.7k

              Users

              11.9k

              Topics

              103.5k

              Posts