C#游戏程序-重绘问题? [英] C# Game Program - Redrawing Issues?

查看:117
本文介绍了C#游戏程序-重绘问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为学校作业而这样做,但是我陷入了代码末尾.首先,我无法将"man"动画重新绘制到面板上.但是,在图片框中,它将使用数组进行动画处理.其次,按下钥匙,男人根本不会动弹.它可以在我的第一个版本上使用完全相同的代码副本并粘贴.我不确定是否还有其他更好的方法,或者是否以某种方式搞砸了.整个程序的代码是第一个,第二个是按键,第三个是我认为需要更改重绘以使其具有动画效果的位置.

I am doing this for a school assignment, but I got stuck at the end of the code. For one, I am unable to get the ''man'' animation to be redrawn onto the panel. Though, in the picture box it will animate using the array. Secondly, using the key down, the man will not move at all. It works on my first version with the exact same code copy and pasted. I am unsure if there is another, or better way to do it, or if I somehow messed up. The code for the entire program is first, the second is for the key down, the third is for where I think I need to change the redrawing for it to be animated.

public partial class Form1 : Form
            //Array for animated man
            for (int i = 1; i <= 7; i++)
            {
                images[i] = Image.FromFile(Application.StartupPath + @"\Man" + i.ToString() + ".png");
            }
            PicMan.Image = images[1];

            // Give form focus
            this.Focus();
        
        private void BtnStart_Click(object sender, EventArgs e)
        {
            //Animates Man
            TimeMan.Enabled = !TimeMan.Enabled;

            if (BtnStart.Text == "Start")
            {
                // New Game
                myGraphics.Clear(PnlGameContent.BackColor);
                BtnStart.Text = "Stop";
                BtnExit.Enabled = false;
                TxtScore.Text = "0";
                // set each coin off top of panel and give new speed
                for (int i = 0; i < 5; i++)
                {
                    coinY[i] = -coinSize;
                    coinSpeed[i] = myRandom.Next(4) + 3;
                }
                // Set man near the center
                manX = (int)(PnlGameContent.Width / 2);
                myGraphics.DrawImage(PicMan.Image, manX, PnlGameContent.Height - manSize, manSize, manSize);
                // Give form focus so it can accept KeyDown events
                this.Focus();
    
        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            // Erase man at old location
            myGraphics.FillRectangle(blankBrush, manX, PnlGameContent.Height - manSize, manSize, manSize);
            // Check for F key (left) and J key (right) and compute the mans position
            if (e.KeyCode == Keys.F)
            {
                manX = manX - 5;
            }
            else if (e.KeyCode == Keys.J)
            {
                manX = manX + 5;
            }
            // Position man
            myGraphics.DrawImage(PicMan.Image, manX, PnlGameContent.Height - manSize, manSize, manSize);
        }

        private void startToolStripMenuItem1_Click(object sender, EventArgs e)
        {
      
        private void TimeMan_Tick(object sender, EventArgs e)
        {
            //Counts through the images and if the last image is selected
            //then image gets reset back to image one
            PicMan.Image = images[count];
            count++;
            if (count > 7)
                count = 0;

        }
        }





private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            // Erase man at old location
            myGraphics.FillRectangle(blankBrush, manX, PnlGameContent.Height - manSize, manSize, manSize);
            // Check for F key (left) and J key (right) and compute the mans position
            if (e.KeyCode == Keys.F)
            {
                manX = manX - 5;
            }
            else if (e.KeyCode == Keys.J)
            {
                manX = manX + 5;
            }
            // Position man
            myGraphics.DrawImage(PicMan.Image, manX, PnlGameContent.Height - manSize, manSize, manSize);
        }





// Set man near the center
               manX = (int)(PnlGameContent.Width / 2);
               myGraphics.DrawImage(PicMan.Image, manX, PnlGameContent.Height - manSize, manSize, manSize);



无论是固定的编码,链接还是小的建议,任何帮助都将不胜感激.提前致谢.

很抱歉,冗长的代码和问题,但是我不确定我哪里出错了.



Any help is greatly appreciated, whether is be fixed coding, links, or small suggestions. Thanks in advance.

Sorry for the long code and question, but I am unsure of where I went wrong.

推荐答案

您不应该在这些方法中进行绘制.您应该使用它们来捕获有关需要如何重画图片或图片的任何部分的信息,并保存该信息.然后,您应该覆盖 System.Windows.Forms.Control.OnPaint() [ ^ ]方法和使用先前捕获的信息来确定所有对象的放置位置.
You should not be doing the drawing inside these methods. You should use them to capture information about how the picture, or any part of it, needs to be redrawn, and save that information. You should then override the System.Windows.Forms.Control.OnPaint()[^] method and do all rendering there, using the previously captured information to decide where to position the various objects.


这篇关于C#游戏程序-重绘问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆