I had seen some problem on other forum, and i tried to solve them but unsuccesfully, now is too intersting for me and i just need to solve this for my ego... lol
Main problem is when snake eat first food, snake added one rectangle, when snake eat second food nothings changed?
When snake head position and food position are same, then i try to add another rectangle, i have list where i stored rectangles coordinates x and y.
What do you think, how i can to solve this problem? I think that i'm so close, but my brain has stopped. :(
code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Zmijice
{
enum Kontrole
{
Desno,Levo,Gore,Dole,Stoj
}
public partial class Zmijice : Form
{
int koox;
int kooy;
private int x;
private int y;
private int c;
private int b;
private const int visina=20; //width
private const int sirina=20; //height
//private int m = 20;
//private int n = 20;
private List<int> SegmentX = new List<int>();
private List<int> SegmentY = new List<int>();
Random rnd = new Random();
private Kontrole Pozicija;
public Zmijice()
{
InitializeComponent();
//slucajne pozicije pri startu
x = rnd.Next(1, 20) * 20;
y = rnd.Next(1, 20) * 20;
c = rnd.Next(1, 20) * 20;
b = rnd.Next(1, 20) * 20;
Pozicija = Kontrole.Stoj; //stop on start
}
private void Tajmer_Tick(object sender, EventArgs e)
{
if (Pozicija == Kontrole.Stoj)
{
//none
}
if (Pozicija == Kontrole.Desno)
{
x += 20;
}
else if (Pozicija == Kontrole.Levo)
{
x -= 20;
}
else if (Pozicija == Kontrole.Gore)
{
y -= 20;
}
else if (Pozicija == Kontrole.Dole)
{
y += 20;
}
Invalidate();
}
private void Zmijice_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Down)
{
Pozicija = Kontrole.Dole;
}
else if (e.KeyCode == Keys.Up)
{
Pozicija = Kontrole.Gore;
}
else if (e.KeyCode == Keys.Right)
{
Pozicija = Kontrole.Desno;
}
else if (e.KeyCode == Keys.Left)
{
Pozicija = Kontrole.Levo;
}
}
private void Zmijice_Paint(object sender, PaintEventArgs e)
{
int counter = 1;
//ako je pojela hranu
if (x==c && y==b)
{
counter++;
c = rnd.Next(1, 20); //nova pozicija hrrane
c = c * 20;
b = rnd.Next(1, 20);
b = b * 20;
//povecaj zmijicu
SegmentX.Add(x);
SegmentY.Add(y);
//label1.Text = SegmentX.Count().ToString() + " " + SegmentY.Count().ToString();
//left
if (Pozicija == Kontrole.Levo)
{
koox = x+20;
kooy = y;
}
//right
if (Pozicija == Kontrole.Desno)
{
koox = x-20;
kooy = y;
}
//up
if (Pozicija == Kontrole.Gore)
{
koox = x;
kooy = y+20;
}
//down
if (Pozicija == Kontrole.Dole)
{
koox = x;
kooy = y-20;
}
}
foreach (int k in SegmentX)
{
foreach (int l in SegmentY)
{
e.Graphics.FillRectangle(Brushes.Orange, koox, kooy, sirina, visina);
}
}
koox = x;
kooy = y;
e.Graphics.FillRectangle(Brushes.DarkRed, x, y, sirina, visina);
e.Graphics.FillRectangle(Brushes.Aqua, c, b, sirina, visina);
}
}
}