我編了一個貪吃蛇的游戲,代碼如下。編譯無錯誤,但運行後蛇不會動啊~我真的真的不知道問題出在哪兒。多謝各位大神指教~
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Timers;
namespace GreedySnake3
{
public partial class Form2 : Form
{
Snake snack1 = new Snake(4);
private System.Drawing.Color foodColor = System.Drawing.Color.Red;
Bean food = new Bean();
bool play = false;
bool foodDraw = false;
private void Form2_Load(object sender, EventArgs e)
{
pictureBox1.Paint += pictureBox1_Paint;
timer1.Tick+=timer1_Tick;
}
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
play = true;
this.timer1.Enabled = true;
this.timer1.Interval = 500;
}
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
DrawGameFrame(e.Graphics);
snack1.DrawSnack(e.Graphics);
if (foodDraw == false)
{
food.GetFoodPoint();
foodDraw = true;
}
food.DrawFood(e.Graphics);
if (play)
{
snack1.SnackMoce(e.Graphics);
}
if (this.timer1.Enabled == true)
{
this.button1.Focus();
}
}
private void timer1_Tick(object sender, EventArgs e)
{
if (play)
{
snack1.Addsnack();
}
if (snack1.HeadPoint == food.Origin)
{
foodDraw = false;
snack1.Addsnack();
}
if (snack1.Diedif())
{
timer1.Enabled = false;
play = false;
MessageBox.Show("Game Over!");
}
}
public void DrawGameFrame(Graphics g)
{
for (int i = 0; i < this.pictureBox1.Width; i += 10)
for (int j = 0; j < this.pictureBox1.Height; j += 10)
{
g.FillEllipse(Brushes.LightGreen, i, j, 10, 10);
}
}
private void button1_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.A && snack1.Direction != 1)
snack1.Direction = 2;
else if (e.KeyCode == Keys.D && snack1.Direction != 2)
snack1.Direction = 1;
else if (e.KeyCode == Keys.W && snack1.Direction != 4)
snack1.Direction = 3;
else if (e.KeyCode == Keys.S && snack1.Direction != 3)
snack1.Direction = 4;
}
private void button2_Click(object sender, EventArgs e)
{
this.Close();
}
}
public class Bean
{
Point origin=new Point(100,100);
public Point Origin
{
get { return origin; }
set { origin = value; }
}
public void GetFoodPoint()
{
Random random = new Random();
int x = random.Next(1, 40)*10;
int y = random.Next(1, 30)*10;
origin = new Point(x, y);
}
public void DrawFood(Graphics g)
{
g.FillEllipse(Brushes.Red, origin.X, origin.Y, 10, 10);
}
}
public class Snake
{
public Point startPoint = new Point(0, 0);
Point addPoint;
public ArrayList snackPoint = new ArrayList();
Point headPoint;
public Point HeadPoint
{
get { return headPoint; }
set { headPoint = value; }
}
int direction = 1;
public int Direction
{
get { return direction; }
set { direction = value; }
}
public Snake(int lenth)
{
for (int i = 0; i < lenth; i++)
{
snackPoint.Add(startPoint);
if (i == lenth - 1)
{
HeadPoint = startPoint;
return;
}
startPoint = new Point(startPoint.X + 10, startPoint.Y);
}
}
public void DrawSnack(Graphics g)
{
for (int i = 0; i < snackPoint.Count; i++)
{
g.FillEllipse(Brushes.Blue, ((Point)snackPoint[i]).X, ((Point)snackPoint[i]).Y, 10, 10);
}
}
public void Addsnack()
{
if (direction == 1)
addPoint = new Point(headPoint.X + 10, headPoint.Y);
else if (direction == 2)
addPoint = new Point(headPoint.X - 10, headPoint.Y);
else if (direction == 3)
addPoint = new Point(headPoint.X, headPoint.Y - 10);
else if (direction == 4)
addPoint = new Point(headPoint.X, headPoint.Y + 10);
snackPoint.Add(addPoint);
headPoint = addPoint;
}
public void RemoveSnackNode()
{
snackPoint.RemoveAt(0);
}
public void SnackMoce(Graphics g)
{
DrawSnack(g);
RemoveSnackNode();
}
public bool Diedif()
{
for (int i = 0; i < snackPoint.Count - 1; i++)
{
if ((Point)snackPoint[i] == headPoint)
return true;
}
if (headPoint.X < 0 || headPoint.X > 400 || headPoint.Y < 0 || headPoint.Y > 300)
return true;
return false;
}
}
}
pictureBox1 的paint 事件不會時時觸發 在timer1_tick中添加 pictureBox1.Refresh(); 讓它定時觸發paint 重新繪制自己
private void timer1_Tick(object sender, EventArgs e)
{
if (play)
{
pictureBox1.Refresh();
snack1.Addsnack();
}
if (snack1.HeadPoint == food.Origin)
{
pictureBox1.Refresh();
foodDraw = false;
snack1.Addsnack();
}
if (snack1.Diedif())
{
pictureBox1.Refresh();
timer1.Enabled = false;
play = false;
MessageBox.Show("Game Over!");
}
}