A complete bipartite graph is a graph with two sets of vertices where there is no edge between vertices belonging to the same set; but all vertices of different sets are fully connected. Draw the K(10,10) where each set have 10 vertices.
Category Archives: nested for loop
Rainbow Colored Fully Connected 48-gon
We have project that draws fully connected 24-gon. Use colorsys library to gradually change the hue of lines as they get longer.
Fully Connected 24-gon
Refer to fully connected octagon before drawing this fully connected 24-gon.
Fully Connected Octagon
Use nested for loop and list to draw the following fully connected octagon.
Tic Tac Toe (Source Code Included)
Design a two player tic-tac-toe game with Python Turtle. You will need to use onclick() event to let two human players play against each other.
Source Code:
import turtle
screen = turtle.Screen()
screen.setup(800,800)
screen.title("Tic Tac Toe - PythonTurtle.Academy")
screen.setworldcoordinates(-5,-5,5,5)
screen.bgcolor('light gray')
screen.tracer(0,0)
turtle.hideturtle()
def draw_board():
turtle.pencolor('green')
turtle.pensize(10)
turtle.up()
turtle.goto(-3,-1)
turtle.seth(0)
turtle.down()
turtle.fd(6)
turtle.up()
turtle.goto(-3,1)
turtle.seth(0)
turtle.down()
turtle.fd(6)
turtle.up()
turtle.goto(-1,-3)
turtle.seth(90)
turtle.down()
turtle.fd(6)
turtle.up()
turtle.goto(1,-3)
turtle.seth(90)
turtle.down()
turtle.fd(6)
def draw_circle(x,y):
turtle.up()
turtle.goto(x,y-0.75)
turtle.seth(0)
turtle.color('red')
turtle.down()
turtle.circle(0.75, steps=100)
def draw_x(x,y):
turtle.color('blue')
turtle.up()
turtle.goto(x-0.75,y-0.75)
turtle.down()
turtle.goto(x+0.75,y+0.75)
turtle.up()
turtle.goto(x-0.75,y+0.75)
turtle.down()
turtle.goto(x+0.75,y-0.75)
def draw_piece(i,j,p):
if p==0: return
x,y = 2*(j-1), -2*(i-1)
if p==1:
draw_x(x,y)
else:
draw_circle(x,y)
def draw(b):
draw_board()
for i in range(3):
for j in range(3):
draw_piece(i,j,b[i][j])
screen.update()
# return 1 if player 1 wins, 2 if player 2 wins, 3 if tie, 0 if game is not over
def gameover(b):
if b[0][0]>0 and b[0][0] == b[0][1] and b[0][1] == b[0][2]: return b[0][0]
if b[1][0]>0 and b[1][0] == b[1][1] and b[1][1] == b[1][2]: return b[1][0]
if b[2][0]>0 and b[2][0] == b[2][1] and b[2][1] == b[2][2]: return b[2][0]
if b[0][0]>0 and b[0][0] == b[1][0] and b[1][0] == b[2][0]: return b[0][0]
if b[0][1]>0 and b[0][1] == b[1][1] and b[1][1] == b[2][1]: return b[0][1]
if b[0][2]>0 and b[0][2] == b[1][2] and b[1][2] == b[2][2]: return b[0][2]
if b[0][0]>0 and b[0][0] == b[1][1] and b[1][1] == b[2][2]: return b[0][0]
if b[2][0]>0 and b[2][0] == b[1][1] and b[1][1] == b[0][2]: return b[2][0]
p = 0
for i in range(3):
for j in range(3):
p += (1 if b[i][j] > 0 else 0)
if p==9: return 3
else: return 0
def play(x,y):
global turn
i = 3-int(y+5)//2
j = int(x+5)//2 - 1
if i>2 or j>2 or i<0 or j<0 or b[i][j]!=0: return
if turn == 'x': b[i][j], turn = 1, 'o'
else: b[i][j], turn = 2, 'x'
draw(b)
r = gameover(b)
if r==1:
screen.textinput("Game over!","X won!")
elif r==2:
screen.textinput("Game over!","O won!")
elif r==3:
screen.textinput("Game over!", "Tie!")
b = [ [ 0,0,0 ], [ 0,0,0 ], [ 0,0,0 ] ]
draw(b)
turn = 'x'
screen.onclick(play)
turtle.mainloop()
Café Wall Illusion with Python Turtle
All lines are actually parallel! For more information about this illusion check out Wikipedia page.
Hermann Grid Illusion with Python Turtle with Source Code
What do you see? Draw this simple shape. Check out this Wikipedia article to know more about this illusion.
import turtle
screen = turtle.Screen()
screen.setup(500,500)
screen.title('Hermann Grid Illusion - PythonTurtle.Academy')
screen.tracer(0)
turtle.hideturtle()
turtle.speed(0)
turtle.bgcolor('black')
turtle.pensize(4)
turtle.color('light gray')
for x in range(-502,500,25):
turtle.up()
turtle.goto(x,-500)
turtle.down()
turtle.seth(90)
turtle.fd(1000)
for y in range(-502,500,25):
turtle.up()
turtle.goto(-500,y)
turtle.down()
turtle.seth(0)
turtle.fd(1000)
turtle.up()
for x in range(-502,500,25):
for y in range(-502,500,25):
turtle.goto(x,y)
turtle.dot('white')
screen.update()
Grapes
Use loops to draw the following shape:
Tutorial: Conway’s Game of Life with Python Turtle
In this tutorial, we are you going show how to implement Conway’s Game of Life simulation with Python Turtle.
Step 1. Drawing The Grid
This step should be straightforward. We just need n horizontal and n vertical lines centered in the screen. The following is the code snippet that draws the grid.
screen=turtle.Screen()
turtle.setup(1000,1000)
turtle.title("Conway's Game of Life - PythonTurtle.Academy")
turtle.hideturtle()
turtle.speed(0)
turtle.tracer(0,0)
n = 50 # nxn grid
def draw_line(x1,y1,x2,y2): # this function draw a line between x1,y1 and x2,y2
turtle.up()
turtle.goto(x1,y1)
turtle.down()
turtle.goto(x2,y2)
def draw_grid(): # this function draws nxn grid
turtle.pencolor('gray')
turtle.pensize(3)
x = -400
for i in range(n+1):
draw_line(x,-400,x,400)
x += 800/n
y = -400
for i in range(n+1):
draw_line(-400,y,400,y)
y += 800/n
draw_grid()
screen.update()
It should a grid like the following picture:
Step 2: Creating Life
We need data structure to store the lives in the n x n cells. The natural data structure for this purpose is a list of lists. We are going to use value 1 to represent ‘life’ and 0 to represent ‘no life’. The lives in cells will be randomly generated with 1/7 probability of having life. The following is the code snippet that creates and initializes lives:
life = list() # create an empty list
def init_lives():
for i in range(n):
liferow = [] # a row of lives
for j in range(n):
if random.randint(0,7) == 0: # 1/7 probability of life
liferow.append(1) # 1 means life
else:
liferow.append(0) # 0 means no life
life.append(liferow) # add a row to the life list -> life is a list of list
Step 3: Displaying Life in Cells
The next task is to draw live cells in the grid. We will create and use a new turtle called lifeturtle to draw the live cells. Because cells can become alive or dead, we need to erase them and redraw in each cycle. However, there is no need to erase the grid. By just clearing the lifeturtle, there is no need to redraw the grid. The following is complete code for the first 3 steps.
import turtle
import random
screen=turtle.Screen()
turtle.setup(1000,1000)
turtle.title("Conway's Game of Life - PythonTurtle.Academy")
turtle.hideturtle()
turtle.speed(0)
turtle.tracer(0,0)
lifeturtle = turtle.Turtle() # turtle for drawing life
lifeturtle.up()
lifeturtle.hideturtle()
lifeturtle.speed(0)
lifeturtle.color('black')
n = 50 # nxn grid
def draw_line(x1,y1,x2,y2): # this function draw a line between x1,y1 and x2,y2
turtle.up()
turtle.goto(x1,y1)
turtle.down()
turtle.goto(x2,y2)
def draw_grid(): # this function draws nxn grid
turtle.pencolor('gray')
turtle.pensize(3)
x = -400
for i in range(n+1):
draw_line(x,-400,x,400)
x += 800/n
y = -400
for i in range(n+1):
draw_line(-400,y,400,y)
y += 800/n
life = list() # create an empty list
def init_lives():
for i in range(n):
liferow = [] # a row of lives
for j in range(n):
if random.randint(0,7) == 0: # 1/7 probability of life
liferow.append(1) # 1 means life
else:
liferow.append(0) # 0 means no life
life.append(liferow) # add a row to the life list -> life is a list of list
def draw_square(x,y,size): # draws a filled square
lifeturtle.up()
lifeturtle.goto(x,y)
lifeturtle.down()
lifeturtle.seth(0)
lifeturtle.begin_fill()
for i in range(4):
lifeturtle.fd(size)
lifeturtle.left(90)
lifeturtle.end_fill()
def draw_life(x,y): # draws life in (x,y)
lx = 800/n*x - 400 # converts x,y to screen coordinate
ly = 800/n*y - 400
draw_square(lx+1,ly+1,800/n-2)
def draw_all_life(): # draws all life
global life
for i in range(n):
for j in range(n):
if life[i][j] == 1: draw_life(i,j) # draw live cells
draw_grid()
init_lives()
draw_all_life()
screen.update()
It should draw a shape like the following image:
Step 4: Updating Life Forever
The next step is to update the life based on the Conway’s Rule:
- If a cell with fewer than two or more than three live neighbors dies because of underpopulation or overpopulation.
- If a live cell has two or three live neighbors, the cell survives to the next generation.
- If a dead cell has exactly three live neighbors, it becomes alive.
We will create a function update_life() that will update the cells based on these rules. Then we will call this function again with the Turtle’s timer event. The following is the complete code for animating the Conway’s Game of Life:
import turtle
import random
import copy
screen=turtle.Screen()
turtle.setup(1000,1000)
turtle.title("Conway's Game of Life - PythonTurtle.Academy")
turtle.hideturtle()
turtle.speed(0)
turtle.tracer(0,0)
lifeturtle = turtle.Turtle() # turtle for drawing life
lifeturtle.up()
lifeturtle.hideturtle()
lifeturtle.speed(0)
lifeturtle.color('black')
n = 50 # nxn grid
def draw_line(x1,y1,x2,y2): # this function draw a line between x1,y1 and x2,y2
turtle.up()
turtle.goto(x1,y1)
turtle.down()
turtle.goto(x2,y2)
def draw_grid(): # this function draws nxn grid
turtle.pencolor('gray')
turtle.pensize(3)
x = -400
for i in range(n+1):
draw_line(x,-400,x,400)
x += 800/n
y = -400
for i in range(n+1):
draw_line(-400,y,400,y)
y += 800/n
life = list() # create an empty list
def init_lives():
for i in range(n):
liferow = [] # a row of lives
for j in range(n):
if random.randint(0,7) == 0: # 1/7 probability of life
liferow.append(1) # 1 means life
else:
liferow.append(0) # 0 means no life
life.append(liferow) # add a row to the life list -> life is a list of list
def draw_square(x,y,size): # draws a filled square
lifeturtle.up()
lifeturtle.goto(x,y)
lifeturtle.down()
lifeturtle.seth(0)
lifeturtle.begin_fill()
for i in range(4):
lifeturtle.fd(size)
lifeturtle.left(90)
lifeturtle.end_fill()
def draw_life(x,y): # draws life in (x,y)
lx = 800/n*x - 400 # converts x,y to screen coordinate
ly = 800/n*y - 400
draw_square(lx+1,ly+1,800/n-2)
def draw_all_life(): # draws all life
global life
for i in range(n):
for j in range(n):
if life[i][j] == 1: draw_life(i,j) # draw live cells
def num_neighbors(x,y): # computes the number of life neighbours for cell[x,y]
sum = 0
for i in range(max(x-1,0),min(x+1,n-1)+1):
for j in range(max(y-1,0),min(y+1,n-1)+1):
sum += life[i][j]
return sum - life[x][y]
def update_life(): # update life for each cycle
global life
newlife = copy.deepcopy(life) # make a copy of life
for i in range(n):
for j in range(n):
k = num_neighbors(i,j)
if k < 2 or k > 3:
newlife[i][j] = 0
elif k == 3:
newlife[i][j] = 1
life = copy.deepcopy(newlife) # copy back to life
lifeturtle.clear() # clears life in previous cycle
draw_all_life()
screen.update()
screen.ontimer(update_life,200) # update life every 0.2 second
draw_grid()
init_lives()
update_life()
Related Projects:
Find Different Color Game with Python Turtle
Make a fun game that tests how fast you can identify very small color variation. You can use colorsys library to vary hue, saturation, brightness, or any combination of them. Make the game more difficult by reducing the amount of variation as the level increases. Also, make the size of variation smaller as level increases.