图片框上的图片框不给透明

时间:2017-09-26 18:33:02

标签: c# transparency picturebox transparent

我制作了一个游戏(第2场游戏),我正在从文本文件中读取地图 然后依赖于文本文件中的字符,生成图片框。 玩家在积木(墙壁)之前渲染,因为出于某种原因,当我渲染他之后渲染他时,它无法显示。

我几乎到处都看,没有解决方案对我有好处。 我看到了父解决方案,但这是一个问题,因为我从代码手动生成图片框。 每次玩家移动到顶部的父控件的位置时,我也试图设置。但是后来我在x,y点遇到了困难,因为它将锚点重置为父节点,而不是表格的x,y。 例如,如果我在表格中以4,4渲染播放器,它将在那里,但如果我将播放器父级设置为图片框,它将是:pic.x + 4,pic.y + 4

我不知道该怎么做。

这是玩家类:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    using System.Drawing;
    namespace MumiaAdventues
    {
        class Player
        {
            const int width = 20;
            const int height = 30;
            private int x;
            private int y;
            private int speed;
            private PictureBox _playerPic;
            public Player(Panel panel)
            {
                this._playerPic = new PictureBox();
                this._playerPic.Size = new System.Drawing.Size(width, height);
                for(int i=0;i<Enviroment.env.GetLength(1);i++)//y
                {
                    for(int j = 0; j < Enviroment.env.GetLength(0); j++)//x
                    {
                        if(Enviroment.env[j,i].GetEnvType==Enviroment.EnvType.Start)
                        {
                            this.x = j * 34 + 6;
                            this.y = i * 34 -6;    
                            this._playerPic.Location = new Point(this.x, this.y);
                        }
                    }
                }
                this.speed = 2;
                this._playerPic.Visible = true;
                this._playerPic.BackColor = System.Drawing.Color.Transparent;
                this._playerPic.SizeMode = PictureBoxSizeMode.Zoom;
                this._playerPic.Image = MumiaAdventues.Properties.Resources.professor_walk_cycle_no_hat_19;
                this._playerPic.BringToFront();
                this._playerPic.Refresh();
                panel.Controls.Add(this._playerPic);
                this._playerPic.Show();
            }
            public int Speed { get { return this.speed; } set { this.speed = value; } }
            public void Move(Keys key)
            {
                switch (key)
                {
                    case Keys.Right:
                        if (Enviroment.env[(this.x + width + 2) / 34, (this.y) / 34].GetEnvType != Enviroment.EnvType.Wall &&
                           Enviroment.env[(this.x + width + 2) / 34, (this.y) / 34].GetEnvType != Enviroment.EnvType.WallTop &&
                           Enviroment.env[(this.x + width + 2) / 34, (this.y + height) / 34].GetEnvType != Enviroment.EnvType.Wall &&
                           Enviroment.env[(this.x + width + 2) / 34, (this.y + height) / 34].GetEnvType != Enviroment.EnvType.WallTop)
                            this._playerPic.Location = new Point(this.x += speed, this.y);
                        break;
                    case Keys.Left:
                        if (Enviroment.env[(this.x - 2) / 34, (this.y) / 34].GetEnvType != Enviroment.EnvType.Wall &&
                           Enviroment.env[(this.x - 2) / 34, (this.y) / 34].GetEnvType != Enviroment.EnvType.WallTop &&
                           Enviroment.env[(this.x - 2) / 34, (this.y + height) / 34].GetEnvType != Enviroment.EnvType.Wall &&
                           Enviroment.env[(this.x - 2) / 34, (this.y + height) / 34].GetEnvType != Enviroment.EnvType.WallTop)
                            this._playerPic.Location = new Point(this.x -= speed, this.y);
                        break;
                    case Keys.Up:

                        break;

                }
            }
        }
    }

这是块类:

        using System;
    using System.Collections.Generic;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    namespace MumiaAdventues
    {
        class Enviroment
        {
            public enum EnvType
            {
                Wall,
                WallTop,// regular wall block
                Start,
                BronzeCoin,
                SilverCoin,
                GoldCoin,
                Mumia,
                ClosedDoor,
                OpenedDoor,
                Key,
                Lava,
                Nothing,
                Slime,
                Portal,
                PortalDestination,
                Finish
                //~~~~~~~~~~~~~~~~~~~~~
                //Read MapInstructions.txt for more explanation
            }
            const int width = 34; // default width of the enviroment block
            const int height = 34; // default height of the enviroment block
            private PictureBox _envPic; // picture of the enviroment block
            private EnvType _envType; // type of the enviroment block
            public static Enviroment[,] env = new Enviroment[24, 16]; // x = 24 blocks , y = 16 blocks
            public Enviroment(char envType)
            {
                switch (envType)
                {
                    case 'B':
                        this._envType = EnvType.Wall;
                        break;
                    case 'T':
                        this._envType = EnvType.WallTop;
                        break;
                    case 'R':
                        this._envType = EnvType.Nothing;
                        break;
                    case 'S':
                        this._envType = EnvType.Start;
                        break;
                    default:
                        this._envType = EnvType.Nothing;
                        break;
                }
            }
            public void UpdateEnviromentPic(int x,int y,Panel panel)
            {
                switch (this._envType)
                {
                    case EnvType.WallTop:
                        this._envPic = new PictureBox();
                        this._envPic.Size = new System.Drawing.Size(width, height);
                        this._envPic.Location = new System.Drawing.Point(x, y);
                        this._envPic.Image = MumiaAdventues.Properties.Resources.Block_Wall;
                        this._envPic.Refresh();
                        this._envPic.Visible = true;
                        panel.Controls.Add(this._envPic);
                        break;
                    case EnvType.Wall:
                        this._envPic = new PictureBox();
                        this._envPic.Size = new System.Drawing.Size(width, height);
                        this._envPic.Location = new System.Drawing.Point(x, y);
                        this._envPic.Image = MumiaAdventues.Properties.Resources.Block;
                        this._envPic.Refresh();
                        this._envPic.Visible = true;
                        panel.Controls.Add(this._envPic);
                        break;
                }
            }
            public EnvType GetEnvType { get { return this._envType; } set { this._envType = value; } }
            public PictureBox GetEnvPic { get { return this._envPic; } set { this._envPic = value; } }
        }
    }

这就是形式:

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

    namespace MumiaAdventues
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
            static Player player;
            private void Form1_Load(object sender, EventArgs e)
            {

                this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
                this.CenterToScreen(); // centers the game to the center of the screen monitor.  
                string[] lines = Properties.Resources.map1.Split(new string[] { Environment.NewLine }, StringSplitOptions.None); // reads the first map 
                int x = 0;
                int y = 0;
                string temp;
                int count = 0;
                foreach (string line in lines)//y
                {
                    temp = line.Replace("   ", "");//replaces the spaces in the map
                    for(int i = 0; i < temp.Length; i++)//x
                    {
                            Enviroment.env[i, count] = new Enviroment(temp[i]); // changes the panel when the level finishes and loading another one
                    }
                    count++; // y
                }
                x = 0;
                y = 0;
                player = new Player(panel1);
                for(int i=0;i<Enviroment.env.GetLength(1);i++)//y
                {
                    for(int j=0;j<Enviroment.env.GetLength(0); j++)//x
                    {

                            Enviroment.env[j, i].UpdateEnviromentPic(x, y, panel1);
                            x += 34;
                    }
                    x = 0;
                    y += 34;
                }
            }

            private void Form1_KeyDown(object sender, KeyEventArgs e)
            {
                player.Move(e.KeyCode);

            }
        }
    }

这是地图在txt文件中的样子:(它看起来不太好,因为它的24 * 16)

B   T   B   B   T   T   T   T   T   T   B   B   B   B   B   B   B   B   B   B   B   B   B   B
B   S   B   B   R   R   R   R   R   R   B   B   B   B   B   B   B   B   B   B   B   B   B   B
B   R   T   T   R   T   T   T   T   T   T   T   T   T   T   B   B   B   B   B   B   B   B   B
B   R   R   R   R   R   R   R   R   R   R   R   R   R   R   B   B   B   B   B   B   B   B   B
B   B   R   B   B   T   T   T   B   B   B   B   B   B   R   B   B   B   B   B   B   B   B   B
B   B   R   T   T   R   R   R   B   T   T   T   T   T   R   B   B   B   B   B   B   B   B   B
B   B   R   R   R   R   R   R   B   R   R   R   R   R   R   B   B   B   B   B   B   B   B   B
B   B   B   B   B   B   B   B   B   R   B   B   B   B   B   B   B   B   B   B   B   B   B   B
B   T   T   T   T   T   B   B   B   R   B   B   B   B   B   B   B   B   B   B   B   B   B   B
B   R   R   R   R   R   B   B   B   R   B   B   B   B   B   B   B   B   B   B   B   B   B   B
B   R   B   B   B   R   B   B   B   R   B   B   B   B   B   B   B   B   B   B   B   B   B   B
B   R   B   B   B   R   T   T   T   R   B   B   B   B   B   B   B   B   B   B   B   B   B   B
B   R   B   B   B   R   R   R   R   R   B   B   B   B   B   B   B   B   B   B   B   B   B   B
B   R   B   B   B   B   B   B   B   B   B   B   B   B   B   B   B   B   B   B   B   B   B   B
B   R   B   B   B   B   B   B   B   B   B   B   B   B   B   B   B   B   B   B   B   B   B   B
B   R   B   B   B   B   B   B   B   B   B   B   B   B   B   B   B   B   B   B   B   B   B   B

这是问题的图片:

enter image description here

1 个答案:

答案 0 :(得分:0)

如果你真的想使用picturebox http://csharphelper.com/blog/2016/08/make-shaped-pictureboxes-in-c/

但如果你想要一个更好的方法来创建游戏,请使用

Graphics.DrawImage(Resources.(Image), new Point(Position));