Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Snake Game: I just need to know what these blocks of codes do XD

$
0
0
Hi we're having an upcoming test at school about program tracing
and i tried practicing on this code but i'm really confused about what most of the codes do
please help?

i attatched the file and its fully running * 7 *

all i need are comment guides in return maybe i can draw something for you?
here's my gallery frostvandals.deviantart.com

thanks in advance!

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace snake
{
	/// <summary>
	/// Form1 的摘要说明。
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{
		/// <summary>
		/// 必需的设计器变量。
		/// </summary>
		private System.ComponentModel.Container components = null;
		private snake Tom;
		private System.Windows.Forms.MainMenu mainMenu1;
		private System.Windows.Forms.MenuItem menuItem1;
		private Timer timer;

		public Form1()
		{
			//
			// Windows 窗体设计器支持所必需的
			//


			InitializeComponent();


			//
			// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
			//
		}

		/// <summary>
		/// 清理所有正在使用的资源。
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows 窗体设计器生成的代码
		/// <summary>
		/// 设计器支持所需的方法 - 不要使用代码编辑器修改
		/// 此方法的内容。
		/// </summary>
		private void InitializeComponent()
		{
			this.mainMenu1 = new System.Windows.Forms.MainMenu();
			this.menuItem1 = new System.Windows.Forms.MenuItem();
			// 
			// mainMenu1
			// 
			this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																					  this.menuItem1});
			// 
			// menuItem1
			// 
			this.menuItem1.Index = 0;
			this.menuItem1.Text = "HELP";
			this.menuItem1.Click += new System.EventHandler(this.menuItem1_Click);
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
			this.ClientSize = new System.Drawing.Size(360, 350);
			this.Menu = this.mainMenu1;
			this.Name = "Form1";
			this.Text = "Form1";
			this.Load += new System.EventHandler(this.Form1_Load);

		}
		#endregion

		/// <summary>
		/// 应用程序的主入口点。
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new Nform());
			Application.Run(new Form1());
		}

		private void Form1_Load(object sender, System.EventArgs e)
		{
			this.Text="SNAKE!";
			this.Size=new Size(500,320);
			this.FormBorderStyle=FormBorderStyle.FixedToolWindow;
			this.timer=new Timer();
			this.Tom=new snake(this.ClientRectangle,timer);			
			this.timer.Interval=snake.Speed;
			this.timer.Tick+=new EventHandler(timer_Tick);
			this.timer.Start();
			
		}

		private void timer_Tick(object sender,EventArgs e)
		{
			Graphics g=this.CreateGraphics();
			g.Clear(Color.White);
			this.drawsnake();
			this.Tom.move();
		}

		private void drawsnake()
		{
			Graphics g=this.CreateGraphics();
			for(int i=0;i<this.Tom.Now;i++)
			{
				g.FillRectangle(Brushes.Red,new Rectangle((Point)(this.Tom.walls[i]),this.Tom.Wl));
			}

			for(int i=0;i<this.Tom.nuts.Count;i++)
			{
				g.FillRectangle(Brushes.Green,new Rectangle((Point)(this.Tom.nuts[i]),this.Tom.Ssl));
			}

			for(int i=0;i<this.Tom.Cart.Count-1;i++)
			{
				Point it=(Point)this.Tom.Cart[i];
				g.FillRectangle(new SolidBrush(this.Tom.Bodyc),new Rectangle(it,this.Tom.Ssl));
			}

			g.DrawRectangle(new Pen(new SolidBrush(this.Tom.Headc),2),new Rectangle((Point)(this.Tom.Cart[Tom.Cart.Count-1]),this.Tom.Ssl));
			
			string state;
			if(this.Tom.Moving)
				state="暂停";
			else
				state="开始";
			this.Text=this.Tom.Cart.Count.ToString();
		}

		protected override void onkeydown(KeyEventArgs e)
		{
			int a=(int)(e.KeyCode);
			int b=(int)(this.Tom.Direction);
			if(a>=37&&a<=40&&this.Tom.Moving)
			{
				if(Math.Abs(a-B)/>!=2)
				{
					this.Tom.Direction=(Way)a;
					this.Tom.move();
					this.drawsnake();
				}
			}

			if(a==(int)Keys.Space)
			{
				if(this.timer.Enabled)
				{
					this.timer.Stop();
					this.Tom.Moving=false;
				}
				else
				{
					this.timer.Start();
					this.Tom.Moving=true;
				}
			}

			if(a==(int)Keys.Z&&this.timer.Interval>20)
				this.timer.Interval-=20;
			if(a==(int)Keys.X)
				this.timer.Interval+=20;
			if(a==(int)Keys.Escape)
				this.Close();

		}

		private void trackBar1_ValueChanged(object sender, System.EventArgs e)
		{
		
		}

		private void menuItem1_Click(object sender, System.EventArgs e)
		{
			new help().Show();
		}
	}

	public class Nform:Form
	{
		private TrackBar speed;
		private Button ok;
		private Label l1;

		public Nform()
		{
			this.Text="SNAKE!";
			this.FormBorderStyle=FormBorderStyle.FixedToolWindow;

			this.Size=new Size(300,128);
			this.speed=new TrackBar();
			this.speed.Location=new Point(30,(this.ClientRectangle.Height-this.speed.Height)/2);
			this.speed.Maximum=5;
			this.speed.Minimum=1;
			this.speed.ValueChanged+=new EventHandler(sp_vc) ;

			this.ok=new Button();
			this.ok.Text="GO";
			this.ok.Location=new Point(152,this.ClientRectangle.Height/2+this.speed.Height/2);
			this.ok.Click+=new EventHandler(ok_Click);

			this.l1=new Label();
			this.l1.Location=new Point(152,36);
			this.l1.Text="速度: 慢";

			this.Controls.Add(this.l1);
			this.Controls.Add(this.ok);
			this.Controls.Add(this.speed);
		}

		private void ok_Click(object sender,EventArgs e)
		{
			snake.Speed=300/this.speed.Value;

			if(this.speed.Value==4)
				snake.Speed=45;

			if(this.speed.Value==5)
				snake.Speed=10;
			this.Close();
		}

		private void sp_vc(object sender,EventArgs e)
		{
			switch(this.speed.Value)
			{
				case 1:
					this.l1.Text="速度: 慢";
					break;
				case 2:
					this.l1.Text="速度: 中";
					break;
				case 3:
					this.l1.Text="速度: 快";
					break;
				case 4:
					this.l1.Text="速度: 很快";
					break;
				case 5:
					this.l1.Text="速度: 死亡速度";
					break;
			}
		}

		protected override void OnPaint(PaintEventArgs e)
		{
			Graphics g=e.Graphics;
			g.DrawString("made by Kamv",new Font("Century Gothic",16),Brushes.Black,0,0);
			g.DrawString("made by Kamv",new Font("Century Gothic",16),Brushes.White,2,0);
		}


	}
}



ah here's the zip file  snake_game.zip (64.96K)
: 4

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>