def put_bricks(self): """ This method creates the bricks we needed. """ x_point = 0 y_point = 0 + self.b_offset color = 'red' # Loop over how many bricks we needed. for i in range(self.row): for j in range(self.col): brick = GRect(self.b_width, self.b_height) brick.filled = True brick.color = color brick.fill_color = color self.window.add(brick, x_point, y_point) x_point += self.b_width x_point += self.b_space y_point += self.b_height y_point += self.b_space x_point = 0 # The color in each row. if i == 1: color = 'orange' if i == 3: color = 'yellow' if i == 5: color = 'green' if i == 7: color = 'blue'
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 draw_background_hill(hill_num, surface_y): """ draw random hills, random numbers of hill and random locations. """ hill_bottom_x_start = random.randint(0, window.width // hill_num) for i in range(hill_num): random_hill_height = random.randint(3, 9) * PIXEL_SIZE hill_wide = 2 * random_hill_height for h in range(random_hill_height // PIXEL_SIZE): for w in range(h * 2): hill_square = GRect(PIXEL_SIZE, PIXEL_SIZE) hill_square.filled = True hill_square.fill_color = (HILL_R, HILL_G, HILL_B) hill_square.color = (HILL_R, HILL_G, HILL_B) window.add(hill_square, (hill_bottom_x_start + hill_wide) // 2 - h * PIXEL_SIZE + w * PIXEL_SIZE, (surface_y - random_hill_height) + h * PIXEL_SIZE) if i + 1 < hill_num: if hill_bottom_x_start + hill_wide > window.width: hill_bottom_x_start = 0 hill_bottom_x_start = random.randint( hill_bottom_x_start + hill_wide, ((window.width - (hill_bottom_x_start + hill_wide)) // (hill_num - (i + 1)) + hill_bottom_x_start + hill_wide))
def add_stars(): """ adding stars on the sky. """ star_num = SIZE star_x_start = 0 star_x_end = window.width star_y_start = 0 star_y_end = window.height - window.height // 5 * 2 - 6 * SIZE * PIXEL_SIZE + 3 * PIXEL_SIZE for i in range(star_num): random_x = random.randint(star_x_start + PIXEL_SIZE, star_x_end - PIXEL_SIZE) random_y = random.randint(star_y_start + PIXEL_SIZE, star_y_end - PIXEL_SIZE) for x in range(3): for y in range(3): # build a star in "+" shaped. if (x == 0 and y == 0) or (x == 2 and y == 0) or ( x == 0 and y == 2) or (x == 2 and y == 2): pass else: star = GRect(PIXEL_SIZE // 2, PIXEL_SIZE // 2) star.filled = True star.color = (240, 230, 140) star.fill_color = (240, 230, 140) window.add(star, random_x + (x - 1) * PIXEL_SIZE, random_y + (y - 1) * PIXEL_SIZE)
def karel_body(): # Build Karel's body. global body body = GRect(60, 50, x=220, y=62) body.filled = True body.fill_color = 'blue' window.add(body)
def draw_rect(level, width, center_x, center_y): if level == 0: pass else: # rect = GRect(width, width, x = center_x - width/2, y = center_y - width/2) # rect.filled = True # rect.color = 'black' # rect.fill_color = 'snow' # window.add(rect) # # # upper left # draw_rect(level - 1, width/2, center_x - width/2, center_y - width/2) # # upper right # draw_rect(level - 1, width / 2, center_x + width / 2, center_y - width / 2) # # down left # draw_rect(level - 1, width / 2, center_x - width / 2, center_y + width / 2) # # down right # draw_rect(level - 1, width / 2, center_x + width / 2, center_y + width / 2) rect = GRect(width, width, x=center_x - width / 2, y=center_y - width / 2) rect.filled = True rect.color = 'black' rect.fill_color = 'snow' # upper left draw_rect(level - 1, width / 2, center_x - width / 2, center_y - width / 2) # down right draw_rect(level - 1, width / 2, center_x + width / 2, center_y + width / 2) window.add(rect) # upper right draw_rect(level - 1, width / 2, center_x + width / 2, center_y - width / 2) # down left draw_rect(level - 1, width / 2, center_x - width / 2, center_y + width / 2)
def batman(): #batman legs left_leg = GRect(40, 100, x = 280, y = 775) obj_fill_color_add(left_leg, "gray") left_shoe = GPolygon() polygon_helper(left_shoe, "dimgrey", (280, 840), (280, 880), (330, 880), (320, 840)) right_leg = GRect(40, 100, x = 360, y = 775) obj_fill_color_add(right_leg, "gray") right_shoe = GPolygon() polygon_helper(right_shoe, "dimgrey", (360, 840), (360, 880), (410, 880), (400, 840)) #batman head bat_ear1 = GPolygon() polygon_helper(bat_ear1, "gray", (275, 450), (325, 450), (300, 375)) head = GOval(180, 220, x = 260, y = 400) obj_fill_color_add(head, "gray") bat_ear2 = GPolygon() polygon_helper(bat_ear2, "gray", (360, 450), (410, 450), (385, 375)) #batman appearance face = GArc(275, 330, 0, -90, 300, 450) obj_fill_color_add(face, "bisque") eye = GRect(30, 10, x = 400, y = 475) obj_fill_color_add(eye, "white") mouth = GLine(405, 575, 425, 575) obj_fill_color_add(mouth, "black") #batman body left_arm = GPolygon() polygon_helper(left_arm, "gray", (275, 600), (210, 650), (275, 720), (300, 720), (250, 655), (340, 600)) right_arm = GPolygon() polygon_helper(right_arm, "gray", (355, 600), (415, 650), (385, 720), (405, 720), (455, 650), (405, 600)) cape = GPolygon() polygon_helper(cape, "dimgrey", (280, 580), (250, 800), (450, 800), (380, 580))
def set_up_rect(): cx = (WINDOW_WIDTH - SIZE) / 2 cy = (WINDOW_HEIGHT - SIZE) / 2 rect = GRect(SIZE, SIZE, x=cx, y=cy) rect.filled = True rect.fill_color = 'dodgerblue' return rect
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=title) # Create a paddle. self.paddle = GRect(paddle_width, paddle_height) self.window.add(self.paddle, (self.window.width - paddle_width) / 2, self.window.height - paddle_offset - paddle_height) self.paddle.filled = True self.paddle.fill_color = 'blue' self.paddle.color = 'blue' # Center a filled ball in the graphical window. self.ball = GOval(ball_radius * 2, ball_radius * 2) self.ball.filled = True self.reset_ball() # Default initial velocity for the ball. self.__dx = 0 self.__dy = 0 self.switch = False # Initialize our mouse listeners. onmouseclicked(self.game_start) onmousemoved(self.move_paddle) # Draw bricks. by = 0 for i in range(brick_rows): bx = 0 for j in range(brick_cols): self.brick = GRect(brick_width, brick_height) self.brick.filled = True if i < brick_cols / 5: self.brick.fill_color = 'red' elif brick_cols / 5 <= i < brick_cols / 5 * 2: self.brick.fill_color = 'orange' elif brick_cols / 5 * 2 <= i < brick_cols / 5 * 3: self.brick.fill_color = 'yellow' elif brick_cols / 5 * 3 <= i < brick_cols / 5 * 4: self.brick.fill_color = 'green' else: self.brick.fill_color = 'blue' self.window.add(self.brick, bx, brick_offset+by) bx += brick_width + brick_spacing by += brick_height + brick_spacing # scoreboard self.earned_score = 0 self.total_score = brick_cols * brick_rows self.scoreboard = GLabel(f'score: {self.earned_score}/{self.total_score}', x=10, y=30) self.scoreboard.font = 'courier-20' self.window.add(self.scoreboard)
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 draw_rect(level, width, center_x, center_y): if level == 0: return else: pause(300) # upper left draw_rect(level - 1, width / 2, center_x - width / 2, center_y - width / 2) # lower right draw_rect(level - 1, width / 2, center_x + width / 2, center_y + width / 2) rect = GRect(width, width, x=center_x - width / 2, y=center_y - width / 2) rect.filled = True rect.fill_color = 'snow' window.add(rect) # upper right draw_rect(level - 1, width / 2, center_x + width / 2, center_y - width / 2) # lower left draw_rect(level - 1, width / 2, center_x - width / 2, center_y + width / 2)
def add_heart(window): """This function add a heart to the window""" # initial settings for plotting the heart y_max, y_min = 525, 85 # 520, 90 y_interval = 70.83 # 71.6 y_list = [y_min + i * y_interval for i in range(len(COLOR_LIST) + 1)] # start to draw a heart for y in range(0, WINDOW_HEIGHT, INTERVAL): for x in range(0, WINDOW_WIDTH, INTERVAL): # equation for heart cx = (x - X_SHIFT) * X_SCALE cy = (y - Y_SHIFT) * Y_SCALE love = ((cx**2 + cy**2 - 1)**3) - (cx**2 * cy**3) # plot the squares inside the heart if love < 0: for i in range(len(y_list)): if i != len(y_list) - 1: if y_list[i] < y <= y_list[i + 1]: square = GRect(SIZE, SIZE, x=x - SIZE / 2, y=y - SIZE / 2) square.filled = True square.color = COLOR_LIST[i] square.fill_color = COLOR_LIST[i] window.add(square)
def karel_neck(): # Build Karel's neck. global neck neck = GRect(30, 4, x=235, y=58) neck.filled = True neck.color = 'blue' window.add(neck)
def draw_bricks_and_ball(self): """ set the bricks and balls """ self.window.add(self.score_label, x=0, y=15) self.window.add(self.ball) self.ball.filled = True 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 opening(): black_cover = GRect(w.width, w.height, x=0, y=0) black_cover.filled = True black_cover.fill_color = "black" w.add(black_cover) far = GLabel("A long time ago in a galaxy far, \nfar away...") far.color = "blue" far.font = "-20" w.add(far, x=w.width / 2 - far.width / 2 + 60, y=w.height / 2) pause(1200) w.remove(far) main_title = GLabel("STAR\nWARS") main_title.color = "yellow" for i in range(20): size = (160 // (int(i) + 1)) size = -size main_title.font = str(size) w.add(main_title, x=w.width / 2 - main_title.width / 4, y=w.height / 2) pause(FRAME_RATE * 6) w.remove(main_title) opening_crawl = GLabel("It is a period of civil war\n" "Princess Leia races home abroad\n" "her spaceship but was later\n" "captured by Empires's agents\n" "Use your lightsaber to destroy\n" "the force field to save the\n" "princess") opening_crawl.color = "yellow" opening_crawl.font = "-15" w.add(opening_crawl, x=w.width / 2 - 130, y=w.height + 1) for i in range(50): opening_crawl.move(0, -10) pause(FRAME_RATE * 6) w.remove(black_cover) w.remove(opening_crawl)
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=title) self.paddle_offset = PADDLE_OFFSET self.ball_r = BALL_RADIUS # Create a paddle self.paddle = GRect(paddle_width, paddle_height, x=(window_width-paddle_width)/2, y=window_height-paddle_offset-paddle_height) self.paddle.filled = True self.paddle.fill_color = 'blue' self.window.add(self.paddle) # Center a filled ball in the graphical window self.first_ball = GOval(ball_radius*2, ball_radius*2, x=(window_width-ball_radius)/2, y=(window_height-ball_radius)/2) self.first_ball.filled = True self.first_ball.fill_color = 'black' self.window.add(self.first_ball) self.initial_x = (window_width-ball_radius)/2 self.initial_y = (window_height-ball_radius)/2 # Default initial velocity for the ball self.__dx = random.randint(1, MAX_X_SPEED) self.__dy = INITIAL_Y_SPEED if random.random() > 0.5: self.__dx = -self.__dx # Initialize our mouse listeners self.activate = False self.crash_is_paddle = False self.crash_is_brick = False self.point_count = 0 onmousemoved(self.paddle_move) onmouseclicked(self.ball_start) # Draw bricks for y_pos in range(0, (BRICK_HEIGHT+BRICK_SPACING)*BRICK_ROWS-BRICK_SPACING, BRICK_HEIGHT+BRICK_SPACING): for x_pos in range(0, (BRICK_WIDTH+BRICK_SPACING)*BRICK_COLS-BRICK_SPACING, BRICK_WIDTH+BRICK_SPACING): self.brick = GRect(BRICK_WIDTH, BRICK_HEIGHT) self.brick.filled = True if y_pos < BRICK_HEIGHT+BRICK_SPACING * 2 + 1: self.brick.fill_color = 'red' elif y_pos < (BRICK_HEIGHT+BRICK_SPACING) * 4: self.brick.fill_color = 'orange' elif y_pos < (BRICK_HEIGHT+BRICK_SPACING) * 6: self.brick.fill_color = 'yellow' elif y_pos < (BRICK_HEIGHT+BRICK_SPACING) * 8: self.brick.fill_color = 'green' elif y_pos < (BRICK_HEIGHT+BRICK_SPACING) * 10: self.brick.fill_color = 'blue' else: self.brick.fill_color = 'black' self.window.add(self.brick, x=x_pos, y=y_pos)
def set_up_react(): rect = GRect(SIZE, SIZE, x=(window.width - SIZE) / 2, y=(window.height - SIZE) / 2) rect.filled = True rect.fill_color = 'blue' return rect
def make_horizon(window): horizon = GRect(width=window.width, height=HORIZON_HEIGHT, x=0, y=window.height - HORIZON_HEIGHT) horizon.filled = True horizon.fill_color = 'Green' return horizon
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 __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=title) # Create a paddle. self.paddle = GRect(paddle_width, paddle_height, x=(window_width - paddle_width) / 2, y=window_height - paddle_offset) self.paddle.filled = True self.window.add(self.paddle) # Center a filled ball in the graphical window. self.ball = GOval(ball_radius * 2, ball_radius * 2, x=window_width / 2 - ball_radius, y=window_height / 2 - ball_radius) self.window.add(self.ball) self.ball.filled = True # Default initial velocity for the ball. random_x = random.randint(1, MAX_X_SPEED) self.__vx = random_x if random.random() > 0.5: self.__vx = -self.__vx self.__vy = INITIAL_Y_SPEED # Initialize our mouse listeners. self.moved = onmousemoved(self.move_paddle) self.__is_gaming = False # Draw bricks. for i in range(brick_rows): for j in range(brick_cols): self.bricks = GRect(brick_width, brick_height, x=+j * (brick_width + brick_spacing), y=brick_offset + i * (brick_height + brick_spacing)) self.bricks.filled = True self.set_brick_color(self.bricks, i, j) self.window.add(self.bricks)
def build_background(): """ This function creates a rectangle as a background for the window with GRect :return: object, background """ back_ground = GRect(800, 500) back_ground.filled = True back_ground.fill_color = 'silver' back_ground.color = 'silver' return back_ground
def tie_fighter_l_skeleton(self, hull): size = self.brick_smaller() l_skeleton = GRect(size * 3, size * 2) l_skeleton.filled = True l_skeleton.fill_color = "grey" l_skeleton.color = "grey" self.window.add(l_skeleton, x=hull.x + hull.width, y=self.window_height / 2 - l_skeleton.height / 2 - 400) return l_skeleton
def refill_bricks(self): """ Refill the bricks when game restart. """ for x in range(self.__brick_rows): for y in range(self.__brick_cols): __x_coordinate = x * (self.__brick_width + self.__brick_spacing) __y_coordinate = self.__brick_offset + y * (self.__brick_height + self.__brick_spacing) if self.__window.get_object_at(__x_coordinate, __y_coordinate) is None: __brick = GRect(self.__brick_width, self.__brick_height) __brick.filled = True if y < 2: __brick.fill_color = (RED_R, RED_G, RED_B) elif y < 4: __brick.fill_color = (ORANGE_R, ORANGE_G, ORANGE_B) elif y < 6: __brick.fill_color = (YELLOW_R, YELLOW_G, YELLOW_B) elif y < 8: __brick.fill_color = (GREEN_R, GREEN_G, GREEN_B) else: __brick.fill_color = (BLUE_R, BLUE_G, BLUE_B) __brick.color = (0, 0, 0) # if y == 9: (This line switch for building only one row of bricks.) # self.__window.add(__brick, __x_coordinate, __y_coordinate) # self.__brick_nums += 1 self.__window.add(__brick, __x_coordinate, __y_coordinate) self.__brick_nums += 1
def tie_fighter_r_wing(self, r_skeleton): size = self.brick_smaller() r_wing = GRect(size / 2, size * 12) r_wing.filled = True r_wing.fill_color = "grey" r_wing.color = "grey" self.window.add(r_wing, x=r_skeleton.x - r_wing.width, y=r_skeleton.y + r_skeleton.height / 2 - r_wing.height / 2) return r_wing
def start_page(self): """ This method draw the start page and restart page. """ if self._page == 0: check = self._window.get_object_at(self._window.width // 2, self._window.height // 2) if check is None: progress_rate_board = GLabel(f'Loading...0%') progress_rate_board.font = 'Chalkduster-15' self._window.add( progress_rate_board, (self._window.width - progress_rate_board.width) // 2, (self._window.height + PROGRESS_BAR_SIZE) // 2 + progress_rate_board.height + LABEL_SPACING) pause_time = 300 for i in range(10): color = COLOR_LIST[i % len(COLOR_LIST)] progress_bar = GRect(PROGRESS_BAR_SIZE * (i + 1), PROGRESS_BAR_SIZE) progress_bar.filled = True progress_bar.fill_color = color progress_bar.color = color self._window.add( progress_bar, self._window.width // 2 - PROGRESS_BAR_SIZE * 5, self._window.height // 2 - PROGRESS_BAR_SIZE // 2) progress_rate_board.text = f'Loading...{10*(i+1)}' pause(pause_time) pause_time += 100 self._window.clear() self.draw_bricks() self._start_label.text = f'Click to start' self._window.add( self._start_label, (self._window.width - self._start_label.width) // 2, (self._window.height + self._start_label.height) // 2) elif self._page == 2: self._window.clear() # self.draw_bricks() self._start_label.text = f'Click to restart' self._window.add( self._start_label, (self._window.width - self._start_label.width) // 2, (self._window.height + self._start_label.height) // 2) highscore_board = GLabel(f'High score: {self._highscore}') highscore_board.font = 'Chalkduster-60' highscore_board.color = 'navy' self._window.add( highscore_board, (self._window.width - highscore_board.width) // 2, self._start_label.y - self._start_label.height - LABEL_SPACING * 3)
def fail_game2(self): fail_frame = GRect(450, 80) fail_frame.filled = True fail_frame.fill_color = 'white' fail_frame.color = 'white' self.window.add(fail_frame, (self.window.width - fail_frame.width) / 2, (self.window.height - fail_frame.height) / 2) failed = GLabel('Game Over') failed.font = '-60-bold' failed.color = 'black' self.window.add(failed, fail_frame.x + 70, fail_frame.y + 75)
def tie_fighter_l_wing(self, l_skeleton): size = self.brick_smaller() l_wing = GRect(size / 2, size * 12) l_wing.filled = True l_wing.fill_color = "grey" l_wing.color = "grey" self.window.add(l_wing, x=l_skeleton.x + l_skeleton.width, y=l_skeleton.y + l_skeleton.height / 2 - l_wing.height / 2) return l_wing
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=title) # Create a paddle self.paddle_touch = 0 # (0:untouch paddle; 1: touch paddle) # self.paddle = GRect(paddle_width, paddle_height) self.paddle = GRect(paddle_width, paddle_height) # Test width "430" self.paddle.filled = True self.window.add(self.paddle, x=(window_width-paddle_width)/2, y=window_height-paddle_offset) # Center a filled ball in the graphical window self.ball_radius = ball_radius self.remove_succ = 0 # (0: untouch bricks; 1: touch bricks) self.ball = GOval(ball_radius*2, ball_radius*2) self.ball.filled = True self.window.add(self.ball, x=(window_width-ball_radius)/2, y=(window_height-ball_radius)/2) # Default initial velocity for the ball self.__dy = INITIAL_Y_SPEED self.__dx = random.randint(1, MAX_X_SPEED+1) if random.random() > 0.5: self.__dx = -self.__dx # Initialize our mouse listeners self.switch = 0 # This variable controls if the game start or not. (1:start, 0: unstart) onmouseclicked(self.click_m) onmousemoved(self.move_m) # Draw bricks self.sum_bricks = brick_rows * brick_cols self.bricks_remove = 0 # record how many bricks were removed brick_spacing = (window_width - (brick_width*brick_rows))/(brick_rows-1) position_height = brick_offset for i in range(brick_cols): position_width = 0 for j in range(brick_rows): bricks = GRect(brick_width, brick_height) bricks.filled = True self.window.add(bricks, x=position_width, y=position_height) position_width += (brick_spacing + brick_width) position_height += (brick_spacing + brick_height) # The following variables are used to control user's life or die self.dead = 0 # (0: still life; 1: die)
def tie_fighter_r_skeleton(self, hull): size = self.brick_smaller() r_skeleton = GRect(size * 3, size * 2) r_skeleton.filled = True r_skeleton.fill_color = "grey" r_skeleton.color = "grey" self.window.add(r_skeleton, x=self.window_width / 2 - hull.width / 2 - r_skeleton.width, y=self.window_height / 2 - r_skeleton.height / 2 - 400) return r_skeleton
def win(self): self.window.clear() win_background = GRect(self.window.width, self.window.height) win_background.filled = True win_background.color = '#e5abbe' win_background.fill_color = '#e5abbe' win_label = GLabel('WIN') win_label.font = '-50' win_label.color = '#fdeff2' self.window.add(win_background) self.window.add(win_label, self.window.width/2-win_label.width/2, self.window.height/2.5) self.window.add(self.score_label, (self.window_width-self.score_label.width)/2, win_label.y+50)