Загрузка данных
import tkinter as tk
import random
CELL_SIZE = 42
GRID_COLS = 15
GRID_ROWS = 12
START_OXYGEN = 100
START_BATTERY = 100
RESOURCE_SCORES = {
"crystal": 10,
"pearl": 20,
"gold": 35
}
class World:
def __init__(self, cols, rows):
self.cols = cols
self.rows = rows
self.grid = [["empty"] * cols for _ in range(rows)]
def generate(self):
for _ in range(10):
x = random.randint(0, self.cols - 1)
y = random.randint(0, self.rows - 1)
self.grid[y][x] = random.choice([
"crystal",
"pearl",
"gold"
])
def get_cell(self, x, y):
return self.grid[y][x]
def set_cell(self, x, y, value):
self.grid[y][x] = value
class Bathyscaphe:
def __init__(self):
self.x = 1
self.y = 1
self.oxygen = START_OXYGEN
self.battery = START_BATTERY
self.inventory = {}
def move(self, dx, dy):
self.x += dx
self.y += dy
self.oxygen -= 2
self.battery -= 1
class BathyscapheApp:
def __init__(self, root):
self.root = root
self.root.title("Батискаф — сбор ресурсов")
self.score = 0
self.world = World(GRID_COLS, GRID_ROWS)
self.world.generate()
self.player = Bathyscaphe()
self.canvas = tk.Canvas(
root,
width=GRID_COLS * CELL_SIZE,
height=GRID_ROWS * CELL_SIZE,
bg="#12335a"
)
self.canvas.pack()
self.info = tk.Label(root, text="")
self.info.pack()
self.draw_world()
root.bind("<Up>", lambda e: self.move(0, -1))
root.bind("<Down>", lambda e: self.move(0, 1))
root.bind("<Left>", lambda e: self.move(-1, 0))
root.bind("<Right>", lambda e: self.move(1, 0))
def draw_world(self):
self.canvas.delete("all")
for y in range(GRID_ROWS):
for x in range(GRID_COLS):
px = x * CELL_SIZE
py = y * CELL_SIZE
self.canvas.create_rectangle(
px,
py,
px + CELL_SIZE,
py + CELL_SIZE,
fill="#12335a",
outline="#1e3a5f"
)
cell = self.world.get_cell(x, y)
if cell == "crystal":
self.canvas.create_oval(
px + 10,
py + 10,
px + 30,
py + 30,
fill="cyan"
)
elif cell == "pearl":
self.canvas.create_oval(
px + 10,
py + 10,
px + 30,
py + 30,
fill="white"
)
elif cell == "gold":
self.canvas.create_rectangle(
px + 10,
py + 10,
px + 30,
py + 30,
fill="gold"
)
self.draw_player()
def draw_player(self):
px = self.player.x * CELL_SIZE
py = self.player.y * CELL_SIZE
self.canvas.create_oval(
px + 5,
py + 5,
px + 37,
py + 37,
fill="orange"
)
def move(self, dx, dy):
new_x = self.player.x + dx
new_y = self.player.y + dy
if 0 <= new_x < GRID_COLS and 0 <= new_y < GRID_ROWS:
self.player.move(dx, dy)
cell = self.world.get_cell(new_x, new_y)
if cell in RESOURCE_SCORES:
self.score += RESOURCE_SCORES[cell]
self.player.inventory[cell] = (
self.player.inventory.get(cell, 0) + 1
)
self.world.set_cell(new_x, new_y, "empty")
self.info.config(
text=f"Очки: {self.score} | "
f"Кислород: {self.player.oxygen} | "
f"Батарея: {self.player.battery}"
)
self.draw_world()
def main():
root = tk.Tk()
app = BathyscapheApp(root)
root.mainloop()
if __name__ == "__main__":
main()