Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed
Eng.cpp
00001 #include "Eng.h" 00002 00003 Eng::Eng() 00004 { 00005 00006 } 00007 00008 Eng::~Eng() 00009 { 00010 00011 } 00012 00013 void Eng::init() 00014 { 00015 dSnake.init(5,10); //she de chu shi zuo biao 00016 _noodles.init(20,7); 00017 _gameOver = false; 00018 00019 } 00020 00021 00022 00023 void Eng::userinput(Gamepad &pad) 00024 { 00025 _d = pad.get_direction(); 00026 } 00027 00028 void Eng::draw(N5110 &lcd) 00029 { 00030 lcd.drawRect(0,0,84,48,FILL_TRANSPARENT); 00031 // 00032 for (int i = 0; i < 84; i++) { 00033 for (int j = 0; j < 48; j++) { 00034 if (snake1[j][i] != 0) { 00035 lcd.drawRect((2 * j) + 2,(2 * i) + 2,2,2,FILL_BLACK); 00036 } 00037 } 00038 } 00039 } 00040 00041 void Eng::score(N5110 &lcd) 00042 { 00043 00044 lcd.clear(); 00045 lcd.printString("Score ",0,0); 00046 char buffer[14]; 00047 int _score = (dSnake.getLength() - 5); 00048 00049 sprintf(buffer,"%3d",_score); 00050 lcd.printString(buffer,0,1); 00051 00052 00053 00054 lcd.refresh(); 00055 00056 wait(5.0); 00057 } 00058 00059 void Eng::update(Gamepad &pad) 00060 { 00061 00062 memset(snake1, 0, sizeof(snake1)); 00063 dSnake.update(_d); 00064 00065 dead(); 00066 00067 snake1[_noodles.xcoordinate()][_noodles.ycoordinate()] = 2; 00068 00069 if (checkFood()) { 00070 00071 pad.tone(1000.0,0.1); 00072 00073 growSnake(); 00074 00075 bool empty = false; 00076 00077 while (!empty) { 00078 00079 _noodles.random(); 00080 00081 if ( snake1[_noodles.xcoordinate()][_noodles.ycoordinate()] == 0) { 00082 00083 empty = true; 00084 } 00085 } 00086 } 00087 } 00088 00089 void Eng::dead() 00090 { 00091 00092 int _l =dSnake.getLength(); 00093 00094 for (int i = 0; i < _l; i++) { 00095 00096 if (dSnake.xcoordinate(i) > 40) { 00097 _gameOver = true; 00098 } else if( dSnake.xcoordinate(i) < 0) { 00099 _gameOver = true; 00100 } else if (dSnake.ycoordinate(i) > 22 ) { 00101 _gameOver = true; 00102 } else if(dSnake.ycoordinate(i) < 0) { 00103 _gameOver = true; 00104 } 00105 00106 if (snake1[dSnake.xcoordinate(i)][dSnake.ycoordinate(i)] != 1) { 00107 snake1[dSnake.xcoordinate(i)][dSnake.ycoordinate(i)] = 1; 00108 } else { 00109 _gameOver = true; 00110 } 00111 } 00112 } 00113 00114 bool Eng::checkFood() 00115 { 00116 if (snake1[dSnake.xcoordinate(0)][dSnake.ycoordinate(0)] == 2) { 00117 return true; 00118 } else { 00119 return false; 00120 } 00121 } 00122 00123 void Eng::growSnake() 00124 { 00125 if (dSnake.getLength()<4032) { 00126 00127 dSnake.grow(); 00128 00129 } 00130 } 00131 00132 float Eng::getScore() 00133 { 00134 return _score; 00135 } 00136 00137 00138 bool Eng::getGameOver() 00139 { 00140 return _gameOver; 00141 }
Generated on Fri Jul 15 2022 14:43:24 by
1.7.2