87 lines
No EOL
2.7 KiB
C#
87 lines
No EOL
2.7 KiB
C#
|
|
|
|
using System;
|
|
using System.ComponentModel;
|
|
using System.Drawing;
|
|
using System.Windows.Media;
|
|
using System.Windows.Media.Imaging;
|
|
using System.Windows.Threading;
|
|
using Tetris.Models;
|
|
using Color = System.Drawing.Color;
|
|
|
|
namespace Tetris.ViewsModels;
|
|
|
|
public class GameViewModel : INotifyPropertyChanged
|
|
{
|
|
public event PropertyChangedEventHandler? PropertyChanged;
|
|
|
|
private const int RendererHertz = 5;
|
|
private const int GameRendererHertz = (1 / RendererHertz) * 1000;
|
|
private readonly int _width = 480, _height = 640;
|
|
private readonly WriteableBitmap _writeableBitmap;
|
|
|
|
private const int GridWidth = 10;
|
|
private const int GridHeight = 20;
|
|
private static readonly Grid Grid = new(new Color[GridWidth, GridHeight]);
|
|
private readonly Tetrominoe _currentTetrominoe = new(Grid, "J", new Point(10, 10), 0, Color.Aqua);
|
|
|
|
public GameViewModel()
|
|
{
|
|
_writeableBitmap = BitmapFactory.New(_width, _height);
|
|
|
|
var dispatcherRenderTimer = new DispatcherTimer
|
|
{
|
|
Interval = new TimeSpan(0, 0, 0, 0, GameRendererHertz)
|
|
};
|
|
|
|
dispatcherRenderTimer.Tick += Render;
|
|
dispatcherRenderTimer.Start();
|
|
|
|
var dispatcherUpdateTimer = new DispatcherTimer
|
|
{
|
|
Interval = new TimeSpan(0, 0, 0, 0, 25)
|
|
};
|
|
|
|
dispatcherUpdateTimer.Tick += Update;
|
|
dispatcherUpdateTimer.Start();
|
|
}
|
|
|
|
public int Width => _width;
|
|
public int Height => _height;
|
|
public ImageSource Source => _writeableBitmap;
|
|
|
|
private void Render(object? sender, EventArgs eventArgs)
|
|
{
|
|
_writeableBitmap.Lock();
|
|
_writeableBitmap.Clear(Colors.Black);
|
|
|
|
for (var x = 0; x < GridWidth; x++)
|
|
{
|
|
for (var y = 0; y < GridHeight; y++)
|
|
{
|
|
var color = Grid.CGrid[x, y];
|
|
_writeableBitmap.SetPixel(x, y, color.R, color.G, color.B);
|
|
}
|
|
}
|
|
|
|
var tetrominoeWidth = _currentTetrominoe.Shape.GetLength(0);
|
|
var tetrominoeHeight = _currentTetrominoe.Shape.GetLength(1);
|
|
|
|
for (int x = 0; x < tetrominoeWidth; x++)
|
|
{
|
|
for (int y = 0; y < tetrominoeHeight; y++)
|
|
{
|
|
if (!_currentTetrominoe.Shape[x, y]) continue;
|
|
var color = _currentTetrominoe.Color;
|
|
_writeableBitmap.FillRectangle(_currentTetrominoe.Coordinates.X * 10, _currentTetrominoe.Coordinates.Y * 10, _currentTetrominoe.Coordinates.X * 10 + 10, _currentTetrominoe.Coordinates.Y * 10 + 10, color.ToArgb());
|
|
}
|
|
}
|
|
|
|
_writeableBitmap.Unlock();
|
|
}
|
|
|
|
private void Update(object? sender, EventArgs eventArgs)
|
|
{
|
|
_currentTetrominoe.GoDown();
|
|
}
|
|
} |