Created
June 4, 2020 08:16
-
-
Save objarni/a2d46b574a0a16a619c87ecb50c64059 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# | |
# Python script to wish Merry Christmas using turtle. | |
# Author - Anurag Rana | |
# | |
from random import randint | |
### | |
# Added for approval testing purposes | |
TESTMODE = False | |
import random | |
from sys import argv | |
from mock import Mock | |
if len(argv) == 2 and argv[1] == "test": | |
print("Running program in test mode.") | |
TESTMODE = True | |
random.seed(1) | |
RETURN_VALUE = 1000 | |
from pprint import pformat | |
def generate_class(name): | |
class FakeClass: | |
def __getattr__(self, attr): | |
def fn(*args, **kwargs): | |
arglist = pformat(args)[1:-2] | |
kwarglist = pformat(kwargs) | |
vars = locals() | |
vars["name"] = name | |
vars["attr"] = attr | |
fmtstr = "{name}.{attr}()" | |
if args and kwargs: | |
fmtstr = "{name}.{attr}({arglist}, {kwarglist})" | |
elif args: | |
fmtstr = "{name}.{attr}({arglist})" | |
elif kwargs: | |
fmtstr = "{name}.{attr}({kwarglist})" | |
print(fmtstr.format(**vars)) | |
global RETURN_VALUE | |
RETURN_VALUE += 10 | |
return RETURN_VALUE | |
return fn | |
return FakeClass | |
FakeScreen = generate_class("screen") | |
Turtle = generate_class("turtle") | |
# Added for approval testing purposes | |
### | |
if not TESTMODE: | |
from turtle import * | |
def set_color(turtle, color): | |
turtle.color(color) | |
turtle.fillcolor(color) | |
def draw_rectangle(turtle, color, x, y, width, height): | |
turtle.penup() | |
set_color(turtle, color) | |
turtle.goto(x, y) | |
turtle.pendown() | |
turtle.begin_fill() | |
turtle.forward(width) | |
turtle.left(90) | |
turtle.forward(height) | |
turtle.left(90) | |
turtle.forward(width) | |
turtle.left(90) | |
turtle.forward(height) | |
turtle.left(90) | |
# fill the above shape | |
turtle.end_fill() | |
# Reset the orientation of the turtle | |
turtle.setheading(0) | |
def draw_circle(turtle, x, y, radius, color): | |
turtle.penup() | |
set_color(turtle, color) | |
turtle.goto(x, y) | |
turtle.pendown() | |
turtle.begin_fill() | |
turtle.circle(radius) | |
turtle.end_fill() | |
BG_COLOR = "#0080ff" | |
## <-- APPROVAL RIGGING | |
def get_screen(turtle): | |
return turtle.getscreen() if not TESTMODE else FakeScreen() | |
## --> | |
def draw_moon(turtle): | |
# create a full circle | |
draw_circle(turtle, 230, 180, 60, "white") | |
# overlap with full circle of BG color to make a crescent shape | |
draw_circle(turtle, 220, 180, 60, BG_COLOR) | |
def draw_star(turtle, x_star, y_star, size, color): | |
turtle.penup() | |
turtle.color(color) | |
turtle.goto(x_star, y_star) | |
turtle.begin_fill() | |
turtle.pendown() | |
for i in range(5): | |
turtle.forward(size) | |
turtle.right(144) | |
turtle.end_fill() | |
def draw_tree(turtle, y, width): | |
# create trunk | |
draw_rectangle(turtle, "red", -15, y - 60, 30, 60) | |
# create branches and needles | |
turtle.speed(10) | |
while width > 10: | |
width = width - 10 | |
height = 10 | |
x = 0 - width / 2 | |
draw_rectangle(turtle, "green", x, y, width, height) | |
y = y + height | |
# create a star a top of tree | |
star_height = 40 | |
turtle.speed(1) | |
draw_star(turtle, -20, y + 10, 40, "yellow") | |
return y + star_height | |
def run(): | |
# "Yesterday is history, tomorrow is a mystery, but today is a gift. That is why it is called the present.” | |
# — Oogway to Po under the peach tree, Kung Fu Panda | |
oogway = Turtle() | |
# set turtle speed | |
oogway.speed(2) | |
screen = get_screen(oogway) | |
# set background color | |
screen.bgcolor(BG_COLOR) | |
# set tile of screen | |
screen.title("Merry Christmas") | |
# maximize the screen | |
screen.setup(width=1.0, height=1.0) | |
# create tree | |
tree_height = draw_tree(oogway, y=-100, width=240) | |
# create moon in sky | |
draw_moon(oogway) | |
# now add few stars in sky | |
oogway.speed(10) | |
number_of_stars = randint(20, 30) | |
# print(number_of_stars) | |
for _ in range(0, number_of_stars): | |
x_star = randint( | |
-(screen.window_width() // 2), screen.window_width() // 2 | |
) | |
y_star = randint(tree_height, screen.window_height() // 2) | |
size = randint(5, 20) | |
draw_star(oogway, x_star, y_star, size, "white") | |
# print greeting message | |
oogway.speed(1) | |
oogway.penup() | |
msg = "Merry Christmas from ThePythonDjango.Com" | |
oogway.goto(0, -200) # minus because tree trunk was below x axis | |
oogway.color("white") | |
oogway.pendown() | |
oogway.write(msg, move=False, align="center", font=("Arial", 15, "bold")) | |
oogway.hideturtle() | |
screen.mainloop() | |
if __name__ == "__main__": | |
run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment