Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions C#/forSpbu/Game/EventLoop.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
namespace Game;

public class EventLoop
{
public event EventHandler<EventArgs> LeftHandler = (_, _) => { };
public event EventHandler<EventArgs> RightHandler = (_, _) => { };
public event EventHandler<EventArgs> UpHandler = (_, _) => { };
public event EventHandler<EventArgs> DownHandler = (_, _) => { };
public event EventHandler<EventArgs> EndHandler = (_, _) => { };
public event EventHandler<EventArgs> StartHandler = (_, _) => { };

public void Run()
{
var endCondition = false;
StartHandler(this, EventArgs.Empty);
while (true)
{
if (endCondition)
{
break;
}

switch (Console.ReadKey(true).Key)
{
case ConsoleKey.LeftArrow:
LeftHandler(this, EventArgs.Empty);
break;
case ConsoleKey.RightArrow:
RightHandler(this, EventArgs.Empty);
break;
case ConsoleKey.UpArrow:
UpHandler(this, EventArgs.Empty);
break;
case ConsoleKey.DownArrow:
DownHandler(this, EventArgs.Empty);
break;
case ConsoleKey.Enter:
EndHandler(this, EventArgs.Empty);
endCondition = true;
break;
}
}
}
}
74 changes: 74 additions & 0 deletions C#/forSpbu/Game/Game.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
using System.Security.Principal;

namespace Game;

public class Game
{
private Map _map;
private int _xPosition;
private int _yPosition;
private Action<int, int, int, int> _movePlayer;

public enum Side
{
Right,
Left,
Up,
Down
}

public Game(string path, Action<int, int, int, int> movePlayer)
{
_map = new Map(path);
_xPosition = _map.PlayerMapWidth / 2;
_yPosition = _map.PlayerMapHeight / 2;
_movePlayer = movePlayer;
}

public void OnStart(object? sender, EventArgs args)
{
_map.DrawMap();
Console.SetCursorPosition(0, 0);
}

public void OnLeft(object? sender, EventArgs args)
{
if (_xPosition > 0 && _map.PlayerMap[_yPosition][_xPosition - 1])
{
_movePlayer(_xPosition, _yPosition, _xPosition - 1, _yPosition);
_xPosition--;
}
}

public void OnRight(object? sender, EventArgs args)
{
if (_xPosition + 1 < _map.PlayerMapWidth && _map.PlayerMap[_yPosition][_xPosition + 1])
{
_movePlayer(_xPosition, _yPosition, _xPosition + 1, _yPosition);
_xPosition++;
}
}

public void OnDown(object? sender, EventArgs args)
{
if (_yPosition + 1 < _map.PlayerMapHeight && _map.PlayerMap[_yPosition + 1][_xPosition])
{
_movePlayer(_xPosition, _yPosition, _xPosition, _yPosition + 1);
_yPosition++;
}
}

public void OnUp(object? sender, EventArgs args)
{
if (_yPosition > 0 && _map.PlayerMap[_yPosition - 1][_xPosition])
{
_movePlayer(_xPosition, _yPosition, _xPosition, _yPosition - 1);
_yPosition--;
}
}

public static void OnEnd(object? sender, EventArgs args)
{
Console.Clear();
}
}
10 changes: 10 additions & 0 deletions C#/forSpbu/Game/Game.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

</Project>
8 changes: 8 additions & 0 deletions C#/forSpbu/Game/IncorrectMapException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace Game;

public class IncorrectMapException : Exception
{
public IncorrectMapException(string message) : base(message)
{
}
}
23 changes: 23 additions & 0 deletions C#/forSpbu/Game/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using Game;

var eventLoop = new EventLoop();
var game = new Game.Game("../../../map.txt", movePlayer);

void movePlayer(int oldXPosition, int oldYPosition, int newXPosition, int newYPosition) {
Console.SetCursorPosition(oldXPosition + 1, oldYPosition + 1);
Console.Write(' ');
Console.SetCursorPosition(oldXPosition + 1, oldYPosition + 1);
Console.SetCursorPosition(newXPosition + 1, newYPosition + 1);
Console.Write('@');
Console.SetCursorPosition(newXPosition + 1, newYPosition + 1);
}

eventLoop.StartHandler += game.OnStart;
eventLoop.EndHandler += Game.Game.OnEnd;

eventLoop.LeftHandler += game.OnLeft;
eventLoop.RightHandler += game.OnRight;
eventLoop.DownHandler += game.OnDown;
eventLoop.UpHandler += game.OnUp;

eventLoop.Run();
11 changes: 11 additions & 0 deletions C#/forSpbu/Game/map.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
____________________
| |
| | |
| |
| |
| |
| |
| |
| |
| |
|____________________|
17 changes: 13 additions & 4 deletions C#/forSpbu/forSpbu.sln
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "03.03", "03.03", "{882A9B9C
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "10.03", "10.03", "{EA6FC7D9-BDFB-49CD-AC00-FC5DDC5274B0}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "31.03", "31.03", "{DF15DDE4-7F0B-4B93-BC7B-8815B1E966B6}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Game", "Game\Game.csproj", "{1239ED0E-09B4-44EB-96F6-4945F308E2AD}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -21,12 +25,17 @@ Global
{E007586F-9760-4744-BB25-EDEFD6BA860C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E007586F-9760-4744-BB25-EDEFD6BA860C}.Release|Any CPU.Build.0 = Release|Any CPU
{A4F6ADD5-85FD-4F67-8B29-549DDDF6F82E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A4F6ADD5-85FD-4F67-8B29-549DDDF6F82E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A4F6ADD5-85FD-4F67-8B29-549DDDF6F82E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A4F6ADD5-85FD-4F67-8B29-549DDDF6F82E}.Release|Any CPU.Build.0 = Release|Any CPU
{A4F6ADD5-85FD-4F67-8B29-549DDDF6F82E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A4F6ADD5-85FD-4F67-8B29-549DDDF6F82E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A4F6ADD5-85FD-4F67-8B29-549DDDF6F82E}.Release|Any CPU.Build.0 = Release|Any CPU
{1239ED0E-09B4-44EB-96F6-4945F308E2AD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{1239ED0E-09B4-44EB-96F6-4945F308E2AD}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1239ED0E-09B4-44EB-96F6-4945F308E2AD}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1239ED0E-09B4-44EB-96F6-4945F308E2AD}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{E007586F-9760-4744-BB25-EDEFD6BA860C} = {D3FCB669-E93F-4F0B-B9C5-6592CE93AC7F}
{E007586F-9760-4744-BB25-EDEFD6BA860C} = {D3FCB669-E93F-4F0B-B9C5-6592CE93AC7F}
{A4F6ADD5-85FD-4F67-8B29-549DDDF6F82E} = {D3FCB669-E93F-4F0B-B9C5-6592CE93AC7F}
{1239ED0E-09B4-44EB-96F6-4945F308E2AD} = {DF15DDE4-7F0B-4B93-BC7B-8815B1E966B6}
EndGlobalSection
EndGlobal