add: Food

This commit is contained in:
Павел Терехов 2025-05-06 14:10:14 +03:00
parent bbc92b1ca9
commit ec9b9c90ef
4 changed files with 93 additions and 24 deletions

File diff suppressed because one or more lines are too long

View File

@ -32,12 +32,13 @@ class filler():
} }
], ],
], ],
"enemy_spawns": [ "enemy_spawns": [[
{ "x" : 1, "y" : 2 }, { "x" : 1, "y" : 3, "tier": 1, "sleep": 600, "delay": 600},
{ "x" : 3, "y" : 12 }, { "x" : 12, "y" : 7,"tier": 1, "sleep": 9000, "delay": 9000 },
{ "x" : 1, "y" : 6 }, { "x" : 1, "y" : 4, "tier": 1,"sleep": 800, "delay": 800 },
{ "x" : 10, "y" : 10 } { "x" : 10, "y" : 0,"tier": 1, "sleep": 600, "delay": 600 }
], ]],
"size" : [], "size" : [],
"floor": [] "floor": []
} }

102
main.py
View File

@ -1,11 +1,18 @@
import arcade import arcade
import json import json
import math import math
import random
from fill_level import filler from fill_level import filler
SCREEN_WIDTH = 800 SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600 SCREEN_HEIGHT = 600
class food():
def __init__(self, x, y, healf = 1):
self.x = x
self.y = y
self.healf = healf
class body(): class body():
def __init__(self, x, y, rotate, type): def __init__(self, x, y, rotate, type):
self.x = x self.x = x
@ -19,16 +26,32 @@ class SNAKE():
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")
] ]
def check_contact(self,x,y):
cont = 0
for body in self.body:
if body.x == x and body.y == y:
cont = 1
return cont
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): def move(self, vector):
self.body[0].rotate = vector * 90 self.body[0].rotate = vector * 90
i = 0 i = len(self.body)-1
while i < len(self.body): while i > 0:
if self.body[i].type == "body" or self.body[i].type == "tail": if self.body[i].type == "body" or self.body[i].type == "tail":
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 i = i - 1
a = int(math.cos(-self.body[0].rotate * math.pi / 180))
self.body[0].x = int(-math.sin(-self.body[0].rotate * math.pi / 180)) + self.body[0].x 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].y = int(math.cos(-self.body[0].rotate * math.pi / 180)) + self.body[0].y
@ -68,15 +91,20 @@ class MYGAME(arcade.Window):
self.player_list = arcade.SpriteList() self.player_list = arcade.SpriteList()
self.floor_list = arcade.SpriteList() self.floor_list = arcade.SpriteList()
self.eat_list = arcade.SpriteList()
self.enemies_list = arcade.SpriteList() self.enemies_list = arcade.SpriteList()
self.enemy_spanw = arcade.SpriteList() self.enemy_spawn = arcade.SpriteList()
grid = fill_empty_grid() grid = fill_empty_grid()
self.floor_grid = self.arr2grid(self.Lvl_data.floor, grid) self.floor_grid = self.arr2grid(self.Lvl_data.floor, grid)
self.print_grid(self.floor_grid) self.print_grid(self.floor_grid)
self.snake = SNAKE(self.Lvl_data.start_point["x"], self.Lvl_data.start_point["y"]) self.snake = SNAKE(self.Lvl_data.start_point["x"], self.Lvl_data.start_point["y"])
# food
self.food_list = arcade.SpriteList()
self.food_sleep = 300
self.delay = 400
self.food = []
# Счет # Счет
self.score = 0 self.score = 0
self.scale_ = 1.3 self.scale_ = 1.3
@ -103,9 +131,22 @@ class MYGAME(arcade.Window):
# if "enemies" in self.level_data: # if "enemies" in self.level_data:
# for enemy in self.level_data["enemies"]: # for enemy in self.level_data["enemies"]:
# enemy_sprite = arcade.Sprite(enemy["sprite"], 1) # enemy_sprite = arcade.Sprite(enemy["sprite"], 1)
# enemy_sprite.center_x = enemy["x"] * 32 # enemy_sprite.center_x = self.level_start[0] - 16 + ( enemy["x"] + 1 ) * 32 * self.scale_
# enemy_sprite.center_y = enemy["y"] * 32 # enemy_sprite.center_y = self.level_start[0] - 16 + ( enemy["y"] + 1 ) * 32 * self.scale_
# self.enemies_list.append(enemy_sprite) # self.enemies_list.append(enemy_sprite)
def spawn_food(self):
spawn = False
while spawn == False:
x = random.randint(0, 14)
y = random.randint(0, 14)
if self.floor_grid[y][x] == 1 and not(self.snake.check_contact(x,y)):
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 + 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): def update(self):
if len(self.snake.body) > 0: if len(self.snake.body) > 0:
@ -116,12 +157,37 @@ class MYGAME(arcade.Window):
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
# Food
self.food_sleep -= 1
if self.food_sleep <= 0:
self.spawn_food()
self.food_sleep = self.delay
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_dody()
self.player_list.append(arcade.Sprite())
i += 1
# Enemy_spawn
for spawn in self.Lvl_data.enemy_spawns[0]:
spawn["sleep"] -= 1
if spawn["sleep"] <= 0:
spawn["sleep"] = spawn["delay"]
self.spawn_enemy(spawn["y"], spawn["x"], spawn["tier"])
def spawn_enemy(self, y, x, tier):
type = random.randint(0, len(self.Lvl_data.enemy_types[tier-1]))
print_spawn("tier: " + str(type), x, y)
def print_grid(self, grid): def print_grid(self, grid):
x = 0 x = 0
y = 15 y = 14
print_ = '' print_ = ''
while y > 0: while y >= 0:
while x < 15: while x < 15:
print_ += " " if grid[y][x] == 0 else "1 " print_ += " " if grid[y][x] == 0 else "1 "
x += 1 x += 1
@ -144,21 +210,21 @@ class MYGAME(arcade.Window):
# Get the first sprite (head) from the player list # Get the first sprite (head) from the player list
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: 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)):
self.snake.move(0) self.snake.move(0)
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: 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)):
self.snake.move(2) self.snake.move(2)
elif key in (arcade.key.LEFT, arcade.key.A): 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: 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.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: 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)):
self.snake.move(1) self.snake.move(1)
def on_key_release(self, key, modifiers): def on_key_release(self, key, modifiers):
@ -177,9 +243,11 @@ class MYGAME(arcade.Window):
# Отрисовываем все спрайты # Отрисовываем все спрайты
self.floor_list.draw() self.floor_list.draw()
self.enemies_list.draw() self.enemies_list.draw()
self.eat_list.draw() self.food_list.draw()
self.player_list.draw() self.player_list.draw()
def print_spawn(who, x, y):
print(f"Spawn: {who}\nx: {x}\ny: {y}")
def fill_empty_grid(): def fill_empty_grid():
grid = [] grid = []