Add render
This commit is contained in:
parent
0b5114290e
commit
427986f599
14 changed files with 229 additions and 164 deletions
3
App.xaml
3
App.xaml
|
@ -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>
|
23
App.xaml.cs
23
App.xaml.cs
|
@ -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
|
||||||
}
|
{
|
||||||
|
}
|
|
@ -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)
|
||||||
)]
|
)]
|
|
@ -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));
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -1,44 +1,49 @@
|
||||||
using System;
|
using System.Drawing;
|
||||||
using System.Drawing;
|
|
||||||
using Color = System.Drawing.Color;
|
|
||||||
|
|
||||||
namespace Tetris.Models;
|
namespace Tetris.Models;
|
||||||
|
|
||||||
public class Grid {
|
public class Grid
|
||||||
private Color[,] _grid;
|
{
|
||||||
|
private readonly Color[,] _grid;
|
||||||
|
|
||||||
public Point MinGrid => new Point(0,0);
|
public Grid(Color[,] grid)
|
||||||
public Point MaxGrid => new Point(_grid.GetLength(0)-1, _grid.GetLength(1)-1);
|
{
|
||||||
|
|
||||||
public Grid(Color[,] grid) {
|
|
||||||
_grid = grid;
|
_grid = grid;
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool CanGo(Tetrominoe tetrominoe, Point point) {
|
public Point MinGrid => new(0, 0);
|
||||||
bool[,] shape = tetrominoe.Shape;
|
public Point MaxGrid => new(_grid.GetLength(0) - 1, _grid.GetLength(1) - 1);
|
||||||
|
|
||||||
|
public bool CanGo(Tetrominoe tetrominoe, Point point)
|
||||||
|
{
|
||||||
|
var shape = tetrominoe.Shape;
|
||||||
if (point.X < MinGrid.X || point.Y < MinGrid.Y)
|
if (point.X < MinGrid.X || point.Y < MinGrid.Y)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
else if ((point.X + tetrominoe.Shape.GetLength(0)) > MaxGrid.X || (point.Y + tetrominoe.Shape.GetLength(1)) > MaxGrid.Y)
|
if (point.X + tetrominoe.Shape.GetLength(0) > MaxGrid.X || point.Y + tetrominoe.Shape.GetLength(1) > MaxGrid.Y)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
for (uint x = 0; x < shape.GetLength(0); x++)
|
for (uint x = 0; x < shape.GetLength(0); x++)
|
||||||
for (uint y = 0; y < shape.GetLength(1); y++) {
|
for (uint y = 0; y < shape.GetLength(1); y++)
|
||||||
Point s = point + new Size((int) x, (int) y);
|
{
|
||||||
if (shape[x, y] && _grid[s.X, s.Y] != Color.Empty)
|
var s = point + new Size((int)x, (int)y);
|
||||||
return false;
|
if (shape[x, y] && _grid[s.X, s.Y] != Color.Empty)
|
||||||
}
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public override string ToString() {
|
public override string ToString()
|
||||||
String s = "";
|
{
|
||||||
for (uint y = 0; y < _grid.GetLength(1); y++) {
|
var s = "";
|
||||||
|
for (uint y = 0; y < _grid.GetLength(1); y++)
|
||||||
|
{
|
||||||
for (uint x = 0; x < _grid.GetLength(0); x++)
|
for (uint x = 0; x < _grid.GetLength(0); x++)
|
||||||
s += _grid[x,y].Name + "\t";
|
s += _grid[x, y].Name + "\t";
|
||||||
s += "\n";
|
s += "\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
return s;
|
return s;
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -1,65 +1,22 @@
|
||||||
using System;
|
using System.ComponentModel;
|
||||||
using System.ComponentModel;
|
|
||||||
using System.Drawing;
|
using System.Drawing;
|
||||||
|
|
||||||
namespace Tetris.Models;
|
namespace Tetris.Models;
|
||||||
|
|
||||||
public class Tetrominoe {
|
|
||||||
public event PropertyChangedEventHandler PropertyChanged;
|
|
||||||
|
|
||||||
private Grid _grid;
|
|
||||||
|
|
||||||
private bool[,] _shape;
|
|
||||||
public bool[,] Shape {
|
|
||||||
get {
|
|
||||||
bool[,] shape = (bool[,]) _shape.Clone();
|
|
||||||
for (int i = 0; i < _orientation; i++)
|
|
||||||
shape = _rotateLeft(shape);
|
|
||||||
return shape;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private Point _coordinates;
|
|
||||||
public Point Coordinates {
|
|
||||||
get => _coordinates;
|
|
||||||
set {
|
|
||||||
if (! _grid.CanGo(this, value))
|
|
||||||
return;
|
|
||||||
_coordinates = value;
|
|
||||||
OnPropertyChanged("Coordinates");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private short _orientation = 0;
|
|
||||||
public short Orientation {
|
|
||||||
get => _orientation;
|
|
||||||
set {
|
|
||||||
if (value > 3)
|
|
||||||
value = 0;
|
|
||||||
else if (value < 0)
|
|
||||||
value = 3;
|
|
||||||
|
|
||||||
short oldOrientation = _orientation;
|
|
||||||
_orientation = value;
|
|
||||||
|
|
||||||
Point newCoords = _coordinates;
|
|
||||||
//ToDo edit position ?
|
|
||||||
//ToDo move position when hot a wall
|
|
||||||
|
|
||||||
if (!_grid.CanGo(this, newCoords)) {
|
|
||||||
_orientation = oldOrientation;
|
|
||||||
} else {
|
|
||||||
OnPropertyChanged("Orientation");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
public class Tetrominoe
|
||||||
|
{
|
||||||
private Color _color;
|
private Color _color;
|
||||||
public Color Color {
|
|
||||||
get;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Tetrominoe(Grid grid, bool[,] shape, Point coordinates, short orientation, Color color) {
|
private Point _coordinates;
|
||||||
|
|
||||||
|
private readonly Grid _grid;
|
||||||
|
|
||||||
|
private short _orientation;
|
||||||
|
|
||||||
|
private readonly bool[,] _shape;
|
||||||
|
|
||||||
|
public Tetrominoe(Grid grid, bool[,] shape, Point coordinates, short orientation, Color color)
|
||||||
|
{
|
||||||
_grid = grid;
|
_grid = grid;
|
||||||
_shape = shape;
|
_shape = shape;
|
||||||
_coordinates = coordinates;
|
_coordinates = coordinates;
|
||||||
|
@ -70,44 +27,104 @@ public class Tetrominoe {
|
||||||
// ToDO: ascpect technique test
|
// ToDO: ascpect technique test
|
||||||
}
|
}
|
||||||
|
|
||||||
private bool[,] _rotateLeft(bool[,] shape) {
|
public bool[,] Shape
|
||||||
bool[,] rotatedArr = new bool[shape.GetLength(1), shape.GetLength(0)];
|
{
|
||||||
|
get
|
||||||
for (int i=shape.GetLength(0)-1;i>=0;--i)
|
{
|
||||||
for (int j=0;j<shape.GetLength(1);++j)
|
var shape = (bool[,])_shape.Clone();
|
||||||
rotatedArr[j,(shape.GetLength(0)-1)-i] = shape[i,j];
|
for (var i = 0; i < _orientation; i++)
|
||||||
|
shape = _rotateLeft(shape);
|
||||||
|
return shape;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public Point Coordinates
|
||||||
|
{
|
||||||
|
get => _coordinates;
|
||||||
|
set
|
||||||
|
{
|
||||||
|
if (!_grid.CanGo(this, value))
|
||||||
|
return;
|
||||||
|
_coordinates = value;
|
||||||
|
OnPropertyChanged("Coordinates");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public short Orientation
|
||||||
|
{
|
||||||
|
get => _orientation;
|
||||||
|
set
|
||||||
|
{
|
||||||
|
if (value > 3)
|
||||||
|
value = 0;
|
||||||
|
else if (value < 0)
|
||||||
|
value = 3;
|
||||||
|
|
||||||
|
var oldOrientation = _orientation;
|
||||||
|
_orientation = value;
|
||||||
|
|
||||||
|
var newCoords = _coordinates;
|
||||||
|
//ToDo edit position ?
|
||||||
|
//ToDo move position when hot a wall
|
||||||
|
|
||||||
|
if (!_grid.CanGo(this, newCoords))
|
||||||
|
_orientation = oldOrientation;
|
||||||
|
else
|
||||||
|
OnPropertyChanged("Orientation");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public Color Color { get; }
|
||||||
|
|
||||||
|
public event PropertyChangedEventHandler PropertyChanged;
|
||||||
|
|
||||||
|
private bool[,] _rotateLeft(bool[,] shape)
|
||||||
|
{
|
||||||
|
var rotatedArr = new bool[shape.GetLength(1), shape.GetLength(0)];
|
||||||
|
|
||||||
|
for (var i = shape.GetLength(0) - 1; i >= 0; --i)
|
||||||
|
for (var j = 0; j < shape.GetLength(1); ++j)
|
||||||
|
rotatedArr[j, shape.GetLength(0) - 1 - i] = shape[i, j];
|
||||||
|
|
||||||
return rotatedArr;
|
return rotatedArr;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void RotateLeft() {
|
public void RotateLeft()
|
||||||
|
{
|
||||||
Orientation -= 1;
|
Orientation -= 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void RotateRight() {
|
public void RotateRight()
|
||||||
|
{
|
||||||
Orientation += 1;
|
Orientation += 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void GoRight() {
|
public void GoRight()
|
||||||
|
{
|
||||||
Coordinates += new Size(1, 0);
|
Coordinates += new Size(1, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void GoLeft() {
|
public void GoLeft()
|
||||||
|
{
|
||||||
Coordinates -= new Size(1, 0);
|
Coordinates -= new Size(1, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void GoDown() {
|
public void GoDown()
|
||||||
|
{
|
||||||
Coordinates += new Size(0, 1);
|
Coordinates += new Size(0, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
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));
|
||||||
}
|
}
|
||||||
|
|
||||||
public override string ToString() {
|
public override string ToString()
|
||||||
String s = "";
|
{
|
||||||
for (uint y = 0; y < Shape.GetLength(1); y++) {
|
var s = "";
|
||||||
|
for (uint y = 0; y < Shape.GetLength(1); y++)
|
||||||
|
{
|
||||||
for (uint x = 0; x < Shape.GetLength(0); x++)
|
for (uint x = 0; x < Shape.GetLength(0); x++)
|
||||||
if (Shape[x, y])
|
if (Shape[x, y])
|
||||||
s += "x";
|
s += "x";
|
||||||
|
@ -115,6 +132,7 @@ public class Tetrominoe {
|
||||||
s += " ";
|
s += " ";
|
||||||
s += "\n";
|
s += "\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
return s;
|
return s;
|
||||||
}
|
}
|
||||||
}
|
}
|
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,6 +5,14 @@
|
||||||
<Nullable>enable</Nullable>
|
<Nullable>enable</Nullable>
|
||||||
<UseWPF>true</UseWPF>
|
<UseWPF>true</UseWPF>
|
||||||
<TargetFramework>net6.0-windows</TargetFramework>
|
<TargetFramework>net6.0-windows</TargetFramework>
|
||||||
|
<ApplicationIcon>Resources\icon.ico</ApplicationIcon>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<None Remove="Resources\tetris.png"/>
|
||||||
|
<Resource Include="Resources\tetris.png">
|
||||||
|
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||||
|
</Resource>
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</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>
|
17
Views/GameWindow.xaml.cs
Normal file
17
Views/GameWindow.xaml.cs
Normal file
|
@ -0,0 +1,17 @@
|
||||||
|
using System.Runtime.InteropServices;
|
||||||
|
using System.Windows;
|
||||||
|
|
||||||
|
namespace Tetris.Views;
|
||||||
|
|
||||||
|
public partial class GameWindow : Window
|
||||||
|
{
|
||||||
|
public GameWindow()
|
||||||
|
{
|
||||||
|
AttachConsole(-1);
|
||||||
|
InitializeComponent();
|
||||||
|
// new GameManager((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: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>
|
|
@ -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();
|
||||||
|
}
|
||||||
|
}
|
|
@ -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));
|
||||||
}
|
}
|
||||||
}
|
}
|
Reference in a new issue