Answer to Question #167273 in Python for Abdollah

Question #167273

In this assignment, you will use all of the graphics commands you have learned to create an animated scene. Your program should have a clear theme and tell a story. You may pick any school-appropriate theme that you like.

The program must include a minimum of:

  • 5 circles
  • 5 polygons
  • 5 line commands
  • 2 for loops
  • 1 global variable

You may wish to use the standard code for simplegui graphics below:

import simplegui

def draw_handler(canvas):

frame = simplegui.create_frame('Testing', 600, 600)
frame.set_canvas_background("Black")
frame.set_draw_handler(draw_handler)
frame.start()
1
Expert's answer
2021-02-26T19:54:23-0500
from math import sqrt

try:
    import simpleguik
except ImportError:
    import SimpleGUICS2Pygame.simpleguics2pygame as simplegui

message = 'Welcome! Click your mouse to start animation'
colors = ['Green', 'Red', 'Yellow']
color = 0
width = 600
height = 600
limits = [20, width, 180, height]
square_size = 25
circle_size = 0.5 * sqrt(2) * square_size
position = [limits[0], limits[2]]
pen_width = 4
pen_hwidth = 2
font_size = 24
hacked = False


def keydown(key):
    global color
    color = (color + 1) % len(colors)


def click(position):
    global message, hacked
    message = 'Y got H.@.C.K.E.DDD !!!'
    hacked = True


def square(x, y, a):
    return [[x, y], [x + a, y], [x + a, y + a], [x, y + a]]


def center(x, y, a):
    return x + a / 2.0, y + a / 2.0


def plus(x, y, a):
    cx = x + a / 2.0
    cy = y + a / 2.0
    return [[[cx, y], [cx, y + a]], [[x, cy], [x + a, cy]]]


def screensaver(canvas):
    global position
    position[0] += 2
    if not position[0] < limits[1] - square_size:
        position[0] = limits[0]
    position[1] += 3
    if not position[1] < limits[3] - square_size:
        position[1] = limits[2]

    for i in range(5):
        dx, dy = i * square_size / 2., i * square_size / 0.7,
        # dx, dy = 0, 0
        px, py = position[0] + dx, position[1] + dy
        if px >= limits[1] - square_size:
            px = limits[0] + px - limits[1] + square_size
        if py >= limits[3] - square_size:
            py = limits[2] + py - limits[3] + square_size

        sq = square(px, py, square_size)
        canvas.draw_polygon(sq, pen_width, 'White')

        cnt = center(px, py, square_size)
        canvas.draw_circle(cnt, circle_size, pen_width / 2, colors[color])

        pl = plus(px, py, square_size)
        for ln in pl:
            canvas.draw_line(ln[0], ln[1], pen_width / 2, 'Blue')


def draw_handler(canvas):
    text_width = frame.get_canvas_textwidth(message, font_size)
    centered_x = (width - text_width) / 2
    canvas.draw_text(message, [centered_x, 112], font_size, 'Red')

    if hacked:
        screensaver(canvas)


frame = simplegui.create_frame('Animation', width, height)
frame.set_canvas_background('Black')
frame.set_keydown_handler(keydown)
frame.set_mouseclick_handler(click)
frame.set_draw_handler(draw_handler)
frame.start()

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS