Compare commits
	
		
			No commits in common. "51b43d541e069e4b2518ea817f860240592961c0" and "978473cf4d73db3d8366d42b299ecd16622defd0" have entirely different histories.
		
	
	
		
			51b43d541e
			...
			978473cf4d
		
	
		
							
								
								
									
										294
									
								
								main.py
									
									
									
									
									
								
							
							
						
						
									
										294
									
								
								main.py
									
									
									
									
									
								
							| @ -33,32 +33,99 @@ class SNAKE(): | ||||
|             if body.x == x and body.y == y: | ||||
|                 cont = 1 | ||||
|         return cont | ||||
|      | ||||
|     def add_body(self): | ||||
|          | ||||
|         last_body = self.body[len(self.body)-1] | ||||
| 
 | ||||
|         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 = 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 - угол дальней от голове части  | ||||
|         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 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_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): | ||||
|         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 | ||||
|             self.body[i].type = self.body[i-1].type | ||||
|             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 | ||||
| 
 | ||||
|         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() | ||||
| 
 | ||||
|          | ||||
| 
 | ||||
| @ -95,8 +162,9 @@ class MYGAME(arcade.Window): | ||||
|         else: | ||||
|             print("Error to load level") | ||||
|          | ||||
|         self.player_body_list = arcade.SpriteList() | ||||
|         self.player_list = arcade.SpriteList() | ||||
|         self.floor_list = arcade.SpriteList() | ||||
|          | ||||
|         self.enemies_list = arcade.SpriteList() | ||||
|         self.enemy_spawn = arcade.SpriteList() | ||||
| 
 | ||||
| @ -112,7 +180,7 @@ class MYGAME(arcade.Window): | ||||
|         self.food = [] | ||||
|         # Счет | ||||
|         self.score = 0 | ||||
|         self.scale_ = 1.4 | ||||
|         self.scale_ = 1.3 | ||||
| 
 | ||||
|         screen_center = [SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2] | ||||
|         self.level_start = [screen_center[0] - self.Lvl_data.size[0] * 16 * self.scale_, screen_center[1] - self.Lvl_data.size[1] * 16 * self.scale_] | ||||
| @ -120,104 +188,25 @@ class MYGAME(arcade.Window): | ||||
|         # Загрузка змеи | ||||
|         for body in self.snake.body: | ||||
|             snake_sprite = arcade.Sprite(f"./DATA/Sprites/Snake/{body.type}.png", self.scale_) | ||||
|             snake_sprite.type = body.type | ||||
|             snake_sprite.angle = 0 | ||||
|             snake_sprite.center_x = self.level_start[0] - 16 + ( body.x  ) * 32 * self.scale_ | ||||
|             snake_sprite.center_y = self.level_start[1] - 16 + ( body.y  ) * 32 * self.scale_ | ||||
|             self.player_body_list.append(snake_sprite) | ||||
|             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_ | ||||
|             self.player_list.append(snake_sprite) | ||||
| 
 | ||||
|         # Загрузка пола | ||||
| 
 | ||||
|         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"] ) * 32 * self.scale_ | ||||
|             floor_sprite.center_y = self.level_start[1] - 16  + ( floor["y"] ) * 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) | ||||
|          | ||||
|          | ||||
|     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 calc_tail_rotate(self): | ||||
|         tail = self.player_body_list[len(self.player_body_list)-1] | ||||
|         pre_last = self.player_body_list[len(self.player_body_list)-2] | ||||
| 
 | ||||
|         grad = 0 | ||||
|         pos_x = tail.center_x | ||||
|         pos_y = tail.center_y | ||||
| 
 | ||||
|         while not((pre_last.center_x == pos_x) and (pre_last.center_y == pos_y)): | ||||
|             pos_x = tail.center_x + int(math.cos(math.radians(grad))) * 32 * self.scale_ | ||||
|             pos_y = tail.center_y + int(math.sin(math.radians(grad))) * 32 * self.scale_ | ||||
|             grad += 90 | ||||
|         return grad - 90 | ||||
|      | ||||
|     def update_sprite(self, type): | ||||
|         return f"./DATA/Sprites/Snake/{type}.png" | ||||
|      | ||||
|     def player_move(self, vector): | ||||
|         i = len(self.player_body_list)-1  | ||||
|         self.tail_rot = self.player_body_list[i].angle | ||||
| 
 | ||||
|         while i > 0: | ||||
|             self.player_body_list[i].center_x = self.player_body_list[i-1].center_x | ||||
|             self.player_body_list[i].center_y = self.player_body_list[i-1].center_y | ||||
|             self.player_body_list[i].angle = self.player_body_list[i-1].angle | ||||
|             self.player_body_list[i].type = self.player_body_list[i-1].type | ||||
|             self.player_body_list[i].texture = self.player_body_list[i-1].texture | ||||
|             i -= 1 | ||||
| 
 | ||||
|         self.player_body_list[0].angle = vector * 90 | ||||
|         self.player_body_list[0].center_x = int(math.cos(math.radians(self.player_body_list[0].angle))) * 32 * self.scale_ + self.player_body_list[0].center_x | ||||
|         self.player_body_list[0].center_y = int(math.sin(math.radians(self.player_body_list[0].angle))) * 32 * self.scale_ + self.player_body_list[0].center_y | ||||
|         self.player_body_list[0].angle = 360 - self.player_body_list[0].angle | ||||
| 
 | ||||
|         if self.player_body_list[0].angle != self.player_body_list[1].angle: | ||||
|             self.player_body_list[1].type = "body-rotate" | ||||
|             self.player_body_list[1].angle = 360 - self.calculate_rot(360 - self.player_body_list[0].angle, 360 - self.player_body_list[1].angle) | ||||
|         else: | ||||
|             self.player_body_list[1].type = "body" | ||||
|              | ||||
|         self.player_body_list[1].texture = arcade.load_texture(self.update_sprite(self.player_body_list[1].type)) | ||||
| 
 | ||||
|         self.player_body_list[ len(self.player_body_list) - 1 ].type = "tail" | ||||
|         self.player_body_list[ len(self.player_body_list) - 1 ].texture = arcade.load_texture(self.update_sprite( self.player_body_list[ len(self.player_body_list) - 1 ].type )) | ||||
|         self.player_body_list[ len(self.player_body_list) - 1 ].angle = 360 - self.calc_tail_rotate() | ||||
| 
 | ||||
|     def add_body(self): | ||||
|          | ||||
|         last_body = self.player_body_list[len(self.player_body_list)-1] | ||||
| 
 | ||||
|         new_body = arcade.Sprite(self.update_sprite( "tail" ), self.scale_,last_body.center_x, last_body.center_y, self.tail_rot) | ||||
|         v_x = int(math.cos(math.radians(360 - self.tail_rot))) * 32 * self.scale_ | ||||
|         v_y = int(math.sin(math.radians(360 - self.tail_rot))) * 32 * self.scale_  | ||||
| 
 | ||||
|         new_body.center_x = last_body.center_x - v_x | ||||
|         new_body.center_y = last_body.center_y - v_y | ||||
| 
 | ||||
|         l = len(self.player_body_list)-1 | ||||
| 
 | ||||
|         if 360 - self.player_body_list[l].angle != 360 - self.tail_rot: | ||||
|             self.player_body_list[l].type = "body-rotate"  | ||||
|             self.player_body_list[l].angle = 360 - self.calculate_rot(360 - self.player_body_list[l].angle, 360 - self.tail_rot) | ||||
|         else: | ||||
|             self.player_body_list[l].type = "body" | ||||
|              | ||||
|         self.player_body_list[l].texture = arcade.load_texture(self.update_sprite(self.player_body_list[l].type)) | ||||
|         self.player_body_list.append(new_body) | ||||
| 
 | ||||
|         # Загрузка врагов | ||||
|         # if "enemies" in self.level_data: | ||||
|         #     for enemy in self.level_data["enemies"]: | ||||
|         #         enemy_sprite = arcade.Sprite(enemy["sprite"], 1) | ||||
|         #         enemy_sprite.center_x = self.level_start[0] - 16 + ( enemy["x"] + 1 ) * 32 * self.scale_ | ||||
|         #         enemy_sprite.center_y = self.level_start[0] - 16 + ( enemy["y"] + 1 ) * 32 * self.scale_ | ||||
|         #         self.enemies_list.append(enemy_sprite) | ||||
|     def spawn_food(self): | ||||
|         spawn = False | ||||
|         while spawn == False: | ||||
| @ -227,25 +216,34 @@ class MYGAME(arcade.Window): | ||||
|                 self.food.append(food(x,y)) | ||||
|                 food_sprite = arcade.Sprite(f"./DATA/Sprites/Enemies/Tier 0/healthy_food.png", self.scale_) | ||||
|                 food_sprite.angle = 0 | ||||
|                 food_sprite.center_x = self.level_start[0] - 16 + ( x ) * 32 * self.scale_ | ||||
|                 food_sprite.center_y = self.level_start[1] - 16 + ( y ) * 32 * self.scale_ | ||||
|                 food_sprite.center_x = self.level_start[0] - 16 + ( x + 1 ) * 32 * self.scale_ | ||||
|                 food_sprite.center_y = self.level_start[1] - 16 + ( y + 1 ) * 32 * self.scale_ | ||||
|                 self.food_list.append(food_sprite) | ||||
|                 spawn = True | ||||
|      | ||||
| 
 | ||||
|     def update(self): | ||||
|         if len(self.snake.body) > 0: | ||||
|             i = 0 | ||||
|             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].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_y = self.level_start[1] - 16 + ( self.snake.body[i].y + 1 ) * 32 * self.scale_ | ||||
|                 i = i + 1 | ||||
|         # Food | ||||
|         self.food_sleep -= 1 | ||||
|         if self.food_sleep <= 0: | ||||
|             self.spawn_food() | ||||
|             self.food_sleep = self.delay | ||||
| 
 | ||||
|         list = arcade.check_for_collision_with_list(self.player_body_list[0], self.food_list) | ||||
|         if list != []: | ||||
|             self.food_list.remove(list[0]) | ||||
|             self.score += 1 | ||||
|             self.spawn_food() | ||||
|             self.add_body() | ||||
| 
 | ||||
|         i = 0 | ||||
|         while i < len(self.food_list): | ||||
|             if self.snake.check_contact(self.food[i].x, self.food[i].y): | ||||
|                 self.food_list[i].kill() | ||||
|                 self.food.pop(i) | ||||
|                 self.score += 1 | ||||
|                 self.snake.add_body() | ||||
|                 self.player_list.append(arcade.Sprite()) | ||||
|             i += 1 | ||||
|         # Enemy_spawn | ||||
|         for spawn in self.Lvl_data.enemy_spawns[0]: | ||||
|             spawn["sleep"] -= 1 | ||||
| @ -279,47 +277,33 @@ class MYGAME(arcade.Window): | ||||
|      | ||||
|     # Обработка нажатий | ||||
|     MOVEMENT_SPEED = 1 | ||||
|     def check_floor(self, vector): | ||||
|         rotate = 90 * vector | ||||
|         v_x = int(math.cos(math.radians(rotate))) * 32 * self.scale_ | ||||
|         v_y = int(math.sin(math.radians(rotate))) * 32 * self.scale_ | ||||
| 
 | ||||
|         new_pos = arcade.Sprite( "./DATA/Sprites/Floor/floor_grey.png", self.scale_, self.player_body_list[0].center_x + v_x, self.player_body_list[0].center_y + v_y ) | ||||
|         posible = arcade.check_for_collision_with_list(new_pos, self.floor_list) != [] | ||||
|          | ||||
|         new_pos.kill() | ||||
|         print("1 " + str(posible)) | ||||
|         return posible  | ||||
|     def on_key_press(self, key, modifiers): | ||||
|         """Вызывается при нажатии пользователем клавиши""" | ||||
| 
 | ||||
|         # Get the first sprite (head) from the player list | ||||
|         if len(self.player_body_list) == 0: | ||||
|             return | ||||
|          | ||||
|         if key in (arcade.key.UP, arcade.key.W): | ||||
|             if  self.check_floor(1) and self.player_body_list[0].angle != 90 and arcade.check_for_collision_with_list(self.player_body_list[0], self.player_body_list ) == []:    | ||||
|                 self.player_move(1) | ||||
|         if len(self.player_list) > 0: | ||||
|              | ||||
|             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 != 270 and not(self.snake.check_contact(self.snake.body[0].x ,self.snake.body[0].y+1)):    | ||||
|                     self.snake.move(1) | ||||
|                  | ||||
|         elif key in (arcade.key.DOWN, arcade.key.S): | ||||
|             if  self.check_floor(3) and self.player_body_list[0].angle != 270 and arcade.check_for_collision_with_list(self.player_body_list[0], self.player_body_list ) == []:   | ||||
|                 self.player_move(3) | ||||
|             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 != 90 and not(self.snake.check_contact(self.snake.body[0].x ,self.snake.body[0].y-1)):    | ||||
|                     self.snake.move(3) | ||||
| 
 | ||||
|         elif key in (arcade.key.LEFT, arcade.key.A): | ||||
|             if  self.check_floor(2) and (self.player_body_list[0].angle != 360 and self.player_body_list[0].angle != 0) and arcade.check_for_collision_with_list(self.player_body_list[0], self.player_body_list ) == []:  | ||||
|                 self.player_move(2) | ||||
|             else: | ||||
|                 print("2 " + str(self.player_body_list[0].angle != 0 )) | ||||
|                 a = arcade.check_for_collision_with_list(self.player_body_list[0], self.player_body_list ) | ||||
|                 print("3 " + str(arcade.check_for_collision_with_list(self.player_body_list[0], self.player_body_list ) == []) ) | ||||
|             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): | ||||
|             if  self.check_floor(0) and self.player_body_list[0].angle != 180 and arcade.check_for_collision_with_list(self.player_body_list[0], self.player_body_list) == []:    | ||||
|                 self.player_move(0) | ||||
|             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 != 180 and not(self.snake.check_contact(self.snake.body[0].x + 1,self.snake.body[0].y)):    | ||||
|                     self.snake.move(0) | ||||
| 
 | ||||
|     def on_key_release(self, key, modifiers): | ||||
|         """Вызывается, когда пользователь отпускает клавишу""" | ||||
|         if len(self.player_body_list) > 0: | ||||
|             self.player_sprite = self.player_body_list[0] | ||||
|         if len(self.player_list) > 0: | ||||
|             self.player_sprite = self.player_list[0] | ||||
|              | ||||
|             if key in (arcade.key.UP, arcade.key.DOWN, arcade.key.W, arcade.key.S): | ||||
|                 self.player_sprite.change_y = 0 | ||||
| @ -333,7 +317,7 @@ class MYGAME(arcade.Window): | ||||
|         self.floor_list.draw() | ||||
|         self.enemies_list.draw() | ||||
|         self.food_list.draw() | ||||
|         self.player_body_list.draw() | ||||
|         self.player_list.draw() | ||||
| 
 | ||||
| def print_spawn(who, x, y): | ||||
|     print(f"Spawn: {who}\nx: {x}\ny: {y}") | ||||
|  | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user