Rhombus Madness

Use Python and Turtle and Random library to draw the 50 random Rhombus shapes as shown in the following figure.

50 Random Rhombus

Hints:

  • Make a function that draws a rhombus given the center, width, height, and the direction.
  • Use random library’s uniform function to generate positions, size, direction, and color

Related Projects:

Projects with similar difficulty

Geometry

Circle of Isosceles Triangles with Python and Turtle

Use the Isosceles triangle function created in this project, draw the circle of isosceles triangles as shown below. Your code should easily change the number of triangles, and radius and height of the triangles.

Source Code:

from turtle import *
import math
import random

title('Isosceles Triangle Circle - PythonTurtle.Academy')
setup(1000,1000)
setworldcoordinates(-500,-500,500,500)
hideturtle()
tracer(0,0)

# x,y is the center of the base, width: length of base, height: height of triangle from the top to base
# direction:direction from the center of base to top
def IsoscelesTriangle(x,y,width,height,direction,c):
    up()
    goto(x,y)
    seth(direction-90)
    fd(width/2)
    p1x, p1y = xcor(), ycor() # first point: bottom right
    back(width)
    p2x, p2y = xcor(), ycor() # second point: bottom left
    goto(x,y)
    seth(direction)
    fd(height)
    p3x, p3y = xcor(), ycor() # third point: top
    goto(p1x,p1y)
    down()
    fillcolor(c)
    begin_fill()
    goto(p2x,p2y)
    goto(p3x,p3y)
    goto(p1x,p1y)
    end_fill()

n=12
r=300
width=2*r*math.sin(math.radians(180/n))
height=200
for i in range(n):    
    IsoscelesTriangle(r*math.cos(math.radians(180/n))*math.cos(math.radians(i*360/n)),
                      r*math.cos(math.radians(180/n))*math.sin(math.radians(i*360/n)),width,height,i*360/n,'blue')
    
update()

Related Projects:

Geometry

Loops

Isosceles Triangle Madness

Use Python and Turtle and Random library to draw the 50 random isosceles triangles as shown in the following figure.

50 Random Isosceles Triangles

Hints:

  • Make a function that draws isosceles triangle given the center of the base, length of the base, height, and the direction from the base to the top of the triangle.
  • Use random library’s uniform function to generate positions, size, direction, and color.

Source Code:

from turtle import *
import math
import random

title('Isosceles Triangle Madness - PythonTurtle.Academy')
setup(1000,1000)
setworldcoordinates(-500,-500,500,500)
hideturtle()
tracer(0,0)

# x,y is the center of the base, width: length of base, height: height of triangle from the top to base
# direction:direction from the center of base to top
def IsoscelesTriangle(x,y,width,height,direction,c):
    up()
    goto(x,y)
    seth(direction-90)
    fd(width/2)
    p1x, p1y = xcor(), ycor() # first point: bottom right
    back(width)
    p2x, p2y = xcor(), ycor() # second point: bottom left
    goto(x,y)
    seth(direction)
    fd(height)
    p3x, p3y = xcor(), ycor() # third point: top
    goto(p1x,p1y)
    down()
    fillcolor(c)
    begin_fill()
    goto(p2x,p2y)
    goto(p3x,p3y)
    goto(p1x,p1y)
    end_fill()

for _ in range(50):
    IsoscelesTriangle(random.uniform(-400,400),random.uniform(-400,400),
                      random.uniform(30,300), random.uniform(30,300),
                      random.uniform(0,360),
                      (random.uniform(0,1),random.uniform(0,1),random.uniform(0,1)))
              
update()

Related Projects

Automatic Wordle Solver with Python and Turtle (Source Code Included)

Wordle is a very popular puzzle game. Here is the source code that automatically solves the wordle problems. There isn’t too much trick. This code just randomly picks a valid word. Click on the letters to match the colors on wordle and then hit enter. This program will automatically guess new words until the word has been guessed correctly.

import random
import turtle
from tkinter import * 
from tkinter import messagebox

screen = turtle.Screen()
screen.setup(1000,1000)
screen.title("Wordle Solver - PythonTurtle.Academy")
turtle.speed(0)
turtle.hideturtle()
screen.tracer(0,0)
screen.bgcolor('black')
turtle.color('white')

gs = 0
state = []
for i in range(6):
  state.append([-1]*5)

def getwords(words, cs, count=False):
  res = []
  cnt = 0
  for w in words:
    t = list(w)
    flag = True
    cnt = dict()
    # first loop checks only positions are set
    for l,p in cs:
      if p<0: continue
      if cs[(l,p)] > 0:
        if t[p] == l:
          t[p] = '*'
          if l in cnt: cnt[l] += 1
          else: cnt[l] = 1
        else:
          flag = False
          break
      else:
      	if t[p] == l:
      		flag = False
      		break
    if (not flag): continue
    # second loop checks only positions are not set
    for l,p in cs:
      if p!=-1: continue
      v = 0 if l not in cnt else cnt[l]
      for _ in range(cs[(l,p)]-v):
      	try:
      		p = t.index(l)
      		t[p] = '*'
      	except ValueError:
      		flag = False
      		break
      if (not flag): break
    if (not flag): continue
    # third loops checks non-existent letter
    for l,p in cs:
      if p!=-2: continue
      if l in t:
        flag = False
        break
    
    if flag: 
    	if count: cnt += 1
    	else: res.append(w)
  if count: return cnt
  else: return res

def guess_random(words):
  return random.choice(words)
		
def draw_square(coord,s,fc='black'):
  turtle.up()
  x = coord[0]
  y = coord[1]
  turtle.goto(x-s/2,y-s/2)
  turtle.seth(0)
  turtle.down()
  turtle.fillcolor(fc)
  turtle.begin_fill()
  for _ in range(4):
    turtle.fd(s)
    turtle.left(90)
  turtle.end_fill()

def get_coord(i,j):
  return -200+100*j, 300-100*i

def draw_board():
  turtle.pencolor('dark gray')
  for i in range(6):
    for j in range(5):
      draw_square(get_coord(i,j),80)

def display_word(w):
  turtle.up()
  turtle.color('white')
  for i in range(5):
    x,y = get_coord(gs,i)
    turtle.goto(x,y-23)
    turtle.write(w[i].upper(),align='center',font=('Arial',40,'bold'))

def update_cell(i,j):
  global w,state
  x, y = get_coord(i,j)
  turtle.pencolor('dark gray')
  if state[i][j] == 0:
    fc = 'dark gray'
  elif state[i][j] == 1:
    fc = 'goldenrod'
  else: fc = 'green'
  draw_square(get_coord(i,j),80,fc)
  turtle.up()
  turtle.color('white')
  turtle.goto(x,y-23)
  turtle.write(w[j].upper(),align='center',font=('Arial',40,'bold'))
  screen.update()
  
def play(x,y):
  flag = False
  for i in range(6):
    if flag: break
    for j in range(5):
      cx, cy = get_coord(i,j)
      if (cx-x)**2 + (cy-y)**2 < 1600:
        flag = True
        ci = i
        cj = j
        break
  if not flag: return
  if ci != gs: return
  state[ci][cj] = (state[ci][cj] + 1) % 3
  update_cell(ci,cj)

def submit():
  global state
  global gs
  global w,words

  for i in range(5):
    if state[gs][i] == -1: return

  cs = dict()
  for i in range(5):
    if state[gs][i] == 0: # letter doesn't exist
      cs[(w[i],-2)] = 1
    else:
      if (w[i],-1) not in cs:
        cs[(w[i],-1)] = 1
      else:
        cs[(w[i],-1)] += 1
      if state[gs][i] == 1: cs[(w[i],i)] = 0
      else: cs[(w[i],i)] = 1
  words = getwords(words,cs)
  print(len(words))
  w = guess_random(words)
  gs += 1
  display_word(w)
  if len(words) == 1:
    messagebox.showinfo("Done", "Congratulations!")
    turtle.bye()

  screen.update()

orig_words = []
f = open('wordle_words.txt','r')
for w in f:
  orig_words.append(w.strip())

cs = dict()
words = getwords(orig_words,cs)
w = guess_random(words)
w = 'tesla'
draw_board()
display_word(w)
screen.update()
screen.onclick(play)
screen.onkey(submit, 'Return')
screen.listen()
screen.mainloop()

You will also need to download a list of words as ‘wordle_words.txt’.

Diamond Suit Shape with Python and Turtle (Source Code)

Diamond Shape

Write a program that draw a diamonds suit shape with Python and Turtle. Please note that four sides are curves not straight lines.

A more challenging task is to make a function that draws a diamond shape at any center location , width, height, and arc angle. For example the diamond shape above has width/height ratio of 3/4 and four sides are arcs with 20 degree angle. You may need to apply basic trigonometry knowledge to solve this problem.

Source Code:

import turtle
import math

screen = turtle.Screen()
screen.title('Diamond Shape - PythonTurtle.Academy')
screen.setup(1000,1000)
screen.setworldcoordinates(-1000,-1000,1000,1000)
turtle.speed(0)
turtle.hideturtle()

def diamond(x,y, width, height, angle):
    turtle.up()
    turtle.color('red')
    turtle.goto(x,y-height/2)
    d = ((width/2)**2 + (height/2)**2)**0.5
    radius = d*0.5/math.sin(math.radians(angle/2))
    turtle.down()
    turtle.begin_fill()
    turtle.seth(turtle.towards(x-width/2,y)-angle/2)
    turtle.circle(radius, angle,20)
    turtle.seth(turtle.towards(x,y+height/2)-angle/2)
    turtle.circle(radius, angle,20)
    turtle.seth(turtle.towards(x+width/2,y)-angle/2)
    turtle.circle(radius, angle,20)
    turtle.seth(turtle.towards(x,y-height/2)-angle/2)
    turtle.circle(radius, angle,20)
    turtle.end_fill()    

diamond(0,0,1200,1600,20)