Video of my TV remote controlled 8*10 LED dotmatrix SNAKE GAME
(Also, I made similar LED dotmatrix CAR RACING game and PONG game , those videos are at bottom of the page)
DOWNLOAD CIRCUIT DIAGRAM + SOURCE CODE + HEX CODE (all in a ZIP archive) FROM BELOW:
Click here
Introduction:
Snake is a video game that originated during the late 1970s in arcades and has maintained popularity since then, becoming something of a classic. After it became the standard pre-loaded game on Nokia phones in 1998, Snake found a massive audience. For more details about history of the game, click here.
Here I made a simple snake game in an 8x10 LED dotmatrix display. I used the same display setup which i used in the scrolling text display (in previous post). Instead of a joystick, i could use a phillips TV remote to play the game (keys 2,4,6,8). Also, I provided a UART control (via RS232 in PC) to play the game using PC keyboard. I used a TSOP1738 for the IR signal reception(the concept of RC-5 decoder is explained in my previous post). A PIC16F877A is the heart of the system. Circuit diagram is almost same as that in previous post ( the 10x8 scrolling text display), only modification is I introduced a TSOP1738 (38khz IR signal receiver) and a simple UART interface. (using single transistor circuit).
I think no need to explain more about the working of the game ,as it is there in the video.Now, I feel it much difficult to explain the game logic by words... But I will try to update this post (explanation to the game logic) when I get enough time... Any one interested to do this can use the source code or hex file at the bottom of this page.....
PIC is connected to RS232 of PC using a simple transistor inverter which works as an inverter and a level shifter ('-12' to '+5' and '12' to '0') and this eliminates the need of special level convertor chips like MAX232. Just to receive data via UART, it is better to use the above circuit, more over, i don't have a MAX232 at the time when i made the game.
C Program:
/* ------------------------------------------------------ 8*10 LED DOTMATRIX SNAKE GAME ------------------------------------------------------ This game could be controlled by two input units: 1>COMPUTER KEYBOARD 2>USING A PHILIPS TV REMOTE ----------------------------------------------------- DATE: 25/04/2011 ----------------------------------------------------- by Vinod.S <vinodstanur@gmail.com> ----------------------------------------------------- */ #include<pic.h> #define _XTAL_FREQ 20e6 __CONFIG(0x3F3A); #define out PORTD #define ms __delay_ms #define clock RB6=1;RB6=0; #define reset RB7=1;RB7=0; #define us __delay_us unsigned char head[10], body[10], xcord, ycord, XYbuffer[40], food[10], rec = 0, buf = 0, j, p, length = 1, still, s; unsigned int fdata = 1, speed = 200, i; void display() { reset; for (int s = 0; s < speed; s++) { for (i = 0; i < 10; i++) { p = 1; for (char j = 0; j < 9; j++) { if ((s & 0b00000011) == 1) { out = ~((body[i] | head[i]) & p); } else { out = ~((head[i] | food[i]) & p); } p <<= 1; } PORTD = 255; clock; } } } void pic_init() { TRISD = 0; TRISB6 = 0; TRISB7 = 0; PORTD = 255; TRISB0 = 1; INTE = 1; INTEDG = 0; GIE = 1; PEIE = 1; } void usrt_init() { SYNC = 0; BRGH = 1; SPEN = 1; CREN = 1; RX9 = 0; SPBRG = 10; GIE = 1; PEIE = 1; RCIE = 1; INTF = 0; } void head_coordinate() { if (rec == 2) { if (ycord != 0) { ycord--; } else { ycord = 9; } } else if (rec == 8) { if (ycord != 9) { ycord++; } else { ycord = 0; } } else if (rec == 4) { if (xcord != 0) { xcord--; } else { xcord = 7; } } else if (rec == 6) { if (xcord != 7) { xcord++; } else { xcord = 0; } } } void timer_init() { T2CON = 0b00000100; PR2 = 9; } void interrupt usr() { if (INTF == 1) /////a simple RC5 DECODING//// { PORTD = 255; INTF = 0; if (RB0 == 0) { us(100); if (RB0 == 0) { us(100); if (RB0 == 0) { us(889); if (RB0 == 1) { us(889); if (RB0 == 0) { ms(10.632); buf = 0; for (int xx = 0; xx < 6; xx++) { buf <<= 1; if (RB0 == 0) { buf++; } ms(1.778); } buf = ~buf; buf &= 0b00111111; if (buf == 3) { speed = (speed / 2); if (speed < 5) { speed = 5; } } else if (buf == 1) { speed = (speed * 2); if (speed > 5000) { speed == 5000; } } else { if ((rec != 8) && (buf == 2)) { rec = 2; } if ((rec != 6) && (buf == 4)) { rec = 4; } if ((rec != 4) && (buf == 6)) { rec = 6; } if ((rec != 2) && (buf == 8)) { rec = 8; } } } } } } } } if (RCIF == 1) //////keyboard playing (use w,a,s,d) baud rate 57600) { RCIF = 0; if (rec != 8) if (RCREG == 'w') { rec = 2; } if (rec != 6) if (RCREG == 'a') { rec = 4; } if (rec != 4) if (RCREG == 'd') { rec = 6; } if (rec != 2) if (RCREG == 's') { rec = 8; } } } ////////////to display "out" or any string/////////////////////// void scroll(char a, char b, char c, char d, char e) { short int count = 6; while (count > 0) { body[0] = ~a; a = b; b = c; c = d; d = e; e = 255; reset; for (s = 0; s < still; s++) { for (i = 0; i < 10; i++) { p = 1; for (j = 0; j < 9; j++) { PORTD = ~(body[i] & p); __delay_ms(.1); p <<= 1; } clock; } } for (i = 9; i > 0; i--) { body[i] = body[i - 1]; } count--; } } ////////////////////////font data /////////////////// void A() { scroll(0xC1, 0xB7, 0x77, 0xB7, 0xC1); } //letter A void B() { scroll(0x01, 0x6D, 0x6D, 0x6D, 0x93); } void C() { scroll(0x83, 0x7D, 0x7D, 0x7D, 0xBB); } void D() { scroll(0x01, 0x7D, 0x7D, 0xBB, 0xC7); } void E() { scroll(0x01, 0x6D, 0x6D, 0x6D, 0x7D); } void F() { scroll(0x01, 0x6F, 0x6F, 0x6F, 0x7F); } void G() { scroll(0x83, 0x7D, 0x65, 0x6D, 0xA3); } void H() { scroll(0x01, 0xEF, 0xEF, 0xEF, 0x01); } void I() { scroll(0x7D, 0x7D, 0x1, 0x7D, 0x7D); } void J() { scroll(0xF3, 0x7D, 0x7D, 0x03, 0x7F); } void K() { scroll(0x01, 0xEF, 0xD7, 0xBB, 0x7D); } void L() { scroll(0x01, 0xFD, 0xFD, 0xFD, 0xFD); } void M() { scroll(0x01, 0xBF, 0xDF, 0xBF, 0x01); } void N() { scroll(0x01, 0xBF, 0xDF, 0xEF, 0x01); } void O() { scroll(0x83, 0x7D, 0x7D, 0x7D, 0x83); } void P() { scroll(0x01, 0x6F, 0x6F, 0x6F, 0x9F); } void Q() { scroll(0x83, 0x7D, 0x75, 0x79, 0x81); } void R() { scroll(0x01, 0x6F, 0x6F, 0x6F, 0x91); } void S() { scroll(0x9B, 0x6D, 0x6D, 0x6D, 0xB3); } void T() { scroll(0x7F, 0x7F, 0x01, 0x7F, 0x7F); } void U() { scroll(0x03, 0xFD, 0xFD, 0xFD, 0x03); } void V() { scroll(0x07, 0xFB, 0xFD, 0xFB, 0x07); } void W() { scroll(0x01, 0xFB, 0xF7, 0xFB, 0x01); } void X() { scroll(0x39, 0xD7, 0xEF, 0xD7, 0x39); } void Y() { scroll(0x3F, 0xDF, 0xE1, 0xDF, 0x3F); } void Z() { scroll(0x79, 0x75, 0x6D, 0x5D, 0x3D); } //letter Z void D0() { scroll(0x83, 0x7D, 0x7D, 0x7D, 0x83); } //NUM 0 void D1() { scroll(0xDD, 0xBD, 0x1, 0xFD, 0xFD); } void D2() { scroll(0xBD, 0x79, 0x75, 0x6D, 0x9D); } void D3() { scroll(0x7B, 0x7D, 0x5D, 0x4D, 0x33); } void D4() { scroll(0xCF, 0xAF, 0x6F, 0xEF, 0x1); } void D5() { scroll(0xB, 0x5D, 0x5D, 0x5D, 0x63); } void D6() { scroll(0x83, 0x6D, 0x6D, 0x6D, 0xB3); } void D7() { scroll(0x79, 0x77, 0x6F, 0x5F, 0x3F); } void D8() { scroll(0x93, 0x6D, 0x6D, 0x6D, 0x93); } void D9() { scroll(0x9B, 0x6D, 0x6D, 0x6D, 0x83); } //NUM 9 void SP() { scroll(255, 255, 255, 255, 255); } //SPACE ///////////////////////////////////////// main() { restart: pic_init(); usrt_init(); timer_init(); xcord = 3; ycord = 4; rec = 6; food[8] = (0b00001000); still = 8; length = 1; while (1) { TMR2ON = 0; fdata = (1 << TMR2); TMR2ON = 1; if (fdata > 0b10000000) { fdata = (1 << (TMR2 - 6)); } display(); XYbuffer[0] = ycord; XYbuffer[0] &= 0x0f; XYbuffer[0] |= (xcord << 4); for (i = length; i > 0; i--) { XYbuffer[i] = XYbuffer[i - 1]; } head_coordinate(); XYbuffer[0] = ycord; XYbuffer[0] &= 0x0f; XYbuffer[0] |= (xcord << 4); for (i = 0; i < 10; i++) { if ((XYbuffer[0] & 0x0f) != i) { head[i] = 0; } else { head[i] = (0b10000000 >> (XYbuffer[0] >> 4)); } body[i] = 0; for (j = 1; j < (length + 1); j++) { if ((XYbuffer[j] & 0x0f) == i) { body[i] |= (0b10000000 >> (XYbuffer[j] >> 4)); } } if (head[i] != 0) { if (head[i] == food[i]) { TMR2ON = 0; length++; food[i] = 0; food[TMR2] = fdata; TMR2ON = 1; } } } if ((head[(XYbuffer[0] & 0x0f)] | body[(XYbuffer[0] & 0x0f)]) == body[(XYbuffer[0] & 0x0f)]) { SP(); O(); U(); T(); SP(); for (i = 0; i < 300; i++) { PORTD = 0; ms(.1); clock; } for (i = 0; i <= length; i++) { XYbuffer[i] = 0; food[i] = 0; body[i] = 0; } length = 0; body[0] = 0; goto restart; } } } /*PROGRAM END */
DOWNLOAD THE SOURCE CODE + CIRCUIT DIAGRAM + HEX CODE (all in a ZIP archive) FROM TOP OF THE POST
:100000000A128A110C28FF3FFE000308F2000A08BA :10001000F3000A128A1196290A128A116B2B0A120E :100020008A1195230A128A110A128A1187230A1249 :100030008A110A128A11AB230A128A110330831221 :10004000D5000430D6000630F6000830B200D400E7 :10005000D301D30A1211CF01CF0AD001110A3328DC :100060000310CF0DD00DFF3E031D302812150030B8 :100070005002813003194F02031C4B28CF01CF0AD5 :10008000D0011108FA3E013E48280310CF0DD00DD3 :10009000FF3E031D45280A128A11F5220A128A1111 :1000A00056088316A0000F30A0058312550EF039B4 :1000B0008316A00483125308BE00BF013F083E040C :1000C000031974283E089F3E840083130008CB0068 :1000D0003E08A03E84004B0880000130BE02003084 :1000E000031CBF03BF025E280A128A113B230A12B7 :1000F0008A1156088316A0000F30A0058312550EF2 :10010000F0398316A0048312BE01BF0100303F0204 :100110000A3003193E0203182429831620080F39D8 :100120008312CB00CC013F084C06031D99283E08E2 :100130004B060319A1283E08343E84008313800136 :10014000B9283E08343E84008316200E0F398312EE :10015000CB00CB0A8030CC000030CD00B2284D0D52 :10016000CD0CCC0CCB0BAF28831203134C0883139C :1001700080003E08203E84008001C001C00A530870 :10018000013ECB00003003180130CC00803A803CA7 :10019000031DCC284B0840020318FC2883120313CC :1001A0004008A03E8400831300080F39CB00CC0127 :1001B0003F084C06031DDE283E084B06031DBE28E3 :1001C0004008A03E8400000E0F39CB00CB0A8030DF :1001D000CC000030CD00EF284D0DCD0CCC0CCB0B5E :1001E000EC28831203134C08CE003E08203E840006 :1001F0004E0883138004BE28831203133E08343E46 :10020000840083138008031920293E082A3E8400B5 :100210008313000883120313CB003E08343E84008E :1002200000084B06031D20291211D30A3E082A3E5E :100230008400800111082A3E84004F0880001215B6 :10024000BE0A0319BF0A8628831620080F39203EEC :1002500084008313000883120313CB008316200845 :100260000F39203E84008313000883120313CC004F :10027000831620080F39343E8400000883124C0492 :100280004B06031D2A280A128A116A220A128A11B1 :100290000A128A1181220A128A110A128A11712203 :1002A0000A128A110A128A1179220A128A110A1272 :1002B0008A116A220A128A1183120313BE01BF0136 :1002C00001303F022C3003193E02031875298801C2 :1002D000A630CB00CB0B6A296400831203130617E8 :1002E0000613BE0A0319BF0A6029BE01BF015308E5 :1002F000CB00CC013F084C02031D80293E084B0275 :10030000031C93293E08A03E8400831380013E080D :100310002A3E840080013E08203E84008001BE0AFF :100320000319BF0A7729D301A0010F288B1C342A97 :10033000FF30672288008B100618342AA630F000A0 :10034000F00BA029640067220618342AA630F000BA :10035000F00BA829640067220618342A0630F10041 :10036000C430F000F00BB229F10BB229B729672293 :10037000061C342A0630F100C430F000F00BBE2910 :10038000F10BBE29C32967220618342A4630F10032 :100390000830F000F00BCA29F10BCA296400F701FC :1003A000F401F5010310F70D6722061CF70A0C3063 :1003B000F1008A30F000F00BDB29F10BDB2964003F :1003C000F40A0319F50A7508803AFE0080307E02AF :1003D000031DEC2906307402031CD329FF30F706F5 :1003E0003F30F7057708033A031D052A67220310FB :1003F000D20CD10C003052020530031951020318FF :10040000342A0530D100D201342A770B0C2A672216 :100410000310D10DD20D342A7608083A0319162A92 :100420007708023A031D162A0230F6007608063ACB :100430000319202A7708043A031D202A0430F60005 :100440007608043A03192A2A7708063A031D2A2A4D :100450000630F6007608023A0319342A7708083A7B :10046000031D342A0830F60067228C1E602A8C1285 :100470007608083A0319422A1A08773A031D422AD5 :100480000230F6007608063A03194C2A1A08613A37 :10049000031D4C2A0430F6007608043A0319562A44 :1004A0001A08643A031D562A0630F6007608023A06 :1004B0000319602A1A08733A031D602A0830F600EF :1004C00073088A0072088300FE0E7E0E09008312F4 :1004D00003130800FF30F222C300C400C500C600A9 :1004E000892AFD30F222C300C400C5000330C600D3 :1004F000892A7F30F222C300C401C40AC500C600A5 :10050000892A7D30F222C300C400C5008330C600B2 :10051000892AC8000630C900CA014A08803AFE008C :1005200080307E02031D962A01304902031C080018 :10053000F2224809A0004308C8004408C300450847 :10054000C4004608C500FF30C60086178613C201E6 :10055000540842020318D62ABE01BF0100303F02F0 :100560000A3003193E020318D42AC101C10AC0018E :10057000093040020318CE2A3E08203E840083132F :1005800000084105FF3A8800A630C700C70BC62AFD :100590006400F2220310C10DC00AB82A0617061320 :1005A000BE0A0319BF0AAE2AC20AA82A0930BE0031 :1005B000BF013F083E040319ED2A3E081F3E840098 :1005C00083130008C7003E08203E840047088000CF :1005D0000130BE02031CBF03D92AFF30C907031C28 :1005E000CA038D2A83120313080086178613C401D9 :1005F000C50152084502031DFF2A51084402031891 :100600000800BE01BF0100303F020A3003193E025C :100610000318372BC101C10AC60144080339013A46 :10062000031D182B3E08203E840083130008252B51 :100630003E082A3E84008313000883120313C3007C :100640003E08343E8400000843044105FF3A880018 :100650000310C10DC60A09304602031C0D2BFF30E2 :10066000880006170613BE0A0319BF0A032BC40A23 :100670000319C50AF92A7608023A031D472B5608C2 :100680000319442BD60308000930D6000800760869 :10069000083A031D532B5608093A0319512BD60A61 :1006A0000800D60108007608043A031D5F2B5508A0 :1006B00003195C2BD50308000730D5000800760825 :1006C000063A031D08005508073A0319692BD50A95 :1006D0000800D5010800F70183132030840043305F :1006E0000A128A11A3230A128A11A0308400C8308A :1006F0000A128A11A3230130CF00D001C830D100E3 :10070000D2010130D30083010A128A110F28181276 :10071000181583129817181618130A3083169900A3 :100720008B170B178C168B10080083168801061385 :100730008613FF3083128800831606140B160113EC :100740008B170B17080064008001840A0406031944 :1007500000340406A42B043083129200093083165F :1007600092000800FF3FFF3FFF3FFF3FFF3FFF3F7B :00000001FF
1> MY 'CAR RACING' GAME:
Hey, man,
ReplyDeleteMy name is Jefferson, Im Brasilian! Sorry my English is bad.
A... your blog and project is very very good.
good by, thank you for sharing your talents.
Hi Vinod,
ReplyDeleteReally awesome project.. Congrats !!
Hey Vinod,
ReplyDeleteReally great work !! Good going. Keep at it. All the very best.
Hitesh
Its amazing.....I like to know where u are working now..?
ReplyDeleteNOT WORKING ANY WHERE...:-)
ReplyDeleteHello Vinod.S
ReplyDeleteGreat idea on this project, can you please modify your download link? It gives an 1,6 Kb Archive which is broken.
Thanks, Alex.
hi vinod great work buddy
ReplyDeletecan u explain that if i want to run this snake game on 8/8 dot matrix then should i have to change the code or the same code work on that
This comment has been removed by the author.
ReplyDeleteThis comment has been removed by the author.
ReplyDeletehey vinod
ReplyDeletegreat projects ideas i m impressed i m ur bid fan
pls send me .hex file on this email nurulaming@yahoo.in or nurul1khan@yahoo.in
thankx alot
Before requesting for the hex file, did you checked if the hex file is already posted in the blog post or not?
ReplyDeleteya u r right but i want hex file in .hex format because my programmer is not working on another file format, it's required only .hex or .bin file
ReplyDeletethere r many error in this code . it is ot working , many error anybody not try this project i hope it is fake
ReplyDeletecheck sum is 0*ofcf
error no. wise
ReplyDeleteClean: Deleting intermediary and output files.
Clean: Done.
Executing: "C:\Program Files\Microchip\MPASM Suite\MPASMWIN.exe" /q /p16F877A "game.asm" /l"game.lst" /e"game.err"
Error[105] C:\USERS\MINI\DESKTOP\GAME.ASM 1 : Cannot open file (Include File "pic.h" not found)
Warning[205] C:\USERS\MINI\DESKTOP\GAME.ASM 3 : Found directive in column 1. (__CONFIG)
Error[122] C:\USERS\MINI\DESKTOP\GAME.ASM 9 : Illegal opcode (char)
Error[122] C:\USERS\MINI\DESKTOP\GAME.ASM 10 : Illegal opcode (int)
Error[122] C:\USERS\MINI\DESKTOP\GAME.ASM 11 : Illegal opcode (display)
Error[108] C:\USERS\MINI\DESKTOP\GAME.ASM 12 : Illegal character ({)
Warning[207] C:\USERS\MINI\DESKTOP\GAME.ASM 13 : Found label after column 1. (RB7)
Warning[207] C:\USERS\MINI\DESKTOP\GAME.ASM 14 : Found label after column 1. (for)
Error[108] C:\USERS\MINI\DESKTOP\GAME.ASM 14 : Illegal character (()
Warning[207] C:\USERS\MINI\DESKTOP\GAME.ASM 15 : Found label after column 1. (for)
Error[108] C:\USERS\MINI\DESKTOP\GAME.ASM 15 : Illegal character (()
Warning[207] C:\USERS\MINI\DESKTOP\GAME.ASM 16 : Found label after column 1. (p)
Warning[207] C:\USERS\MINI\DESKTOP\GAME.ASM 17 : Found label after column 1. (for)
Error[108] C:\USERS\MINI\DESKTOP\GAME.ASM 17 : Illegal character (()
Error[113] C:\USERS\MINI\DESKTOP\GAME.ASM 18 : Symbol not previously defined (s)
Error[108] C:\USERS\MINI\DESKTOP\GAME.ASM 18 : Illegal character ({)
Error[142] C:\USERS\MINI\DESKTOP\GAME.ASM 125 : IFs nested too deep
Error[142] C:\USERS\MINI\DESKTOP\GAME.ASM 128 : IFs nested too deep
Error[142] C:\USERS\MINI\DESKTOP\GAME.ASM 131 : IFs nested too deep
Error[142] C:\USERS\MINI\DESKTOP\GAME.ASM 134 : IFs nested too deep
Error[142] C:\USERS\MINI\DESKTOP\GAME.ASM 144 : IFs nested too deep
Error[142] C:\USERS\MINI\DESKTOP\GAME.ASM 147 : IFs nested too deep
Error[142] C:\USERS\MINI\DESKTOP\GAME.ASM 148 : IFs nested too deep
Error[142] C:\USERS\MINI\DESKTOP\GAME.ASM 151 : IFs nested too deep
Error[142] C:\USERS\MINI\DESKTOP\GAME.ASM 152 : IFs nested too deep
Error[142] C:\USERS\MINI\DESKTOP\GAME.ASM 155 : IFs nested too deep
Error[142] C:\USERS\MINI\DESKTOP\GAME.ASM 156 : IFs nested too deep
Error[142] C:\USERS\MINI\DESKTOP\GAME.ASM 159 : IFs nested too deep
Error[142] C:\USERS\MINI\DESKTOP\GAME.ASM 160 : IFs nested too deep
Error[142] C:\USERS\MINI\DESKTOP\GAME.ASM 360 : IFs nested too deep
Error[142] C:\USERS\MINI\DESKTOP\GAME.ASM 375 : IFs nested too deep
Error[142] C:\USERS\MINI\DESKTOP\GAME.ASM 382 : IFs nested too deep
Error[142] C:\USERS\MINI\DESKTOP\GAME.ASM 386 : IFs nested too deep
Error[142] C:\USERS\MINI\DESKTOP\GAME.ASM 387 : IFs nested too deep
Error[142] C:\USERS\MINI\DESKTOP\GAME.ASM 396 : IFs nested too deep
Halting build on first failure as requested.
----------------------------------------------------------------------
Release build of project `C:\Users\mini\Desktop\game.disposable_mcp' failed.
Language tool versions: MPASMWIN.exe v5.35, mplink.exe v4.35
Sat Jun 23 10:07:19 2012
----------------------------------------------------------------------
BUILD FAILED
Hey how you are compiling the code???
ReplyDeleteWhich tool you are using???
Use Hi Tech C compiler in MPLAB to compile this C program.........
i m using ,mplab for make hex file and using emu8086 for make assembly file. so plz uplode the .hex file in .hex format. or send me on my e mail address
ReplyDeletenurul1khan@yahoo.in
nurulaming@yahoo.in
So which is your compiler?????
ReplyDeleteMPLAB is an IDE, not a compiler. There will be some compiler program integrated with the MPLAB, in default it will be Hi Tech C I think... U need to select it while creating new project. Don't select MPASM tools because here the program is in C, not in assembly... just select Hi Tech C tool....
Also I have already posted the .hex file in the blog. Just copy it (double click on the code) and paste it on notepad and save it as .hex file.
ya but plz send me .hex file because when i m start the programming in ic.
ReplyDeleteit gives an error :-configuration bits not found in hex file
program in pic might not run as axpected
Hi vinod, your projects were awesome. How much time you took to complete those projects?
ReplyDelete