Merge remote-tracking branch 'origin/game-system'
# Conflicts: # Models/Grid.cs # Models/Tetrominoe.cs # Tetris.csproj
This commit is contained in:
commit
c0d999e5f2
13 changed files with 157 additions and 64 deletions
3
App.xaml
3
App.xaml
|
@ -1,9 +1,8 @@
|
|||
<Application x:Class="Tetris.App"
|
||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:local="clr-namespace:Tetris"
|
||||
StartupUri="Views/MainWindow.xaml">
|
||||
<Application.Resources>
|
||||
|
||||
</Application.Resources>
|
||||
</Application>
|
||||
</Application>
|
23
App.xaml.cs
23
App.xaml.cs
|
@ -1,15 +1,10 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Configuration;
|
||||
using System.Data;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows;
|
||||
using System.Windows;
|
||||
|
||||
namespace Tetris {
|
||||
/// <summary>
|
||||
/// Interaction logic for App.xaml
|
||||
/// </summary>
|
||||
public partial class App : Application {
|
||||
}
|
||||
}
|
||||
namespace Tetris;
|
||||
|
||||
/// <summary>
|
||||
/// Interaction logic for App.xaml
|
||||
/// </summary>
|
||||
public partial class App : Application
|
||||
{
|
||||
}
|
|
@ -7,4 +7,4 @@ using System.Windows;
|
|||
ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located
|
||||
//(used if a resource is not found in the page,
|
||||
// app, or any theme specific resource dictionaries)
|
||||
)]
|
||||
)]
|
|
@ -1,37 +1,43 @@
|
|||
using System;
|
||||
using System.ComponentModel;
|
||||
using System.ComponentModel;
|
||||
|
||||
namespace Tetris.Models;
|
||||
|
||||
public class Party : INotifyPropertyChanged {
|
||||
public event PropertyChangedEventHandler PropertyChanged;
|
||||
public class Party : INotifyPropertyChanged
|
||||
{
|
||||
private int _score;
|
||||
|
||||
private String _userName;
|
||||
private string _userName;
|
||||
|
||||
public String UserName {
|
||||
public Party(string userName)
|
||||
{
|
||||
_userName = userName;
|
||||
}
|
||||
|
||||
public string UserName
|
||||
{
|
||||
get => _userName;
|
||||
set {
|
||||
set
|
||||
{
|
||||
_userName = value;
|
||||
OnPropertyChanged("UserName");
|
||||
}
|
||||
}
|
||||
|
||||
private int _score = 0;
|
||||
|
||||
public int Score {
|
||||
public int Score
|
||||
{
|
||||
get => _score;
|
||||
set {
|
||||
set
|
||||
{
|
||||
_score = value;
|
||||
OnPropertyChanged("Score");
|
||||
}
|
||||
}
|
||||
|
||||
public Party(String userName) {
|
||||
_userName = userName;
|
||||
}
|
||||
public event PropertyChangedEventHandler PropertyChanged;
|
||||
|
||||
protected virtual void OnPropertyChanged(string propertyName) {
|
||||
protected virtual void OnPropertyChanged(string propertyName)
|
||||
{
|
||||
if (PropertyChanged != null)
|
||||
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
|
||||
}
|
||||
}
|
||||
}
|
BIN
Resources/icon.ico
Normal file
BIN
Resources/icon.ico
Normal file
Binary file not shown.
After Width: | Height: | Size: 17 KiB |
BIN
Resources/tetris.png
Normal file
BIN
Resources/tetris.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 48 KiB |
|
@ -5,10 +5,19 @@
|
|||
<Nullable>enable</Nullable>
|
||||
<UseWPF>true</UseWPF>
|
||||
<TargetFramework>net6.0-windows</TargetFramework>
|
||||
<ApplicationIcon>Resources\icon.ico</ApplicationIcon>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Pastel" Version="3.0.0" />
|
||||
<None Remove="Resources\tetris.png" />
|
||||
<Resource Include="Resources\tetris.png">
|
||||
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||
</Resource>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Pastel" Version="3.0.0" />
|
||||
<PackageReference Include="WriteableBitmapEx" Version="1.6.8" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
|
11
Views/GameWindow.xaml
Normal file
11
Views/GameWindow.xaml
Normal file
|
@ -0,0 +1,11 @@
|
|||
<Window x:Class="Tetris.Views.GameWindow"
|
||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
||||
mc:Ignorable="d"
|
||||
Title="GameWindow" Height="450" Width="800">
|
||||
<Grid>
|
||||
<Image x:Name="ImageControl" />
|
||||
</Grid>
|
||||
</Window>
|
18
Views/GameWindow.xaml.cs
Normal file
18
Views/GameWindow.xaml.cs
Normal file
|
@ -0,0 +1,18 @@
|
|||
using System.Runtime.InteropServices;
|
||||
using System.Windows;
|
||||
using Tetris.ViewsModels;
|
||||
|
||||
namespace Tetris.Views;
|
||||
|
||||
public partial class GameWindow : Window
|
||||
{
|
||||
public GameWindow()
|
||||
{
|
||||
AttachConsole(-1);
|
||||
InitializeComponent();
|
||||
new GameModel((int)Width, (int)Height, ImageControl);
|
||||
}
|
||||
|
||||
[DllImport("kernel32.dll")]
|
||||
private static extern bool AttachConsole(int dwProcessId);
|
||||
}
|
|
@ -3,12 +3,14 @@
|
|||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||
xmlns:local="clr-namespace:Tetris"
|
||||
xmlns:viewsModels="clr-namespace:Tetris.ViewsModels"
|
||||
mc:Ignorable="d"
|
||||
d:DataContext="{d:DesignInstance viewsModels:ViewModel, IsDesignTimeCreatable=True}"
|
||||
Title="MainWindow" Height="450" Width="800">
|
||||
<Grid>
|
||||
|
||||
<Image Source="pack://application:,,,/Resources/tetris.png" Stretch="Fill" HorizontalAlignment="Center"
|
||||
VerticalAlignment="Center" />
|
||||
<Button Click="StartButton_OnClick" HorizontalAlignment="Center" VerticalAlignment="Center" Width="100"
|
||||
Height="50" Content="Start" Margin="0 200 0 0" />
|
||||
</Grid>
|
||||
</Window>
|
||||
</Window>
|
|
@ -1,27 +1,29 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Windows;
|
||||
using System.Windows.Controls;
|
||||
using System.Windows.Data;
|
||||
using System.Windows.Documents;
|
||||
using System.Windows.Input;
|
||||
using System.Windows.Media;
|
||||
using System.Windows.Media.Imaging;
|
||||
using System.Windows.Navigation;
|
||||
using System.Windows.Shapes;
|
||||
using Tetris.ViewsModels;
|
||||
|
||||
namespace Tetris.Views {
|
||||
/// <summary>
|
||||
/// Interaction logic for MainWindow.xaml
|
||||
/// </summary>
|
||||
public partial class MainWindow : Window {
|
||||
public MainWindow() {
|
||||
DataContext = new ViewModel();
|
||||
InitializeComponent();
|
||||
}
|
||||
namespace Tetris.Views;
|
||||
|
||||
/// <summary>
|
||||
/// Interaction logic for MainWindow.xaml
|
||||
/// </summary>
|
||||
public partial class MainWindow : Window
|
||||
{
|
||||
public MainWindow()
|
||||
{
|
||||
AttachConsole(-1);
|
||||
DataContext = new ViewModel();
|
||||
InitializeComponent();
|
||||
}
|
||||
}
|
||||
|
||||
[DllImport("kernel32.dll")]
|
||||
private static extern bool AttachConsole(int dwProcessId);
|
||||
|
||||
private void StartButton_OnClick(object sender, RoutedEventArgs e)
|
||||
{
|
||||
Console.WriteLine("Start game...");
|
||||
new GameWindow().Show();
|
||||
Close();
|
||||
}
|
||||
}
|
49
ViewsModels/GameModel.cs
Normal file
49
ViewsModels/GameModel.cs
Normal file
|
@ -0,0 +1,49 @@
|
|||
using System;
|
||||
using System.Windows.Controls;
|
||||
using System.Windows.Media;
|
||||
using System.Windows.Media.Imaging;
|
||||
using System.Windows.Threading;
|
||||
|
||||
namespace Tetris.ViewsModels;
|
||||
|
||||
public class GameModel
|
||||
{
|
||||
private const int RendererHertz = 5;
|
||||
private const int GameRendererHertz = (1 / RendererHertz) * 1000;
|
||||
private readonly WriteableBitmap _writeableBitmap;
|
||||
|
||||
public GameModel(int width, int height, Image image)
|
||||
{
|
||||
_writeableBitmap = BitmapFactory.New(width, height);
|
||||
image.Source = _writeableBitmap;
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
private void Render(object? sender, EventArgs eventArgs)
|
||||
{
|
||||
_writeableBitmap.Lock();
|
||||
_writeableBitmap.Clear(Colors.Black);
|
||||
|
||||
_writeableBitmap.Unlock();
|
||||
}
|
||||
|
||||
private void Update(object? sender, EventArgs eventArgs)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
|
@ -1,12 +1,14 @@
|
|||
using System.ComponentModel;
|
||||
|
||||
namespace Tetris.ViewsModels;
|
||||
namespace Tetris.ViewsModels;
|
||||
|
||||
public class ViewModel : INotifyPropertyChanged {
|
||||
public class ViewModel : INotifyPropertyChanged
|
||||
{
|
||||
public event PropertyChangedEventHandler PropertyChanged;
|
||||
|
||||
protected virtual void OnPropertyChanged(string propertyName) {
|
||||
|
||||
protected virtual void OnPropertyChanged(string propertyName)
|
||||
{
|
||||
if (PropertyChanged != null)
|
||||
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
|
||||
}
|
||||
}
|
||||
}
|
Reference in a new issue