xmas.py

Created by vnap0v

Created on December 16, 2025

1.13 KB

Drawing a xmas tree.

Uses following modules:

  • turtle for deawing
  • kandinsky for background color
  • random for colors of stars and position of background stars
  • math to calculate the spirals using cos() and sin()


# XMAS tree using turtle
from turtle import *
from math import * 
from random import *
import kandinsky

# draws 1 star at x,y with given size
def star(x,y,size):
  penup()
  goto(x,y)
  pendown()
  for _ in range(5):
    forward(size)
    right(144)

# parameters
N=250
Nstar=20
cycles=13
col=("blue","red","yellow","pink","orange","purple")
col_bg=(20,20,100)
col_bg_star=(30,30,150)
# turtle settings
speed(0)
hideturtle()
colormode(255)
# background
kandinsky.fill_rect(0,0,320,240,kandinsky.color(col_bg))
# background stars
colormode(255)
pensize(2)
color(col_bg_star)
for _ in range(25):
  star(randint(-150,150),randint(-50,100),18)
# draw trunk
color("brown")
pensize(10)
penup()
goto(0,-50)
pendown()
goto(0,-90)
# draw tree
color("green")
pensize(4)
for k in range(N):
  kn=k/(N-1)
  angle=kn*2*3.14*cycles
  x=int((90*kn+4)*cos(angle))
  y=int(100-150*kn+20*kn*sin(angle))
  if k==0:
    penup()
  elif k==1:
    pendown()
  goto(x,y)
# draw stars on tree
pensize(7)
for k in range(Nstar):
  kn=k/(Nstar)
  angle=kn*2*3.14*cycles
  x=int((100*kn+4)*cos(angle))
  y=int(100-150*kn+20*kn*sin(angle))
  color(choice(col))
  star(x,y,8)



During your visit to our site, NumWorks needs to install "cookies" or use other technologies to collect data about you in order to:

With the exception of Cookies essential to the operation of the site, NumWorks leaves you the choice: you can accept Cookies for audience measurement by clicking on the "Accept and continue" button, or refuse these Cookies by clicking on the "Continue without accepting" button or by continuing your browsing. You can update your choice at any time by clicking on the link "Manage my cookies" at the bottom of the page. For more information, please consult our cookies policy.