def __draw_bricks(self): for i in range(self.__brick_rows): for j in range(self.__brick_cols): br1 = GRect(self.__brick_width, self.__brick_height) br1.filled = True br1.x = (self.__brick_width + self.__brick_spacing) * j br1.y = self.__brick_offset + (self.__brick_height + self.__brick_spacing) * i rows_in_sec = self.__brick_rows / BRICK_SECTION if i <= rows_in_sec - 1: # Section 1 br1.fill_color = 'red' br1.color = 'red' elif rows_in_sec - 1 < i <= rows_in_sec * 2 - 1: # Section 2 br1.fill_color = 'orange' br1.color = 'orange' elif rows_in_sec * 2 - 1 < i <= rows_in_sec * 3 - 1: # Section 3 br1.fill_color = 'yellow' br1.color = 'yellow' elif rows_in_sec * 3 - 1 < i <= rows_in_sec * 4 - 1: # Section 4 br1.fill_color = 'green' br1.color = 'green' else: # Section 5 and so on. br1.fill_color = 'lightseagreen' br1.color = 'lightseagreen' self.__window.add(br1)
def move(): rect = GRect(20, 35, x=252 + 205, y=303) window.add(rect) vx = 3 while True: rect.move(vx, 0) rect.filled = True rect.color = 'darkgrey' rect.fill_color = 'darkgrey' if rect.x + rect.width >= 550: rect.x = 457 rect.y = 303 pause(DELAY)
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'): """ The basic parameters for building these breakout game. :param ball_radius: The radius of the ball. :param paddle_width: The width of the paddle. :param paddle_height: The height of the paddle. :param paddle_offset: The distance between paddle and the bottom of the window. :param brick_rows: The number of rows in bricks. :param brick_cols: The number of column in bricks. :param brick_width: The width of each brick. :param brick_height: The height of each brick. :param brick_offset: The distance between the first row of bricks and the top of the window. :param brick_spacing: The spacing between each brick. :param title: The name of this program. """ # 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.paddle = GRect(paddle_width, paddle_height, x=(self.window_width - paddle_width) / 2, y=(self.window_height - paddle_offset)) self.paddle.filled = True self.paddle.color = 'black' self.paddle.fill_color = 'black' self.window.add(self.paddle) self.paddle_width = paddle_width self.paddle_height = paddle_height self.paddle_offset = paddle_offset # Center a filled ball in the graphical window self.ball = GOval(ball_radius * 2, ball_radius * 2, x=(self.window_width - ball_radius * 2) / 2, y=(self.window_height - ball_radius * 2) / 2) self.ball.filled = True self.ball.fill_color = 'black' self.window.add(self.ball) self.ball_radius = ball_radius # Default initial velocity for the ball self.__dx = 0 # self.__dx = random.randint(1, MAX_X_SPEED) self.__dy = 0 # self.__dy = INITIAL_Y_SPEED # if random.random() > 0.5: # self.__dx = -self.__dx # The above is the mistake I made during doing this homework. # Draw bricks for i in range(brick_cols): for j in range(brick_rows): # Crucial point! This can't be placed at the outside of for loop. brick = GRect(brick_width, brick_height) brick.x = (brick_width + brick_spacing) * i brick.y = brick_offset + (brick_height + brick_spacing) * j brick.filled = True if j < 2: brick.fill_color = 'red' elif j < 4: brick.fill_color = 'orange' elif j < 6: brick.fill_color = 'yellow' elif j < 8: brick.fill_color = 'green' elif j < 10: brick.fill_color = 'blue' elif j < 12: brick.fill_color = 'teal' elif j < 14: brick.fill_color = 'chocolate' self.window.add(brick) # Initialize our mouse listeners onmouseclicked(self.is_start_game) onmousemoved(self.moving_paddle) # Total bricks self.total_bricks = brick_cols * brick_rows