在2D游戏中根据对象移动平铺背景并不起作用

时间:2017-09-15 21:59:42

标签: c# visual-studio 2d 2d-games

我正在开发一款2D游戏,你可以将主角移动到平铺的2d迷宫中,并且随时只能看到迷宫的一部分。我希望迷宫背景与超级马里奥中的角色一起移动。这是我到目前为止所尝试的:

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

       Graphics g = e.Graphics;

        tileme = Resources.me;
        tilewall = Resources.wall;
        tilemystery = Resources.mystery;
        tilefinish = Resources.finish;
        tileredlock = Resources.lock_red;
        tileredkey = Resources.key_red;
        tilebluelock = Resources.lock_blue;
        tilebluekey = Resources.key_blue;
        tilegreenlock = Resources.lock_green;
        tilegreenkey = Resources.key_green;

            e.Graphics.DrawImage(tileme, me.GetCenterX() - 24, me.GetCenterY() - 24, 48, 48);
            int px = (me.GetCenterX() - 24) / 48;
            int py = (me.GetCenterY() - 24) / 48; int nx = 0; ny=0;
            if(zx==0){mx=6;}else{mx=7;} 
            for (int j = fx; j < mx; j++)
            {

                ny = 0;
                for (int i = fy; i < my; i++)
                {


                    if (i < my)
                    {
                        float tileX = ((j-fx) * 48) - zx;
                        float tileY = ((i-fy) * 48) - zy;
                        int type = lines[ny,nx];
                        switch (type)
                        {
                            case 1:
                                e.Graphics.DrawImage(tilewall, tileX, tileY, 48, 48);
                                break;
                            case 2:
                                e.Graphics.DrawImage(tilemystery, tileX, tileY, 48, 48);
                                break;
                            case 3:
                                e.Graphics.DrawImage(tilefinish, tileX, tileY, 48, 48);
                                break;
                            case 4:
                                e.Graphics.DrawImage(tilebluelock, tileX, tileY, 48, 48);
                                break;
                            case 5:
                                e.Graphics.DrawImage(tilebluekey, tileX, tileY, 48, 48);
                                break;
                            case 6:
                                e.Graphics.DrawImage(tileredlock, tileX, tileY, 48, 48);
                                break;
                            case 7:
                                e.Graphics.DrawImage(tileredkey, tileX, tileY, 48, 48);
                                break;
                            case 8:
                                e.Graphics.DrawImage(tilegreenlock, tileX, tileY, 48, 48);
                                break;
                            case 9:
                                e.Graphics.DrawImage(tilegreenkey, tileX, tileY, 48, 48);
                                break;
                            case 11:
                                e.Graphics.DrawImage(tileplateh, tileX, tileY, 48, 48);
                                break;
                            case 12:
                                e.Graphics.DrawImage(tileplateh, tileX, tileY, 48, 48);
                                break;
                        } 
                    }ny++;

                } nx++;
            }
            try
            {
                Thread.Sleep(17);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }


    }




    protected override bool ProcessDialogKey(Keys keyData)
    {
        OnKeyDown(new KeyEventArgs(keyData));
        return base.ProcessDialogKey(keyData);
    }

    private void Form1_keydown(object sender, KeyEventArgs e)
    {

        if (e.KeyCode == Keys.Left)
        {
            int ml = me.moveLeft();
            if (ml == 0)
            {
                sx = me.update(); //sx=0 means we need to scroll the object, sx=2 means we need to scroll the background
                if (sx == 2) { if (zx == 0) { zx = 48; fx += -1; } zx += -12; }
                sx = 0;
                this.Refresh();
            }

        }
        if (e.KeyCode == Keys.Right)
        {
            int mr = me.moveRight();
            if (mr == 0)
            {
                sx = me.update();
                if (sx == 2) { zx += 12; if (zx == 48) { zx = 0; fx += 1; } }
                sx = 0;
                this.Refresh();

            }
        }
        if (e.KeyCode == Keys.Up)
        {
            int mu = me.moveUp();
            if (mu == 0)
            {
               sy = me.update();
                if (sy == 2) { if (zy == 0) { zy = 48; fy += -1; } zy += -12; }
                sy = 0;
                this.Refresh();
            }
        }
        if (e.KeyCode == Keys.Down)
        {
            int md = me.moveDown();
            if (md == 0)
            {
                sx = me.update();
                if (sy == 2) { zy += 12; if (zy == 48) { zy = 0; fy += 1; } }
                sy = 0;
                this.Refresh();
            }
        }
    }

这不起作用,而当我尝试移动背景时,奇怪的可见错误会发生。有人请帮助我。如何使它工作?如何获得我想要的效果?

0 个答案:

没有答案