gpt4 book ai didi

c# - 在另一个带有透明孔的图像后面移动图像

转载 作者:行者123 更新时间:2023-11-30 23:31:40 24 4
gpt4 key购买 nike

`int[,] board = new int[6, 7]; PictureBox boardpb = new PictureBox(); PictureBox[] pb = new PictureBox[42]; 图像表,红色,蓝色; int c = 0;

    public Form1()
{
InitializeComponent();

red = Properties.Resources.red; red = (Image)(new Bitmap(red, new Size(110, 110)));
blue = Properties.Resources.blue; blue = (Image)(new Bitmap(blue, new Size(110, 110)));
table = Properties.Resources.Board; table = (Image)(new Bitmap(table, new Size(700, 700)));

boardpb.Location = new Point(0, 0);
boardpb.Image = table;
boardpb.Size = table.Size;
boardpb.BackColor = Color.Transparent;
boardpb.MouseClick += new MouseEventHandler(Human_Play);
this.Controls.Add(boardpb);
}

void PlayCircle(int x, int y, Image im) //plays a red/blue circle in the corresponding (y,x) position
{
pb[c] = new PictureBox();
pb[c].Location = new Point((int)(-2 + 98.8 * x), (int)(83 + 94.4 * y));
pb[c].Image = im;
pb[c].Size = im.Size - new Size(10, 10);
pb[c].BackColor = Color.Transparent;
pb[c].MouseClick += new MouseEventHandler(Human_Play);
boardpb.Controls.Add(pb[c]);
c++;
}

int Y(int x) // gives the lowest empty space in the column, or -1 if the column is full.
{
for (int y = 5; y >= 0; y--)
if (board[y, x] == 0) return y;
return -1;
}

void Human_Play(object sender, MouseEventArgs e)
{
int x, y;
x = (Cursor.Position.X) / 99;
if ((x <= 6) && (x >= 0))
{
y = Y(x);
if (y != -1)
{
PlayCircle(x, y, red);
board[y, x] = -1;
}
}
}`

我正在使用 WinForms C# 为我的 uni 项目制作一个 connect 4 游戏。在试图让棋子落入棋盘时,它们就像在棋盘图像下方移动,将棋盘线隐藏在它们后面。知道如何使棋子图像移动到板后,但仍然可以通过板孔看到(如以下链接中的图片)吗?PS:棋盘和2张图片确实有透明背景。提前致谢^^ http://mathworld.wolfram.com/images/gifs/connect4.gif更新:我添加了代码。 (表格、红色、蓝色是我资源中的 3 张图片)如何编辑此代码,使棋子落在棋盘后面而不是突然出现?

最佳答案

你有两个选择:

  • 使用 GDI+ 命令 FillRectangleFillEllipse 绘制所有图形。
  • 使用控件保存网格和片段的位图。

我猜你正在尝试选择第二个选项。

然而,在 Winforms 中,只有嵌套 控件才支持透明度。然而,直接方法会与控件重叠,因此不起作用。

相反,您需要将底部控件嵌套在运动场中,并将上面的每一层都嵌套在下面的层中。由于您只需要在棋子上方一层,即网格,因此您必须将网格嵌套在棋子中。

这是可能的,但不是很自然,要实现这一点,您必须使该 block 足够大以容纳整个网格以及它在动画期间将占据的每个位置。因此,如果您在每次移动开始时设置它,它必须是棋盘高度及其全宽的两倍。

然后在 Timer.Tick 中将棋子向下移动并将嵌套在其中的网格向上移动,直到棋子找到它的位置。

最后将棋子绘制到棋盘PictureBoxImage中。使 BackgroundImage 显示相同的网格,这样您就可以在为下一步设置它之前简单地隐藏它..

因此,您可以通过在大小为 (board.width, board .height*2) 并将其分配给 PictureBox movingPiece

您已经准备了一张包含网格的位图并将其指定为 PictureBox 网格 的图像。现在嵌套三个控件:

movingPiece.Parent = board;
grid.Parent = movingPiece;

在 Tick 中,你将 pice doen 和它包含的网格向上移动:

void timer1_Tick(object sender, EventArgs e)
{
movingPiece.Top++;
grid.Top--;
// if finalposition etc..
}
  • 选项二更简单 imo:在 BackgroundImage 中创建网格。将设置好的棋子绘制到 Image 中。并在 Tick&Paint 事件中通过在重叠位置填充一个 Circle 和一个 Rectangle 来制作动画。

我愿意这样做;理解起来不是很自然,移动的像素也少得多,所以它可能也更快..

关于c# - 在另一个带有透明孔的图像后面移动图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34528440/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com