【问题标题】:picturebox on picturebox not giving transparancy图片框上的图片框不透明
【发布时间】:2018-03-08 02:13:57
【问题描述】:

我正在制作一个游戏(2d 游戏),其中我正在从文本文件中读取地图 然后根据文本文件中的字符,生成图片框。 播放器在块(墙)之前渲染,因为由于某种原因,当我在渲染块后渲染他时它没有显示。

我几乎到处都看过,没有适合我的解决方案。 我看到了父解决方案,但这是一个问题,因为我从代码手动生成图片框。 此外,我尝试在玩家每次移动到其顶部的块的父控件时设置。但后来我在 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

这是问题的图片:

【问题讨论】:

  • 您可能需要在不使用 UI 快捷方式的情况下绘制整个图像...首先是背景图块,然后是角色在正确位置的正确帧。
  • 顺便说一句,在我解析你的地图并看到T字符是什么之前,我真的认为玩家角色背后的差距应该是这样的。看起来像是他刚从门口走出来的:P

标签: c# transparency picturebox transparent


【解决方案1】:

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

但如果您想要更好的方式来创建游戏,请使用

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

【讨论】:

  • 感谢您的回复,它对我有所帮助。我的问题是:是否有一种方法可以在没有功能 Paint 的情况下使用图形?
  • @DanielNagornov 为什么需要 Paint 功能?您应该使用建议的DrawImage 函数在空白画布图像上绘制精灵图像。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-13
  • 1970-01-01
相关资源
最近更新 更多