问题描述
- 程序媛小白求助:C#贪吃蛇精灵游戏
-
我编了一个贪吃蛇的游戏,代码如下。编译无错误,但运行后蛇不会动啊~我真的真的不知道问题出在哪儿。多谢各位大神指教~
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!");
}
}
解决方案二:
你的代码是直接复制粘贴的么?选中你的PictureBox1,在属性窗格里点闪电按钮,看Paint事件关联给pictureBox1_Paint了没有。
解决方案三:
你的timer好像没有start
时间: 2024-09-11 21:46:56