[C#学习记录]C#实现简单类似贪吃蛇游戏

本文介绍了如何通过C#在WindowsForms环境中开发一个简单的贪吃蛇游戏,包括游戏的开始方式、角色移动基于用户输入、食物生成和得分系统。作者还提及了游戏设计的延伸想法,如将其应用到射击游戏中的可能实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

概要:通过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;
            }
            }
        }
    }


想法延伸

虽然那样,但是那样,所以我觉得这种判断移动的方法可以延伸到射击游戏中,比如飞机大战之类的小游戏                                                                                                                                  


总结

没有总结

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值