用C#写的贪吃蛇程序,移动的代码没写,谁能帮我写完。WinForm。
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 WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Graphics g;
public Pen p;
public Direction currentDirection = Direction.Up;
public int interval = 20;
public int x = 12;
public int y = 10;
public int snakeLong;
public Queue<Point> pointQueue = new Queue<Point>();
public int foodX = 10;
public int foodY = 4;
public int scores = 0;
public int speed = 1;
public bool eated = false;
public Form1()
{
InitializeComponent();
}
public enum Direction
{
Left=37,
Up,
Right,
Down
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
g = this.CreateGraphics();
p = new Pen(Color.Blue, 1);
for (int i = 0; i < 19; i++)
{
g.DrawLine(p, 10 + i * interval, 10, 10 + i * interval, 370);
g.DrawLine(p, 10, 10 + i * interval, 370, 10 + i * interval);
}
}
protected override bool ProcessDialogKey(Keys keyData)
{
if (keyData == Keys.Enter)
timer1.Start();
if (keyData == Keys.Space)
timer1.Stop();
int diff = Math.Abs((int)currentDirection - (int)keyData);
if (diff == 1 || diff == 3)
{
currentDirection = (Direction)keyData;
return true;
}
// return base.ProcessDialogKey(keyData);
return false;
}
private void button1_Click(object sender, EventArgs e)
{
this.numericUpDown1.Enabled = false;
this.numericUpDown2.Enabled = false;
this.button1.Enabled = false;
snakeLong = (int)numericUpDown1.Value;
speed = (int)numericUpDown2.Value;
CreateSnake();
timer1.Start();
}
public void CreateSnake()
{
x = 12;
y = 10;
scores = 0;
currentDirection = Direction.Up;
timer1.Interval = 1000 - (speed - 1) * 100;
pointQueue.Clear();
//label5.Text = scores.ToString();
g.FillRectangle(new SolidBrush(BackColor), 0, 0, 370, 370);
for (int i = 0; i < 19; i++)
{
g.DrawLine(p, 10 + i * interval, 10, 10 + i * interval, 370);
g.DrawLine(p, 10, 10 + i * interval, 370, 10 + i * interval);
}
SolidBrush b2 = new SolidBrush(Color.Red);
SolidBrush b1 = new SolidBrush(Color.Green);
SolidBrush b3 = new SolidBrush(Color.Gray);
//g.FillRectangle(b1, 11+foodX*interval,11+foodY*interval, 18, 18);
g.FillRectangle(b2, 11 + x * interval, 11 + y * interval, 18, 18);
pointQueue.Enqueue(new Point(x, y));
for (int i = 1; i <= snakeLong; i++)
{
g.FillRectangle(b3, 11 + x * interval - i * 20, 11 + y * interval, 18, 18);
this.pointQueue.Enqueue(new Point(x - i, y));
}
CreateFood();
}
public void CreateFood()
{
Point p;
eated = false;
bool foundBody;
Random r = new Random();
// label1.Text = "kjl";
int k = 0;
while (true)
{
foundBody = false;//******
foodX =r.Next(0,18);
foodY = r.Next(0, 18);
for (int j = 0; j < pointQueue.LongCount(); j++)
{
k++;
// label3.Text = k.ToString();
p = pointQueue.ElementAt(j);
//label1.Text = foodX.ToString() + "/" + p.X.ToString();
//label2.Text = foodY.ToString() + "/" + p.Y.ToString();
if (foodX == p.X && foodY == p.Y)
{ foundBody = true; break; }
}
if (!foundBody)
break;
}
g.FillRectangle(new SolidBrush(Color.Green), 11 + foodX * interval, 11 + foodY * interval, 18, 18);
}
private void numericUpDown1_ValueChanged(object sender, EventArgs e)
{
}
public void GameOver()
{
timer1.Stop();
MessageBox.Show("Game Over!");
this.numericUpDown1.Enabled = true;
this.numericUpDown2.Enabled = true;
this.button1.Enabled = true;
}
private void timer1_Tick(object sender, EventArgs e)
{
snakeMove();
}
public void snakeMove()
{
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
} --------------------编程问答-------------------- 就是这里
public void snakeMove()
{
} --------------------编程问答-------------------- 给多少钱呢? --------------------编程问答-------------------- 如果给分的话,可以帮写。。。
补充:.NET技术 , 其他语言