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 c0d999e5f2
13 changed files with 157 additions and 64 deletions

View file

@ -1,7 +1,6 @@
<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>

View file

@ -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
{
}

View file

@ -1,36 +1,42 @@
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

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

@ -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
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: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>

View file

@ -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
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

@ -2,10 +2,12 @@
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));
}