I added a web page to PHPoC Shield for Arduino(P4S-347/348) to control a snake on the 8X8 Matrix LED.
If a user presses one of the direction buttons, the direction value is sent to Arduino through HTML5 Web socket. Then the snake changes its direction.
The Snake Game
For someone who doesn't know about the Snake game. I will introduce the rules of this game. The snake moves to have a fruit. Whenever the snake eats a fruit, it gets longer. If the snake bites itself, the game is over, but if it eats 15 fruits, the user wins the game.
1. Upload PHPoC Shield source code Please refert to the manual of PHPoC Debugger below and upload the source code(snake_game.php). http://www.phpoc.com/support/manual/phpoc_debugger_manual/contents.php?id=major_upload
/*************************************************** This is a library for our I2C LED Backpacks Designed specifically to work with the Adafruit LED Matrix backpacks ----> http://www.adafruit.com/products/872 ----> http://www.adafruit.com/products/871 ----> http://www.adafruit.com/products/870 These displays use I2C to communicate, 2 pins are required to interface. There are multiple selectable I2C addresses. For backpacks with 2 Address Select pins: 0x70, 0x71, 0x72 or 0x73. For backpacks with 3 Address Select pins: 0x70 thru 0x77 Adafruit invests time and resources providing this open source code, please support Adafruit and open-source hardware by purchasing products from Adafruit! Written by Limor Fried/Ladyada for Adafruit Industries. BSD license, all text above must be included in any redistribution ****************************************************/// Snake on 8x8Matrix // 2013-06-15 JorgVisch (http://fritzing.org/projects/snake-8x8-led-matrix-ada-fruit)// 2017-04-12 Amy Kim#include<Wire.h>#include<Adafruit_GFX.h>#include<Adafruit_LEDBackpack.h>#include"SPI.h"#include"Phpoc.h"PhpocServerserver(80);// LED facestaticconstuint8_tPROGMEMsmile_bmp[]={B00111100,B01000010,B10100101,B10000001,B10100101,B10011001,B01000010,B00111100},frown_bmp[]={B00111100,B01000010,B10100101,B10000001,B10011001,B10100101,B01000010,B00111100};// directionconstintTOP=0;constintRIGHT=1;constintBOTTOM=2;constintLEFT=3;// SnakeconstintMAX_SNAKE_LENGTH=15;// VariablesAdafruit_BicolorMatrixmatrix=Adafruit_BicolorMatrix();// Displayintdirection=TOP;// direction of movementintsnakeX[MAX_SNAKE_LENGTH];// X-coordinates of snakeintsnakeY[MAX_SNAKE_LENGTH];// Y-coordinates of snakeintsnakeLength=1;// nr of parts of snakeunsignedlongprevTime=0;// for gamedelay (ms)unsignedlongdelayTime=500;// Game step in msintfruitX,fruitY;unsignedlongfruitPrevTime=0;unsignedlongfruitBlinkTime=1000/250;intfruitLed=LED_RED;intmatrixColor;voidsetup(){Serial.begin(9600);while(!Serial);Phpoc.begin(PF_LOG_SPI|PF_LOG_NET);server.beginWebSocket("snake");Serial.print("WebSocket server address : ");Serial.println(Phpoc.localIP());Serial.println("Game is started.");randomSeed(analogRead(0));// Init led matrix matrix.begin(0x70);matrix.setRotation(3);// init snakesnakeX[0]=4;snakeY[0]=7;for(inti=1;i<MAX_SNAKE_LENGTH;i++)snakeX[i]=snakeY[i]=-1;makeFruit();printString("S");}voidloop(){checkButtons();unsignedlongcurrentTime=millis();if(currentTime-prevTime>=delayTime){nextstep();prevTime=currentTime;}draw();}voidcheckButtons(){// wait for a new client:PhpocClientclient=server.available();if(client){if(client.available()>0){// read the bytes incoming from the client:charthisChar=client.read();if(thisChar=='0')direction=TOP;if(thisChar=='1')direction=LEFT;if(thisChar=='2')direction=RIGHT;if(thisChar=='3')direction=BOTTOM;}}}voiddraw(){matrix.clear();drawSnake();drawFruit();matrix.writeDisplay();}voiddrawSnake(){for(inti=0;i<snakeLength;i++)matrix.drawPixel(snakeX[i],snakeY[i],LED_GREEN);}voiddrawFruit(){if(inPlayField(fruitX,fruitY)){unsignedlongcurrenttime=millis();if(currenttime-fruitPrevTime>=fruitBlinkTime){fruitLed=(fruitLed==LED_RED)?LED_OFF:LED_RED;fruitPrevTime=currenttime;}matrix.drawPixel(fruitX,fruitY,fruitLed);}}booleaninPlayField(intx,inty){return(x>=0)&&(x<8)&&(y>=0)&&(y<8);}voidnextstep(){for(inti=snakeLength;i>0;i--){if((direction==RIGHT)&&(snakeX[0]-snakeLength==7))snakeX[0]=-1;elseif((direction==LEFT)&&(snakeX[0]+snakeLength==0))snakeX[0]=8;elsesnakeX[i]=snakeX[i-1];if((direction==TOP)&&(snakeY[0]+snakeLength==0))snakeY[0]=8;elseif((direction==BOTTOM)&&(snakeY[0]-snakeLength==7))snakeY[0]=-1;elsesnakeY[i]=snakeY[i-1];}switch(direction){caseTOP:snakeY[0]=snakeY[0]-1;break;caseRIGHT:snakeX[0]=snakeX[0]+1;break;caseBOTTOM:snakeY[0]=snakeY[0]+1;break;caseLEFT:snakeX[0]=snakeX[0]-1;break;}if((snakeX[0]==fruitX)&&(snakeY[0]==fruitY)){snakeLength++;if(snakeLength<MAX_SNAKE_LENGTH)makeFruit();elsefruitX=fruitY=-1;}snakeCheck();}voidmakeFruit(){intx,y;x=random(0,8);y=random(0,8);while(isPartOfSnake(x,y)){x=random(0,8);y=random(0,8);}fruitX=x;fruitY=y;}booleanisPartOfSnake(intx,inty){for(inti=0;i<snakeLength-1;i++){if((x==snakeX[i])&&(y==snakeY[i]))returntrue;}returnfalse;}voidsnakeCheck(){for(inti=1;i<snakeLength;i++){// snake touches itselfif((snakeX[0]==snakeX[i])&&(snakeY[0]==snakeY[i]))userLose();}if(snakeLength==MAX_SNAKE_LENGTH)userWin();}voiduserLose(){Serial.println("Game Over");printString("O");matrix.clear();matrix.drawBitmap(0,0,frown_bmp,8,8,LED_RED);matrix.writeDisplay();delay(1000);snakeLength=1;setup();loop();}voiduserWin(){Serial.println("You Win");printString("W");matrix.clear();matrix.drawBitmap(0,0,smile_bmp,8,8,LED_GREEN);matrix.writeDisplay();delay(1000);snakeLength=1;setup();loop();}voidprintString(Stringstr){StringmatrixComment;matrix.setTextWrap(false);// we dont want text to wrap so it scrolls nicelymatrix.setTextSize(1);if(str=="O"){matrixComment="Game Over!";matrixColor=LED_RED;}elseif(str=="W"){matrixComment="You Win!";matrixColor=LED_GREEN;}elseif(str=="S"){matrixComment="Go!";matrixColor=LED_YELLOW;}elsematrixColor=LED_YELLOW;matrix.setTextColor(matrixColor);for(int8_tx=7;x>=-60;x--){matrix.clear();matrix.setCursor(x,0);matrix.print(matrixComment);matrix.writeDisplay();delay(70);}}
Comments
Please log in or sign up to comment.