update: movement
This commit is contained in:
		
							parent
							
								
									81404a51da
								
							
						
					
					
						commit
						c7c5b6429b
					
				
										
											Binary file not shown.
										
									
								
							
										
											Binary file not shown.
										
									
								
							| Before Width: | Height: | Size: 371 B After Width: | Height: | Size: 342 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 | 
										
											Binary file not shown.
										
									
								
							
							
								
								
									
										105
									
								
								main.py
									
									
									
									
									
								
							
							
						
						
									
										105
									
								
								main.py
									
									
									
									
									
								
							| @ -21,7 +21,7 @@ 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.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") | ||||||
| @ -37,44 +37,79 @@ class SNAKE(): | |||||||
|         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") |         new_body = body(0,0, last_body.rotate, "tail") | ||||||
|         v_x = int(-math.sin(-last_body.rotate * math.pi / 180)) |         v_x = int(math.cos(math.radians(last_body.rotate))) | ||||||
|  |         v_y = int(math.sin(math.radians(last_body.rotate))) | ||||||
|  | 
 | ||||||
|         new_body.x = -v_x + last_body.x |         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 |         new_body.y = -v_y + last_body.y | ||||||
| 
 | 
 | ||||||
|         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 - 90)) + math.sin(math.radians(degr + 90)) | ||||||
|  |      | ||||||
|  |     def convert_rot_to_tail(self, deg0, deg2 ): | ||||||
|  |         # deg1 - угол ближайшей к голове части | ||||||
|  |         # deg0 - угол переходной части (он особый) | ||||||
|  |         deg0 = deg0 % 360 | ||||||
|  |         deg2 = deg2 % 360 | ||||||
|  |         if (deg0 == 180 and deg2 == 270) or (deg0 == 270 and deg2 == 90): | ||||||
|  |             return 180 | ||||||
|  |         if (deg0 == 90 and deg2 == 270) or (deg0 == 0 and deg2 == 90): | ||||||
|  |             return 0 | ||||||
|  |         if ((deg0 == 90) and deg2 == 180) or (deg0 == 180 and deg2 == 0): | ||||||
|  |             return 90 | ||||||
|  |         if (deg0 == 0 and deg2 == 180) or (deg0 == 270 and deg2 == 0): | ||||||
|  |             return 270 | ||||||
|  |         return 0 | ||||||
| 
 | 
 | ||||||
|     def move(self, vector): |     def move(self, vector): | ||||||
|         self.body[0].rotate = vector * 90 |         l = len(self.body)-1 | ||||||
|  |         i = l | ||||||
|          |          | ||||||
| 
 |  | ||||||
|         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: |         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 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].type = self.body[i-1].type  | ||||||
|              |              | ||||||
|             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)): |             # elif (self.body[i].type == "tail") and self.body[i-1].type == "body-rotate": | ||||||
|                 self.body[i].type = "body" |             #     self.body[l].rotate = self.convert_rot_to_tail(self.body[l-1].rotate, self.body[l].rotate) | ||||||
|                 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 |             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 |  | ||||||
| 
 | 
 | ||||||
|  |                   | ||||||
|  |         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[l].type = "tail" | ||||||
|  |         # self.body[l].rotate = lf.body[l-1].type == "body-rotate" else self.body[l-1].rotate | ||||||
|         # print(f"Move {vector}") |         # print(f"Move {vector}") | ||||||
|          |          | ||||||
|  | 
 | ||||||
| class Level_data(): | class Level_data(): | ||||||
|     def __init__(self, level_data_json = []): |     def __init__(self, level_data_json = []): | ||||||
|         if level_data_json: |         if level_data_json: | ||||||
| @ -120,8 +155,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 | ||||||
| @ -133,7 +168,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) | ||||||
| @ -171,7 +206,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 | ||||||
| @ -230,20 +265,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): | ||||||
|         """Вызывается, когда пользователь отпускает клавишу""" |         """Вызывается, когда пользователь отпускает клавишу""" | ||||||
|  | |||||||
		Loading…
	
		Reference in New Issue
	
	Block a user