def main(): rect = GRect(100, 100) rect.filled = True rect.fill_color = 'green' window.add(rect, 0, 0) change_color(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) # Play pilot animation self.pilot() # After the pilot, start the game # Create a paddle self.paddle_offset = paddle_offset self.paddle = GRect(paddle_width, paddle_height) self.paddle.filled = True self.paddle.fill_color = 'black' self.paddle.color = 'black' self.window.add(self.paddle, x=(self.window.width - self.paddle.width) / 2, y=(self.window.height - paddle_offset)) # Draw bricks self.brick_width = brick_width self.brick_height = brick_height self.brick_offset = brick_offset self.brick_spacing = brick_spacing self.brick_rows = brick_rows self.brick_cols = brick_cols for i in range(self.brick_rows): for j in range(self.brick_cols): self.brick = GRect(brick_width, brick_height) self.brick.filled = True if i // 2 == 0: self.brick.fill_color = 'salmon' self.brick.color = 'salmon' elif i // 2 == 1: self.brick.fill_color = 'gold' self.brick.color = 'gold' elif i // 2 == 2: self.brick.fill_color = 'lightskyblue' self.brick.color = 'lightskyblue' elif i // 2 == 3: self.brick.fill_color = 'cornflowerblue' self.brick.color = 'cornflowerblue' else: self.brick.fill_color = 'royalblue' self.brick.color = 'royalblue' self.window.add(self.brick, x=(j * (brick_width + brick_spacing)), y=(brick_offset + i * (brick_height + brick_spacing))) # Center a filled ball in the graphical window self.radius = ball_radius self.ball = GOval(self.radius * 2, self.radius * 2) self.ball.filled = True self.ball.fill_color = 'black' self.ball.color = 'black' self.window.add(self.ball, x=(self.window.width - self.ball.width) / 2, y=(self.window.height - self.ball.height) / 2) # Default initial velocity for the ball self.__dx = 0 self.__dy = 0 # Initialize our mouse listeners onmouseclicked(self.starter) onmousemoved(self.paddle_control) self.mouse_switch = True # Show the score board self.__score = 0 self.score_board = GLabel('Score: ' + str(self.__score)) self.score_board.font = 'Courier-10' self.window.add(self.score_board, 0, 20)
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) self.bg = GRect(self.window.width, self.window.height) self.bg.filled = True self.bg.filled_color = 'black' self.bg.color = 'black' self.window.add(self.bg) # Create a paddle. self.paddle = GRect(paddle_width, paddle_height) self.paddle.filled = True self.paddle.fill_color = 'crimson' self.paddle.color = 'honeydew' self.window.add(self.paddle, (self.window_width-paddle_width)/2, self.window_height-paddle_offset) # Center a filled ball in the graphical window. self.ball = GOval(ball_radius*2, ball_radius*2) self.ball.filled = True self.ball.fill_color = 'hotpink' self.ball.color = 'hotpink' self.window.add(self.ball, (self.window_width-ball_radius)/2, (self.window_height-ball_radius)/2) # Default initial velocity for the ball. self.__dx = 0 self.__dy = INITIAL_Y_SPEED # Initialize our mouse listeners. onmousemoved(self.move) onmouseclicked(self.start) # Draw bricks. for i in range(0, brick_rows): for j in range(0, brick_cols): self.brick = GRect(brick_width, brick_height) self.brick.filled = True if i < brick_rows / 5: self.brick.color = 'red' self.brick.fill_color = 'red' elif i < brick_rows / 5 * 2: self.brick.color = 'coral' self.brick.fill_color = 'coral' elif i < brick_rows / 5 * 3: self.brick.color = 'gold' self.brick.fill_color = 'gold' elif i < brick_rows / 5 * 4: self.brick.color = 'green' self.brick.fill_color = 'green' elif i <= brick_rows: self.brick.color = 'royalblue' self.brick.fill_color = 'royalblue' self.window.add(self.brick, (brick_width+brick_spacing)*j, brick_offset+(brick_height+brick_spacing)*i) self.bricks = brick_rows * brick_cols # lives self.l1 = GOval(ball_radius*2, ball_radius*2) self.l1.filled = True self.l1.fill_color = 'hotpink' self.l1.color = 'hotpink' self.window.add(self.l1, (self.window_width-10*ball_radius), (self.window_height-3*ball_radius)) self.l2 = GOval(ball_radius * 2, ball_radius * 2) self.l2.filled = True self.l2.fill_color = 'hotpink' self.l2.color = 'hotpink' self.window.add(self.l2, (self.window_width - 7 * ball_radius), (self.window_height - 3 * ball_radius)) self.l3 = GOval(ball_radius * 2, ball_radius * 2) self.l3.filled = True self.l3.fill_color = 'hotpink' self.l3.color = 'hotpink' self.window.add(self.l3, (self.window_width - 4 * ball_radius), (self.window_height - 3 * ball_radius)) self.lives = 0 # score_label self.score_label = GLabel('scores: 0') self.score_label.font = 'SansSerif-20-bold' self.score_label.color = 'cornsilk' self.window.add(self.score_label, 1, self.window.height-1) # A switch controls whether to start the game. self.lock = False # An object is used to store a value while using window.get_object_at(). self.obj = None
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.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 __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.paddle.filled = True self.paddle.fill_color = 'black' paddle_x = (self.window.width - self.paddle.width) / 2 # The initial x coordinate of the paddle self.__paddle_y = self.window.height - self.paddle.height - paddle_offset # The permanent y coordinate of the paddle self.window.add(self.paddle, paddle_x, self.__paddle_y) # Center a filled ball in the graphical window self.ball = GOval(ball_radius * 2, ball_radius * 2) self.ball.filled = True self.ball.fill_color = 'black' self.__ball_x = (self.window.width - self.ball.width) / 2 # The initial x coordinate of the ball self.__ball_y = (self.window.height - self.ball.height) / 2 # The initial y coordinate of the ball self.window.add(self.ball, self.__ball_x, self.__ball_y) # Default initial velocity for the ball self.__dx = random.randint(1, MAX_X_SPEED) # The initial x velocity self.__dy = INITIAL_Y_SPEED # The initial y velocity # Initialize our mouse listeners onmousemoved(self.move_paddle) self.__start = False # This variable that detects when the game starts onmouseclicked(self.start_game) # Draw bricks self.__brick_number = brick_rows * brick_cols brick_y = brick_offset # The initial y coordinate of the brick for i in range(brick_rows): brick_x = 0 # The initial x coordinate of the brick if i > 0: brick_y = brick_y + self.__brick.height + brick_spacing for j in range(brick_cols): self.__brick = GRect(brick_width, brick_height) self.__brick.filled = True if i <= 1: self.__brick.fill_color = 'red' self.__brick.color = 'red' elif i <= 3: self.__brick.fill_color = 'orange' self.__brick.color = 'orange' elif i <= 5: self.__brick.fill_color = 'yellow' self.__brick.color = 'yellow' elif i <= 7: self.__brick.fill_color = 'green' self.__brick.color = 'green' else: self.__brick.fill_color = 'blue' self.__brick.color = 'blue' self.window.add(self.__brick, brick_x, brick_y) brick_x = brick_x + brick_width + brick_spacing # Check collision self.obj = None # The object that the ball touches self.check_collision()
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=self.window.width - paddle_width / 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.ball = GOval(ball_radius * 2, ball_radius * 2, x=self.window.width / 2 - BALL_RADIUS, y=self.window.height / 2 - BALL_RADIUS) self.ball.filled = True self.window.add(self.ball) # Default initial velocity for the ball. self.__dx = 0 self.__dy = 0 # Initialize our mouse listeners. onmousemoved(self.move_paddle) onmouseclicked(self.move_ball) # Draw bricks. for yi in range(BRICK_ROWS): for xi in range(BRICK_COLS): self.brick = GRect(BRICK_WIDTH, BRICK_HEIGHT, x=xi * (BRICK_SPACING + BRICK_WIDTH), y=BRICK_OFFSET + yi * (BRICK_SPACING + BRICK_HEIGHT)) self.brick.filled = True self.brick.fill_color = BRICK_COL_LIST[yi // 2] self.brick.color = BRICK_COL_LIST[yi // 2] self.window.add(self.brick) # Number of bricks self.brick_num = BRICK_ROWS * BRICK_COLS