Add project files.
This commit is contained in:
3
Snake.slnx
Normal file
3
Snake.slnx
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
<Solution>
|
||||||
|
<Project Path="Snake/Snake.csproj" />
|
||||||
|
</Solution>
|
||||||
22
Snake/Cell.cs
Normal file
22
Snake/Cell.cs
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace Snake
|
||||||
|
{
|
||||||
|
public class Cell
|
||||||
|
{
|
||||||
|
public int X;
|
||||||
|
public int Y;
|
||||||
|
public CellTypes Type;
|
||||||
|
|
||||||
|
public Cell(int x, int y, CellTypes type)
|
||||||
|
{
|
||||||
|
X = x;
|
||||||
|
Y = y;
|
||||||
|
Type = type;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
9
Snake/CellTypes.cs
Normal file
9
Snake/CellTypes.cs
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
namespace Snake
|
||||||
|
{
|
||||||
|
public enum CellTypes
|
||||||
|
{
|
||||||
|
None,
|
||||||
|
Food,
|
||||||
|
Snake,
|
||||||
|
}
|
||||||
|
}
|
||||||
16
Snake/Directions.cs
Normal file
16
Snake/Directions.cs
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace Snake
|
||||||
|
{
|
||||||
|
public enum Directions
|
||||||
|
{
|
||||||
|
Up,
|
||||||
|
Down,
|
||||||
|
Left,
|
||||||
|
Right,
|
||||||
|
}
|
||||||
|
}
|
||||||
68
Snake/Form1.Designer.cs
generated
Normal file
68
Snake/Form1.Designer.cs
generated
Normal file
@@ -0,0 +1,68 @@
|
|||||||
|
namespace Snake
|
||||||
|
{
|
||||||
|
partial class Form1
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Required designer variable.
|
||||||
|
/// </summary>
|
||||||
|
private System.ComponentModel.IContainer components = null;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Clean up any resources being used.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
|
||||||
|
protected override void Dispose(bool disposing)
|
||||||
|
{
|
||||||
|
if (disposing && (components != null))
|
||||||
|
{
|
||||||
|
components.Dispose();
|
||||||
|
}
|
||||||
|
base.Dispose(disposing);
|
||||||
|
}
|
||||||
|
|
||||||
|
#region Windows Form Designer generated code
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Required method for Designer support - do not modify
|
||||||
|
/// the contents of this method with the code editor.
|
||||||
|
/// </summary>
|
||||||
|
private void InitializeComponent()
|
||||||
|
{
|
||||||
|
components = new System.ComponentModel.Container();
|
||||||
|
gamearea = new PictureBox();
|
||||||
|
timer = new System.Windows.Forms.Timer(components);
|
||||||
|
((System.ComponentModel.ISupportInitialize)gamearea).BeginInit();
|
||||||
|
SuspendLayout();
|
||||||
|
//
|
||||||
|
// gamearea
|
||||||
|
//
|
||||||
|
gamearea.Location = new Point(200, 25);
|
||||||
|
gamearea.Name = "gamearea";
|
||||||
|
gamearea.Size = new Size(400, 400);
|
||||||
|
gamearea.TabIndex = 0;
|
||||||
|
gamearea.TabStop = false;
|
||||||
|
//
|
||||||
|
// timer
|
||||||
|
//
|
||||||
|
timer.Enabled = true;
|
||||||
|
timer.Interval = 500;
|
||||||
|
timer.Tick += timer_Tick;
|
||||||
|
//
|
||||||
|
// Form1
|
||||||
|
//
|
||||||
|
AutoScaleDimensions = new SizeF(10F, 25F);
|
||||||
|
AutoScaleMode = AutoScaleMode.Font;
|
||||||
|
ClientSize = new Size(800, 450);
|
||||||
|
Controls.Add(gamearea);
|
||||||
|
Name = "Form1";
|
||||||
|
Text = "Form1";
|
||||||
|
Load += Form1_Load;
|
||||||
|
((System.ComponentModel.ISupportInitialize)gamearea).EndInit();
|
||||||
|
ResumeLayout(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
private System.Windows.Forms.Timer timer;
|
||||||
|
public PictureBox gamearea;
|
||||||
|
}
|
||||||
|
}
|
||||||
24
Snake/Form1.cs
Normal file
24
Snake/Form1.cs
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
namespace Snake
|
||||||
|
{
|
||||||
|
public partial class Form1 : Form
|
||||||
|
{
|
||||||
|
private Game _game;
|
||||||
|
|
||||||
|
public Form1()
|
||||||
|
{
|
||||||
|
InitializeComponent();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void Form1_Load(object sender, EventArgs e)
|
||||||
|
{
|
||||||
|
_game = new Game(this);
|
||||||
|
_game.Start();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void timer_Tick(object sender, EventArgs e)
|
||||||
|
{
|
||||||
|
_game.Update();
|
||||||
|
Invalidate();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
123
Snake/Form1.resx
Normal file
123
Snake/Form1.resx
Normal file
@@ -0,0 +1,123 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<root>
|
||||||
|
<!--
|
||||||
|
Microsoft ResX Schema
|
||||||
|
|
||||||
|
Version 2.0
|
||||||
|
|
||||||
|
The primary goals of this format is to allow a simple XML format
|
||||||
|
that is mostly human readable. The generation and parsing of the
|
||||||
|
various data types are done through the TypeConverter classes
|
||||||
|
associated with the data types.
|
||||||
|
|
||||||
|
Example:
|
||||||
|
|
||||||
|
... ado.net/XML headers & schema ...
|
||||||
|
<resheader name="resmimetype">text/microsoft-resx</resheader>
|
||||||
|
<resheader name="version">2.0</resheader>
|
||||||
|
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
|
||||||
|
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
|
||||||
|
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
|
||||||
|
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
|
||||||
|
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
|
||||||
|
<value>[base64 mime encoded serialized .NET Framework object]</value>
|
||||||
|
</data>
|
||||||
|
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
|
||||||
|
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
|
||||||
|
<comment>This is a comment</comment>
|
||||||
|
</data>
|
||||||
|
|
||||||
|
There are any number of "resheader" rows that contain simple
|
||||||
|
name/value pairs.
|
||||||
|
|
||||||
|
Each data row contains a name, and value. The row also contains a
|
||||||
|
type or mimetype. Type corresponds to a .NET class that support
|
||||||
|
text/value conversion through the TypeConverter architecture.
|
||||||
|
Classes that don't support this are serialized and stored with the
|
||||||
|
mimetype set.
|
||||||
|
|
||||||
|
The mimetype is used for serialized objects, and tells the
|
||||||
|
ResXResourceReader how to depersist the object. This is currently not
|
||||||
|
extensible. For a given mimetype the value must be set accordingly:
|
||||||
|
|
||||||
|
Note - application/x-microsoft.net.object.binary.base64 is the format
|
||||||
|
that the ResXResourceWriter will generate, however the reader can
|
||||||
|
read any of the formats listed below.
|
||||||
|
|
||||||
|
mimetype: application/x-microsoft.net.object.binary.base64
|
||||||
|
value : The object must be serialized with
|
||||||
|
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
|
||||||
|
: and then encoded with base64 encoding.
|
||||||
|
|
||||||
|
mimetype: application/x-microsoft.net.object.soap.base64
|
||||||
|
value : The object must be serialized with
|
||||||
|
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
|
||||||
|
: and then encoded with base64 encoding.
|
||||||
|
|
||||||
|
mimetype: application/x-microsoft.net.object.bytearray.base64
|
||||||
|
value : The object must be serialized into a byte array
|
||||||
|
: using a System.ComponentModel.TypeConverter
|
||||||
|
: and then encoded with base64 encoding.
|
||||||
|
-->
|
||||||
|
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
|
||||||
|
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
|
||||||
|
<xsd:element name="root" msdata:IsDataSet="true">
|
||||||
|
<xsd:complexType>
|
||||||
|
<xsd:choice maxOccurs="unbounded">
|
||||||
|
<xsd:element name="metadata">
|
||||||
|
<xsd:complexType>
|
||||||
|
<xsd:sequence>
|
||||||
|
<xsd:element name="value" type="xsd:string" minOccurs="0" />
|
||||||
|
</xsd:sequence>
|
||||||
|
<xsd:attribute name="name" use="required" type="xsd:string" />
|
||||||
|
<xsd:attribute name="type" type="xsd:string" />
|
||||||
|
<xsd:attribute name="mimetype" type="xsd:string" />
|
||||||
|
<xsd:attribute ref="xml:space" />
|
||||||
|
</xsd:complexType>
|
||||||
|
</xsd:element>
|
||||||
|
<xsd:element name="assembly">
|
||||||
|
<xsd:complexType>
|
||||||
|
<xsd:attribute name="alias" type="xsd:string" />
|
||||||
|
<xsd:attribute name="name" type="xsd:string" />
|
||||||
|
</xsd:complexType>
|
||||||
|
</xsd:element>
|
||||||
|
<xsd:element name="data">
|
||||||
|
<xsd:complexType>
|
||||||
|
<xsd:sequence>
|
||||||
|
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
|
||||||
|
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
|
||||||
|
</xsd:sequence>
|
||||||
|
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
|
||||||
|
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
|
||||||
|
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
|
||||||
|
<xsd:attribute ref="xml:space" />
|
||||||
|
</xsd:complexType>
|
||||||
|
</xsd:element>
|
||||||
|
<xsd:element name="resheader">
|
||||||
|
<xsd:complexType>
|
||||||
|
<xsd:sequence>
|
||||||
|
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
|
||||||
|
</xsd:sequence>
|
||||||
|
<xsd:attribute name="name" type="xsd:string" use="required" />
|
||||||
|
</xsd:complexType>
|
||||||
|
</xsd:element>
|
||||||
|
</xsd:choice>
|
||||||
|
</xsd:complexType>
|
||||||
|
</xsd:element>
|
||||||
|
</xsd:schema>
|
||||||
|
<resheader name="resmimetype">
|
||||||
|
<value>text/microsoft-resx</value>
|
||||||
|
</resheader>
|
||||||
|
<resheader name="version">
|
||||||
|
<value>2.0</value>
|
||||||
|
</resheader>
|
||||||
|
<resheader name="reader">
|
||||||
|
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
|
||||||
|
</resheader>
|
||||||
|
<resheader name="writer">
|
||||||
|
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
|
||||||
|
</resheader>
|
||||||
|
<metadata name="timer.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
|
||||||
|
<value>17, 17</value>
|
||||||
|
</metadata>
|
||||||
|
</root>
|
||||||
44
Snake/Game.cs
Normal file
44
Snake/Game.cs
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace Snake
|
||||||
|
{
|
||||||
|
public class Game
|
||||||
|
{
|
||||||
|
//new Cell(10, 10, CellTypes.Snake)
|
||||||
|
Worm worm;
|
||||||
|
Directions direction = Directions.Down;
|
||||||
|
Map map;
|
||||||
|
Form1 form;
|
||||||
|
|
||||||
|
public Game(Form1 form)
|
||||||
|
{
|
||||||
|
this.form = form;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Start()
|
||||||
|
{
|
||||||
|
Cell startCell = new Cell(10, 10, CellTypes.Snake);
|
||||||
|
map = new Map(20, 20);
|
||||||
|
map.SetCell(startCell);
|
||||||
|
worm = new Worm(startCell);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Update()
|
||||||
|
{
|
||||||
|
map.Render(form.gamearea.CreateGraphics());
|
||||||
|
form.Invalidate();
|
||||||
|
|
||||||
|
int x = worm.Body[0].X;
|
||||||
|
int y = worm.Body[0].Y;
|
||||||
|
|
||||||
|
Cell snakeCell = map.Cells[x, y];
|
||||||
|
Cell cellBelow = map.Cells[x, y-1];
|
||||||
|
|
||||||
|
map.SwapCells(snakeCell, cellBelow);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
85
Snake/Map.cs
Normal file
85
Snake/Map.cs
Normal file
@@ -0,0 +1,85 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Security.Cryptography.Xml;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace Snake
|
||||||
|
{
|
||||||
|
public class Map
|
||||||
|
{
|
||||||
|
public Cell[,] Cells;
|
||||||
|
private int _h;
|
||||||
|
private int _w;
|
||||||
|
private const int CELL_SIZE = 20;
|
||||||
|
|
||||||
|
public Map(int h, int w)
|
||||||
|
{
|
||||||
|
Cells = new Cell[h, w];
|
||||||
|
_h = h;
|
||||||
|
_w = w;
|
||||||
|
|
||||||
|
for (int x = 0; x < _w; x++)
|
||||||
|
{
|
||||||
|
for (int y = 0; y < _h; y++)
|
||||||
|
{
|
||||||
|
Cells[x, y] = new Cell(x, y, CellTypes.None);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void SetCell(Cell cell)
|
||||||
|
{
|
||||||
|
Cells[cell.X,cell.Y] = cell;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void SwapCells(Cell cell1, Cell cell2)
|
||||||
|
{
|
||||||
|
int tmpX = cell1.X;
|
||||||
|
int tmpY = cell1.Y;
|
||||||
|
|
||||||
|
cell1.X = cell2.X; cell1.Y = cell2.Y;
|
||||||
|
|
||||||
|
cell2.X = tmpX; cell2.Y = tmpY;
|
||||||
|
|
||||||
|
Cells[cell1.X, cell1.Y] = cell1;
|
||||||
|
Cells[cell2.X, cell2.Y] = cell2;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Render(Graphics graphics)
|
||||||
|
{
|
||||||
|
SolidBrush brush = new SolidBrush(Color.RebeccaPurple);
|
||||||
|
|
||||||
|
for (int x = 0; x < _w; x++)
|
||||||
|
{
|
||||||
|
for (int y = 0; y < _h; y++)
|
||||||
|
{
|
||||||
|
Cell cell = Cells[x,y];
|
||||||
|
|
||||||
|
if (cell.Type == CellTypes.None)
|
||||||
|
{
|
||||||
|
brush.Color = Color.LightGray;
|
||||||
|
}
|
||||||
|
else if (cell.Type == CellTypes.Snake)
|
||||||
|
{
|
||||||
|
brush.Color = Color.Green;
|
||||||
|
}
|
||||||
|
else if (cell.Type == CellTypes.Food)
|
||||||
|
{
|
||||||
|
brush.Color = Color.Red;
|
||||||
|
}
|
||||||
|
|
||||||
|
int totalHeight = CELL_SIZE * _h;
|
||||||
|
|
||||||
|
graphics.FillRectangle(brush,
|
||||||
|
x * 20,
|
||||||
|
totalHeight - y * 20,
|
||||||
|
CELL_SIZE,
|
||||||
|
CELL_SIZE
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
12
Snake/Program.cs
Normal file
12
Snake/Program.cs
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
namespace Snake
|
||||||
|
{
|
||||||
|
internal static class Program
|
||||||
|
{
|
||||||
|
[STAThread]
|
||||||
|
static void Main()
|
||||||
|
{
|
||||||
|
ApplicationConfiguration.Initialize();
|
||||||
|
Application.Run(new Form1());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
11
Snake/Snake.csproj
Normal file
11
Snake/Snake.csproj
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<OutputType>WinExe</OutputType>
|
||||||
|
<TargetFramework>net8.0-windows</TargetFramework>
|
||||||
|
<Nullable>enable</Nullable>
|
||||||
|
<UseWindowsForms>true</UseWindowsForms>
|
||||||
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
</Project>
|
||||||
19
Snake/Worm.cs
Normal file
19
Snake/Worm.cs
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace Snake
|
||||||
|
{
|
||||||
|
public class Worm
|
||||||
|
{
|
||||||
|
public List<Cell> Body = new List<Cell>();
|
||||||
|
|
||||||
|
public Worm(Cell startCell)
|
||||||
|
{
|
||||||
|
Body.Add(startCell);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user