using System;
using System.Text;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Drawing;
using SdlDotNet.Graphics;
using SdlDotNet.Graphics.Sprites;
using SdlDotNet.Core;
using SdlDotNet.Input;
namespace SNAKE
{
public class MainSnake
{
#region Fields
private Surface background;
public Surface screen;
private Sprite bg;
public SpriteCollection main = new SpriteCollection();
public Collection<Rectangle> rects = new Collection<Rectangle>();
private Snake snake;
public Surface text;
private Surface esctext;
public SdlDotNet.Graphics.Font font;
#endregion
#region MainSnake
public MainSnake()
{
Events.Fps = 120;
Events.Tick += new EventHandler<TickEventArgs>(EngineTick);
screen = Video.SetVideoMode(800, 600);
Video.WindowCaption = "PRO SNAKE 2D:: By Profas";
background = new Surface(@"failai/map.jpg");
bg = new Sprite(background);
bg.Position = new Point(0, 0);
main.Add(bg);
rects = screen.Blit(main);
snake = new Snake(new Point(250,250), this);
main.Add(snake._head);
AddTail();
font = new SdlDotNet.Graphics.Font(@"failai/comic.ttf", 18);
esctext = font.Render("PRO SNAKE 2D :: By Profas :: ESC to quit", Color.Black);
text = font.Render("SCORE "+ snake.score.ToString()+", FPS: "+Events.Fps.ToString(), Color.Gold);
Events.Run();
}
~MainSnake() { }
#endregion
#region EngineTick
private void EngineTick(object sender, TickEventArgs args)
{
rects = screen.Blit(main);
screen.Blit(text, new Point(70, 560));
screen.Blit(esctext, new Point(200, 10));
screen.Update();
screen.Update(rects);
snake.Move();
Video.Screen.Fill(Color.Black);
}
#endregion
#region AddTail
private void AddTail()
{
for (int i = 0; i < snake.tail_count; i++)
main.Add(snake._tail[i]);
}
#endregion
}
static class Program
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Main());
}
}
#region eWalk
public enum walk
{
none = 0,
up,
right,
down,
left
}
#endregion
public class Snake
{
#region Fields
private walk direction = new walk();
private Surface head;
public Sprite _head;
public Tail[] _tail;
public int tail_count = 6;
public int speed = 0;
private System.Windows.Forms.Timer permission = new System.Windows.Forms.Timer();
private bool stop = true;
private int pos = 2;
private Point head_last_pos;
private MainSnake _engine;
private int step = 25;
private Liux liux;
private bool dead = false;
public int score;
private Surface tl_surf;
#endregion
#region Snake
public Snake(Point location, MainSnake engine)
{
_engine = engine;
score = (tail_count - 1) * 10;
Events.KeyboardDown += new EventHandler<KeyboardEventArgs>(KeyboardDown);
Events.KeyboardUp += new EventHandler<KeyboardEventArgs>(KeyboardUp);
_tail = new Tail[tail_count];
tl_surf = new Surface(@"failai/tail.bmp");
for (int i = 0; i < tail_count; i++)
_tail[i] = new Tail(tl_surf);
head = new Surface(@"failai/head.bmp");
_head = new Sprite(head);
_head.Position = location;
head_last_pos = location;
_head.Transparent = true;
_head.TransparentColor = Color.White;
_tail[1].Surface = head;
permission.Interval = 25;
permission.Tick += new EventHandler(PermissionTick);
permission.Enabled = true;
liux = new Liux(this);
_engine.main.Add(liux);
direction = walk.right;
}
~Snake() { }
#endregion
#region Move
public void Move()
{
if (!dead)
{
LiuxCollision();
if ((!TailCollision()) && (WallsCollision()))
{
if (!stop)
if (_head.Y % 25 != 0)
{
if (pos == 1)
_head.Y -= step;
if (pos == 3)
_head.Y += step;
}
else if (_head.X % 25 != 0)
{
if (pos == 2)
_head.X += step;
if (pos == 4)
_head.X -= step;
}
else
{
stop = true;
permission.Enabled = true;
}
}
else
Die();
}
}
#endregion
#region Keyboard
private void KeyboardDown(object sender, KeyboardEventArgs args)
{
switch (args.Key)
{
case (Key.UpArrow):
if(direction!=walk.down)
direction = walk.up;
break;
case (Key.RightArrow):
if(direction != walk.left)
direction = walk.right;
break;
case (Key.DownArrow):
if(direction != walk.up)
direction = walk.down;
break;
case (Key.LeftArrow):
if(direction != walk.right)
direction = walk.left;
break;
case (Key.Escape):
Events.QuitApplication();
break;
}
}
private void KeyboardUp(object sender, KeyboardEventArgs args)
{
}
#endregion
#region PermissionTick
private void PermissionTick(object sender, EventArgs args)
{
if(speed++ == 1)
{
stop = false;
speed = 0;
switch (direction)
{
case walk.up:
pos = 1;
_head.Y -= step;
break;
case walk.right:
pos = 2;
_head.X += step;
break;
case walk.down:
pos = 3;
_head.Y += step;
break;
case walk.left:
pos = 4;
_head.X -= step;
break;
}
TailMove();
_engine.text = _engine.font.Render("SCORE " + score.ToString() + ", FPS: " + Events.Fps.ToString(), Color.Gold);
permission.Enabled = false;
}
}
#endregion
#region PickUp
private void PickUp()
{
score += 10;
//_engine.text = _engine.font.Render("SCORE " + score.ToString() + ", FPS: " + Events.Fps.ToString(), Color.Gold);
Grow();
liux.ChangeLocation();
}
#endregion
#region LiuxCollision
private void LiuxCollision()
{
Point head_xy = new Point(_head.X + (_head.Width / 2), _head.Y + (_head.Height / 2));
if ((head_xy.X > liux.X) && (head_xy.X < liux.X + liux.Width))
if ((head_xy.Y > liux.Y) && (head_xy.Y < liux.Y + liux.Height))
PickUp();
}
#endregion
#region WallsCollision
private bool WallsCollision()
{
Point head_xy = new Point(_head.X + (_head.Width / 2), _head.Y + (_head.Height / 2));
if ((head_xy.X > 50) && (head_xy.X < 750))
if ((head_xy.Y > 50) && (head_xy.Y < 550))
return true;
else
return false;
else
return false;
}
#endregion
#region TailCollision
private bool TailCollision()
{
Point head_xy = new Point(_head.X + (_head.Width / 2), _head.Y + (_head.Height / 2));
for (int i = 0; i < tail_count; i++)
{
if (i != 1)
if ((head_xy.X > _tail[i].X) && (head_xy.X < _tail[i].X + _tail[i].Width))
if ((head_xy.Y > _tail[i].Y) && (head_xy.Y < _tail[i].Y + _tail[i].Height))
return true;
}
return false;
}
#endregion
#region TailMove
public void TailMove()
{
for (int i = 0; i < tail_count; i++)
{
if (i != 0)
{
_tail[i].last_pos = _tail[i].Position;
_tail[i].Position = _tail[i-1].last_pos;
}
else
{
_tail[i].Position = _tail[i].last_pos;
_tail[i].last_pos = _head.Position;
}
}
}
#endregion
#region Grow
private void Grow()
{
Tail[] _newTail = new Tail[++tail_count];
for (int i = 0; i < tail_count - 1; i++)
_newTail[i] = _tail[i];
_tail = new Tail[tail_count];
for (int i = 0; i < tail_count; i++)
_tail[i] = _newTail[i];
_tail[tail_count - 1] = new Tail(tl_surf);
_tail[tail_count - 1].last_pos = _tail[tail_count - 2].Position;
_engine.main.Add(_tail[tail_count - 1]);
_tail[tail_count - 1].Position = _tail[tail_count - 1].last_pos;
_newTail = new Tail[0];
}
#endregion
#region Die
private void Die()
{
dead = true;
direction = walk.none;
System.Windows.Forms.MessageBox.Show("YOUR SCORE IS: " + score.ToString() + ".", "GAME OVER");
Events.QuitApplication();
}
#endregion
}
public class Tail : Sprite
{
#region Fields
public Point last_pos = new Point(-50, -50);
#endregion
#region Tail
public Tail(Surface tail)
{
this.Surface = tail;
this.Position = new Point(-50, -50);
this.Transparent = true;
this.TransparentColor = Color.White;
}
~Tail() { }
#endregion
}
public class Liux : Sprite
{
#region Fields
private Random rand = new Random();
private bool ok = false;
private Snake _snake;
#endregion
#region Liux
public Liux(Snake snake)
{
int x = rand.Next(10, 50);
_snake = snake;
Surface liux = new Surface(@"failai/liux.bmp");
this.Surface = liux;
this.Transparent = true;
this.TransparentColor = Color.White;
ChangeLocation();
}
~Liux() { }
#endregion
#region ChangeLocation
public void ChangeLocation()
{
int posx = 0;
int posy = 0;
while (!ok)
{
posx = rand.Next(50, 675);
posy = rand.Next(50, 475);
while (posx % 25 != 0)
posx++;
while (posy % 25 != 0)
posy++;
for (int i = 0; i < _snake.tail_count; i++)
if (_snake._tail[i].Position == new Point(posx, posy))
{
ok = false;
break;
}
else
ok = true;
}
ok = false;
this.Position = new Point(posx, posy);
}
#endregion
}
public partial class Main : Form
{
public Main()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
MainSnake mainsnake = new MainSnake();
}
private void button2_Click(object sender, EventArgs e)
{
this.Close();
}
}
}