I’m working on a program on CodeHS and a large portion of my code isn’t working. We’re using the brython graphics library. Most of the flags for functions completing functions aren’t running.
Documentation is here: Documentation - Python 3 Graphics (Brython) | CodeHS
Code is here:
# This function iterates 100 times to create 100 slightly differently colored rectangles to create the gradient in the background
def make_bg():
bg = Rectangle(get_width(), get_height())
bg.set_position(0,0)
bg.set_color(Color.black)
add(bg)
for i in range(100):
rect = Rectangle(get_width(), get_height()/100)
rect.set_position(0, (get_height()/100)*i)
rect.set_color(f'rgba(92, 52, 117, {i/100})')
add(rect)
def draw_office(color, computer):
print('flag draw_office() runs')
clear()
rect = Rectangle(get_width(), get_height())
rect.set_position(0,0)
rect.set_color(Color.gray)
add_rect()
print('flag draw_office() completes')
# This function iterates 100 times to create 100 slightly differently colored rectangles to create the gradient in the background
def draw_building(height, width, x, win_height, win_width, sep, top, win_rows, win_cols, color = Color.black, win_color = Color.yellow):
building=Rectangle(width, height)
building.set_position(x, get_height()-height)
building.set_color(color)
add(building)
if top == "circle":
circ = Circle(width/2)
circ.set_position(x + width/2, get_height()-height)
circ.set_color(color)
add(circ)
'''
elif top == "triangle":
rect = Rectangle(width/1.4142, width/1.4142)
rect.set_position(x, get_height()-height-(width/4))
rect.set_rotation(math.radians(45))
rect.set_color(Color.black)
add(rect)
'''
if top == "spire":
rect = Rectangle(width/2, width/2)
rect.set_position(x + width/4, get_height()-height-width/4)
rect.set_color(color)
add(rect)
line = Line(x + width/2, get_height()-height, x + width/2, get_height()-height - height/6)
line.set_color(color)
add(line)
vert_spacing = height/(win_rows * win_height)
for j in range(win_cols):
for i in range(win_rows):
win = Rectangle(win_width, win_height)
win.set_position(x + sep + (width/win_cols)*j + 1, get_height() - height + win_height*(i) + (((i + 1)/win_rows)*win_height)) #if i > 0 else win_rows*win_height))
win.set_color(win_color)
add(win)
# This function creates the moon in the corner, spaced according to the radius passed
def draw_moon(rad):
circ = Circle(rad)
circ.set_position(get_width() - rad*3, 0 + rad*3)
circ.set_color('#e3d0cf')
add(circ)
# Templates for colored text, call by reassigning index 1 to text and using ''.join({color}_template)
green_template = ["\033[32m", "This text is green." "\033[0m"]
red_template = ["\033[31m", "This text is red." "\033[0m"]
# This function draws each ghost
def draw_ghost(x, y, color):
# Constants for body
HEAD_RADIUS = 10
BODY_WIDTH = HEAD_RADIUS * 2
BODY_HEIGHT = 17.16
NUM_FEET = 3
FOOT_RADIUS = (BODY_WIDTH) / (NUM_FEET * 2)
# Constants for eyes
PUPIL_RADIUS = 1.136
PUPIL_OFFSET = 1.704
EYE_RADIUS = 2.84
EYE_OFFSET = 3.976
rect = Rectangle(BODY_WIDTH, BODY_HEIGHT)
rect.set_position(x, y)
rect.set_color(color)
add(rect)
circ = Circle(HEAD_RADIUS)
circ.set_position(x + (BODY_WIDTH/2), y)
circ.set_color(color)
add(circ)
for i in range(NUM_FEET):
circ = Circle(FOOT_RADIUS)
circ.set_position((x + FOOT_RADIUS + ((FOOT_RADIUS * 2) * (i))), y+BODY_HEIGHT)
circ.set_color(color)
add(circ)
draw_eyes(x, y, Color.white, Color.blue)
def draw_eyes(x, y, eye_color, pupil_color):
circ = Circle(EYE_RADIUS)
circ.set_position((x + (BODY_WIDTH/2) - EYE_RADIUS - EYE_OFFSET) + EYE_RADIUS, y)
circ.set_color(eye_color)
add(circ)
circ_2 = Circle(EYE_RADIUS)
circ_2.set_position((x + (BODY_WIDTH/2) + EYE_OFFSET), y)
circ_2.set_color(eye_color)
add(circ_2)
circ_3 = Circle(PUPIL_RADIUS)
circ_3.set_position(circ.get_x() + PUPIL_OFFSET, y)
circ_3.set_color(pupil_color)
add(circ_3)
circ_4 = Circle(PUPIL_RADIUS)
circ_4.set_position(circ_2.get_x() + PUPIL_OFFSET, y)
circ_4.set_color(pupil_color)
add(circ_4)
"""
Draws the first scene on the canvas and outputs the first
section of text for the story.
"""
def draw_scene1():
make_bg()
draw_moon(25)
draw_building(300, 100, 30, 20, 10, 6, 'spire', 11, 4)
txt = Text("Food Bank")
txt.set_position(45, get_height() - 40)
txt.set_color(Color.white)
txt.set_font("15px Arial")
add(txt)
rect = Rectangle(15, 30)
rect.set_position(72.5, get_height() - rect.get_height())
rect.set_color(Color.brown)
add(rect)
print('flag building happens')
draw_ghost(100, get_height() - BODY_WIDTH - FOOT_RADIUS/2, Color.red)
print('flag ghost 1 draws')
draw_ghost(140, get_height() - BODY_WIDTH - FOOT_RADIUS/2, Color.green)
green_template[1] = "Hello Thomas, I have an exciting offer for you!"
print(''.join(green_template))
red_template[1] = "How much will it pay?"
print(''.join(red_template))
green_template[1] = "We'll triple your current salary!"
print(''.join(green_template))
red_template[1] = "What about my job here?"
print(''.join(red_template))
green_template[1] = "Who cares?"
print(''.join(green_template))
red_template[1] = "I'll have to think about it."
print(''.join(red_template))
"""
Draws the second scene on the canvas and outputs the second
section of text for the story.
"""
def draw_scene2():
print('flag second scene happens')
draw_office(Color.brown, True)
print('flag second scene completes')
"""
Draws the third scene on the canvas and outputs the second
section of text for the story.
"""
def draw_scene3():
print("This is scene 3")
"""
Draws the fourth scene on the canvas and outputs the second
section of text for the story.
"""
def draw_scene4():
print("This is scene 4")
"""
This is set up code that makes the story advance from
scene to scene. Feel free to check out this code and
learn how it works!
But be careful! If you modify this code the story might
not work anymore.
"""
scene_counter = 0
# When this function is called the next scene is drawn.
def draw_next_screen(x, y):
global scene_counter
scene_counter += 1
if scene_counter == 1:
draw_scene1()
elif scene_counter == 2:
draw_scene2()
elif scene_counter == 3:
draw_scene3()
else:
draw_scene4()
welcome = Text("Click to Begin!")
welcome.set_position(get_width() / 2 - welcome.get_width() / 2, get_height() / 2)
add(welcome)
add_mouse_click_handler(draw_next_screen)