no idea speedrun

This commit is contained in:
2024-10-23 23:10:30 +02:00
parent e2c9116575
commit a2726c715c
6 changed files with 61 additions and 55 deletions
+1 -1
View File
@@ -1,6 +1,6 @@
namespace view
{
partial class Form1
partial class MainForm
{
/// <summary>
/// Required designer variable.
+41 -41
View File
@@ -3,19 +3,19 @@ using Snake.Persistance;
namespace view
{
public partial class Form1 : Form
public partial class MainForm : Form
{
#region Fields
const int line_width = 2; // Width of the table lines
SnakeLevel? CurrentLevel; // Current game state
SnakeLevelLoader level_loader; // Level loader
SnakeLevel? _current_level; // Current game state
SnakeLevelLoader _level_loader; // Level loader
#endregion
#region Constructor
public Form1()
public MainForm()
{
InitializeComponent();
level_loader = new SnakeLevelLoader(Path.Combine(Directory.GetCurrentDirectory(), "levels.json"));
_level_loader = new SnakeLevelLoader(Path.Combine(Directory.GetCurrentDirectory(), "levels.json"));
}
#endregion
@@ -25,7 +25,7 @@ namespace view
/// </summary>
private void Draw()
{
if (CurrentLevel == null) return;
if (_current_level == null) return;
using Graphics canvas_graphics = Canvas.CreateGraphics();
@@ -35,31 +35,31 @@ namespace view
graphics.Clear(Color.White);
var canvas_size = Math.Min(Canvas.Width, Canvas.Height);
double cell_size = (double)(canvas_size - (CurrentLevel.Size + 1) * line_width) / CurrentLevel.Size;
double cell_size = (double)(canvas_size - (_current_level.Size + 1) * line_width) / _current_level.Size;
// vertical lines
for (int i = 0; i <= CurrentLevel.Size; i++)
for (int i = 0; i <= _current_level.Size; i++)
{
int x = (int)(i * (cell_size + line_width));
graphics.FillRectangle(Brushes.Black, x, 0, line_width, canvas_size);
}
// horizontal lines
for (int i = 0; i <= CurrentLevel.Size; i++)
for (int i = 0; i <= _current_level.Size; i++)
{
int y = (int)(i * (cell_size + line_width));
graphics.FillRectangle(Brushes.Black, 0, y, canvas_size, line_width);
}
// Ssnake
foreach (var pos in CurrentLevel.Snake)
foreach (var pos in _current_level.Snake)
{
int x = (int)(cell_size * pos.X + line_width * (pos.X + 1));
int y = (int)(cell_size * pos.Y + line_width * (pos.Y + 1));
graphics.FillRectangle(Brushes.Green, x, y, (int)cell_size, (int)cell_size);
}
// snake head
int snake_head_x = (int)(cell_size * CurrentLevel.SnakeHead.X + line_width * (CurrentLevel.SnakeHead.X + 1));
int snake_head_y = (int)(cell_size * CurrentLevel.SnakeHead.Y + line_width * (CurrentLevel.SnakeHead.Y + 1));
int snake_head_x = (int)(cell_size * _current_level.SnakeHead.X + line_width * (_current_level.SnakeHead.X + 1));
int snake_head_y = (int)(cell_size * _current_level.SnakeHead.Y + line_width * (_current_level.SnakeHead.Y + 1));
graphics.FillRectangle(Brushes.Red, snake_head_x, snake_head_y, (int)cell_size, (int)cell_size);
switch (CurrentLevel.SnakeHeadDirection)
switch (_current_level.SnakeHeadDirection)
{
case SnakeLevel.SnakeDirection.Up:
graphics.DrawLine(Pens.Black, snake_head_x + (int)cell_size / 2, snake_head_y, snake_head_x + (int)cell_size / 2, snake_head_y + (int)cell_size);
@@ -85,7 +85,7 @@ namespace view
}
// obstacles
foreach (var pos in CurrentLevel.Obstacles)
foreach (var pos in _current_level.Obstacles)
{
int x = (int)(cell_size * pos.X + line_width * (pos.X + 1));
int y = (int)(cell_size * pos.Y + line_width * (pos.Y + 1));
@@ -93,7 +93,7 @@ namespace view
}
//eggs
foreach (var pos in CurrentLevel.Eggs)
foreach (var pos in _current_level.Eggs)
{
int x = (int)(cell_size * pos.X + line_width * (pos.X + 1));
int y = (int)(cell_size * pos.Y + line_width * (pos.Y + 1));
@@ -107,24 +107,24 @@ namespace view
#region Form event handlers
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (CurrentLevel == null) return;
if (_current_level == null) return;
switch (e.KeyCode)
{
case Keys.Up:
CurrentLevel.SnakeHeadDirection = SnakeLevel.SnakeDirection.Up;
_current_level.SnakeHeadDirection = SnakeLevel.SnakeDirection.Up;
break;
case Keys.Down:
CurrentLevel.SnakeHeadDirection = SnakeLevel.SnakeDirection.Down;
_current_level.SnakeHeadDirection = SnakeLevel.SnakeDirection.Down;
break;
case Keys.Left:
CurrentLevel.SnakeHeadDirection = SnakeLevel.SnakeDirection.Left;
_current_level.SnakeHeadDirection = SnakeLevel.SnakeDirection.Left;
break;
case Keys.Right:
CurrentLevel.SnakeHeadDirection = SnakeLevel.SnakeDirection.Right;
_current_level.SnakeHeadDirection = SnakeLevel.SnakeDirection.Right;
break;
case Keys.Escape:
if (CurrentLevel.State == SnakeLevel.GameState.Running)
CurrentLevel.Pause();
if (_current_level.State == SnakeLevel.GameState.Running)
_current_level.Pause();
break;
}
@@ -132,31 +132,31 @@ namespace view
private void btnStart_Click(object sender, EventArgs e)
{
if (CurrentLevel is null || CurrentLevel.State == SnakeLevel.GameState.Dead)
if (_current_level is null || _current_level.State == SnakeLevel.GameState.Dead)
{
var MapChooser = new MapChooser(this.level_loader);
var MapChooser = new MapChooser(this._level_loader);
MapChooser.ShowDialog();
if (MapChooser.SelectedLevel != null)
{
CurrentLevel = level_loader.LoadLevel(MapChooser.SelectedLevel);
CurrentLevel.GameStateUpdate += CurrentLevel_GameStateUpdate;
CurrentLevel.GameUpdate += CurrentLevel_GameUpdate;
_current_level = _level_loader.LoadLevel(MapChooser.SelectedLevel);
_current_level.GameStateUpdate += CurrentLevel_GameStateUpdate;
_current_level.GameUpdate += CurrentLevel_GameUpdate;
}
else
{
CurrentLevel = null;
_current_level = null;
}
}
if (CurrentLevel != null)
if (_current_level != null)
{
CurrentLevel.Start();
_current_level.Start();
this.Focus();
}
}
private void Form1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
if (CurrentLevel?.State is SnakeLevel.GameState.Running)
if (_current_level?.State is SnakeLevel.GameState.Running)
{
e.IsInputKey = true;
}
@@ -165,7 +165,7 @@ namespace view
private void Form1_Shown(object sender, EventArgs e)
{
btnEndGame.Visible = false; // It will trigger a whole repaint the first time it is shown unless this is done
level_loader.ReadAllDataAsync().ContinueWith((task) =>
_level_loader.ReadAllDataAsync().ContinueWith((task) =>
{
if (!this.IsDisposed) // The form can be closed before the file is loaded
{
@@ -188,7 +188,7 @@ namespace view
private void button1_Click(object sender, EventArgs e)
{
CurrentLevel = null;
_current_level = null;
btnEndGame.Visible = false;
Canvas.Refresh();
txtScore.Text = "";
@@ -196,13 +196,13 @@ namespace view
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (CurrentLevel != null)
if (_current_level != null)
{
if (CurrentLevel.State == SnakeLevel.GameState.Running)
if (_current_level.State == SnakeLevel.GameState.Running)
{
CurrentLevel.Pause();
_current_level.Pause();
}
CurrentLevel = null;
_current_level = null;
}
}
@@ -211,17 +211,17 @@ namespace view
#region Game event handlers
private void CurrentLevel_GameUpdate(object? sender, EventArgs e)
{
if (CurrentLevel == null) return;
if (_current_level == null) return;
this.Invoke(() =>
{
Draw();
txtScore.Text = CurrentLevel.Score.ToString();
txtScore.Text = _current_level.Score.ToString();
});
}
private void CurrentLevel_GameStateUpdate(object? sender, SnakeLevel.GameState e)
{
if (CurrentLevel == null) return;
if (_current_level == null) return;
this.Invoke(() =>
{
switch (e)
@@ -237,7 +237,7 @@ namespace view
break;
case SnakeLevel.GameState.Dead:
btnStart.Visible = true;
MessageBox.Show($"Skill issue.\nScore: {CurrentLevel.Score}", "Snake", MessageBoxButtons.OK, MessageBoxIcon.Information);
MessageBox.Show($"Skill issue.\nScore: {_current_level.Score}", "Snake", MessageBoxButtons.OK, MessageBoxIcon.Information);
break;
}
});
+1 -1
View File
@@ -11,7 +11,7 @@ namespace view
// To customize application configuration such as set high DPI settings or default font,
// see https://aka.ms/applicationconfiguration.
ApplicationConfiguration.Initialize();
Application.Run(new Form1());
Application.Run(new MainForm());
}
}
}