import random
x1=random.randint(0,10)
y1=random.randint(0,10)
p=(x1,y1)
class snake:
body = [(0,0)]
length = 1
size = 10
direction = 90
def __init__(self):
self.body=[(0,0)]
self.length = len(self.body)
def move(self,d):
if d==90:
x =self.body[0][0]+1
y = self.body[0][1]
self.body.insert(0,(x,y))
self.body.pop()
self.direction=90
#del self.body[self.length-1]
if d==180:
x =self.body[0][0]
y = self.body[0][1]+1
self.body.insert(0,(x,y))
self.direction=180
self.body.pop()
if d==-90:
x =self.body[0][0]-1
y = self.body[0][1]
self.body.insert(0,(x,y))
self.direction=-90
self.body.pop()
if d==0:
x =self.body[0][0]
y = self.body[0][1]-1
self.body.insert(0,(x,y))
self.direction=0
self.body.pop()
def eat(self):
if self.direction==90:
x =self.body[0][0]+1
y = self.body[0][1]
self.body.insert(0,(x,y))
self.direction=90
#del self.body[self.length-1]
if self.direction==180:
x =self.body[0][0]
y = self.body[0][1]+1
self.body.insert(0,(x,y))
self.direction=180
if self.direction==-90:
x =self.body[0][0]-1
y = self.body[0][1]
self.body.insert(0,(x,y))
self.direction=-90
if self.direction==0:
x =self.body[0][0]
y = self.body[0][1]-1
self.body.insert(0,(x,y))
self.direction=0
def show(self):
print(self.body)
return self.body
def get_body(self):
return self.body
def panduan(self):
o = self.body[0]
hh = self.body[1:]
if o in hh:
self.body=[(0,0)]
s = snake()
s.show()
s.move(90)
s.show()
s.move(180)
s.show()
s.eat()
s.show()
s.move(0)
s.move(0)
s.panduan()
s.show()