在以下程序的基础上实现:
5.根据用户的要求设置背景、蛇和豆子的颜色
哪位高手帮忙啊!我要交了阿!谢谢阿!急啊
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace snake
{
enum Direction {east,west,north,south};
public class Form1 : System.Windows.Forms.Form{ /// 必需的设计器变量。
/// </summary>
Size s;
ArrayList snake;
Brush b_p;
Brush b_s;
Brush b_sh;
Brush b_b;
Point bean;//豆子
int speed;
Keys direction;
int acc_speed
{
get
{
return speed;
}
set
{if(value>=0&&value<9)
speed=value;
}
}
Direction dr;
private System.Windows.Forms.Timer timer1;
private System.ComponentModel.IContainer components;
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
s=new Size(29,29);
Initialize_snake();
b_p=new SolidBrush(Color.Pink );
b_s=new SolidBrush(Color.BlueViolet);
b_sh=new SolidBrush(Color.Yellow );
b_b=new SolidBrush(Color.Black );
bean=new Point();
Generate_bean();
acc_speed=0;
direction=Keys.Right;
}
private void Initialize_snake()
{
snake = new ArrayList();
snake.Add(new Point(0, 0));
// snake.Add(new Point(0, 30));
// snake.Add(new Point(0, 60));
// snake.Add(new Point(0, 90));
// snake.Add(new Point(0, 120));
}
private void Generate_bean()
{
Random r=new Random();
do
{
int x=r.Next(10);//随机生成大于等于0小于10的整数
int y=r.Next(10);
bean.X=x*30;
bean.Y=y*30;
}while(point_in_snake(bean));//判断豆子在不在蛇上
}
private bool point_in_snake(Point p)//p——豆子的点坐标
{
for(int i=0;i<snake.Count;i++)
{
if(p.Equals(snake[i]))
return true;
}
return false;
}
private void draw_bean(Graphics g)
{
draw_rec(bean,g,b_b);
}
/// <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.components = new System.ComponentModel.Container();
this.timer1 = new System.Windows.Forms.Timer(this.components);
//
// timer1
//
this.timer1.Enabled = true;
this.timer1.Interval = 1000;
this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.BackColor = System.Drawing.SystemColors.WindowText;
this.ClientSize = new System.Drawing.Size(296, 295);
this.MaximizeBox = false;
this.MaximumSize = new System.Drawing.Size(304, 330);
this.Name = "Form1";
this.Text = "Form1";
this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.Form1_KeyDown);
5.根据用户的要求设置背景、蛇和豆子的颜色
哪位高手帮忙啊!我要交了阿!谢谢阿!急啊
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace snake
{
enum Direction {east,west,north,south};
public class Form1 : System.Windows.Forms.Form{ /// 必需的设计器变量。
/// </summary>
Size s;
ArrayList snake;
Brush b_p;
Brush b_s;
Brush b_sh;
Brush b_b;
Point bean;//豆子
int speed;
Keys direction;
int acc_speed
{
get
{
return speed;
}
set
{if(value>=0&&value<9)
speed=value;
}
}
Direction dr;
private System.Windows.Forms.Timer timer1;
private System.ComponentModel.IContainer components;
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
s=new Size(29,29);
Initialize_snake();
b_p=new SolidBrush(Color.Pink );
b_s=new SolidBrush(Color.BlueViolet);
b_sh=new SolidBrush(Color.Yellow );
b_b=new SolidBrush(Color.Black );
bean=new Point();
Generate_bean();
acc_speed=0;
direction=Keys.Right;
}
private void Initialize_snake()
{
snake = new ArrayList();
snake.Add(new Point(0, 0));
// snake.Add(new Point(0, 30));
// snake.Add(new Point(0, 60));
// snake.Add(new Point(0, 90));
// snake.Add(new Point(0, 120));
}
private void Generate_bean()
{
Random r=new Random();
do
{
int x=r.Next(10);//随机生成大于等于0小于10的整数
int y=r.Next(10);
bean.X=x*30;
bean.Y=y*30;
}while(point_in_snake(bean));//判断豆子在不在蛇上
}
private bool point_in_snake(Point p)//p——豆子的点坐标
{
for(int i=0;i<snake.Count;i++)
{
if(p.Equals(snake[i]))
return true;
}
return false;
}
private void draw_bean(Graphics g)
{
draw_rec(bean,g,b_b);
}
/// <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.components = new System.ComponentModel.Container();
this.timer1 = new System.Windows.Forms.Timer(this.components);
//
// timer1
//
this.timer1.Enabled = true;
this.timer1.Interval = 1000;
this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.BackColor = System.Drawing.SystemColors.WindowText;
this.ClientSize = new System.Drawing.Size(296, 295);
this.MaximizeBox = false;
this.MaximumSize = new System.Drawing.Size(304, 330);
this.Name = "Form1";
this.Text = "Form1";
this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.Form1_KeyDown);