Roll the dice

with functions

Draw a dice with one dot on the canvas. The dice should appear at the x- and y-coordinate 0. The side of the dice should be 100 pixels wide.

Background, Circle, Rect, RectMode, Fill

Background(228);

RectMode(RectModes.Corner);
Rect(0, 0, 100, 100);

Fill(0);
Circle(50, 50, 25);

 

Create a method DrawDice from the first exercise. It should be given the x and y coordinates as parameters.

void, double, Background, Circle, Rect, RectMode, Fill

DrawDice(20, 40);

void DrawDice(double x, double y)
{
	Background(228);
	
	RectMode(RectModes.Corner);
	Rect(x, y, 100, 100);
	
	Fill(0);
	Circle(50+x, 50+y, 25);
}

 

Set the timer variable to your own draw function. In the Draw function, you call your DrawDice method. Use the mouse position for x- and y-coordinates.
Timer, MouseX, MouseY, void, double, Background, Circle, Rect, RectMode, Fill

Timer = Draw;

void Draw()
{
	DrawDice(MouseX, MouseY);
}

void DrawDice(double x, double y)
{
	Background(228);
	
	RectMode(RectModes.Corner);
	Rect(x, y, 100, 100);
	
	Fill(0);
	Circle(50+x, 50+y, 25);
}

 

Now change the function DrawDice and add a parameter points of the data type double. In DrawDice you then paint the dice with the given number of points.
Note that you have 7 different positions for the points on a dice. To draw the 3, you can also simply call DrawDice with 1 and 2. To draw the 4, you can also simply call DrawDice with 2 and then draw the last two circles.

if...else..., +, /, Timer, MouseX, MouseY, void, double, Background, Circle, Rect, RectMode, Fill


Background(228);

DrawDice(0,0,1);
DrawDice(100,0,2);
DrawDice(200,0,3);
DrawDice(300,0,4);
DrawDice(400,0,5);
DrawDice(500,0,6);

void DrawDice(double x, double y, double points)
{
	Fill(255, 0);	// 0 = transparent, otherwise white
	
	RectMode(RectModes.Corner);
	Rect(x, y, 100, 100);
	
	Fill(0);
	
	if(points == 1)
	{
		Circle(100/2+x, 100/2+y, 20);
	}
	else if(points == 2)
	{
		Circle(100/4+x,    100/4+y, 20);		
		Circle(100/4+50+x, 100/4+50+y, 20);		
	}
	else if(points == 3)
	{
		DrawDice(x, y, 1);
		DrawDice(x, y, 2);
	}
	else if(points == 4)
	{
		DrawDice(x, y, 2);
		Circle(100/4+50+x, 100/4+y, 20);		
		Circle(100/4+x,    100/4+50+y, 20);		
	}
	else if(points == 5)
	{
		DrawDice(x, y, 1);
		DrawDice(x, y, 4);		
	}
	else if(points == 6)
	{
		DrawDice(x, y, 4);
		Circle(100/4+50+x, 100/2+y, 20);		
		Circle(100/4+x,    100/2+y, 20);				
	}
}

 

Modify Exercise 5 so that the dice is drawn at the mouse position. You get the points of the dice using the random function. Whenever the mouse button is pressed, the dice should be re-rolled.
Since the Random function returns a number with decimal places, you have to determine the integer part. To do this, use the Math.Floor function and pass the value returned by the Random function.

Random, Math.Floor, if...else..., +, /, Timer, MouseX, MouseY, void, double, Background, Circle, Rect, RectMode, Fill

Background(228);

Timer = Draw;
double points = Random(1, 6);
points = Math.Floor(points); 

void Draw()
{
	Background(228);
	
	if(MouseIsPressed)
	{
		points = Random(1, 6);
		points = Math.Floor(points);
		
		PrintLn(points);
	}
	DrawDice(MouseX, MouseY, points);
}

void DrawDice(double x, double y, double points)
{
	Fill(255, 0);	// 0 = transparent, otherwise white
	
	RectMode(RectModes.Corner);
	Rect(x, y, 100, 100);
	
	Fill(0);
	
	if(points == 1)
	{
		Circle(100/2+x, 100/2+y, 20);
	}
	else if(points == 2)
	{
		Circle(100/4+x,    100/4+y, 20);		
		Circle(100/4+50+x, 100/4+50+y, 20);		
	}
	else if(points == 3)
	{
		DrawDice(x, y, 1);
		DrawDice(x, y, 2);
	}
	else if(points == 4)
	{
		DrawDice(x, y, 2);
		Circle(100/4+50+x, 100/4+y, 20);		
		Circle(100/4+x,    100/4+50+y, 20);		
	}
	else if(points == 5)
	{
		DrawDice(x, y, 1);
		DrawDice(x, y, 4);		
	}
	else if(points == 6)
	{
		DrawDice(x, y, 4);
		Circle(100/4+50+x, 100/2+y, 20);		
		Circle(100/4+x,    100/2+y, 20);				
	}
}

 This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License