Snake
Dependencies: mbed
Fork of el17x2l by
Snake.cpp
00001 #include"Snake.h" 00002 00003 Snake::Snake() 00004 { 00005 curr=head=new Body(24,17); 00006 curr->next=new Body(24,13); 00007 curr->next->next=new Body(24,9); 00008 tail=curr->next->next; 00009 tail->next=NULL; 00010 length=3; 00011 direction=4; 00012 _height=4; 00013 _width=4; 00014 } 00015 00016 Snake::~Snake() 00017 { 00018 curr=head; 00019 while(curr!=NULL) 00020 { 00021 head=head->next; 00022 delete curr; 00023 curr=head; 00024 } 00025 } 00026 00027 void Snake::grow() 00028 { 00029 curr=head; 00030 Body* temp=new Body; 00031 00032 while(curr->next!=tail) 00033 { 00034 curr=curr->next; 00035 } 00036 00037 if(curr->x==tail->x) 00038 { 00039 if(curr->y>tail->y) 00040 { 00041 temp->x=tail->x; 00042 temp->y=tail->y+_height; 00043 } 00044 else 00045 { 00046 temp->x=tail->x; 00047 temp->y=tail->y-_height; 00048 } 00049 } 00050 else if(curr->y==tail->y) 00051 { 00052 if(curr->x>tail->x) 00053 { 00054 temp->x=tail->x-_width; 00055 temp->y=tail->y; 00056 } 00057 else 00058 { 00059 temp->x=tail->x+_width; 00060 temp->y=tail->y; 00061 } 00062 } 00063 tail=temp; 00064 tail->next=NULL; 00065 curr->next->next=tail; 00066 curr=head; 00067 length++; 00068 } 00069 00070 const int& Snake::getdir() 00071 { 00072 return direction; 00073 } 00074 00075 void Snake::move(N5110 &lcd) 00076 { 00077 curr=head; 00078 Body* temp=new Body; 00079 switch (direction){ 00080 case 1: 00081 temp->x=head->x-_width; 00082 temp->y=head->y; 00083 head=temp; 00084 head->next=curr; 00085 break; 00086 case 2: 00087 temp->x=head->x+_width; 00088 temp->y=head->y; 00089 head=temp; 00090 head->next=curr; 00091 break; 00092 case 3: 00093 temp->x=head->x; 00094 temp->y=head->y-_height; 00095 head=temp; 00096 head->next=curr; 00097 break; 00098 case 4: 00099 temp->x=head->x; 00100 temp->y=head->y+_height; 00101 head=temp; 00102 head->next=curr; 00103 break; 00104 } 00105 while(curr->next!=tail) 00106 { 00107 curr=curr->next; 00108 } 00109 00110 while(lcd.getPixel(head->x,head->y)==true) 00111 { 00112 lcd.clear(); 00113 lcd.printString(" GAME OVER ",0,3); 00114 lcd.refresh(); 00115 lcd.inverseMode(); 00116 wait(0.5); 00117 lcd.normalMode(); 00118 wait(0.5); 00119 } 00120 delete tail; 00121 tail=curr; 00122 tail->next=NULL; 00123 curr=head; 00124 draw(lcd); 00125 }//把尾巴接到头前面的一个,实现移动控制 00126 00127 void Snake::update(Gamepad &pad) 00128 { 00129 if (pad.check_event(Gamepad::X_PRESSED) == true) 00130 { 00131 if (direction != 2)//不能控制蛇走相反的反向 00132 direction = 1; 00133 } 00134 else if (pad.check_event(Gamepad::B_PRESSED) == true) 00135 { 00136 if (direction != 1) 00137 direction = 2; 00138 } 00139 else if (pad.check_event(Gamepad::Y_PRESSED) == true) 00140 { 00141 if (direction != 4) 00142 direction = 3; 00143 } 00144 else if (pad.check_event(Gamepad::A_PRESSED) == true) 00145 { 00146 if (direction != 3) 00147 direction = 4; 00148 } 00149 } 00150 00151 const Body* Snake::gethead() 00152 { 00153 return head; 00154 } 00155 00156 const Body* Snake::gettail() 00157 { 00158 return tail; 00159 } 00160 00161 const Body* Snake::getcurr() 00162 { 00163 return curr; 00164 } 00165 00166 const int &Snake::getlength() 00167 { 00168 return length; 00169 } 00170 00171 void Snake::backcurr() 00172 { 00173 curr=head; 00174 } 00175 00176 void Snake::draw(N5110 &lcd) 00177 { 00178 lcd.drawRect(gethead()->x, gethead()->y,_height,_width,FILL_TRANSPARENT); 00179 lcd.setPixel(gettail()->x,gettail()->y,false); 00180 lcd.setPixel(gettail()->x+1,gettail()->y,false); 00181 lcd.setPixel(gettail()->x+2,gettail()->y,false); 00182 lcd.setPixel(gettail()->x+3,gettail()->y,false); 00183 lcd.setPixel(gettail()->x,gettail()->y+1,false); 00184 lcd.setPixel(gettail()->x,gettail()->y+2,false); 00185 lcd.setPixel(gettail()->x,gettail()->y+3,false); 00186 lcd.setPixel(gettail()->x+1,gettail()->y+3,false); 00187 lcd.setPixel(gettail()->x+2,gettail()->y+3,false); 00188 lcd.setPixel(gettail()->x+3,gettail()->y+3,false); 00189 lcd.setPixel(gettail()->x+3,gettail()->y+1,false); 00190 lcd.setPixel(gettail()->x+3,gettail()->y+2,false); 00191 } 00192 /* 00193 void Snake::add_score() 00194 { 00195 score_v = _menu.get_score_v(); 00196 _score += score_v; 00197 } 00198 00199 int Snake::get_score() 00200 { 00201 return _score; 00202 } 00203 */
Generated on Tue Jul 26 2022 09:08:54 by
1.7.2
