Last active
January 11, 2025 22:52
-
-
Save MadStool/227faa6c72ac498905b1771273ffb251 to your computer and use it in GitHub Desktop.
аквариум про креветку в пивной кружке
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Threading; | |
namespace AquariumSimulation | |
{ | |
internal class Program | |
{ | |
static void Main(string[] args) | |
{ | |
Console.CursorVisible = false; | |
Aquarium aquarium = new Aquarium(); | |
aquarium.Draw(); | |
while (true) | |
{ | |
aquarium.Update(); | |
Thread.Sleep(500); | |
} | |
} | |
} | |
class Aquarium | |
{ | |
private char[,] _layout; | |
private Random _random; | |
private Shrimp _shrimp; | |
private Grass _grass; | |
private List<Bubble> _bubbles; | |
private Food _food; | |
public Aquarium() | |
{ | |
_random = new Random(); | |
_layout = InitializeLayout(); | |
_shrimp = new Shrimp(_random.Next(1, _layout.GetLength(1) - 1), _random.Next(1, _layout.GetLength(0) - 1)); | |
_bubbles = new List<Bubble>(); | |
_grass = new Grass(); | |
_food = null; | |
} | |
public void Draw() | |
{ | |
for (int y = 0; y < _layout.GetLength(0); y++) | |
{ | |
for (int x = 0; x < _layout.GetLength(1); x++) | |
{ | |
Console.SetCursorPosition(x, y); | |
Console.Write(_layout[y, x]); | |
} | |
} | |
_grass.DrawGrass(); | |
DrawBubbles(); | |
DrawItem(_shrimp.ShrimpChar, _shrimp.X, _shrimp.Y, _shrimp.Color); | |
if (_food != null) | |
DrawItem(_food.FoodChar, _food.X, _food.Y, ConsoleColor.Yellow); | |
} | |
public void Update() | |
{ | |
if (_random.Next(10) < 2) | |
_bubbles.Add(new Bubble(_layout.GetLength(0) - 2, _random.Next(1, _layout.GetLength(1) - 1))); | |
for (int i = _bubbles.Count - 1; i >= 0; i--) | |
{ | |
var bubble = _bubbles[i]; | |
bubble.Move(); | |
if (bubble.IsOffScreen()) | |
_bubbles.RemoveAt(i); | |
} | |
int oldShrimpX = _shrimp.X; | |
int oldShrimpY = _shrimp.Y; | |
if (_food != null) | |
_shrimp.MoveTowardsFood(_food); | |
else | |
_shrimp.MoveRandomly(_random); | |
_grass.Update(); | |
if (Console.KeyAvailable && Console.ReadKey(true).Key == ConsoleKey.D1) | |
FeedShrimp(); | |
if (oldShrimpX != _shrimp.X || oldShrimpY != _shrimp.Y) | |
{ | |
DrawItem(' ', oldShrimpX, oldShrimpY, ConsoleColor.Black); | |
DrawItem(_shrimp.ShrimpChar, _shrimp.X, _shrimp.Y, _shrimp.Color); | |
} | |
if (_food != null) | |
{ | |
_food.Move(); | |
if (_food.IsOnGround()) | |
{ | |
_shrimp.Eat(); | |
_food = null; | |
} | |
} | |
Draw(); | |
} | |
private void DrawBubbles() | |
{ | |
foreach (var bubble in _bubbles) | |
DrawItem(bubble.BubbleChar, bubble.X, bubble.Y, ConsoleColor.Cyan); | |
} | |
private void DrawItem(char itemChar, int x, int y, ConsoleColor color) | |
{ | |
Console.SetCursorPosition(x, y); | |
Console.ForegroundColor = color; | |
Console.Write(itemChar); | |
Console.ResetColor(); | |
} | |
private char[,] InitializeLayout() | |
{ | |
return new char[,] { | |
{'-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-'}, | |
{'|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|'}, | |
{'|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|'}, | |
{'|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|'}, | |
{'|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|'}, | |
{'|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|'}, | |
{'|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|'}, | |
{'|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|'}, | |
{'|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|'}, | |
{'|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|'}, | |
{'-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-'}}; | |
} | |
private void FeedShrimp() | |
{ | |
if (_food == null) | |
_food = new Food(5, 2); | |
} | |
} | |
class Shrimp | |
{ | |
private int _x; | |
private int _y; | |
private const char _shrimpChar = '9'; | |
public ConsoleColor Color { get; private set; } | |
public Shrimp(int startX, int startY) | |
{ | |
_x = startX; | |
_y = startY; | |
Color = ConsoleColor.Magenta; | |
} | |
public int X => _x; | |
public int Y => _y; | |
public char ShrimpChar => _shrimpChar; | |
public void MoveRandomly(Random random) | |
{ | |
Console.SetCursorPosition(_x, _y); | |
Console.Write(' '); | |
int direction = random.Next(4); | |
switch (direction) | |
{ | |
case 0: if (_y > 1) _y--; break; | |
case 1: if (_y < 8) _y++; break; | |
case 2: if (_x > 1) _x--; break; | |
case 3: if (_x < 12) _x++; break; | |
} | |
} | |
public void MoveTowardsFood(Food food) | |
{ | |
Console.SetCursorPosition(_x, _y); | |
Console.Write(' '); | |
if (_x < food.X) | |
_x++; | |
else if (_x > food.X) | |
_x--; | |
if (_y < food.Y) | |
_y++; | |
else if (_y > food.Y) | |
_y--; | |
} | |
public void Eat() => Color = ConsoleColor.Yellow; | |
} | |
class Grass | |
{ | |
private const char _grass1 = '('; | |
private const char _grass2 = ')'; | |
private int _row; | |
private int[] _columns = { 2, 6, 10 }; | |
private bool[] _movingUp; | |
public Grass() | |
{ | |
_row = 8; | |
_movingUp = new bool[_columns.Length]; | |
} | |
public void Update() | |
{ | |
for (int i = 0; i < _columns.Length; i++) | |
{ | |
Console.SetCursorPosition(_columns[i], _row); | |
Console.Write(" "); | |
_movingUp[i] = !_movingUp[i]; | |
} | |
} | |
public void DrawGrass() | |
{ | |
Console.ForegroundColor = ConsoleColor.Green; | |
for (int i = 0; i < _columns.Length; i++) | |
{ | |
Console.SetCursorPosition(_columns[i], _row); | |
Console.Write(_movingUp[i] ? _grass1 : _grass2); | |
Console.SetCursorPosition(_columns[i], _row + 1); | |
Console.Write(_movingUp[i] ? _grass2 : _grass1); | |
} | |
Console.ResetColor(); | |
} | |
} | |
class Bubble | |
{ | |
private int _y; | |
private int _x; | |
private const char _bubbleChar = 'o'; | |
public Bubble(int startY, int startX) | |
{ | |
_y = startY; | |
_x = startX; | |
} | |
public int X => _x; | |
public int Y => _y; | |
public char BubbleChar => _bubbleChar; | |
public void Move() | |
{ | |
Console.SetCursorPosition(_x, _y); | |
Console.Write(' '); | |
if (_y > 1) | |
_y--; | |
} | |
public bool IsOffScreen() => _y <= 1; | |
} | |
class Food | |
{ | |
private int _x; | |
private int _y; | |
private const char _foodChar = '*'; | |
public Food(int startX, int startY) | |
{ | |
_x = startX; | |
_y = startY; | |
} | |
public int X => _x; | |
public int Y => _y; | |
public char FoodChar => _foodChar; | |
public void Move() | |
{ | |
if (_y < 9) | |
{ | |
Console.SetCursorPosition(_x, _y); | |
Console.Write(' '); | |
_y++; | |
} | |
} | |
public bool IsOnGround() => _y >= 9; | |
} | |
} |
можно кормить, и она бегает за едой становясь жёлто
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
пока что это хуйня