C#开发的小游戏,只为满足儿时对网游的好奇,提供源码抛砖引玉

一直感觉网游里面的人物会动很好奇,但是一直没有找到相关资料(能让我觉得我也能行的),最近看到一个人用C++写的含有背景图片,有人物,可以移动的简单MFC小游戏,下载一看很复杂写的,但是功能却非常简单,我想C#也能做到,于是就用C#的PictureBox试着写了一个功能相对于下载的复杂一些但是代码简单的小游戏,有游戏的基本功能了,上下左右按键可以控制游戏里面的狼移动来抓羊,有简单计分功能(不过不正确的很),呵呵,由于我也只是下班后写了两个晚上,所以bug很多,由于线程问题可能还经常自动退出,总之抛砖引玉,期待探讨指正赐教,哈哈。图片都是手工ps的,技术不行,勿喷!

先上一张游戏效果图

C#开发的小游戏,只为满足儿时对网游的好奇,提供源码抛砖引玉_第1张图片

简单说一下程序很简单,都是用的PicturBox来装角色,一个MainForm类进行主要功能处理,一个Animal类封装了角色类型。两个线程,一个线程负责角色自动移动,一个线程负责检查是否狼抓住了羊,时间短写的很粗略。纯娱乐,如果有学C#的新手也希望可以激发你学习的乐趣,这便能让我感到很欣慰了,兴趣很重要的说。生气

源码下载地址:苦逼等了一下午也没能审核通过。。。贴代码了

csdn真的无语啊,上传了东西,等了那久就是不出来,害我白等那么久,不是打击我分享的积极性嘛,好在我是第一次分享,坚持住了啊。

感觉CSDN很坑啊,我奉献还要给分,每次都没分提问。。。。。大哭


上传了审核太慢,直接贴代码了:

主窗体类:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;


namespace MyGame
{
    public partial class MainForm : Form
    {
        PictureBox picLang = new PictureBox();
        PictureBox picMYY = new PictureBox();
        PictureBox picLYY = new PictureBox();
        //动物的起始位置
        Point langStartPoint = new Point(10, 10);
        Point myyStartPoint = new Point(300, -10);
        Point lyyStartPoint = new Point(550, -10);
        //动物实时的位置
        int langX, langY;
        Point langCurrectPoint, myyCurrentPoint, lyyCurrentPoint;
        //动物存储列表
        List<Animal> animalList = new List<Animal>();
        int score = 0;

        public delegate void MoveEvent(int y);
        public MainForm()
        {
            InitializeComponent();
            this.DoubleBuffered = true;
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            string langPath=@"..\..\res\111.bmp";
            picLang.Parent=this.pictureBox_Parent;
            Animal huiTaiLang = new Animal(langPath, langStartPoint,ref picLang);
            huiTaiLang.Name = "huiTaiLang";

            string MYYPath = @"..\..\res\meiyangyang.bmp";
            picMYY.Parent = this.pictureBox_Parent;
            Animal MYY = new Animal(MYYPath, myyStartPoint, ref picMYY);
            MYY.Name = "meiyangyang";

            string LYYPath = @"..\..\res\lanyangyang.bmp";
            picLYY.Parent = this.pictureBox_Parent;
            Animal LYY = new Animal(LYYPath, lyyStartPoint, ref picLYY);
            LYY.Name = "lanyangyang";

            animalList.AddRange(new Animal[] { huiTaiLang, MYY, LYY });

            langX = langStartPoint.X;
            langY = langStartPoint.Y;

            Thread thread_Move = new Thread(new ThreadStart(MoveFunc));
            thread_Move.IsBackground = true;
            thread_Move.Start();

            Thread thread_TouchCheck = new Thread(new ThreadStart(TouchChecking));
            thread_TouchCheck.IsBackground = true;
            thread_TouchCheck.Start();
            
        }

        /// <summary>
        /// 羊移动处理
        /// </summary>
        private void MoveFunc()
        {
            int y = -10;
            while (true)
            {
                y += 10;
                if (y > (this.splitContainer1.Panel2.Height - 100))
                    y = -10;
                if (this.InvokeRequired)
                {
                    this.Invoke(new MoveEvent(ChangAnimalLocation),new object[]{y});
                }
                else
                {
                    picMYY.Location = new Point(myyStartPoint.X, y);
                    picLYY.Location = new Point(lyyStartPoint.X, y);
                    this.Refresh();
                    this.label1.Text = score.ToString();
                }
                myyCurrentPoint = picMYY.Location;
                lyyCurrentPoint = picLYY.Location;
                Thread.Sleep(700);
            }
        }
        private void ChangAnimalLocation(int y)
        {
            picMYY.Location = new Point(myyStartPoint.X, y);
            picLYY.Location = new Point(lyyStartPoint.X, y);
            this.Refresh();
            this.label1.Text = score.ToString();
        }
        /// <summary>
        /// 狼接触羊检测
        /// </summary>
        private void TouchChecking()
        {
            Size langSize=new Size(1,1);
            Size myySize = new Size(1, 1);
            Size lyySize=new Size(1,1);
            while (true)
            {
                lock (this)
                {
                    foreach (var animal in animalList)
                    {
                        switch (animal.Name)
                        {
                            case "huiTaiLang": langSize = animal.BMPSize; break;
                            case "meiyangyang": myySize = animal.BMPSize; break;
                            case "lanyangyang": lyySize = animal.BMPSize; break;
                            default: break;
                        }

                    }
                    RectangleF langRectan = new RectangleF(langCurrectPoint, langSize);
                    RectangleF myyRectan = new RectangleF(myyCurrentPoint, myySize);
                    RectangleF lyyRectan = new RectangleF(lyyCurrentPoint, lyySize);
                    if (CrossLine(langRectan, myyRectan))
                    {
                        score++;
                        if (score >= 65535)
                            score = 0;
                    }
                    if (CrossLine(langRectan, lyyRectan))
                    {
                        score++;
                        if (score >= 65535)
                            score = 0;
                    }
                }
                Thread.Sleep(400);
            }
        }
        /// <summary>
        /// 判断两个矩形是否相交
        /// </summary>
        /// <param name="r1"></param>
        /// <param name="r2"></param>
        /// <returns></returns>
        private bool CrossLine(RectangleF r1, RectangleF r2)
        {
            if ((Math.Abs((r1.X + r1.Width / 2) - (r2.X + r2.Width / 2)) < ((r1.Width + r2.Width) / 2)) && (Math.Abs((r1.Y + r1.Height / 2) - (r2.Y + r2.Height / 2)) < ((r1.Height + r2.Height) / 2)))
                return true;
            else
                return false;
        }
        /// <summary>
        /// 狼移动处理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MainForm_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
        {
            switch (e.KeyValue)
            {
                //上
                case 0x26:
                    langY -= 7;
                    if (langY <= 0)
                        langY = 0;
                    break;
                //下
                case 0x28:
                    langY += 7;
                    if (langY >= (this.splitContainer1.Panel2.Height - 150))
                        langY = this.splitContainer1.Panel2.Height - 150;
                    break;
                //左
                case 0x25:
                    langX -= 7;
                    if (langX <= 0)
                        langX = 0;
                    break;
                //右
                case 0x27:
                    langX += 7;
                    if (langX >= (this.splitContainer1.Panel2.Width - 100))
                        langX = this.splitContainer1.Panel2.Width - 100;
                    break;
                default:
                    break;
            }
            picLang.Location = new Point(langX, langY);
            langCurrectPoint = picLang.Location;
        }
    }
}

动物封装类:

using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Windows.Forms;
using System.IO;

namespace MyGame
{
    class Animal
    {
        private PictureBox usePicName;
        Bitmap bmp;
        public Bitmap BMP
        {
            set
            {
                bmp = value;
                bmp.MakeTransparent();
            }
        }
        public string Name
        {
            get;
            set;
        }
        public Size BMPSize
        {
            get { return bmp.Size;}
        }

        public Animal(string bmpPath, Point initPosition,ref PictureBox picName)
        {
            if (File.Exists(bmpPath))
            {
                bmp = new Bitmap(bmpPath);
                bmp.MakeTransparent();

                this.usePicName = picName;
                picName.Width = bmp.Width;
                picName.Height = bmp.Height;
                picName.Image = bmp;
                picName.Location = initPosition;
                picName.Paint += new PaintEventHandler(usePicName_Paint);
            }
            else
            {
                MessageBox.Show(bmpPath + "位置的BMP图片不存在");
            }
        }
        private void usePicName_Paint(object sender, PaintEventArgs e)
        {
            this.usePicName.Width = bmp.Width;
            this.usePicName.Height = bmp.Height;
            this.usePicName.Image = bmp;
        }
    }
}


你可能感兴趣的:(C#小游戏)