Reputation: 53
I want to draw four shapes(rectangle, square, triangle, circle) on a panel in C# user control paint method. I have tried below code but it will draw shapes at same location(x,y). I declared four random variables but it did not work. I need to draw those shapes separately different locations. Is there anyone can help me?
Here is my code:
int recX;
int recY;
int squX;
int squY;
int circleX;
int circleY;
int triX;
int triY;
public int shapeType { get; set; }
public GameArea()
{
InitializeComponent();
food = new Food(randFood);
Random randRectangle = new Random();
recX = randRectangle.Next(1, 35) * 10;
recY = randRectangle.Next(1, 35) * 10;
Random randSquare = new Random();
squX = randSquare.Next(1, 35) * 10;
squY = randSquare.Next(1, 35) * 10;
Random randCircle = new Random();
circleX = randCircle.Next(1, 35) * 10;
circleY = randCircle.Next(1, 35) * 10;
Random randTriangle = new Random();
triX = randTriangle.Next(1, 35) * 10;
triY = randTriangle.Next(1, 35) * 10;
}
private void GameArea_Paint(object sender, PaintEventArgs e)
{
paper = e.Graphics;
if (shapeType != 0)
{
if(shapeType == Convert.ToInt32(Enums.ShapeTypes.Rectangle))
{
food.drawFood(paper, shapeType, recX,recY);
food.drawSquare(paper, squX, squY);
food.drawCircle(paper, circleX, circleY);
}
}
Upvotes: 0
Views: 1172
Reputation: 1702
Use the same random over and over otherwise you'll be using the same seed.
Example:
Random rndLocGen = new Random();
recX = rndLocGen.Next(1, 35) * 10;
recY = rndLocGen.Next(1, 35) * 10;
cirX = rndLocGen.Next(1, 35) * 10;
cirY = rndLocGen.Next(1, 35) * 10;
Upvotes: 1