如何将 png 保存在矩形中?
How can i save a png in a rectangle?
我在 Winforms 中构建了一个贪吃蛇游戏,我需要一些 png 文件作为食物。
如何将 png 保存在矩形中?
这是一些代码:
class Food
{
#region Variables
private int x, y, widht = 20, hight = 20;
private SolidBrush brush;
public Rectangle foodrectangle;
int fieldsize_X = 47;// spawnrange
int fieldsize_Y = 38;// spawnrange
int squaresize = 20;
#endregion
"worth"变量是一个随机数,所以我可以有随机颜色。
#region Normal Food
public void changecolor()
{
switch (Form1.worth)
{
case 1:
brush = new SolidBrush(Color.Red);
break;
case 2:
brush = new SolidBrush(Color.Orange);
break;
case 3:
brush = new SolidBrush(Color.Yellow);
break;
case 4:
brush = new SolidBrush(Color.Blue);
break;
case 5:
brush = new SolidBrush(Color.White);
break;
default: brush = new SolidBrush(Color.Red); break;
}
}
public Food(Random randomfood)
{
changecolor();
x = randomfood.Next(0, fieldsize_X) * squaresize;
y = randomfood.Next(0, fieldsize_Y) * squaresize;
foodrectangle = new Rectangle(x, y, widht, hight);
}//creat Food
我使用计时器,所以我需要在时间开始前生成食物。当您按下箭头键时,计时器开始计时。
public void Foodlocation (Random randomfood)
{
changecolor();
x = randomfood.Next(0, fieldsize_X) * squaresize;// 0 = spawnrange to the next food
y = randomfood.Next(0, fieldsize_Y) * squaresize;
foodrectangle.X = x; //X.Max = 47 * 20 = 940
foodrectangle.Y = y; //Y.Max = 38 * 20 = 760
foodrectangle = new Rectangle(x, y, widht, hight);//spawn first food
}
public void drawFood (Graphics paper)
{
paper.FillRectangle(brush, foodrectangle);
}
#endregion
}
我想要一个依赖于 "worth" 变量的 png 而不是颜色。
如果 "worth" = 1,那么正方形有 apple.png 或类似的东西。
这应该有效
public void drawFood (Graphics paper)
{
// Load a png object from a file.
Image sourceImage = Image.FromFile(@"C:\my.png");
// Draw a portion of the source image.
paper.DrawImage(sourceImage, foodrectangle.X, foodrectangle.Y, new Rectangle(0, 0, squaresize, squaresize), GraphicsUnit.Pixel);
}
我在 Winforms 中构建了一个贪吃蛇游戏,我需要一些 png 文件作为食物。 如何将 png 保存在矩形中? 这是一些代码:
class Food
{
#region Variables
private int x, y, widht = 20, hight = 20;
private SolidBrush brush;
public Rectangle foodrectangle;
int fieldsize_X = 47;// spawnrange
int fieldsize_Y = 38;// spawnrange
int squaresize = 20;
#endregion
"worth"变量是一个随机数,所以我可以有随机颜色。
#region Normal Food
public void changecolor()
{
switch (Form1.worth)
{
case 1:
brush = new SolidBrush(Color.Red);
break;
case 2:
brush = new SolidBrush(Color.Orange);
break;
case 3:
brush = new SolidBrush(Color.Yellow);
break;
case 4:
brush = new SolidBrush(Color.Blue);
break;
case 5:
brush = new SolidBrush(Color.White);
break;
default: brush = new SolidBrush(Color.Red); break;
}
}
public Food(Random randomfood)
{
changecolor();
x = randomfood.Next(0, fieldsize_X) * squaresize;
y = randomfood.Next(0, fieldsize_Y) * squaresize;
foodrectangle = new Rectangle(x, y, widht, hight);
}//creat Food
我使用计时器,所以我需要在时间开始前生成食物。当您按下箭头键时,计时器开始计时。
public void Foodlocation (Random randomfood)
{
changecolor();
x = randomfood.Next(0, fieldsize_X) * squaresize;// 0 = spawnrange to the next food
y = randomfood.Next(0, fieldsize_Y) * squaresize;
foodrectangle.X = x; //X.Max = 47 * 20 = 940
foodrectangle.Y = y; //Y.Max = 38 * 20 = 760
foodrectangle = new Rectangle(x, y, widht, hight);//spawn first food
}
public void drawFood (Graphics paper)
{
paper.FillRectangle(brush, foodrectangle);
}
#endregion
}
我想要一个依赖于 "worth" 变量的 png 而不是颜色。 如果 "worth" = 1,那么正方形有 apple.png 或类似的东西。
这应该有效
public void drawFood (Graphics paper)
{
// Load a png object from a file.
Image sourceImage = Image.FromFile(@"C:\my.png");
// Draw a portion of the source image.
paper.DrawImage(sourceImage, foodrectangle.X, foodrectangle.Y, new Rectangle(0, 0, squaresize, squaresize), GraphicsUnit.Pixel);
}