C# 超级小玛丽小游戏,练一练,娱乐一下自己吧。[亲测有效]

C# 超级小玛丽小游戏,练一练,娱乐一下自己吧。[亲测有效]按下空格键控制跳起动作: private void Form1_KeyDown(object sender, KeyEventAr

欢迎大家来到IT世界,在知识的湖畔探索吧!

按下空格键控制跳起动作:

        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Space && !isJumping && !gameOver)
            {
                
                jumpSpeed = -10;
            }

        }

欢迎大家来到IT世界,在知识的湖畔探索吧!

显示图片与得分情况:

欢迎大家来到IT世界,在知识的湖畔探索吧!       private void Form1_Paint(object sender, PaintEventArgs e)
        {
            e.Graphics.FillRectangle(Brushes.SkyBlue, 0, 0, ClientSize.Width, ClientSize.Height);
            e.Graphics.DrawImage(Properties.Resources._1, 50, marioY, MarioSize, MarioSize);//图片
            e.Graphics.FillRectangle(Brushes.Red, objectX, objectY, ObjectSize, ObjectSize);
            e.Graphics.DrawString(#34;得分:{score}", Font, Brushes.Black, 10, 10);
        }
    

窗体初始化:

         private const int MarioSize = 50;
        private const int ObjectSize = 50;
        private const int ObjectSpeed = 10;

        private int marioY;
        private int objectX;
        private int objectY;
        private bool isJumping;
        private int jumpSpeed;
        private bool gameOver;

        private int score;
        public Form1()
        {
            InitializeComponent();
            marioY = ClientSize.Height - MarioSize;
            objectX = ClientSize.Width;
            objectY = ClientSize.Height - ObjectSize;
            isJumping = false;
            jumpSpeed = 10;
            gameOver = false;
            score = 0;

            timer1.Interval = 1000 / 60; // 每秒60帧
            timer1.Enabled = true;
        }

添加时间控件:

欢迎大家来到IT世界,在知识的湖畔探索吧!private void timer1_Tick(object sender, EventArgs e)
        {
            if (!gameOver)
            {
                objectX -= ObjectSpeed;

                if (objectX < -ObjectSize)
                {
                    objectX = ClientSize.Width + ObjectSize;
                    objectY = ClientSize.Height - ObjectSize - new Random().Next(50, 150);
                    score++;
                }

                if (isJumping)
                {
                    marioY -= jumpSpeed;
                    jumpSpeed--;

                    if (marioY >= ClientSize.Height - MarioSize)
                    {
                        isJumping = false;
                        marioY = ClientSize.Height - MarioSize;
                        jumpSpeed = 10;
                    }
                }
                else
                {
                    marioY += jumpSpeed;
                    jumpSpeed = Math.Min(jumpSpeed + 1, 20);
                    if (marioY + MarioSize > ClientSize.Height)//停在底部
                    {
                        marioY = ClientSize.Height - MarioSize;
                    }
                }

                if (objectX < 100 && objectX > 0 && marioY + MarioSize > objectY)
                {
                    gameOver = true;
                    timer1.Enabled = false;
                    MessageBox.Show(#34;游戏结束!得分:{score}");
                }
                

                Invalidate();
            }

#头条创作挑战赛##大湾区寻宝#

免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://itzsg.com/22906.html

(0)

相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

联系我们YX

mu99908888

在线咨询: 微信交谈

邮件:itzsgw@126.com

工作时间:时刻准备着!

关注微信