资源描述
python贪吃蛇设计报告_Python写个贪吃蛇#Write By Guobao#2017/4/7#贪吃蛇#做边界,*做物,o做体和头部#python 3.6.1importcopyimportrandomimportosimportmsvcrt#the board class,used to put everythingclassboard:_points=def _init_(self):self._points.clear()for i in range(22):line=if i=0 or i=21:for j in range(22):line.append(#)else:line.append(#)for j in range(20):line.append()line.append(#)self._points.append(line)defgetPoint(self,location):return self._pointslocation0location1defclear(self):self._points.clear()for i in range(22):line=if i=0 or i=21:for j in range(22):line.append(#)else:line.append(#)for j in range(20):line.append()line.append(#)self._points.append(line)defput_snake(self,snake_locations):#clear the boardself.clear()#put the snake pointsfor x insnake_locations:self._pointsx0 x1=o#the headx=snake_locationslen(snake_locations)-1self._pointsx0 x1=Odefput_food(self,food_location):self._pointsfood_location0food_location1=*defshow(self):os.system(cls)for i in range(22):for j in range(22):print(self._pointsij,end=)print()#the snake classclasssnake:_points=def _init_(self):for i in range(1,6):self._points.append(1,i)defgetPoints(self):return self._points#move to the next position#give the next headdefmove(self,next_head):self._points.pop(0)self._points.append(next_head)#eat the food#give the next headdefeat(self,next_head):self._points.append(next_head)#calc the next state#and return the directiondef next_head(self,direction=default):#need to change the value,so copy ithead=copy.deepcopy(self._pointslen(self._points)-1)#calc the default directionif direction=default:neck=self._pointslen(self._points)-2if neck0 head0:direction=upelif neck0direction=downelif neck1 head1:direction=leftelif neck1=15:self.show()print(Good Job)breakelse:self.new_food()self.show()#反向Q神仙elif new_head=self.snake.getPoints()len(self.snake.getPoints()-2:pass#rush the wallelif self.board.getPoint(new_head)=#or self.board.getPoint(new_head)=o:print(GG)break#normal moveelse:self.snake.move(new_head)self.show()op=msvcrt.getch()game().run()
展开阅读全文