Archived
1
0
Fork 0
This repository has been archived on 2024-02-17. You can view files and clone it, but cannot push or open issues or pull requests.
Tetris/Views/GameWindow.xaml.cs

49 lines
1.2 KiB
C#
Raw Permalink Normal View History

2022-05-04 10:40:15 +02:00
using System;
using System.Runtime.InteropServices;
2022-04-06 11:15:55 +02:00
using System.Windows;
2022-05-04 10:40:15 +02:00
using System.Windows.Input;
2022-04-06 11:30:44 +02:00
using Tetris.ViewsModels;
2022-04-06 11:15:55 +02:00
namespace Tetris.Views;
public partial class GameWindow : Window
{
2022-05-04 11:08:04 +02:00
private static readonly GameViewModel GameViewModel = new();
2022-05-04 10:40:15 +02:00
2022-04-06 11:15:55 +02:00
public GameWindow()
{
AttachConsole(-1);
InitializeComponent();
2022-05-04 10:40:15 +02:00
DataContext = GameViewModel;
2022-04-06 11:15:55 +02:00
}
[DllImport("kernel32.dll")]
private static extern bool AttachConsole(int dwProcessId);
2022-05-04 10:40:15 +02:00
private void UIElement_OnKeyDown(object sender, KeyEventArgs e)
{
// If key is space
if (e.Key == Key.Space)
{
2022-05-04 11:08:04 +02:00
GameViewModel.Game.CurrentTetrominoe?.RotateRight();
2022-05-04 10:40:15 +02:00
}
// If key is down
else if (e.Key == Key.Down)
{
2022-05-04 11:08:04 +02:00
GameViewModel.Game.CurrentTetrominoe?.GoDown();
2022-05-04 10:40:15 +02:00
}
// If key is left
else if (e.Key == Key.Left)
{
2022-05-04 11:08:04 +02:00
GameViewModel.Game.CurrentTetrominoe?.GoLeft();
2022-05-04 10:40:15 +02:00
}
// If key is right
else if (e.Key == Key.Right)
{
2022-05-04 11:08:04 +02:00
GameViewModel.Game.CurrentTetrominoe?.GoRight();
2022-05-04 10:40:15 +02:00
}
}
2022-04-06 11:15:55 +02:00
}