def createEye(pos): eye = GOval(EYE_SIZE, EYE_SIZE) eye.setFilled(True) if pos == 'left': snowman.add(eye, L_EYE_X, EYE_Y) else: snowman.add(eye, R_EYE_X, EYE_Y)
def __init__(self, diameter, color, filled=True): super().__init__() self.oval = GOval(-diameter // 2, -diameter // 2, diameter, diameter) self.oval.setFillColor(color) self.oval.setColor(color) self.oval.setFilled(filled) self.diameter = diameter self.add(self.oval)
def createFilledCircle(x, y, r, fill='black', border=None): circle = GOval(x - r, y - r, 2 * r, 2 * r) circle.setFilled(True) if border is None: circle.setColor(fill) else: circle.setColor(border) circle.setFillColor(fill) return circle
def createSnowball(size): if size == 'base': snowball = GOval(BASE_SIZE, BASE_SIZE) snowman.add(snowball, BASE_X, BASE_Y) elif size == 'body': snowball = GOval(BODY_SIZE, BODY_SIZE) snowman.add(snowball, BODY_X, BODY_Y) else: snowball = GOval(HEAD_SIZE, HEAD_SIZE) snowman.add(snowball, HEAD_X, HEAD_Y)
def __init__(self, name): """Creates a new node with the specified name.""" GCompound.__init__(self) self._name = name r = ArpanetNode.SIZE / 2 oval = GOval(-r, -r, 2 * r, 2 * r) oval.setFilled(True) self.add(oval) self._routingTable = None self._active = True self._neighbors = set()
def create_filled_circle(x, y, r, fill="Black", border=None): """ Creates a circle of radius r centered at the point (x, y) with the specified fill color. If border is specified, the border appears in that color. """ circle = GOval(x - r, y - r, 2 * r, 2 * r) circle.setFilled(True) if border is None: circle.setColor(fill) else: circle.setColor(border) circle.setFillColor(fill) return circle
class Circle(GCompound): def __init__(self, diameter, color, filled=True): super().__init__() self.oval = GOval(-diameter // 2, -diameter // 2, diameter, diameter) self.oval.setFillColor(color) self.oval.setColor(color) self.oval.setFilled(filled) self.diameter = diameter self.add(self.oval) def set_color(self, color): self.oval.setColor(color) self.oval.setFillColor(color) def get_radius(self): return self.diameter / 2 def get_center(self): return (self.getX(), self.getY())
def __init__(self, letter): GCompound.__init__(self) key = GOval(KEY_RADIUS * 2, KEY_RADIUS * 2) key.setLineWidth(KEY_BORDER) key.setColor(KEY_BORDER_COLOR) key.setFillColor(KEY_BGCOLOR) self.add(key, -KEY_RADIUS, -KEY_RADIUS) # create design for keys self.ch = GLabel(letter) self.ch.setColor(KEY_UP_COLOR) self.ch.setFont(KEY_FONT) self.add(self.ch, -self.ch.getWidth() / 2, KEY_LABEL_DY)
def ball(): ball = GOval(GWINDOW_WIDTH//2 - (BALL_SIZE//2), 300, BALL_SIZE, BALL_SIZE) ball.setFilled(True) ball.setColor("black") gw.add(ball)
def createSun(color): sun_radius = WINDOW_HEIGHT//5 sun = GOval(WINDOW_WIDTH//2 - sun_radius, WINDOW_HEIGHT//2-sun_radius, sun_radius*2, sun_radius*2) sun.setColor(color) sun.setFilled(True) return sun
def create_filled_circle(x, y, r, color="black"): """ Draws filled circles of radius r centered at the point (x,y) and with specified color. """ c = GOval(x - r, y - r, 2 * r, 2 * r) c.set_filled(True) c.set_color(color) return c
def createButton(s):# makes a button with the string s button=GCompound() buttonSize=75 label=GLabel(s) label.setColor("white") label.setFont("8px 'Sans-Serif'") c=GOval(-(label.getWidth()+20)//2,-buttonSize//2,label.getWidth()+20,buttonSize) c.setFillColor("black") c.setFilled(True) button.add(c) button.add(label,-label.getWidth()//2,0) return button
def __init__(self, letter): GCompound.__init__(self) lamp = GOval(LAMP_RADIUS * 2, LAMP_RADIUS * 2) lamp.setColor(LAMP_BORDER_COLOR) lamp.setFillColor(LAMP_BGCOLOR) self.add(lamp, -LAMP_RADIUS, -LAMP_RADIUS) # create design for lamps self.ch = GLabel(letter) self.ch.setColor(LAMP_OFF_COLOR) self.ch.setFont(LAMP_FONT) self.add(self.ch, -self.ch.getWidth() / 2, LAMP_LABEL_DY)
def background(): gw = GWindow(GWINDOW_WIDTH, GWINDOW_HEIGHT) rect = GRect(-10, -10, 720, 520) rect.setColor("cyan") rect.setFilled(True) gw.add(rect) for i in range(8): arc = GOval(-100, 100 + 30 * i, 900, 500) arc.setFilled(True) arc.setColor(colors[i]) gw.add(arc)
def DrawBangladeshFlag(): gw = GWindow(WINDOW_WIDTH, WINDOW_HEIGHT) rect = GRect(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT) rect.setFilled(True) rect.setColor("ForestGreen") gw.add(rect) circ = GOval(200, 100, 200, 200) circ.setFilled(True) circ.setColor("DarkRed") gw.add(circ)
def step(): nonlocal vx, vy, ball, bricks_hit, balls_left, x_text, y_text collider = getCollidingObject() if ball.getX() < 0 or ball.getX() > GWINDOW_WIDTH - BALL_SIZE: vx *= -1 elif ball.getY() < 0: vy *= -1 elif ball.getY() > GWINDOW_HEIGHT - BALL_SIZE: timer.stop() gw.remove(ball) balls_left -= 1 if balls_left > 0: ball = GOval((GWINDOW_WIDTH - BALL_SIZE) / 2, (GWINDOW_HEIGHT - BALL_SIZE) / 2, BALL_SIZE, BALL_SIZE) ball.setFilled(True) gw.add(ball) gw.add(instruct) else: msg = GLabel('You Lose.') msg.setColor('red') msg.setFont('bold 36px sans-serif') x = (GWINDOW_WIDTH - msg.getWidth()) / 2 y = (GWINDOW_HEIGHT - msg.getHeight()) / 2 gw.add(msg, x, y) if collider == paddle: vy *= -1 elif not (collider == paddle or collider == gw.getElementAt( x_text, y_text)) and collider is not None: vy *= -1 gw.remove(collider) bricks_hit += 1 if bricks_hit == N_COLS * N_ROWS: timer.stop() msg = GLabel('You Win!') msg.setColor('green') msg.setFont('bold 36px sans-serif') x = (GWINDOW_WIDTH - msg.getWidth()) / 2 y = (GWINDOW_HEIGHT - msg.getHeight()) / 2 gw.add(msg, x, y) ball.move(vx, vy) gw.remove(gw.getElementAt(x_text, y_text)) lives = GLabel('Lives: ' + str(balls_left)) gw.add(lives, x_text, y_text)
def DrawMaldivesFlag(): sun_radius = WINDOW_HEIGHT//5 gw = GWindow(WINDOW_WIDTH, WINDOW_HEIGHT) gw.add(createBackground("Crimson")) rect = GRect(WINDOW_WIDTH//8, WINDOW_HEIGHT//8, 3*WINDOW_WIDTH//4, 3*WINDOW_HEIGHT//4) rect.setColor("darkgreen") rect.setFilled(True) gw.add(rect) gw.add(createSun("white")) circ=GOval(WINDOW_WIDTH//2 - (sun_radius-20), WINDOW_HEIGHT//2-(sun_radius), sun_radius*2, sun_radius*2) circ.setColor("darkgreen") circ.setFilled(True) gw.add(circ)
def fireworks(): def step(): nonlocal dx, dy, dot, x, y, timer_2 if dot.getY() < y: timer.stop() timer_2.start() dot.move(dx, dy) def animateDot(): nonlocal current_size, desired_size if current_size < desired_size: current_size += DELTA_RADIUS x = dot.getX() - DELTA_RADIUS / 2 y = dot.getY() - DELTA_RADIUS / 2 dot.setBounds(x, y, current_size, current_size) random.seed() colors = ['red', 'orange', 'yellow', 'green', 'cyan', 'blue', 'violet'] gw = GWindow(GWINDOW_WIDTH, GWINDOW_HEIGHT) dot = GOval((GWINDOW_WIDTH - START_SIZE) / 2, GWINDOW_HEIGHT, START_SIZE, START_SIZE) dot.setColor(random.choice(colors)) gw.add(dot) y = random.uniform(0, GWINDOW_HEIGHT / 2) x = random.uniform(0, GWINDOW_WIDTH) dy = (y - dot.getY()) / FLIGHT_TIME dx = (x - dot.getX()) / FLIGHT_TIME current_size = START_SIZE desired_size = DELTA_RADIUS * EXPANSION_TIME timer = gw.createTimer(step, FLIGHT_TIME) timer.setRepeats(True) timer.start() timer_2 = gw.createTimer(animateDot, EXPANSION_TIME) timer_2.setRepeats(True)
def filledCircle(x, y, diameter, color): circle = GOval(x, y, diameter, diameter) circle.setFilled(True) circle.setFillColor(color) return circle
def createFilledCircle(x, y, r, color): circle = GOval(x - r, y - r, 2 * r, 2 * r) circle.setColor(color) circle.setFilled(True) return circle
def DrawJapanFlag(): gw = GWindow(WINDOW_WIDTH, WINDOW_HEIGHT) circ = GOval(200, 100, 200, 200) circ.setFilled(True) circ.setColor("DarkRed") gw.add(circ)
def Breakout(): """ The main program for the Breakout game. """ def mousemoveAction(e): paddle_X = paddle.getX() dx = e.getX() - paddle_X if 0 <= dx + paddle_X <= GWINDOW_WIDTH - PADDLE_WIDTH: paddle.move(dx, 0) elif 0 > dx + paddle_X: paddle.setLocation(0, PADDLE_Y) else: paddle.setLocation(GWINDOW_WIDTH - PADDLE_WIDTH, PADDLE_Y) def AnimatedBall(): def step(): nonlocal vx, vy, ball, bricks_hit, balls_left, x_text, y_text collider = getCollidingObject() if ball.getX() < 0 or ball.getX() > GWINDOW_WIDTH - BALL_SIZE: vx *= -1 elif ball.getY() < 0: vy *= -1 elif ball.getY() > GWINDOW_HEIGHT - BALL_SIZE: timer.stop() gw.remove(ball) balls_left -= 1 if balls_left > 0: ball = GOval((GWINDOW_WIDTH - BALL_SIZE) / 2, (GWINDOW_HEIGHT - BALL_SIZE) / 2, BALL_SIZE, BALL_SIZE) ball.setFilled(True) gw.add(ball) gw.add(instruct) else: msg = GLabel('You Lose.') msg.setColor('red') msg.setFont('bold 36px sans-serif') x = (GWINDOW_WIDTH - msg.getWidth()) / 2 y = (GWINDOW_HEIGHT - msg.getHeight()) / 2 gw.add(msg, x, y) if collider == paddle: vy *= -1 elif not (collider == paddle or collider == gw.getElementAt( x_text, y_text)) and collider is not None: vy *= -1 gw.remove(collider) bricks_hit += 1 if bricks_hit == N_COLS * N_ROWS: timer.stop() msg = GLabel('You Win!') msg.setColor('green') msg.setFont('bold 36px sans-serif') x = (GWINDOW_WIDTH - msg.getWidth()) / 2 y = (GWINDOW_HEIGHT - msg.getHeight()) / 2 gw.add(msg, x, y) ball.move(vx, vy) gw.remove(gw.getElementAt(x_text, y_text)) lives = GLabel('Lives: ' + str(balls_left)) gw.add(lives, x_text, y_text) vx = random.choice([-1, 1]) * random.uniform(MIN_X_VELOCITY, MAX_X_VELOCITY) vy = INITIAL_Y_VELOCITY x_text = 20 y_text = GWINDOW_HEIGHT - 10 timer = gw.createTimer(step, TIME_STEP) timer.setRepeats(True) timer.start() def clickAction(e): gw.remove(instruct) AnimatedBall() def getCollidingObject(): loc = gw.getElementAt(ball.getX(), ball.getY()) if loc is not None: return loc else: loc = gw.getElementAt(ball.getX() + BALL_SIZE, ball.getY()) if loc is not None: return loc else: loc = gw.getElementAt(ball.getX(), ball.getY() + BALL_SIZE) if loc is not None: return loc else: loc = gw.getElementAt(ball.getX() + BALL_SIZE, ball.getY() + BALL_SIZE) return loc random.seed() gw = GWindow(GWINDOW_WIDTH, GWINDOW_HEIGHT) colors = [ 'red', 'red', 'orange', 'orange', 'green', 'green', 'cyan', 'cyan', 'blue', 'blue' ] for row in range(N_ROWS): for col in range(N_COLS): rect = GRect( ((GWINDOW_WIDTH - ((N_COLS * (BRICK_WIDTH + BRICK_SEP)) - BRICK_SEP)) / 2) + (row * (BRICK_WIDTH + BRICK_SEP)), (TOP_FRACTION * GWINDOW_HEIGHT) + (col * (BRICK_HEIGHT + BRICK_SEP)), BRICK_WIDTH, BRICK_HEIGHT) rect.setFilled(True) rect.setColor(colors[col]) gw.add(rect) paddle = GRect((GWINDOW_WIDTH - PADDLE_WIDTH) / 2, PADDLE_Y, PADDLE_WIDTH, PADDLE_HEIGHT) paddle.setFilled(True) gw.add(paddle) gw.addEventListener('mousemove', mousemoveAction) ball = GOval((GWINDOW_WIDTH - BALL_SIZE) / 2, (GWINDOW_HEIGHT - BALL_SIZE) / 2, BALL_SIZE, BALL_SIZE) ball.setFilled(True) gw.add(ball) gw.addEventListener('click', clickAction) instruct = GLabel('Click to Start!') instruct.setFont('bold 24px sans-serif') x_inst = (GWINDOW_WIDTH - instruct.getWidth()) / 2 y_inst = ((GWINDOW_HEIGHT - instruct.getHeight()) / 2) + (3 * BALL_SIZE) gw.add(instruct, x_inst, y_inst) balls_left = N_BALLS bricks_hit = 0
def rainbow(): gw = GWindow(500, 250) rect = GRect(0, 0, 500, 250) rect.setColor('cyan') rect.setFilled(True) gw.add(rect) red = GOval(X, Y, W, H) red.setColor('Red') red.setFilled(True) orange = GOval(X, 2 * Y, W, H) orange.setColor('Orange') orange.setFilled(True) yellow = GOval(X, 3 * Y, W, H) yellow.setColor('Yellow') yellow.setFilled(True) green = GOval(X, 4 * Y, W, H) green.setColor('Green') green.setFilled(True) blue = GOval(X, 5 * Y, W, H) blue.setColor('blue') blue.setFilled(True) indigo = GOval(X, 6 * Y, W, H) indigo.setColor('indigo') indigo.setFilled(True) violet = GOval(X, 7 * Y, W, H) violet.setColor('violet') violet.setFilled(True) end = GOval(X, 8 * Y, W, H) end.setColor('cyan') end.setFilled(True) gw.add(red) gw.add(orange) gw.add(yellow) gw.add(green) gw.add(blue) gw.add(indigo) gw.add(violet) gw.add(end)
def __init__(self): GCompound.__init__(self) self.digit = "" outer = GOval(-20, -20, 40, 40) outer.setFilled(True) outer.setFillColor("red") outer.setColor("red") self.add(outer) outer = GOval(-16, -16, 32, 32) outer.setFilled(True) outer.setFillColor("white") outer.setColor("white") self.add(outer) strikethrough = GLine(15, -15, -15, 15) strikethrough.setLineWidth(6) strikethrough.setColor("red") self.add(strikethrough)