diff --git a/DATA/Sprites/Snake/body.png b/DATA/Sprites/Snake/body.png index 9d8d667..f3a53f9 100644 Binary files a/DATA/Sprites/Snake/body.png and b/DATA/Sprites/Snake/body.png differ diff --git a/main.py b/main.py index 326728a..ad4515b 100644 --- a/main.py +++ b/main.py @@ -22,6 +22,7 @@ class body(): class SNAKE(): def __init__(self, star_point_x, star_point_y, start_rotate = 0): + self.tail_rot = 0 self.body = [ body(star_point_x, star_point_y, start_rotate, "head"), body(star_point_x - 1, star_point_y, start_rotate, "tail") @@ -32,18 +33,27 @@ class SNAKE(): if body.x == x and body.y == y: cont = 1 return cont - def add_dody(self): - self.body[len(self.body)-1].type = "body" + def add_body(self): + last_body = self.body[len(self.body)-1] - new_body = body(0,0, last_body.rotate, "tail") - v_x = int(math.cos(math.radians(last_body.rotate))) - v_y = int(math.sin(math.radians(last_body.rotate))) + new_body = body(last_body.x,last_body.y, self.tail_rot, "tail") + v_x = int(math.cos(math.radians(self.tail_rot))) + v_y = int(math.sin(math.radians(self.tail_rot))) - new_body.x = -v_x + last_body.x - new_body.y = -v_y + last_body.y + new_body.x = last_body.x -v_x + new_body.y = last_body.y -v_y + + l = len(self.body)-1 + + if self.body[l].rotate != self.tail_rot: + self.body[l].type = "body-rotate" + self.body[l].rotate = self.calculate_rot(self.body[l].rotate, self.tail_rot) + else: + self.body[l].type = "body" self.body.append(new_body) + def calculate_rot(self, degr1, degr2): # deg1 - угол ближайшей к голове части # deg2 - угол дальней от голове части @@ -60,40 +70,50 @@ class SNAKE(): return 90 def calculate_coeff(self, degr): - return math.cos(math.radians(degr - 90)) + math.sin(math.radians(degr + 90)) + return math.cos(math.radians(degr)) + math.sin(math.radians(degr)) - def convert_rot_to_tail(self, deg0, deg2 ): + def convert_rot_to_body(self, deg0, deg1 ): # deg1 - угол ближайшей к голове части # deg0 - угол переходной части (он особый) deg0 = deg0 % 360 - deg2 = deg2 % 360 - if (deg0 == 180 and deg2 == 270) or (deg0 == 270 and deg2 == 90): + deg1 = deg1 % 360 + if (deg0 == 180 and deg1 == 270) or (deg0 == 270 and deg1 == 90): return 180 - if (deg0 == 90 and deg2 == 270) or (deg0 == 0 and deg2 == 90): + if (deg0 == 90 and deg1 == 270) or (deg0 == 0 and deg1 == 90): return 0 - if ((deg0 == 90) and deg2 == 180) or (deg0 == 180 and deg2 == 0): + if ((deg0 == 90) and deg1 == 180) or (deg0 == 180 and deg1 == 0): return 90 - if (deg0 == 0 and deg2 == 180) or (deg0 == 270 and deg2 == 0): + if (deg0 == 0 and deg1 == 180) or (deg0 == 270 and deg1 == 0): return 270 return 0 - - def move(self, vector): - l = len(self.body)-1 - i = l + # def convert_rot_to_tail(self, deg0, deg2): + + def calc_tail_rotate(self): + tail = self.body[len(self.body)-1] + pre_last = self.body[len(self.body)-2] + + grad = 0 + pos_x = tail.x + pos_y = tail.y + + while not((pre_last.x == pos_x) and (pre_last.y == pos_y)): + pos_x = tail.x + int(math.cos(math.radians(grad))) + pos_y = tail.y + int(math.sin(math.radians(grad))) + grad += 90 + return grad - 90 + + def move(self, vector): + i = len(self.body)-1 + self.tail_rot = self.body[len(self.body)-1 ].rotate + while i > 0: 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 if self.body[i-1] != "body-rotate" else self.convert_rot_to_tail(self.body[i-1].rotate, self.body[i].rotate) + self.body[i].rotate = self.body[i-1].rotate + self.body[i].type = self.body[i-1].type + i -= 1 - self.body[i].type = self.body[i-1].type - - # elif (self.body[i].type == "tail") and self.body[i-1].type == "body-rotate": - # self.body[l].rotate = self.convert_rot_to_tail(self.body[l-1].rotate, self.body[l].rotate) - - i = i - 1 - - self.body[0].rotate = vector * 90 self.body[0].x = int(math.cos(math.radians(self.body[0].rotate))) + self.body[0].x self.body[0].y = int(math.sin(math.radians(self.body[0].rotate))) + self.body[0].y @@ -102,14 +122,14 @@ class SNAKE(): self.body[1].type = "body-rotate" self.body[1].rotate = self.calculate_rot(self.body[0].rotate, self.body[1].rotate) else: - self.body[1].type = "body" - - - self.body[l].type = "tail" - # self.body[l].rotate = lf.body[l-1].type == "body-rotate" else self.body[l-1].rotate - # print(f"Move {vector}") + self.body[1].type = "body" + + self.body[len(self.body)-1 ].type = "tail" + self.body[len(self.body)-1 ].rotate = self.calc_tail_rotate() + + class Level_data(): def __init__(self, level_data_json = []): if level_data_json: @@ -176,8 +196,8 @@ class MYGAME(arcade.Window): # Загрузка пола for floor in self.Lvl_data.floor: floor_sprite = arcade.Sprite(floor["sprite"], self.scale_) - floor_sprite.center_x = self.level_start[0] - 16 + ( floor["x"] + 1 ) * 32 * self.scale_ - floor_sprite.center_y = self.level_start[1] - 16 + ( floor["y"] + 1 ) * 32 * self.scale_ + floor_sprite.center_x = self.level_start[0] - 16 + ( floor["x"] + 1 ) * 32 * self.scale_ + floor_sprite.center_y = self.level_start[1] - 16 + ( floor["y"] + 1 ) * 32 * self.scale_ self.floor_list.append(floor_sprite) # Загрузка врагов @@ -221,7 +241,7 @@ class MYGAME(arcade.Window): self.food_list[i].kill() self.food.pop(i) self.score += 1 - self.snake.add_dody() + self.snake.add_body() self.player_list.append(arcade.Sprite()) i += 1 # Enemy_spawn