Archived
1
0
Fork 0

Merge remote-tracking branch 'origin/game-system'

# Conflicts:
#	Models/Grid.cs
#	Models/Tetrominoe.cs
#	Tetris.csproj
This commit is contained in:
Ethanell 2022-04-07 14:27:36 +02:00
commit b4a3a41566
13 changed files with 148 additions and 63 deletions

View file

@ -1,9 +1,8 @@
<Application x:Class="Tetris.App" <Application x:Class="Tetris.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Tetris"
StartupUri="Views/MainWindow.xaml"> StartupUri="Views/MainWindow.xaml">
<Application.Resources> <Application.Resources>
</Application.Resources> </Application.Resources>
</Application> </Application>

View file

@ -1,15 +1,10 @@
using System; using System.Windows;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
namespace Tetris { namespace Tetris;
/// <summary>
/// Interaction logic for App.xaml /// <summary>
/// </summary> /// Interaction logic for App.xaml
public partial class App : Application { /// </summary>
} public partial class App : Application
} {
}

View file

@ -7,4 +7,4 @@ using System.Windows;
ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located
//(used if a resource is not found in the page, //(used if a resource is not found in the page,
// app, or any theme specific resource dictionaries) // app, or any theme specific resource dictionaries)
)] )]

View file

@ -1,37 +1,43 @@
using System; using System.ComponentModel;
using System.ComponentModel;
namespace Tetris.Models; namespace Tetris.Models;
public class Party : INotifyPropertyChanged { public class Party : INotifyPropertyChanged
public event PropertyChangedEventHandler PropertyChanged; {
private int _score;
private String _userName; private string _userName;
public String UserName { public Party(string userName)
{
_userName = userName;
}
public string UserName
{
get => _userName; get => _userName;
set { set
{
_userName = value; _userName = value;
OnPropertyChanged("UserName"); OnPropertyChanged("UserName");
} }
} }
private int _score = 0; public int Score
{
public int Score {
get => _score; get => _score;
set { set
{
_score = value; _score = value;
OnPropertyChanged("Score"); OnPropertyChanged("Score");
} }
} }
public Party(String userName) { public event PropertyChangedEventHandler PropertyChanged;
_userName = userName;
}
protected virtual void OnPropertyChanged(string propertyName) { protected virtual void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null) if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
} }
} }

BIN
Resources/icon.ico Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 17 KiB

BIN
Resources/tetris.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 48 KiB

View file

@ -9,6 +9,7 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="Pastel" Version="3.0.0" /> <PackageReference Include="Pastel" Version="3.0.0" />
<PackageReference Include="WriteableBitmapEx" Version="1.6.8" />
</ItemGroup> </ItemGroup>
</Project> </Project>

11
Views/GameWindow.xaml Normal file
View 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
View 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);
}

View file

@ -3,12 +3,14 @@
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:Tetris"
xmlns:viewsModels="clr-namespace:Tetris.ViewsModels" xmlns:viewsModels="clr-namespace:Tetris.ViewsModels"
mc:Ignorable="d" mc:Ignorable="d"
d:DataContext="{d:DesignInstance viewsModels:ViewModel, IsDesignTimeCreatable=True}" d:DataContext="{d:DesignInstance viewsModels:ViewModel, IsDesignTimeCreatable=True}"
Title="MainWindow" Height="450" Width="800"> Title="MainWindow" Height="450" Width="800">
<Grid> <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> </Grid>
</Window> </Window>

View file

@ -1,27 +1,29 @@
using System; using System;
using System.Collections.Generic; using System.Runtime.InteropServices;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows; 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; using Tetris.ViewsModels;
namespace Tetris.Views { namespace Tetris.Views;
/// <summary>
/// Interaction logic for MainWindow.xaml /// <summary>
/// </summary> /// Interaction logic for MainWindow.xaml
public partial class MainWindow : Window { /// </summary>
public MainWindow() { public partial class MainWindow : Window
DataContext = new ViewModel(); {
InitializeComponent(); 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
View 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)
{
}
}

View file

@ -1,12 +1,14 @@
using System.ComponentModel; using System.ComponentModel;
namespace Tetris.ViewsModels; namespace Tetris.ViewsModels;
public class ViewModel : INotifyPropertyChanged { public class ViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged; public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName) { protected virtual void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null) if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
} }
} }