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(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.

background, circle, rect, rectMode, fill

def drawDice(x, y):
	background(228)
	
	rectMode(CORNER)
	rect(x, y, 100, 100)
	
	fill(0)
	circle(50+x, 50+y, 25)
	
drawDice(20, 40)

 

Use the timer function to set 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, background, circle, rect, rectMode, fill

def drawDice(x, y):
	background(228)
	
	rectMode(CORNER)
	rect(x, y, 100, 100)
	
	fill(0)
	circle(50+x, 50+y, 25)
	
def draw():
	drawDice(mouseX(), mouseY())

timer(draw)

 

Now change the function drawDice and add a parameter points. 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...elif...else..., +, /, timer, mouseX, mouseY, background, circle, rect, rectMode, fill

def drawDice(x, y, points):

	fill(255, 0)	# 0 = transparent, otherwise white
	
	rectMode(CORNER)
	rect(x, y, 100, 100)
	
	fill(0)
	
	if points == 1:
		circle(100/2+x, 100/2+y, 20)
	elif points == 2:
		circle(100/4+x,    100/4+y, 20)	
		circle(100/4+50+x, 100/4+50+y, 20)	
	elif points == 3:
		drawDice(x, y, 1)
		drawDice(x, y, 2)
	elif points == 4:
		drawDice(x, y, 2)
		circle(100/4+50+x, 100/4+y, 20)	
		circle(100/4+x,    100/4+50+y, 20)	
	elif points == 5:
		drawDice(x, y, 1)
		drawDice(x, y, 4)	
	elif points == 6:
		drawDice(x, y, 4)
		circle(100/4+50+x, 100/2+y, 20)		
		circle(100/4+x,    100/2+y, 20)				

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)

 

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 int function and pass the value returned by the random function.

random, int, if...elif...else..., +, /, timer, mouseX, mouseY, background, circle, rect, rectMode, fill

def drawDice(x, y, points):

	fill(255, 0)	# 0 = transparent, otherwise white
	
	rectMode(CORNER)
	rect(x, y, 100, 100)
	
	fill(0)
	
	if points == 1:
		circle(100/2+x, 100/2+y, 20)
	elif points == 2:
		circle(100/4+x,    100/4+y, 20)	
		circle(100/4+50+x, 100/4+50+y, 20)	
	elif points == 3:
		drawDice(x, y, 1)
		drawDice(x, y, 2)
	elif points == 4:
		drawDice(x, y, 2)
		circle(100/4+50+x, 100/4+y, 20)	
		circle(100/4+x,    100/4+50+y, 20)	
	elif points == 5:
		drawDice(x, y, 1)
		drawDice(x, y, 4)	
	elif points == 6:
		drawDice(x, y, 4)
		circle(100/4+50+x, 100/2+y, 20)		
		circle(100/4+x,    100/2+y, 20)				

def draw():
	global points
	background(228)
	
	if mouseIsPressed():
		points = random(1, 7)
		points = int(points)
		
	print(points)
	drawDice(mouseX(), mouseY(), points)

background(228)

timer(draw)
points = random(1, 7)
points = int(points)

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