我如何创建一个图像数组,然后将每个图像加载到连续的索引中

时间:2014-06-24 20:46:46

标签: c# arrays performance indexing

正如您所看到的,我已创建了一系列图像,但我不确定如何将每个图像加载到连续索引中。

有人告诉我为我的游戏做这个,但我不确定它是如何解决我的问题 我制作了一个游戏,角色向左走,然后向上然后向右走,然后是几个加载动画并处理动作的计时器,但是当我使用此代码绘制时

     *e.Graphics.DrawImage(Properties.Resources.Corn_Cobs, 70, 70, 40, 40);*

我的角色变得非常迟钝/慢但是当我不能画出图像时它会顺利运行并且角色会像平常一样加速。

以下是代码:

  namespace Rice_Boy_Tester_2
 {
   public partial class Form1 : Form
{

  bool iggy = false;
    bool left2 = false;
    bool right2 = false;
    bool Up2 = false;
    bool Down2 = false;
    bool Check2 = false;

    bool left = false;
    bool right = false;
    bool Up = false;
    bool Down = false;
    bool Check = false;

    Image[] Animations;



    public Form1()
    {
       InitializeComponent();

    }


    private void Form1_Load(object sender, EventArgs e)
    {


        Animations = new Image[6];
        Animations[0] = Image.FromFile("Rice-Boy-Walking-Left-Bouncing.gif"); 
        Animations[1] = Image.FromFile("Rice-Boy-Walking-Up-Bouncing.gif");
        Animations[2] = Image.FromFile("Rice-Boy-Walking-Right-Bouncing.gif");
        Animations[3] = Image.FromFile("Rice-Boy-Walking-Down.gif");
        Animations[4] = Properties.Resources.Rice_Boy_Standing_Left;
        Animations[5] = Properties.Resources.Corn_Cobs;

    }




    private void Refresh_Tick(object sender, EventArgs e)
    {
        this.Refresh();

    }


    private void PriceBoyWalk_Tick(object sender, EventArgs e)
    {

        if (left)//Goes Left
        {
            Player.Left -= 1;
        }
        if (Player.Left < 170 & Check == false)//checks how far away player is from form
        {
            left = false;
            Up = true;
        }
        if (Up & Player.Left < 170) //Goes Up
        {
            Player.Top -= 1;
            Check = true;
        }

        if (Player.Top < 100 & Check)
        {
            Up = false;
            Down = true;

        }

        if (right)//Goes Right
        {
            Player.Left += 1;

        }

        if (Down)//Goes Down
        {
            Player.Top += 1;

        }

        if (Player.Top + 150 > this.ClientSize.Height)// When RiceBoy goes down and hits bottom  right = true
        {
            Check = false;
            Down = false;
            right = true;
        }


        if (Player.Left + 150 > this.ClientSize.Width)//Stops At Starting Point
        {
            right = false;

        }

    }
    private void B1_Click(object sender, EventArgs e)
    {

        this.Paint += new PaintEventHandler(form1_Pad1_Corn);
        RiceBoyWalkGif.Enabled = true;
        left = true;
        left2 = true;
        RiceBoyWalk.Enabled = true;}


}
 private void form1_Pad1_Corn(object sender, System.Windows.Forms.PaintEventArgs e)
   {




     e.Graphics.DrawImage(Properties.Resources.Corn_Cobs, 70, 70, 400, 400); 


 }
    private void timer1_Tick(object sender, EventArgs e)
    {


        if (left2)
        {

            Player.Image = Animations[0];
            left2 = false;

        }
        if (Player.Left < 170 & Check2 == false)//checks how far away player is from form
        {

            left2 = false;
            Up2 = true;

        }
        if (Up2 & Player.Left < 170) //Goes Up
        {
            this.Player.Size = new System.Drawing.Size(36, 76); // Changes size of the picture box to maintain quaility 
            Player.Image = Animations[1];//Animates RiceBoyWalkingUp
            Check2 = true;
            Up2 = false;



        }

        if (Player.Top < 105 & Check2)//Player.Top < 101 must be +1 greater than the RiceBoyWalkTimer
        {
            Up2 = false;
            Down2 = true;



        }

        if (right2)
        {

            this.Player.Size = new System.Drawing.Size(53, 77); // Changes size of the picture box to maintain quaility 
            Player.Image = Animations[2];//Animates RiceBoyWalkingRight
            right2 = false;

        }

        if (Down2)//Goes Down
        {
           Player.Image = Animations[3];//Animates RiceBoyWalkingDown
            Down2 = false;

        }

        if (Player.Top + 150 > this.ClientSize.Height)// When RiceBoy goes down and hits bottom riceboy walks right
        {
            iggy = true;// shows that riceboy is approching the starting point
            Check2 = false;
            Down2 = false;
            right2 = true;
        }


        if (Player.Left + 150 > this.ClientSize.Width & iggy)//Stops At Starting Point
        {
            right2 = false;
            Player.Image = Animations[4];// Rice boy standing left
        }

0 个答案:

没有答案