C#实现窗体的F1赛车V1.0

好久没写过什么代码了,突然觉得该做个什么东西了,于是想做点游戏什么的。第一浮现在我眼前的就是俄罗斯方块了。想到什么就做什么,于是打开VS2010开始了游戏制作之旅。

可是,无语的是,竟然想着做俄罗斯方块,结果做到了赛车去了。。。。这个令我蛋疼不已啊!既然这样,那就做赛车游戏贝!

  先说下里面用到的一些知识点:

1.获取用户按下的键。利用这个我们才能来控制赛车的移动了,这个是基础,当然我不知道如何获取用户按下的是除了字母或数字意外的键,在此请教各位大虾了。

2.计时器的使用。计时器就是每当多久就会执行一个函数方法,重要的属性就是计时的时间了(Interval),两个重要方法当然就是开始计时(Start)和结束计时(Stop)了。

3.动态创建控件。在创建好控件时,界面是不会有显示的,除非你添加进去,所以很重要的一步就是把控件添加到窗体里。操作控件跟拖的类似,只是需要控件的寻找方法(Find)来获取要操作的控件。

4.重启程序。利用这行代码就可以重启程序了。

 System.Diagnostics.Process.Start(System.Reflection.Assembly.GetExecutingAssembly().Location);

5.XML文件的操作。我利用XML文件来保存获得最高分的名字和分数,有添加节点、添加元素、修改元素三种操作。

6.文件是否存在的判断。直接使用File.Exist();

7.去绝对值函数。System.Math.Abs()用来判断赛车是否相撞,下面会用到。

当然,很重要的一部分就是逻辑了:

1.赛车是从前方慢慢开过来的,所以动态创建好窗体后利用计时器来不停的改变创建好的窗体的位置,当然前方的车是不直线开过来的,只需改变Y值就行了。

2.赛车是随机出现在前方的,此时只需要是把窗体的X值传入个随机数就行了。

3.判断赛车相撞。若赛车相撞,则是两个窗体有重合部分,此时他们的X/Y值的差是小于窗体的宽高的。

4.得分。我这里设计的得分是根据时间来计算的,未撞车的时间越长,分数越高,各位读者可以根据自己爱好来设计。

5.为了增加难度,我把赛车出现的个数变为越来越多,赛车的速度越来越快,这个是通过改变计时器计时来完成的,当然由于计时不能为0或负数,所以加速是有限度的。

耶!奇怪了,貌似没找到添加附件哦!那代码就复制上去吧!需要的同学可以留下地址找我要!

ContractedBlock.gif ExpandedBlockStart.gif View Code
  1 using System;
2 using System.Collections.Generic;
3 using System.ComponentModel;
4 using System.Data;
5 using System.Drawing;
6 using System.Linq;
7 using System.Text;
8 using System.Windows.Forms;
9 using System.Threading;
10 using System.Xml;
11 using System.IO;
12
13
14 namespace 俄罗斯方块
15 {
16 public partial class Form1 : Form
17 {
18 public Form1()
19 {
20 InitializeComponent();
21 }
22 //设置控制键
23 private void Form1_KeyPress(object sender, KeyPressEventArgs e)
24 {
25 if ((e.KeyChar == 'A'||e.KeyChar=='a') && Shot.Location.X - 10 >= 0)//通过大写的A来控制赛车左移
26 {
27 Shot.SetBounds(Shot.Location.X - 10, Shot.Location.Y, Shot.Width, Shot.Height); ;
28 }
29 if ((e.KeyChar == 'D'||e.KeyChar=='d') && Shot.Location.X + 10 <= panel1.Width)//通过大写的D来控制赛车右移
30 {
31 Shot.SetBounds(Shot.Location.X + 10, Shot.Location.Y, Shot.Width, Shot.Height); ;
32 }
33 if ((e.KeyChar == 'W'||e.KeyChar=='w') && Shot.Location.Y - 10 >= 0)//通过大写的W来控制赛车上移
34 {
35 Shot.SetBounds(Shot.Location.X, Shot.Location.Y - 10, Shot.Width, Shot.Height); ;
36 }
37 if ((e.KeyChar == 'S'||e.KeyChar=='s') && Shot.Location.Y + Shot.Size.Height <= panel1.Height)//通过大写的S来控制赛车下移
38 {
39 Shot.SetBounds(Shot.Location.X, Shot.Location.Y + 10, Shot.Width, Shot.Height); ;
40 }
41 }
42
43 private void timer1_Tick(object sender, EventArgs e)
44 {
45 if (Convert.ToInt32(lblGoal.Text) > 500 && timer1.Interval > 50)
46 {
47 timer1.Interval -= 20;//越到最后"赛车"出来速度越快,直到不能再快
48 }
49 Button bt = new Button();//动态创建”赛车“
50 Random rd = new Random();
51 int x = rd.Next(1, panel1.Width);
52 bt.SetBounds(x, 0, Shot.Width, Shot.Height);
53 bt.Text = "1";
54 bt.BackgroundImage = 俄罗斯方块.Properties.Resources.F2;
55 Form1 f1 = new Form1();
56 panel1.Controls.Add(bt);//把“赛车”添加到容器里
57 }
58
59 private void Form1_Load(object sender, EventArgs e)
60 {
61 if (File.Exists("HightScoreRank.xml"))
62 {
63 XmlDataDocument xd = new XmlDataDocument();
64 xd.Load("HightScoreRank.xml");
65 XmlElement xe = xd.SelectNodes("Rank1")[0] as XmlElement;
66 label5.Text = xe.GetAttribute("Name");
67 label7.Text = xe.GetAttribute("HightScore");
68 }
69 //timer1.Start(); timer2.Start(); timer3.Start();//启动计时器
70 }
71
72 private void timer2_Tick(object sender, EventArgs e)
73 {
74 foreach (Button item in panel1.Controls)
75 {
76 if (item.Text != "")
77 {
78 item.SetBounds(item.Location.X, item.Location.Y + 10, Shot.Width, Shot.Height);//使“车”开过来
79 //使车越开越快
80 if (timer2.Interval > 50)
81 {
82 timer2.Interval -= 1;
83 }
84 }
85 }
86 }
87
88 private void timer3_Tick(object sender, EventArgs e)
89 {
90 foreach (Button item in panel1.Controls)
91 {
92 if (item.Text != "")
93 {
94 //取绝对值的函数
95 if ((System.Math.Abs(item.Location.X - Shot.Location.X) < Convert.ToInt32(Shot.Width)) && (System.Math.Abs(item.Location.Y - Shot.Location.Y) < Convert.ToInt32(Shot.Height)))
96 {
97 timer1.Stop();
98 timer2.Stop();
99 timer3.Stop();
100
101 if (File.Exists("HightScoreRank.xml"))
102 {
103 XmlDataDocument xd = new XmlDataDocument();
104 xd.Load("HightScoreRank.xml");
105 XmlElement xe = xd.SelectNodes("Rank1")[0] as XmlElement;
106 if (Convert.ToInt32(lblGoal.Text) > Convert.ToInt32(xe.GetAttribute("HightScore")))
107 {
108 xe.SetAttribute("Name", txtName.Text);
109 xe.SetAttribute("HightScore", lblGoal.Text);
110 xd.Save("HightScoreRank.xml");
111 }
112 }
113 else
114 {
115 XmlDocument xd = new XmlDocument();
116 XmlElement xe = xd.CreateElement("Rank1");
117 xe.SetAttribute("Name", txtName.Text);
118 xe.SetAttribute("HightScore", lblGoal.Text);
119 xd.AppendChild(xe);
120 xd.Save("HightScoreRank.xml");
121 }
122 DialogResult dr = MessageBox.Show("You failed!Try again?", "Notice", MessageBoxButtons.RetryCancel);
123 if (dr != DialogResult.Retry)//选择的不是重试则退出系统
124 {
125 Application.Exit();
126 }
127 else
128 {
129 System.Diagnostics.Process.Start(System.Reflection.Assembly.GetExecutingAssembly().Location);//重启该程序
130 Application.Exit();
131 }
132 }
133 else
134 {
135 lblGoal.Text = (Convert.ToInt32(lblGoal.Text) + 1).ToString();//未撞击到则加分,本程序设计的是越到后面撞击的概率越大,分加得越少
136 }
137
138 }
139 }
140 }
141
142 private void button1_Click(object sender, EventArgs e)
143 {
144 if (txtName.Text.Trim() != "")
145 {
146 timer1.Start(); timer2.Start(); timer3.Start();//启动计时器
147 }
148 else
149 {
150 MessageBox.Show("Please Enter Your Name!");
151 }
152
153 }
154 }
155 }


下载地址有咯!http://files.cnblogs.com/Zeech-Lee/F1%E8%B5%9B%E8%BD%A6.rar  

读者自己有兴趣的话就去做下效果吧!我就没什么时间来处理了!

转载于:https://www.cnblogs.com/Zeech-Lee/archive/2011/09/29/2195727.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值