Archived
1
0
Fork 0
This commit is contained in:
Ziedelth 2022-04-07 17:24:00 +02:00
parent 89ea691229
commit 1c80d4c05a
2 changed files with 36 additions and 3 deletions

View file

@ -8,6 +8,8 @@ namespace Tetris.Models;
public class Grid { public class Grid {
private Color[,] _grid; private Color[,] _grid;
public Color[,] CGrid => (Color[,]) _grid.Clone();
public Point MinGrid => new Point(0,0); public Point MinGrid => new Point(0,0);
public Point MaxGrid => new Point(_grid.GetLength(0)-1, _grid.GetLength(1)-1); public Point MaxGrid => new Point(_grid.GetLength(0)-1, _grid.GetLength(1)-1);

View file

@ -1,8 +1,13 @@
using System; 
using System;
using System.ComponentModel; using System.ComponentModel;
using System.Drawing;
using System.Windows.Media; using System.Windows.Media;
using System.Windows.Media.Imaging; using System.Windows.Media.Imaging;
using System.Windows.Threading; using System.Windows.Threading;
using Tetris.Models;
using Color = System.Drawing.Color;
namespace Tetris.ViewsModels; namespace Tetris.ViewsModels;
@ -15,10 +20,14 @@ public class GameViewModel : INotifyPropertyChanged
private readonly int _width = 480, _height = 640; private readonly int _width = 480, _height = 640;
private readonly WriteableBitmap _writeableBitmap; 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() public GameViewModel()
{ {
_writeableBitmap = BitmapFactory.New(_width, _height); _writeableBitmap = BitmapFactory.New(_width, _height);
// image.Source = _writeableBitmap;
var dispatcherRenderTimer = new DispatcherTimer var dispatcherRenderTimer = new DispatcherTimer
{ {
@ -46,11 +55,33 @@ public class GameViewModel : INotifyPropertyChanged
_writeableBitmap.Lock(); _writeableBitmap.Lock();
_writeableBitmap.Clear(Colors.Black); _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(); _writeableBitmap.Unlock();
} }
private void Update(object? sender, EventArgs eventArgs) private void Update(object? sender, EventArgs eventArgs)
{ {
_currentTetrominoe.GoDown();
} }
} }