没有任何美工
我用了100行代码完成
有没有好的算法
可以让代码短点
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 贪吃蛇
{
public partial class Form1 : Form
{
int k;
char a = 's';
Point[] b = new Point[100];
int c = 0;
Point ai = new Point();
Point bi = new Point();
Random ran = new Random();
PictureBox a1= new PictureBox();
public Form1()
{
KeyPreview = true;
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{
} private void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
string key=e.KeyChar.ToString();
switch (key)
{
case "w": a = 'w'; break;
case "a": a = 'a'; break;
case "s": a = 's'; break;
case "d": a = 'd'; break;
}
} private void timer1_Tick(object sender, EventArgs e)
{
loli();//蛇的行动
switch (a)
{
case 'w': pictureBox1.Top -= 30; break;
case 'a': pictureBox1.Left -= 30; break;
case 's': pictureBox1.Top += 30; break;
case 'd': pictureBox1.Left += 30; break;
}
for (k = 0; k < c; k++)//定位到蛇的行动
foreach (PictureBox i in Controls)
if (i.Name == ("a"+(k + 1).ToString()))
{
i.Location = b[k];
}
lili();//给蛇增加长度
panduan();//判断胜负 }
void loli()
{
for (k = c - 1; k >0; k--)
{
b[k] = b[k - 1];
}
b[0] = pictureBox1.Location;
}
void lili()
{
if (pictureBox1.Location == pictureBox2.Location)
{
c++;
PictureBox a1 = new PictureBox();
ai=a1.Location = pictureBox1.Location;
a1.Size = pictureBox1.Size;
a1.BackColor = Color.Red;
a1.Name = "a" + c.ToString();
Controls.Add(a1);
pictureBox2.Location = new Point( ran.Next(10, 100)%10*30,ran.Next(10, 100)/10*30);
}
}
void panduan()
{
for(k=0;k<c;k++)
foreach (PictureBox i in Controls)
if (i.Location==pictureBox1.Location&&ai!=i.Location&&i.Name!="pictureBox1")
{
timer1.Enabled = false;
MessageBox.Show("游戏失败");
Close();
}
bi = pictureBox1.Location;
if (bi.X < 0 || bi.X > 320 || bi.Y < 0 || bi.Y > 290)
{
timer1.Enabled = false;
MessageBox.Show("游戏失败");
Close();
}
}
}
}
我用了100行代码完成
有没有好的算法
可以让代码短点
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 贪吃蛇
{
public partial class Form1 : Form
{
int k;
char a = 's';
Point[] b = new Point[100];
int c = 0;
Point ai = new Point();
Point bi = new Point();
Random ran = new Random();
PictureBox a1= new PictureBox();
public Form1()
{
KeyPreview = true;
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{
} private void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
string key=e.KeyChar.ToString();
switch (key)
{
case "w": a = 'w'; break;
case "a": a = 'a'; break;
case "s": a = 's'; break;
case "d": a = 'd'; break;
}
} private void timer1_Tick(object sender, EventArgs e)
{
loli();//蛇的行动
switch (a)
{
case 'w': pictureBox1.Top -= 30; break;
case 'a': pictureBox1.Left -= 30; break;
case 's': pictureBox1.Top += 30; break;
case 'd': pictureBox1.Left += 30; break;
}
for (k = 0; k < c; k++)//定位到蛇的行动
foreach (PictureBox i in Controls)
if (i.Name == ("a"+(k + 1).ToString()))
{
i.Location = b[k];
}
lili();//给蛇增加长度
panduan();//判断胜负 }
void loli()
{
for (k = c - 1; k >0; k--)
{
b[k] = b[k - 1];
}
b[0] = pictureBox1.Location;
}
void lili()
{
if (pictureBox1.Location == pictureBox2.Location)
{
c++;
PictureBox a1 = new PictureBox();
ai=a1.Location = pictureBox1.Location;
a1.Size = pictureBox1.Size;
a1.BackColor = Color.Red;
a1.Name = "a" + c.ToString();
Controls.Add(a1);
pictureBox2.Location = new Point( ran.Next(10, 100)%10*30,ran.Next(10, 100)/10*30);
}
}
void panduan()
{
for(k=0;k<c;k++)
foreach (PictureBox i in Controls)
if (i.Location==pictureBox1.Location&&ai!=i.Location&&i.Name!="pictureBox1")
{
timer1.Enabled = false;
MessageBox.Show("游戏失败");
Close();
}
bi = pictureBox1.Location;
if (bi.X < 0 || bi.X > 320 || bi.Y < 0 || bi.Y > 290)
{
timer1.Enabled = false;
MessageBox.Show("游戏失败");
Close();
}
}
}
}
我去构思一下算法 嘿嘿
等下我结贴
把分数平分