add: Snake right movement #5

Merged
stud178861 merged 4 commits from fix-paul into master 2025-05-07 22:32:11 +00:00
2 changed files with 25 additions and 7 deletions
Showing only changes of commit 81404a51da - Show all commits

32
main.py
View File

@ -35,25 +35,43 @@ class SNAKE():
def add_dody(self):
self.body[len(self.body)-1].type = "body"
last_body = self.body[len(self.body)-1]
new_body = body(0,0, last_body.rotate, "tail")
v_x = int(-math.sin(-last_body.rotate * math.pi / 180))
new_body.x = -v_x + last_body.x
v_y = int(math.cos(-last_body.rotate * math.pi / 180))
new_body.y = -v_y + last_body.y
self.body.append(new_body)
def move(self, vector):
self.body[0].rotate = vector * 90
i = len(self.body)-1
self.body[len(self.body)-1].type = "tail"
self.body[i].x = self.body[i-1].x
self.body[i].y = self.body[i-1].y
self.body[i].rotate = self.body[i-1].rotate
i-=1
while i > 0:
if self.body[i].type == "body" or self.body[i].type == "tail":
self.body[i].x = self.body[i-1].x
self.body[i].y = self.body[i-1].y
self.body[i].rotate = self.body[i-1].rotate
i = i - 1
self.body[i].x = self.body[i-1].x
self.body[i].y = self.body[i-1].y
self.body[0].x = int(-math.sin(-self.body[0].rotate * math.pi / 180)) + self.body[0].x
self.body[0].y = int(math.cos(-self.body[0].rotate * math.pi / 180)) + self.body[0].y
if abs(math.cos(self.body[i+1].rotate)) == abs(math.cos(self.body[i-1].rotate)) or abs(math.sin(self.body[i+1].rotate)) == abs(math.sin(self.body[i-1].rotate)):
self.body[i].type = "body"
self.body[i].rotate = self.body[i-1].rotate
else:
self.body[i].type = "body-rotate"
self.body[i].rotate = (self.body[i+1].rotate - 90) % 360
i = i - 1
self.body[0].x = int(-math.sin(-(vector * 90) * math.pi / 180)) + self.body[0].x
self.body[0].y = int(math.cos(-(vector * 90) * math.pi / 180)) + self.body[0].y
# print(f"Move {vector}")