贪食虫贪食蛇.docx
- 文档编号:24542629
- 上传时间:2023-05-28
- 格式:DOCX
- 页数:27
- 大小:29.53KB
贪食虫贪食蛇.docx
《贪食虫贪食蛇.docx》由会员分享,可在线阅读,更多相关《贪食虫贪食蛇.docx(27页珍藏版)》请在冰豆网上搜索。
贪食虫贪食蛇
Form1.cs
usingSystem;
usingSystem.Collections.Generic;
usingSystem.ComponentModel;
usingSystem.Data;
usingSystem.Drawing;
usingSystem.Text;
usingSystem.Windows.Forms;
usingSystem.Collections;
usingSystem.IO;
namespacetanshichong
{
publicpartialclassForm1:
Form
{
Buttonaddbutton;
List
intscore=0;
intaddscore=0;
staticstringfileurl="D:
\\tanshichong.txt";
staticintlength=20;
staticintlength_num=500/length-1;
staticintspeed=10;
staticintevery_score=1;
publicForm1()
{
InitializeComponent();
}
privatevoidisHaveFile()
{
intdate=0;
if(!
File.Exists(fileurl))
{
FileStreamfs1=newFileStream(fileurl,FileMode.Create,FileAccess.Write);//创建写入文件
StreamWritersw=newStreamWriter(fs1);
sw.WriteLine("0");//开始写入值
sw.Close();
fs1.Close();
}
else
{
StreamReadersr=newStreamReader("D:
\\tanshichong.txt");
date=Convert.ToInt32(sr.ReadLine());
sr.Close();
}
label5.Text=date.ToString();
}
privatevoidaddButton()
{
addbutton=newButton();
this.Controls.Add(addbutton);
Randomrandom=newRandom();
inttemp_x=random.Next(0,length_num)*length+25;
inttemp_y=random.Next(0,length_num)*length+25;
for(inti=0;i { if(temp_x==buttons[i].Location.X&&temp_y==buttons[i].Location.Y) { temp_x=random.Next(0,length_num)*length+25; temp_y=random.Next(0,length_num)*length+25; i=-1; } } addbutton.Location=newPoint(temp_x,temp_y); addbutton.Size=newSize(length,length); addbutton.Visible=true; addbutton.Text=""; label1.Text=score.ToString(); } privatevoidbutton2_Click(objectsender,EventArgse) { for(inti=1;i { this.Controls.Remove(buttons[i]); } buttons.Clear(); button1.Visible=true; button1.Location=newPoint(length*3+25,length*3+25); buttons.Add(button1); timer1.Start(); timer5.Start(); addButton(); score=0; label1.Text=score.ToString(); if(radioButton1.Checked==true) { addscore=10*every_score; timer1.Interval=5*speed; timer2.Interval=5*speed; timer3.Interval=5*speed; timer4.Interval=5*speed; timer5.Interval=5*speed; } elseif(radioButton2.Checked==true) { addscore=3*every_score; timer1.Interval=10*speed; timer2.Interval=10*speed; timer3.Interval=10*speed; timer4.Interval=10*speed; timer5.Interval=10*speed; } elseif(radioButton3.Checked==true) { addscore=1*every_score; timer1.Interval=20*speed; timer2.Interval=20*speed; timer3.Interval=20*speed; timer4.Interval=20*speed; timer5.Interval=20*speed; } elseif(radioButton4.Checked==true) { addscore=20*every_score; timer1.Interval=3*speed; timer2.Interval=3*speed; timer3.Interval=3*speed; timer4.Interval=3*speed; timer5.Interval=3*speed; } elseif(radioButton5.Checked==true) { addscore=5*every_score; timer1.Interval=7*speed; timer2.Interval=7*speed; timer3.Interval=7*speed; timer4.Interval=7*speed; timer5.Interval=7*speed; } button2.Enabled=false; } privatevoidsetbuttons() { for(inti=buttons.Count-1;i>0;i--) { buttons[i].Location=newPoint(buttons[i-1].Location.X,buttons[i-1].Location.Y); } } privatevoidend() { if(score>Convert.ToInt32(label5.Text)) { label3.Text="游戏结束,刷新最高分! "; label5.Text=score.ToString(); StreamWritersw=newStreamWriter(fileurl); sw.Write(score.ToString()); sw.Flush(); sw.Close(); } else { label3.Text="游戏结束! "; } this.Controls.Remove(addbutton); button2.Text="重新开始? "; button2.Enabled=true; } privatevoidtimer1_Tick(objectsender,EventArgse) { setbuttons(); button1.Left=button1.Left+length; if(button1.Left+length>525) { timer1.Stop(); end(); } for(inti=1;i { if(button1.Left+length==buttons[i].Left&&button1.Top==buttons[i].Top) { timer1.Stop(); end(); break; } } } privatevoidtimer2_Tick(objectsender,EventArgse) { setbuttons(); button1.Left=button1.Left-length; if(button1.Left<25) { timer2.Stop(); end(); } for(inti=1;i { if(button1.Left==buttons[i].Left+length&&button1.Top==buttons[i].Top) { timer2.Stop(); end(); break; } } } privatevoidtimer3_Tick(objectsender,EventArgse) { setbuttons(); button1.Top=button1.Top+length; if(button1.Top+length>525) { timer3.Stop(); end(); } for(inti=1;i { if(button1.Top+length==buttons[i].Top&&button1.Left==buttons[i].Left) { timer3.Stop(); end(); break; } } } privatevoidtimer4_Tick(objectsender,EventArgse) { setbuttons(); button1.Top=button1.Top-length; if(button1.Top<25) { timer4.Stop(); end(); } for(inti=1;i { if(button1.Top==buttons[i].Top+length&&button1.Left==buttons[i].Left) { timer4.Stop(); end(); break; } } } privatevoidForm1_KeyDown(objectsender,KeyEventArgse) { if(timer1.Enabled==true||timer2.Enabled==true||timer3.Enabled==true||timer4.Enabled==true) { if(e.KeyValue==65) {//左Á¨® if(timer1.Enabled! =true) { timer2.Start(); timer1.Stop(); timer3.Stop(); timer4.Stop(); } } elseif(e.KeyValue==87) {//上¦? if(timer3.Enabled! =true) { timer2.Stop(); timer1.Stop(); timer3.Stop(); timer4.Start(); } } elseif(e.KeyValue==68) {//右®¨° if(timer2.Enabled! =true) { timer2.Stop(); timer1.Start(); timer3.Stop(); timer4.Stop(); } } elseif(e.KeyValue==83) {//下? if(timer4.Enabled! =true) { timer2.Stop(); timer1.Stop(); timer3.Start(); timer4.Stop(); } } } } privatevoidgetscore() { inttemp_x=addbutton.Location.X; inttemp_y=addbutton.Location.Y; buttons.Add(addbutton); for(inti=buttons.Count-1;i>0;i--) { buttons[i].Location=newPoint(buttons[i-1].Location.X,buttons[i-1].Location.Y); } buttons[0].Location=newPoint(temp_x,temp_y); //this.Controls.Remove(addbutton); addButton(); } privatevoidtimer5_Tick(objectsender,EventArgse) { intx=button1.Location.X; inty=button1.Location.Y; if(timer1.Enabled==true) {//向右 if(x==addbutton.Location.X-length&&y==addbutton.Location.Y) { getscore(); score+=addscore; label1.Text=score.ToString(); } } elseif(timer2.Enabled==true) {//向左 if(x==addbutton.Location.X+length&&y==addbutton.Location.Y) { getscore(); score+=addscore; label1.Text=score.ToString(); } } elseif(timer3.Enabled==true) {//向下 if(x==addbutton.Location.X&&y==addbutton.Location.Y-length) { getscore(); score+=addscore; label1.Text=score.ToString(); } } elseif(timer4.Enabled==true) {//向上 if(x==addbutton.Location.X&&y==addbutton.Location.Y+length) { getscore(); score+=addscore; label1.Text=score.ToString(); } } } privatevoidForm1_Load(objectsender,EventArgse) { isHaveFile(); button1.Size=newSize(length,length); } } } Form1.Designer.cs namespacetanshichong { partialclassForm1 { /// ///必À? 需¨¨的Ì? 设¦¨¨计? 器¡Â变À? 量¢? 。 ¡ê /// privateSystem.ComponentModel.IContainercomponents=null; /// ///清? 理¤¨ª所¨´有®D正y在¨²使º1用®? 的Ì? 资Á¨º源¡ä。 ¡ê /// /// 果? 应®|释º¨ª放¤? 托ªD管¨¹资Á¨º源¡ä,ê? 为atrue;ê? 否¤? 则¨°为afalse。 ¡ê protectedoverridevoidDispose(booldisposing) { if(disposing&&(components! =null)) { components.Dispose(); } base.Dispose(disposing); } #regionWindows窗ä¡ã体¬? 设¦¨¨计? 器¡Â生¦¨²成¨¦的Ì? 代䨲码? /// ///设¦¨¨计? 器¡Â支¡ì持? 所¨´需¨¨的Ì? 方¤? 法¤¡§-不? 要°a ///使º1用®? 代䨲码? 编À¨¤辑-器¡Â修T改? 此ä? 方¤? 法¤¡§的Ì? 内¨²容¨Y。 ¡ê /// privatevoidInitializeComponent() { ponents=newSystem.ComponentModel.Container(); this.button1=newSystem.Windows.Forms.Button(); this.button2=newSystem.Windows.Forms.Button(); this.timer1=newSystem.Windows.Forms.Timer(ponents); this.timer2=newSystem.Windows.Forms.Timer(ponents); this.timer3=newSystem.Windows.Forms.Timer(ponents); this.timer4=newSystem.Windows.Forms.Timer(ponents); this.timer5=newSystem.Windows.Forms.Timer(ponents); this.label1=newSystem.Windows.Forms.Label(); this.label2=newSystem.Windows.Forms.Label(); this.label3=newSystem.Windows.Forms.Label(); this.label4=newSystem.Windows.Forms.Label(); this.label5=newSystem.Windows.Forms.Label(); this.radioButton1=newSystem.Windows.Forms.RadioButton(); this.radioButton2=newSystem.Windows.Forms.RadioButton(); this.radioButton3=newSystem.Windows.Forms.RadioButton(); this.radioButton4=newSystem.Windows.Forms.RadioButton(); this.radioButton5=newSystem.Windows.Forms.RadioButton(); this.label6=newSystem.Windows.Forms.Label(); this.label7=newSystem.Windows.Forms.Label(); this.button3=newSystem.Windows.Forms.Button(); this.button4=newSystem.Windows.Forms.Button(); this.button5=newSystem.Windows.Forms.Button(); this.button6=newSystem.Windows.Forms.Button(); this.SuspendLayout(); // //button1 // this.button1.BackColor=System.Drawing.Color.White; this.button1.ForeColor=System.Drawing.Color.Black; this.button1.Location=newSystem.Drawing.Point(138,427); this.button1.Name="button1"; this.button1.Size=newSystem.Drawing.Size(10,10); this.button1.TabIndex=0; this.button1.UseVisualStyleBackColor=false;
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 贪食