Archived
1
0
Fork 0

Fix and update

This commit is contained in:
Ziedelth 2022-05-04 10:40:15 +02:00
parent 1c80d4c05a
commit 562589fc72
3 changed files with 46 additions and 14 deletions

View file

@ -4,8 +4,8 @@
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d" mc:Ignorable="d"
Title="GameWindow" Width="{Binding Width}" Height="{Binding Height}"> Title="GameWindow" Width="{Binding Width}" Height="{Binding Height}" KeyDown="UIElement_OnKeyDown">
<Grid> <Grid Background="Black">
<Image Source="{Binding Source}" /> <Image Source="{Binding Source}" />
</Grid> </Grid>
</Window> </Window>

View file

@ -1,18 +1,49 @@
using System.Runtime.InteropServices; using System;
using System.Runtime.InteropServices;
using System.Windows; using System.Windows;
using System.Windows.Input;
using Tetris.ViewsModels; using Tetris.ViewsModels;
namespace Tetris.Views; namespace Tetris.Views;
public partial class GameWindow : Window public partial class GameWindow : Window
{ {
private static readonly GameViewModel GameViewModel = new GameViewModel();
public GameWindow() public GameWindow()
{ {
AttachConsole(-1); AttachConsole(-1);
InitializeComponent(); InitializeComponent();
DataContext = new GameViewModel(); DataContext = GameViewModel;
} }
[DllImport("kernel32.dll")] [DllImport("kernel32.dll")]
private static extern bool AttachConsole(int dwProcessId); private static extern bool AttachConsole(int dwProcessId);
private void UIElement_OnKeyDown(object sender, KeyEventArgs e)
{
// If key is space
if (e.Key == Key.Space)
{
GameViewModel.CurrentTetrominoe.RotateRight();
}
// If key is down
else if (e.Key == Key.Down)
{
GameViewModel.CurrentTetrominoe.GoDown();
}
// If key is left
else if (e.Key == Key.Left)
{
GameViewModel.CurrentTetrominoe.GoLeft();
}
// If key is right
else if (e.Key == Key.Right)
{
GameViewModel.CurrentTetrominoe.GoRight();
}
}
} }

View file

@ -3,6 +3,7 @@
using System; using System;
using System.ComponentModel; using System.ComponentModel;
using System.Drawing; using System.Drawing;
using System.Windows.Input;
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;
@ -17,17 +18,18 @@ public class GameViewModel : INotifyPropertyChanged
private const int RendererHertz = 5; private const int RendererHertz = 5;
private const int GameRendererHertz = (1 / RendererHertz) * 1000; private const int GameRendererHertz = (1 / RendererHertz) * 1000;
private readonly int _width = 480, _height = 640; private const int Width = 25;
private const int Height = 50;
private readonly WriteableBitmap _writeableBitmap; private readonly WriteableBitmap _writeableBitmap;
private const int GridWidth = 10; private const int GridWidth = 25;
private const int GridHeight = 20; private const int GridHeight = 50;
private static readonly Grid Grid = new(new Color[GridWidth, GridHeight]); private static readonly Grid Grid = new(new Color[GridWidth, GridHeight]);
private readonly Tetrominoe _currentTetrominoe = new(Grid, "J", new Point(10, 10), 0, Color.Aqua); private readonly Tetrominoe _currentTetrominoe = new(Grid, "J", new Point(25 / 2, 0), 0, Color.Aqua);
public GameViewModel() public GameViewModel()
{ {
_writeableBitmap = BitmapFactory.New(_width, _height); _writeableBitmap = BitmapFactory.New(Width, Height);
var dispatcherRenderTimer = new DispatcherTimer var dispatcherRenderTimer = new DispatcherTimer
{ {
@ -39,16 +41,15 @@ public class GameViewModel : INotifyPropertyChanged
var dispatcherUpdateTimer = new DispatcherTimer var dispatcherUpdateTimer = new DispatcherTimer
{ {
Interval = new TimeSpan(0, 0, 0, 0, 25) Interval = new TimeSpan(0, 0, 0, 0, 100)
}; };
dispatcherUpdateTimer.Tick += Update; dispatcherUpdateTimer.Tick += Update;
dispatcherUpdateTimer.Start(); dispatcherUpdateTimer.Start();
} }
public int Width => _width;
public int Height => _height;
public ImageSource Source => _writeableBitmap; public ImageSource Source => _writeableBitmap;
public Tetrominoe CurrentTetrominoe => _currentTetrominoe;
private void Render(object? sender, EventArgs eventArgs) private void Render(object? sender, EventArgs eventArgs)
{ {
@ -73,7 +74,7 @@ public class GameViewModel : INotifyPropertyChanged
{ {
if (!_currentTetrominoe.Shape[x, y]) continue; if (!_currentTetrominoe.Shape[x, y]) continue;
var color = _currentTetrominoe.Color; 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.SetPixel(_currentTetrominoe.Coordinates.X + x, _currentTetrominoe.Coordinates.Y + y, color.R, color.G, color.B);
} }
} }