def fire_work(self): """ This method creates a firework animation. """ # Numbers of the firework for i in range(10): f_x = random.randint(self.window.width // 8, self.window.width * 7 // 8) f_y = random.randint(self.window.height // 10, self.window.height * 9 // 10) size = random.randint(4, 7) # The size of the firework for j in range(size): fire = GOval(10 + 20 * j, 10 + 20 * j, x=f_x - 10 * j, y=f_y - 10 * j) # Choose color randomly fire.color = self.choose_color() self.window.add(fire) pause(100) self.window.remove(fire) pause(500)
def __init__(self, window_width=WINDOW_WIDTH, window_height=WINDOW_HEIGHT, zone_width=ZONE_WIDTH, zone_height=ZONE_HEIGHT, ball_radius=BALL_RADIUS): # Create window self.window = GWindow(window_width, window_height, title='Zone Game') # Create zone self.zone = GRect(zone_width, zone_height, x=(window_width - zone_width) / 2, y=(window_height - zone_height) / 2) self.zone.color = 'blue' self.window.add(self.zone) # Create ball and initialize velocity/position self.ball = GOval(2 * ball_radius, 2 * ball_radius) self.ball.filled = True self.ball.fill_color = 'salmon' self.dx = 0 self.dy = 0 self.reset_ball() # Initialize mouse listeners onmouseclicked(self.handle_click)
def sierpinski_triangle(order, length, upper_left_x, upper_left_y): """ :param order: decide how many layer of sierpinski triangle :param length: length of triangle's side :param upper_left_x: x coordinate of first triangle's start point :param upper_left_y: y coordinate of first triangle's start point :return: sierpinski triangle of given order """ if order == 0: pass else: triangle = GPolygon() triangle.add_vertex((upper_left_x, upper_left_y)) triangle.add_vertex((upper_left_x + length, upper_left_y)) triangle.add_vertex((upper_left_x + 0.5 * length, upper_left_y + 0.866 * length)) circle = GOval(math.sqrt(3) * length / 3, math.sqrt(3) * length /3, x=upper_left_x + 0.5 * length - math.sqrt(3) * length /6, y=upper_left_y ) circle.filled = True circle.fill_color ='snow' window.add(circle) window.add(triangle) sierpinski_triangle(order - 1, length/2, upper_left_x, upper_left_y) sierpinski_triangle(order - 1, length/2, upper_left_x + 0.25 * length, upper_left_y + 0.433 * length) sierpinski_triangle(order - 1, length/2, upper_left_x + 0.5 * length, upper_left_y)
def bounce(m): global n, V, time, end if (time < 3) and (end == 0): window.clear() end = 1 X = START_X Y = START_Y ball = GOval(SIZE, SIZE) ball.filled = True window.add(ball, X, Y) while X < 800: while Y < 500: n += 1 pause(DELAY) window.clear() window.add(ball, X, Y) pause(DELAY) X = X + VX V = GRAVITY * n Y = Y + V V = V * 0.9 n = 0 while V > 0: n += 0.1 X = X + VX V = V - GRAVITY * n Y = Y - V pause(DELAY) window.clear() window.add(ball, X, Y) pause(DELAY) window.add(ball, START_X, START_Y) time += 1 end = 0
def add_rainbow(window): """This function add a rainbow to the window""" # initial setting for the rainbow size_ini_cir = 600 x_cir = WINDOW_WIDTH * 0.5 y_cir = WINDOW_HEIGHT * 0.85 int_cir = 50 # plot the concentric circles for i in range(len(RAINBOW_COLOR_LIST)): size_cir = size_ini_cir - int_cir * i circle = GOval(size_cir, size_cir, x=x_cir - size_cir / 2, y=y_cir - size_cir / 2) circle.filled = True circle.color = RAINBOW_COLOR_LIST[i] circle.fill_color = RAINBOW_COLOR_LIST[i] window.add(circle) # block the lower part of the circles by overlaying a box rect = GRect(size_ini_cir, size_ini_cir / 2, x=x_cir - size_ini_cir / 2, y=y_cir) rect.filled = True rect.color = RAINBOW_COLOR_LIST[-1] rect.fill_color = RAINBOW_COLOR_LIST[-1] window.add(rect)
def create_beeper(e): # create 4 beepers size = 50 for i in (1, 3, 7, 9): beeper = GOval(size, size, x=i * 50 - size / 2, y=400 - size / 2) beeper.filled = True beeper.fill_color = 'blue' window.add(beeper) label1 = GLabel('001', x=50 - size / 2 + 9, y=400 - size / 2 + 37) label2 = GLabel('101', x=150 - size / 2 + 9, y=400 - size / 2 + 37) label3 = GLabel('201', x=350 - size / 2 + 9, y=400 - size / 2 + 37) label4 = GLabel('202', x=450 - size / 2 + 9, y=400 - size / 2 + 37) label1.font = '-15' label2.font = '-15' label3.font = '-15' label4.font = '-15' label1.color = 'white' label2.color = 'white' label3.color = 'white' label4.color = 'white' window.add(label1) window.add(label2) window.add(label3) window.add(label4) onmouseclicked(build_karel)
def activate(event): if mouse_lock == 0: ball_bouncing() else: nothing = GOval(5, 5) nothing.color = 'white' window.add(nothing, event.x, event.y)
def arch_triomphe_body(): """ This function will draw the body part of the Arch de Triomphe. """ body = GRect(280,280, x=860, y=520) body.filled = True body.fill_color = 'white' body.color = 'white' window.add(body) arch = GOval(170,290,x=915,y=610) arch.filled = True arch.fill_color = 'red' arch.color = 'red' window.add(arch) middle_line = GRect(280, 15, x=860, y=660) middle_line.filled = True middle_line.fill_color = 'red' middle_line.color = 'red' window.add(middle_line) upper_line = GRect(280, 15, x=860, y=600) upper_line.filled = True upper_line.fill_color = 'red' upper_line.color = 'red' window.add(upper_line) top_line = GRect(280, 20, x=860, y=550) top_line.filled = True top_line.fill_color = 'red' top_line.color = 'red' window.add(top_line)
def eiffel_tower_bottom(): """ This function will draw the eiffel tower's bottom on the canvas. """ bottom = GRect(398, 100, x=401, y=705) bottom.filled = True bottom.fill_color = 'gray' bottom.color = 'gray' window.add(bottom) for i in range(20): rect = GRect(90-(i*5), 10, x=401, y=700+i*5) rect.filled=True rect.fill_color = 'white' rect.color = 'white' window.add(rect) for i in range(20): rect = GRect(90-(i*5), 10, x=709+i*5, y=700+i*5) rect.filled = True rect.fill_color = 'white' rect.color = 'white' window.add(rect) arch = GOval(150, 150, x=523, y=730) arch.filled = True arch.fill_color = 'white' arch.color = 'white' window.add(arch)
def painter(mouse): global counter, circle_point #on odd click, create circle on current location if counter is 0: circle = GOval(CIRCLE_SIZE, CIRCLE_SIZE, x=mouse.x - CIRCLE_SIZE / 2, y=mouse.y - CIRCLE_SIZE / 2) circle.filled = False #record current circle location circle_point = (circle.x + circle.width / 2, circle.y + circle.height / 2) window.add(circle) # increase counter to one in odd clicks counter += 1 #on even click, draw line and remove circle else: #remove previous circle window.remove(window.get_object_at(circle_point[0], circle_point[1])) #draw a line from previous circle center to current mouse location line = GLine(circle_point[0], circle_point[1], mouse.x, mouse.y) window.add(line) #decrease counter to zero in even clicks counter -= 1
def make_a_ball(): global ball new_ball = GOval(SIZE, SIZE, x=START_X, y=START_Y) new_ball.filled = True new_ball.fill_color = 'black' window.add(new_ball) ball = new_ball
def build_blocks(): """ This function builds the blocks of the drawing """ block_1 = GRect(375, 80, x=20, y=330) block_1.filled = True block_1.color = 'firebrick' block_1.fill_color = 'firebrick' window.add(block_1) block_2 = GRect(375, 80, x=405, y=330) block_2.filled = True block_2.color = 'steelblue' block_2.fill_color = 'steelblue' window.add(block_2) block_3 = GRect(375, 80, x=20, y=420) block_3.filled = True block_3.color = 'goldenrod' block_3.fill_color = 'goldenrod' window.add(block_3) block_4 = GRect(375, 80, x=405, y=420) block_4.filled = True block_4.color = 'forestgreen' block_4.fill_color = 'forestgreen' window.add(block_4) block_5 = GRect(60, 40, x=720, y=120) block_5.filled = True block_5.color = 'dodgerblue' block_5.fill_color = 'dodgerblue' window.add(block_5) circle_1 = GOval(90, 90, x=20, y=170) circle_1.filled = True circle_1.color = 'blueviolet' circle_1.fill_color = 'blueviolet' window.add(circle_1)
def breed(self): """ This method create three circle and one rectangle to compose a cloud shape. Every time the cloud shape will be displayed in random position of window. """ dx = random.randint(10, window.width - 10) dy = random.randint(10, window.height - 10) rect = GRect(70, 30) rect.color = 'mintcream' rect.filled = True rect.fill_color = 'mintcream' self.list.append(rect) for i in range(3): circle = GOval(60, 60) circle.color = 'mintcream' circle.filled = True circle.fill_color = 'mintcream' self.list.append(circle) window.add(self.list[0], window.width + 45 + dx, 55 + dy) window.add(self.list[1], window.width + 10 + dx, 25 + dy) window.add(self.list[2], window.width + 45 + dx, 0 + dy) window.add(self.list[3], window.width + 80 + dx, 25 + dy)
def nose(window): """ :param window:window """ nose_up = GOval(50, 20) nose_up.filled = True window.add(nose_up, x=window.width / 2 - nose_up.width // 2, y=245 + 40)
def __init__(self, ball_radius=BALL_RADIUS, paddle_width=PADDLE_WIDTH, paddle_height=PADDLE_HEIGHT, paddle_offset=PADDLE_OFFSET, brick_rows=BRICK_ROWS, brick_cols=BRICK_COLS, brick_width=BRICK_WIDTH, brick_height=BRICK_HEIGHT, brick_offset=BRICK_OFFSET, brick_spacing=BRICK_SPACING, title='Breakout'): # Create a graphical window, with some extra space. self.window_width = brick_cols * (brick_width + brick_spacing) - brick_spacing self.window_height = brick_offset + 3 * ( brick_rows * (brick_height + brick_spacing) - brick_spacing) self.window = GWindow(width=self.window_width, height=self.window_height, title=title) # Create a paddle. self.pw = paddle_width self.ph = paddle_height self.paddle = GRect(self.pw, self.ph, x=(self.window_width - self.pw) / 2, y=self.window_height - paddle_offset) self.paddle.filled = True self.window.add(self.paddle) # Center a filled ball in the graphical window. self.bra = ball_radius * 2 self.ball = GOval(self.bra, self.bra) self.ball.filled = True self.window.add(self.ball, (self.window_width - self.bra) / 2, (self.window_height - self.bra) / 2) # Default initial velocity for the ball. self.__dx = 0 self.__dy = 0 # Initialize our mouse listeners. onmousemoved(self.paddle_move) onmouseclicked(self.ball_move) # Draw bricks. self.build_bricks() self.score = 0 self.score_text = GLabel('Score: ' + str(self.score)) self.score_text.font = '-15-bold' self.window.add(self.score_text, 0, self.window_height) self.lives_text = GLabel('Lives: ' + str(3)) self.lives_text.font = '-15-bold' self.window.add(self.lives_text, self.window_width - 80, self.window_height)
def __init__(self, ball_radius=BALL_RADIUS, paddle_width=PADDLE_WIDTH, paddle_height=PADDLE_HEIGHT, paddle_offset=PADDLE_OFFSET, brick_rows=BRICK_ROWS, brick_cols=BRICK_COLS, brick_width=BRICK_WIDTH, brick_height=BRICK_HEIGHT, brick_offset=BRICK_OFFSET, brick_spacing=BRICK_SPACING, title='Breakout'): # Create a graphical window, with some extra space window_width = brick_cols * (brick_width + brick_spacing) - brick_spacing window_height = brick_offset + 3 * (brick_rows * (brick_height + brick_spacing) - brick_spacing) self.window = GWindow(width=window_width, height=window_height, title='Breakout') # Element score label self.score = 0 self.score_label = GLabel('Score: ' + str(self.score)) # Element for game winning self.win_show = GLabel('Create by Mike Lin', x=100, y=100) self.win_show2 = GLabel('March 20 stanCode SC101', x=135, y=115) # Create a paddle self.paddle = GRect(paddle_width, paddle_height, x=(window_width - paddle_width) / 2, y=window_height - paddle_offset) self.window.add(self.paddle) self.paddle.filled = True # Center a filled ball in the graphical window self.ball = GOval(2 * ball_radius, 2 * ball_radius, x=window_width / 2 - ball_radius, y=window_height / 2 - ball_radius) self.ball_x = window_width / 2 - ball_radius self.ball_y = window_height / 2 - ball_radius # Default initial velocity for the ball self.__dx = 0 self.__dy = 0 # Other variables self.total_bricks = 0 # Initialize our mouse listeners onmousemoved( self.paddle_move) # using own method must add 'self.' in the front onmouseclicked(self.clicked) self.draw_bricks_and_ball()
def karel_head(): # Build Karel's head. global head head = GOval(60, 40, x=220, y=20) head.filled = True head.fill_color = 'gray' window.add(head) return head
def setup_ball(): """ This function sets the initial condition of the ball """ ball = GOval(SIZE, SIZE, x=START_X, y=START_Y) ball.filled = True window.add(ball) return ball
def make_sun(window): sun = GOval(width=SUN_DIAMETER, height=SUN_DIAMETER, x=(window.width - SUN_DIAMETER) / 2, y=(window.height - SUN_DIAMETER) / 2) sun.filled = True sun.fill_color = 'Yellow' return sun
def __init__(self, ball_radius=BALL_RADIUS, paddle_width=PADDLE_WIDTH, paddle_height=PADDLE_HEIGHT, paddle_offset=PADDLE_OFFSET, brick_rows=BRICK_ROWS, brick_cols=BRICK_COLS, brick_width=BRICK_WIDTH, brick_height=BRICK_HEIGHT, brick_offset=BRICK_OFFSET, brick_spacing=BRICK_SPACING, title='Breakout'): num_bricks = 100 # Create a graphical window, with some extra space. window_width = brick_cols * (brick_width + brick_spacing) - brick_spacing window_height = brick_offset + 3 * (brick_rows * (brick_height + brick_spacing) - brick_spacing) self.window = GWindow(width=window_width, height=window_height, title=title) self.paddle = GRect(paddle_width, paddle_height, x=(window_width - paddle_width)/2, y = window_height - \ paddle_offset) # initializing the paddle self.paddle.filled = True self.paddle.fill_color = 'black' self.window.add(self.paddle) # initializing the ball self.ball = GOval(width=ball_radius * 2, height=ball_radius * 2, x=window_width / 2 - BALL_RADIUS, y=window_height / 2 - BALL_RADIUS) self.ball.filled = True self.ball.fill_color = 'black' self.window.add(self.ball) # initial velocity self.vx = 0 self.vy = INITIAL_Y_SPEED # draw bricks self.draw_bricks() # number of lives left self.num_lives = 3 # running? big question mark self.running = False # brick count self.brick_count = 100 # mouse listeners onmouseclicked(self.start) onmousemoved(self.move_paddle)
def body(): body0 = GOval(150, 200, x=105, y=300) body0.filled = True body0.fill_color = 'silver' window.add(body0) body1 = GArc(130, 200, 0, -180, x=115, y=400) body1.filled = True body1.fill_color = 'red' window.add(body1)
def __init__(self, ball_radius=BALL_RADIUS, paddle_width=PADDLE_WIDTH, paddle_height=PADDLE_HEIGHT, paddle_offset=PADDLE_OFFSET, brick_rows=BRICK_ROWS, brick_cols=BRICK_COLS, brick_width=BRICK_WIDTH, brick_height=BRICK_HEIGHT, brick_offset=BRICK_OFFSET, brick_spacing=BRICK_SPACING, title='Breakout'): # Create a graphical window, with some extra space window_width = brick_cols * (brick_width + brick_spacing) - brick_spacing window_height = brick_offset + 3 * (brick_rows * (brick_height + brick_spacing) - brick_spacing) self.window = GWindow(width=window_width, height=window_height, title='Breakout') # Create a paddle self.paddle = GRect(paddle_width, paddle_height, x=(window_width-paddle_width)/2, y=window_height-paddle_offset) self.window.add(self.paddle) self.paddle.filled = True # Center a filled ball in the graphical window self.ball = GOval(2*ball_radius, 2*ball_radius, x=window_width/2 - ball_radius, y=window_height/2 - ball_radius) self.window.add(self.ball) self.ball.filled = True self.ball_x = window_width/2 - ball_radius self.ball_y = window_height/2 - ball_radius # Default initial velocity for the ball self.__dx = 0 self.__dy = 0 # Other variables self.total_bricks = 0 self.__start = False # Initialize our mouse listeners onmousemoved(self.paddle_move) # using own method must add 'self.' in the front onmouseclicked(self.clicked) # Draw bricks color_num = BRICK_COLS/5 for i in range(BRICK_ROWS): for j in range(BRICK_COLS): if j // color_num == 0: color = 'red' elif j // color_num == 1: color = 'orange' elif j // color_num == 2: color = 'yellow' elif j // color_num == 3: color = 'green' else: color = 'blue' bricks = GRect(BRICK_WIDTH, BRICK_HEIGHT) self.window.add(bricks, x=0+i*(BRICK_WIDTH+BRICK_SPACING), y=BRICK_OFFSET+j*(BRICK_HEIGHT+BRICK_SPACING)) bricks.filled = True bricks.fill_color = color self.total_bricks += 1
def tie_fighter_hull_window2(self): size = self.brick_smaller() hull_window2 = GOval(size * 2, size * 2) hull_window2.filled = True hull_window2.color = "wheat" self.window.add(hull_window2, x=self.window_width / 2 - hull_window2.width / 2, y=self.window_height / 2 - hull_window2.height / 2 - 400) return hull_window2
def build_a_ball(): """ This function creates a ball with GOval :return: object, ball """ ball = GOval(SIZE, SIZE, x=START_X, y=START_Y) ball.filled = True ball.fill_color = 'lightpink' ball.color = 'lightpink' return ball
def tie_fighter_hull(self): size = self.brick_smaller() hull = GOval(size * 6, size * 6) hull.filled = True hull.fill_color = "grey" hull.color = "grey" self.window.add(hull, x=self.window_width / 2 - hull.width / 2, y=self.window_height / 2 - hull.height / 2 - 400) return hull
def draw_circle(r, g, b, x, y): """ draw a circle and add to window on given (x, y), painting with given color. """ circle = GOval(PIXEL_SIZE, PIXEL_SIZE) circle.filled = True circle.fill_color = (r, g, b) circle.color = (r, g, b) window.add(circle, x, y) update_x_y_boundary(x, y)
def ball(self): # Center a filled ball in the graphical window. b = GOval(self.ball_radius * 2, self.ball_radius * 2) # draw the ball b.filled = True b.fill_color = "blue" b.color = "blue" self.window.add(b, x=self.window_width / 2 - self.ball_radius, y=self.window_height / 2 - self.ball_radius) return b
def main(): """ This program simulates a bouncing ball at (START_X, START_Y) that has VX as x velocity and 0 as y velocity. Each bounce reduces y velocity to REDUCE of itself. """ ball = GOval(SIZE, SIZE) ball.filled = True window.add(ball, START_X, START_Y) onmouseclicked(bounce) pass
def hole_puncher(event): global count, x, y, hole count += 1 if count % 2 == 1: hole = GOval(SIZE, SIZE, x=event.x - SIZE/2, y=event.y - SIZE/2) hole.color = 'black' window.add(hole) else: line = GLine(hole.x, hole.y, event.x, event.y) window.add(line) window.remove(hole)
def __init__(self, width, height, x=0, y=0, hp=MONSTER_HP): self.body = GRect(width, height, x=x, y=y) self.body.filled = True self.body.color = 'grey' self.l_eye = GOval(width*0.2, height*0.35, x=x+width*0.2, y=y+height*0.2) self.l_eye.filled = True self.r_eye = GOval(width*0.2, height*0.35, x=x+width*0.6, y=y+height*0.2) self.r_eye.filled = True self.hp = hp self.hp_label = GLabel(f'HP:{self.hp}') self.hp_label.x = self.body.x + (self.body.width - self.hp_label.width)/2 self.hp_label.y = self.body.y + self.body.height