add: Snake right movement #5

Merged
stud178861 merged 4 commits from fix-paul into master 2025-05-07 22:32:11 +00:00
6 changed files with 109 additions and 36 deletions

Binary file not shown.

Binary file not shown.

Before

Width:  |  Height:  |  Size: 371 B

After

Width:  |  Height:  |  Size: 308 B

Binary file not shown.

Binary file not shown.

Before

Width:  |  Height:  |  Size: 329 B

After

Width:  |  Height:  |  Size: 305 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 315 B

After

Width:  |  Height:  |  Size: 222 B

145
main.py
View File

@ -21,7 +21,8 @@ class body():
self.type = type self.type = type
class SNAKE(): class SNAKE():
def __init__(self, star_point_x, star_point_y, start_rotate = 90): def __init__(self, star_point_x, star_point_y, start_rotate = 0):
self.tail_rot = 0
self.body = [ self.body = [
body(star_point_x, star_point_y, start_rotate, "head"), body(star_point_x, star_point_y, start_rotate, "head"),
body(star_point_x - 1, star_point_y, start_rotate, "tail") body(star_point_x - 1, star_point_y, start_rotate, "tail")
@ -32,30 +33,102 @@ class SNAKE():
if body.x == x and body.y == y: if body.x == x and body.y == y:
cont = 1 cont = 1
return cont return cont
def add_dody(self): def add_body(self):
self.body[len(self.body)-1].type = "body"
last_body = self.body[len(self.body)-1] 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 = body(last_body.x,last_body.y, self.tail_rot, "tail")
new_body.x = -v_x + last_body.x v_x = int(math.cos(math.radians(self.tail_rot)))
v_y = int(math.cos(-last_body.rotate * math.pi / 180)) v_y = int(math.sin(math.radians(self.tail_rot)))
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) self.body.append(new_body)
def calculate_rot(self, degr1, degr2):
# deg1 - угол ближайшей к голове части
# deg2 - угол дальней от голове части
degr1 = degr1 % 360
degr2 = degr2 % 360
if (degr1 == 180 and degr2 == 270) or (degr1 == 90 and degr2 == 0):
return 180
if (degr1 == 0 and degr2 == 90) or (degr1 == 270 and degr2 == 180):
return 0
if ((degr1 == 90) and degr2 == 180) or (degr1 == 0 and degr2 == 270):
return 90
if (degr1 == 270 and degr2 == 0) or (degr1 == 180 and degr2 == 90):
return 270
return 90
def calculate_coeff(self, degr):
return math.cos(math.radians(degr)) + math.sin(math.radians(degr))
def convert_rot_to_body(self, deg0, deg1 ):
# deg1 - угол ближайшей к голове части
# deg0 - угол переходной части (он особый)
deg0 = deg0 % 360
deg1 = deg1 % 360
if (deg0 == 180 and deg1 == 270) or (deg0 == 270 and deg1 == 90):
return 180
if (deg0 == 90 and deg1 == 270) or (deg0 == 0 and deg1 == 90):
return 0
if ((deg0 == 90) and deg1 == 180) or (deg0 == 180 and deg1 == 0):
return 90
if (deg0 == 0 and deg1 == 180) or (deg0 == 270 and deg1 == 0):
return 270
return 0
# 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): def move(self, vector):
self.body[0].rotate = vector * 90 i = len(self.body)-1
i = len(self.body)-1 self.tail_rot = self.body[len(self.body)-1 ].rotate
while i > 0:
if self.body[i].type == "body" or self.body[i].type == "tail": while i > 0:
self.body[i].x = self.body[i-1].x self.body[i].x = self.body[i-1].x
self.body[i].y = self.body[i-1].y self.body[i].y = self.body[i-1].y
self.body[i].rotate = self.body[i-1].rotate self.body[i].rotate = self.body[i-1].rotate
i = i - 1 self.body[i].type = self.body[i-1].type
i -= 1
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 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
if self.body[0].rotate != self.body[1].rotate:
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[len(self.body)-1 ].type = "tail"
self.body[len(self.body)-1 ].rotate = self.calc_tail_rotate()
# print(f"Move {vector}")
class Level_data(): class Level_data():
def __init__(self, level_data_json = []): def __init__(self, level_data_json = []):
@ -102,8 +175,8 @@ class MYGAME(arcade.Window):
# food # food
self.food_list = arcade.SpriteList() self.food_list = arcade.SpriteList()
self.food_sleep = 300 self.food_sleep = 200
self.delay = 400 self.delay = 200
self.food = [] self.food = []
# Счет # Счет
self.score = 0 self.score = 0
@ -115,7 +188,7 @@ class MYGAME(arcade.Window):
# Загрузка змеи # Загрузка змеи
for body in self.snake.body: for body in self.snake.body:
snake_sprite = arcade.Sprite(f"./DATA/Sprites/Snake/{body.type}.png", self.scale_) snake_sprite = arcade.Sprite(f"./DATA/Sprites/Snake/{body.type}.png", self.scale_)
snake_sprite.angle = 90 snake_sprite.angle = 0
snake_sprite.center_x = self.level_start[0] - 16 + ( body.x + 1 ) * 32 * self.scale_ snake_sprite.center_x = self.level_start[0] - 16 + ( body.x + 1 ) * 32 * self.scale_
snake_sprite.center_y = self.level_start[1] - 16 + ( body.y + 1 ) * 32 * self.scale_ snake_sprite.center_y = self.level_start[1] - 16 + ( body.y + 1 ) * 32 * self.scale_
self.player_list.append(snake_sprite) self.player_list.append(snake_sprite)
@ -123,8 +196,8 @@ class MYGAME(arcade.Window):
# Загрузка пола # Загрузка пола
for floor in self.Lvl_data.floor: for floor in self.Lvl_data.floor:
floor_sprite = arcade.Sprite(floor["sprite"], self.scale_) 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_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_y = self.level_start[1] - 16 + ( floor["y"] + 1 ) * 32 * self.scale_
self.floor_list.append(floor_sprite) self.floor_list.append(floor_sprite)
# Загрузка врагов # Загрузка врагов
@ -153,7 +226,7 @@ class MYGAME(arcade.Window):
i = 0 i = 0
while i < len(self.snake.body): while i < len(self.snake.body):
self.player_list[i] = arcade.Sprite(f"./DATA/Sprites/Snake/{self.snake.body[i].type}.png", self.scale_) self.player_list[i] = arcade.Sprite(f"./DATA/Sprites/Snake/{self.snake.body[i].type}.png", self.scale_)
self.player_list[i].angle = self.snake.body[i].rotate self.player_list[i].angle = 360 - self.snake.body[i].rotate
self.player_list[i].center_x = self.level_start[0] - 16 + ( self.snake.body[i].x + 1 ) * 32 * self.scale_ self.player_list[i].center_x = self.level_start[0] - 16 + ( self.snake.body[i].x + 1 ) * 32 * self.scale_
self.player_list[i].center_y = self.level_start[1] - 16 + ( self.snake.body[i].y + 1 ) * 32 * self.scale_ self.player_list[i].center_y = self.level_start[1] - 16 + ( self.snake.body[i].y + 1 ) * 32 * self.scale_
i = i + 1 i = i + 1
@ -168,7 +241,7 @@ class MYGAME(arcade.Window):
self.food_list[i].kill() self.food_list[i].kill()
self.food.pop(i) self.food.pop(i)
self.score += 1 self.score += 1
self.snake.add_dody() self.snake.add_body()
self.player_list.append(arcade.Sprite()) self.player_list.append(arcade.Sprite())
i += 1 i += 1
# Enemy_spawn # Enemy_spawn
@ -212,20 +285,20 @@ class MYGAME(arcade.Window):
if len(self.player_list) > 0: if len(self.player_list) > 0:
if key in (arcade.key.UP, arcade.key.W): if key in (arcade.key.UP, arcade.key.W):
if self.floor_grid[self.snake.body[0].y + 1][self.snake.body[0].x] == 1 and self.snake.body[0].rotate != 180 and not(self.snake.check_contact(self.snake.body[0].x ,self.snake.body[0].y+1)): if self.floor_grid[self.snake.body[0].y + 1][self.snake.body[0].x] == 1 and self.snake.body[0].rotate != 270 and not(self.snake.check_contact(self.snake.body[0].x ,self.snake.body[0].y+1)):
self.snake.move(0) self.snake.move(1)
elif key in (arcade.key.DOWN, arcade.key.S): elif key in (arcade.key.DOWN, arcade.key.S):
if self.floor_grid[self.snake.body[0].y - 1][self.snake.body[0].x] == 1 and self.snake.body[0].rotate != 0 and not(self.snake.check_contact(self.snake.body[0].x ,self.snake.body[0].y-1)): if self.floor_grid[self.snake.body[0].y - 1][self.snake.body[0].x] == 1 and self.snake.body[0].rotate != 90 and not(self.snake.check_contact(self.snake.body[0].x ,self.snake.body[0].y-1)):
self.snake.move(2)
elif key in (arcade.key.LEFT, arcade.key.A):
if self.floor_grid[self.snake.body[0].y][self.snake.body[0].x - 1] == 1 and self.snake.body[0].rotate != 90 and not(self.snake.check_contact(self.snake.body[0].x - 1,self.snake.body[0].y)):
self.snake.move(3) self.snake.move(3)
elif key in (arcade.key.LEFT, arcade.key.A):
if self.floor_grid[self.snake.body[0].y][self.snake.body[0].x - 1] == 1 and self.snake.body[0].rotate != 0 and not(self.snake.check_contact(self.snake.body[0].x - 1,self.snake.body[0].y)):
self.snake.move(2)
elif key in (arcade.key.RIGHT, arcade.key.D): elif key in (arcade.key.RIGHT, arcade.key.D):
if self.floor_grid[self.snake.body[0].y][self.snake.body[0].x + 1] == 1 and self.snake.body[0].rotate != 270 and not(self.snake.check_contact(self.snake.body[0].x + 1,self.snake.body[0].y)): if self.floor_grid[self.snake.body[0].y][self.snake.body[0].x + 1] == 1 and self.snake.body[0].rotate != 180 and not(self.snake.check_contact(self.snake.body[0].x + 1,self.snake.body[0].y)):
self.snake.move(1) self.snake.move(0)
def on_key_release(self, key, modifiers): def on_key_release(self, key, modifiers):
"""Вызывается, когда пользователь отпускает клавишу""" """Вызывается, когда пользователь отпускает клавишу"""