概要:通过C#实现类似贪吃蛇的游戏及延伸想法
起因
今天学C#时,突然想到,好像没写过游戏之类的代码,所以准备用已学的知识和自己摸索,写了一下类似贪吃蛇的小游戏.
一、编写思路
1. 创建一个WindowsFormsApp,通过点击按钮开始游戏
2.开始游戏后,通过"WASD"移动游戏主角
3.和贪吃蛇一样,当主角"吃到"食物后,食物消失,并出现在其他地方,得分+1
二、代码实现
1.创建WindowsFormApp并添加控件
添加1个button,1个timer,4个label以及一个TextBox
(不知道怎么在Windows窗口程序中判断用户输入0.o,所以干脆直接通过判断TextBox输入的内容进而移动游戏角色)
2.代码编写
Part 1:基本信息
const int width = 450;
const int height = 450;
int scores = 0;
Part 2:Button按下时开始游戏
this.Size = new Size(width, height);
timer1.Enabled = true;
textBox1.Focus();//使得按下按钮后直接转到TextBox输入内容
button1.Visible = false;
Part 3:通过读取TextBox内容判断移动方向
string text = textBox1.Text;
try {
char firt_way = text[0];
if (firt_way == 'w')
{
this.label1.Top += -1;
textBox1.Text = "";
}
if (firt_way == 's')
{
this.label1.Top += 1;
textBox1.Text = "";
}
if (firt_way == 'a')
{
this.label1.Left += -1;
textBox1.Text = "";
}
if (firt_way == 'd')
{
this.label1.Left += 1;
textBox1.Text = "";
}
}
catch { }
通过读取TextBox内容进行移动主角的位置,使用try-catch-finally自定义错误处理,读取并移动角色后,清空TextBox内容,并准备进行下一次读取
本来想着,if条件写:text == "w/a/s/d"来判断移动方向,但是测试过程中出现了因为timer的interval属性值就算为1也可能出现来不及进行读取,用户就已经将移动行为输入到TextBox中,所以改用直接读取输入到文本框中的第一个字母,解决了该问题.
Part 4 :判断是否"吃"到食物
double a = label1.Location.X - label2.Location.X;
double b = label1.Location.Y - label2.Location.Y;
double waylong = Math.Sqrt(a * a + b * b);
if (waylong < 10) {
Random rnd = new Random();
int x = rnd.Next(width-100);
int y = rnd.Next(height-100);
this.label2.Location = new Point(x, y);
this.label2.BackColor = Color.FromArgb(rnd.Next(256), rnd.Next(256), rnd.Next(256));
scores += 1;
}
double a,b分别代表角色与实物直接的X与Y的距离,根据距离公式计算出角色与食物之间的距离,当距离<10时,即判定角色吃到了食物,分数+1,再利用随机数将食物移动到随机位置
3.完整代码
代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace 贪吃蛇
{
public partial class Form1 : Form
{
const int width = 450;
const int height = 450;
int scores = 0;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
this.Size = new Size(width, height);
timer1.Enabled = true;
textBox1.Focus();
button1.Visible = false;
}
private void timer1_Tick(object sender, EventArgs e)
{
this.label4.Text = scores.ToString();
string text = textBox1.Text;
try {
char firt_way = text[0];
if (firt_way == 'w')
{
this.label1.Top += -1;
textBox1.Text = "";
}
if (firt_way == 's')
{
this.label1.Top += 1;
textBox1.Text = "";
}
if (firt_way == 'a')
{
this.label1.Left += -1;
textBox1.Text = "";
}
if (firt_way == 'd')
{
this.label1.Left += 1;
textBox1.Text = "";
}
}
catch { }
double a = label1.Location.X - label2.Location.X;
double b = label1.Location.Y - label2.Location.Y;
double waylong = Math.Sqrt(a * a + b * b);
if (waylong < 10) {
Random rnd = new Random();
int x = rnd.Next(width-100);
int y = rnd.Next(height-100);
this.label2.Location = new Point(x, y);
this.label2.BackColor = Color.FromArgb(rnd.Next(256), rnd.Next(256), rnd.Next(256));
scores += 1;
}
}
}
}
想法延伸
虽然那样,但是那样,所以我觉得这种判断移动的方法可以延伸到射击游戏中,比如飞机大战之类的小游戏
总结
没有总结