codice:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Forms;
namespace TurboSnake
{
public partial class gamefrm : Form
{
public gamefrm()
{
InitializeComponent();
}
private Random gen = new Random();
private int punteggio=0,livello=1,bonus=0,vite=3,xmela,ymela,xserp,yserp;
private List<Point> ciccio = new List<Point>();
private List<PictureBox> corpo=new List<PictureBox>();
private void gamefrm_Load(object sender, EventArgs e)
{
serpenteimg.Location = new Point(10, 10);
punteggiolbl.Text = Convert.ToString(punteggio);
livellolbl.Text = Convert.ToString(livello);
vitelbl.Text = Convert.ToString(vite);
do
{
xmela = gen.Next(64 - 1) * 10;//genero posiione iniz. mela
ymela = gen.Next(47 - 1) * 10;
} while (ymela == serpenteimg.Location.Y && xmela == serpenteimg.Location.X);
melaimg.Location = new Point(xmela, ymela);
ciccio.Add(new Point(5,5));//aggiunta la testa
ciccio.Add(new Point(5,8));//aggiunto corpo
ciccio.Add(new Point(5,11));//aggiunto corpo
ciccio.Add(new Point(5,14));//aggiunta coda
for (int i = 0; i < ciccio.Count; i++)
{
PictureBox box=new PictureBox();// creo le immagini per il snake iniziali
corpo.Add(box);
}
disegna();
}
private bool su = false, giu = false, dx = false, sx = false;
private void gamefrm_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Up)//intercetto tasto su
{
timer2.Enabled = false;
timer3.Enabled = false;
timer4.Enabled = false;
timer1.Interval = 30;
timer1.Enabled = true;
if (su == false)
{
timer1.Tick += new EventHandler(timer1_Tick);
su = true;
}
}
if (e.KeyCode == Keys.Down)//intercetto tasto giu
{
timer1.Enabled = false;
timer3.Enabled = false;
timer4.Enabled = false;
timer2.Interval =30;
timer2.Enabled = true;
if (giu == false)
{
giu = true;
timer2.Tick += new EventHandler(timer2_Tick);
}
}
if (e.KeyCode == Keys.Left)//intercetto tasto sin
{
timer1.Enabled = false;
timer2.Enabled = false;
timer4.Enabled = false;
timer3.Interval =30;
timer3.Enabled = true;
if (sx == false)
{
sx = true;
timer3.Tick += new EventHandler(timer3_Tick);
}
}
if (e.KeyCode == Keys.Right)//intercetto tasto destr
{
timer1.Enabled = false;
timer2.Enabled = false;
timer3.Enabled = false;
timer4.Interval =30;
timer4.Enabled = true;
if (dx == false)
{
dx = true;
timer4.Tick += new EventHandler(timer4_Tick);
}
}
}
private void timer1_Tick(object sender, EventArgs e)
{
xserp=ciccio[0].X;yserp=ciccio[0].Y;
yserp-= 3;
ciccio.Insert(0, new Point(xserp, yserp));//movimento
ciccio.RemoveAt(ciccio.Count-1);
disegna();
mela_mang();
}
private void timer2_Tick(object sender, EventArgs e)
{
xserp=ciccio[0].X;yserp = ciccio[0].Y;
yserp += 3;
ciccio.Insert(0, new Point(xserp, yserp));//movimento
ciccio.RemoveAt(ciccio.Count - 1);
disegna();
mela_mang();
}
private void timer3_Tick(object sender, EventArgs e)
{
xserp = ciccio[0].X;yserp= ciccio[0].Y;
xserp -= 3;
ciccio.Insert(0, new Point(xserp, yserp));//movimento
ciccio.RemoveAt(ciccio.Count - 1);
disegna();
mela_mang();
}
private void timer4_Tick(object sender, EventArgs e)
{
xserp = ciccio[0].X; yserp=ciccio[0].Y;
xserp += 3;
ciccio.Insert(0, new Point(xserp, yserp));//movimento
ciccio.RemoveAt(ciccio.Count - 1);
disegna();
mela_mang();
}
private void aggiungi()
{
PictureBox box = new PictureBox();
corpo.Add(box);
}
private void disegna()//assegno ogni punto un immagine
{
for(int i=0;i<ciccio.Count;i++)
{
corpo[i].Location=ciccio[i];
corpo[i].Size=new Size(20,20);
corpo[i].BackColor = Color.LawnGreen;
corpo[i].Visible = true;
Controls.Add(corpo[i]);
corpo[i].BringToFront();
}
}
private void mela_mang()
{
if(melaimg.Bounds.IntersectsWith(corpo[0].Bounds))// se mangia mela
{
punteggio += 5;
punteggiolbl.Text = Convert.ToString(punteggio);
xmela = gen.Next(10, terrenopnl.Width - 20);
ymela = gen.Next(10, terrenopnl.Height - 20);
melaimg.Location = new Point(xmela, ymela);
Application.DoEvents();// refresh form mela
incrementa_serp();
if (punteggio % 20 == 0)
livello++;
livellolbl.Text = Convert.ToString(livello);
}
}
private void incrementa_serp()
{
if (su == true)
{
xserp = ciccio[ciccio.Count - 1].X; yserp = ciccio[ciccio.Count - 1].Y;
ciccio.Add(new Point(xserp, yserp));//aggiunga 1# tassello
xserp = ciccio[0].X; yserp = ciccio[0].Y;
yserp += 3;
ciccio.Add(new Point(xserp, yserp));
ciccio.RemoveAt(ciccio.Count - 1);//spostamento
aggiungi();
disegna();
xserp = ciccio[ciccio.Count - 1].X; yserp = ciccio[ciccio.Count - 1].Y;
ciccio.Add(new Point(xserp, yserp));//aggiunga 2# tassello
xserp = ciccio[0].X; yserp = ciccio[0].Y;
yserp += 3;
ciccio.Add(new Point(xserp, yserp));
ciccio.RemoveAt(ciccio.Count - 1);//spostamento
aggiungi();
disegna();
xserp = ciccio[ciccio.Count - 1].X; yserp = ciccio[ciccio.Count - 1].Y;
ciccio.Add(new Point(xserp, yserp));//aggiunga 3# tassello
xserp = ciccio[0].X; yserp = ciccio[0].Y;
yserp += 3;
ciccio.Add(new Point(xserp, yserp));
ciccio.RemoveAt(ciccio.Count - 1);//spostamento
aggiungi();
disegna();
}
if (giu == true)
{
xserp = ciccio[ciccio.Count - 1].X; yserp = ciccio[ciccio.Count - 1].Y;
ciccio.Add(new Point(xserp, yserp));//aggiunga 1# tassello
xserp = ciccio[0].X; yserp = ciccio[0].Y;
yserp -= 3;
ciccio.Add(new Point(xserp, yserp));
ciccio.RemoveAt(ciccio.Count - 1);//spostamento
aggiungi();
disegna();
xserp = ciccio[ciccio.Count - 1].X; yserp = ciccio[ciccio.Count - 1].Y;
ciccio.Add(new Point(xserp, yserp));//aggiunga 2# tassello
xserp = ciccio[0].X; yserp = ciccio[0].Y;
yserp -= 3;
ciccio.Add(new Point(xserp, yserp));
ciccio.RemoveAt(ciccio.Count - 1);//spostamento
aggiungi();
disegna();
xserp = ciccio[ciccio.Count - 1].X; yserp = ciccio[ciccio.Count - 1].Y;
ciccio.Add(new Point(xserp, yserp));//aggiunga 3# tassello
xserp = ciccio[0].X; yserp = ciccio[0].Y;
yserp -= 3;
ciccio.Add(new Point(xserp, yserp));
ciccio.RemoveAt(ciccio.Count - 1);//spostamento
aggiungi();
disegna();
}
if (dx == true)
{
xserp = ciccio[ciccio.Count - 1].X; yserp = ciccio[ciccio.Count - 1].Y;
ciccio.Add(new Point(xserp, yserp));//aggiunga 1# tassello
xserp = ciccio[0].X; yserp = ciccio[0].Y;
xserp += 3;
ciccio.Add(new Point(xserp, yserp));
ciccio.RemoveAt(ciccio.Count - 1);//spostamento
aggiungi();
disegna();
xserp = ciccio[ciccio.Count - 1].X; yserp = ciccio[ciccio.Count - 1].Y;
ciccio.Add(new Point(xserp, yserp));//aggiunga 1# tassello
xserp = ciccio[0].X; yserp = ciccio[0].Y;
xserp += 3;
ciccio.Add(new Point(xserp, yserp));
ciccio.RemoveAt(ciccio.Count - 1);//spostamento
aggiungi();
disegna();
xserp = ciccio[ciccio.Count - 1].X; yserp = ciccio[ciccio.Count - 1].Y;
ciccio.Add(new Point(xserp, yserp));//aggiunga 1# tassello
xserp = ciccio[0].X; yserp = ciccio[0].Y;
xserp += 3;
ciccio.Add(new Point(xserp, yserp));
ciccio.RemoveAt(ciccio.Count - 1);//spostamento
aggiungi();
disegna();
}
if (sx == true)
{
xserp = ciccio[ciccio.Count - 1].X; yserp = ciccio[ciccio.Count - 1].Y;
ciccio.Add(new Point(xserp, yserp));//aggiunga 1# tassello
xserp = ciccio[0].X; yserp = ciccio[0].Y;
xserp -= 3;
ciccio.Add(new Point(xserp, yserp));
ciccio.RemoveAt(ciccio.Count - 1);//spostamento
aggiungi();
disegna();
xserp = ciccio[ciccio.Count - 1].X; yserp = ciccio[ciccio.Count - 1].Y;
ciccio.Add(new Point(xserp, yserp));//aggiunga 1# tassello
xserp = ciccio[0].X; yserp = ciccio[0].Y;
xserp -= 3;
ciccio.Add(new Point(xserp, yserp));
ciccio.RemoveAt(ciccio.Count - 1);//spostamento
aggiungi();
disegna();
xserp = ciccio[ciccio.Count - 1].X; yserp = ciccio[ciccio.Count - 1].Y;
ciccio.Add(new Point(xserp, yserp));//aggiunga 1# tassello
xserp = ciccio[0].X; yserp = ciccio[0].Y;
xserp -= 3;
ciccio.Add(new Point(xserp, yserp));
ciccio.RemoveAt(ciccio.Count - 1);//spostamento
aggiungi();
disegna();
}
}
private void melaimg_Click(object sender, EventArgs e)
{
}
private void terrenopnl_Paint(object sender, PaintEventArgs e)
{
}
}
}
Ecco qui il codice...