Esempio n. 1
0
    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)
Esempio n. 2
0
    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', __dy=INITIAL_Y_SPEED):
        # 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(width=paddle_width, height=paddle_height, x=(self.window_width-paddle_width)/2, y=(self.window_height-paddle_offset-paddle_height))
        self.paddle.filled = True
        self.paddle.color = '#513743'
        self.paddle.fill_color = '#513743'
        self.window.add(self.paddle)

        self.life_label = GLabel('● ● ●')
        self.life_label.font = '-15'
        self.life_label.color = '#c53d43'

        self.score_label = GLabel('score: 0')
        self.score_label.font = '-15'
        self.score_label.color = '#455765'

        self.brick_rows = BRICK_ROWS
        self.brick_cols = BRICK_COLS

        # Initialize our mouse listeners.
        onmousemoved(self.reset_paddle_location)

        # Draw bricks.
        self.create_bricks()
Esempio n. 3
0
    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)
Esempio n. 4
0
    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 __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()
Esempio n. 6
0
def main():
    opening()
    life_ui.font = "-15"
    score_ui.font = "-15"
    w.add(life_ui, x=w.width - life_ui.width, y=w.height - life_ui.height)
    w.add(score_ui, x=0, y=w.height - score_ui.height)
    onmousemoved(mouse_move)
    onmouseclicked(main_game)
Esempio n. 7
0
def main():
    window.add(rect)
    rect.filled = True
    rect.color = COLOR
    rect.fill_color = COLOR
    onmousemoved(reset_position)
    onmouseclicked(my_punch)
    onmousedragged(draw)
Esempio n. 8
0
def main():
    """
    This program will prepare canvas for drawing circle and line,
    while listening to onmouseclicked and onmousemove events.
    """
    prepare_canvas()
    onmouseclicked(click_event)
    onmousemoved(move_event)
Esempio n. 9
0
def main():
    """
    This program will set attribute to the ball and add ball instance on window,
    while listening to onmouseclicked and onmousemoved events.
    """
    ball_setting()
    onmouseclicked(click_event)
    onmousemoved(move_event)
Esempio n. 10
0
    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.pof = paddle_offset
        self.paddle = GRect(paddle_width, paddle_height)
        self.paddle.filled = True
        self.window.add(self.paddle, (window_width - self.paddle.width) / 2,
                        window_height - self.pof)

        # Center a filled ball in the graphical window.
        self.ball = GOval(2 * ball_radius, 2 * ball_radius)
        self.ball.filled = True
        self.window.add(self.ball, self.window.width / 2 - self.ball.width / 2,
                        self.window.height / 2)
        self.initial_x = self.window.width / 2 - self.ball.width / 2
        self.initial_y = self.window.height / 2
        # Default initial velocity for the ball.
        self.__dx = 0
        self.__dy = 0
        self.set_speed()
        self.get_dx()
        self.get_dy()

        # Initialize our mouse listeners.
        self.click = False
        onmousemoved(self.paddle_follow)
        onmouseclicked(self.start)

        # Draw bricks.
        self.br = brick_rows
        self.bc = brick_cols
        self.sp = brick_spacing
        self.of = brick_offset
        self.set_brick()
        # self.check()
        # self.rest_ball()
        self.total = self.br * self.bc
    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 start(self, e):
     """
     Start the game
     :param e: event
     :return: None
     """
     onmousemoved(self.paddle_move)
     onmouseclicked(self.click)
     self.window.remove(self.icon)
Esempio n. 13
0
    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
Esempio n. 14
0
 def handle_click(self, _):
     """
     This function will execute if a click on the mouse. After a single click,
     the game will start, and any clicks can no longer affect the game.
     :param _: Detects a click on the mouse.
     :return: self.count
     """
     self.window.remove(self.click2start)
     self.is_game_started = True
     onmousemoved(self.paddle_move)
Esempio n. 15
0
    def __init__(self, window_object):
        self.window = window_object

        # initialize bar
        self.slide_bar = GRect(WINDOW_DIM - 2 * BAR_OFFSET,
                               BAR_WIDTH,
                               x=BAR_OFFSET,
                               y=WINDOW_DIM - BAR_OFFSET)
        self.slide_bar.filled = True
        self.slide_bar.fill_color = 'darksage'
        self.window.add(self.slide_bar)

        # initialize orb
        self.slider_orb = GOval(BALL_RADIUS * 2,
                                BALL_RADIUS * 2,
                                x=WINDOW_DIM - 2 * BAR_OFFSET,
                                y=WINDOW_DIM - BAR_OFFSET - BALL_RADIUS / 2)
        self.slider_orb.filled = True
        self.slider_orb.fill_color = 'mediumturquoise'
        self.window.add(self.slider_orb)

        # initialize blank fractal
        self.fractal = GImage.from_file('white_600_400.png')

        # initialize description label
        self.description_1 = GLabel("Click to start animation.", 40, 40)
        self.description_1.x = self.window.width / 2 - self.description_1.\
            width / 2
        self.description_1.y = self.window.height - 2*self.description_1.\
            height + 5
        self.window.add(self.description_1)

        self.description_2 = GLabel(
            "Move the slider in order to see the progression of the Julia set fractal pattern.",
            40, 40)
        self.description_2.x = self.window.width / 2 - self.description_2.width / 2
        self.description_2.y = self.window.height - self.description_2.height + 5
        self.window.add(self.description_2)

        # initialize title
        self.title = GImage.from_file('title.PNG')
        self.title.x = (self.window.width - self.title.width) / 2
        self.title.y = 10
        self.window.add(self.title)

        # initialize slider ratio (to measure progression)
        self.slider_ratio = 0

        # add mouse listeners
        onmouseclicked(self.start_sequence)
        onmousemoved(self.zoom_fractal)

        # initialize run statement
        self.running = False
Esempio n. 16
0
    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)
Esempio n. 17
0
    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'):

        self.b_r = brick_rows
        self.b_c = brick_cols
        self.b_w = brick_width
        self.b_h = brick_height
        self.b_o = brick_offset
        self.b_s = brick_spacing
        self.b_amount = brick_rows * brick_cols
        self.ball_r = ball_radius
        self.paddle_o = paddle_offset

        # Create a graphical window, with some extra space.
        self.brick = GRect(brick_width, brick_height)
        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 an image
        self.img = GImage('luv.png')
        self.reset_img()

        # Create a paddle.
        self.paddle = GRect(paddle_width, paddle_height)
        self.paddle.filled = True
        self.paddle.fill_color = 'coral'
        self.paddle.color = 'coral'
        self.window.add(self.paddle, (self.window.width-self.paddle.width)/2, \
                        (self.window.height-paddle_offset))

        # Draw bricks.
        self.add_bricks()

        # 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 = 'royalblue'
        self.ball.color = 'royalblue'
        self.window.add(self.ball, (self.window.width - self.ball.width) / 2,
                        (self.window.height - self.ball.height) / 2)

        # Default initial velocity for the ball.
        self.__dy = 0
        self.__dx = 0
        self.ball_velocity()

        # Initialize our mouse listeners.h
        onmousemoved(self.paddle_move)
Esempio n. 18
0
    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'):

        self.__dx = 0
        self.__dy = 0
        self.paddle_height = paddle_height
        self.paddle_width = paddle_width
        self.paddle_offset = paddle_offset
        self.count = 0

        # 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)
        self.window.add(self.paddle, 0.5 * self.window_width - 0.5 * paddle_width, \
                        self.window_height - paddle_offset - paddle_height)
        self.paddle.filled = True

        # Center a filled ball in the graphical window.
        self.ball = GOval(2 * ball_radius, 2 * ball_radius)
        self.window.add(self.ball, 0.5 * self.window_width - ball_radius, 0.5 * self.window_height - ball_radius)
        self.ball.filled = True

        # Default initial velocity for the ball.

        # Initialize our mouse listeners.
        onmousemoved(self.move)
        onmouseclicked(self.start)

        # Draw bricks.
        n = brick_offset
        m = 0
        color = ['red', 'orange', 'yellow', 'green', 'blue']
        for i in range(brick_rows):
            n += brick_height + brick_spacing
            for j in range(brick_cols):
                m = j * (brick_width + brick_spacing)
                brick = GRect(brick_width, brick_height)
                brick.filled = True
                brick.fill_color = color[i // 2]
                #  why cannot use else at the end?
                self.window.add(brick, m, n)

        self.original_x = 0.5 * self.window_width - ball_radius
        self.original_y = 0.5 * self.window_height - ball_radius
Esempio n. 19
0
 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'):
     # Instance Variable
     self.__game_status = False
     self.life_icon_list = []
     self.__monster_list = []
     self.__monster_dict = {}
     # Create a graphical window, with some extra space.
     window_width = brick_cols * (brick_width + brick_spacing) - brick_spacing
     window_height = brick_offset + 2.5 * (brick_rows * (brick_height + brick_spacing) - brick_spacing)
     self.window = GWindow(width=window_width, height=window_height, title=title)
     # Draw bricks as a monster.
     color = ['0xFFABAB', '0xFFE5B5', '0xFFFF45', '0xB5FFB5	', '0xC2C2FF']
     for row in range(brick_rows):
         for col in range(brick_cols):
             monster = Monster(brick_width, brick_height, x=col * (brick_width + brick_spacing),
                               y=row * (brick_height + brick_spacing), hp=MONSTER_HP*(BRICK_ROWS-row))
             monster.body.fill_color = color[row]
             self.__monster_list.append(monster.body)
             self.__monster_dict[monster.body] = monster
             self.window.add(monster.body)
             self.window.add(monster.l_eye)
             self.window.add(monster.r_eye)
             self.window.add(monster.hp_label)
     # Create a paddle.
     self.paddle = GRect(paddle_width, paddle_height)
     self.paddle.filled = True
     self.paddle.fill_color = 'black'
     self.window.add(self.paddle, x=(self.window.width-self.paddle.width)/2,
                     y=self.window.height-paddle_offset)
     # Create score board
     self.score = 0
     self.score_label = GLabel(f'Score: {self.score}')
     self.score_label.font = '-20'
     self.window.add(self.score_label, x=0, y=self.window.height)
     # Center a filled ball in the graphical window as a hero.
     self.ball_radius = ball_radius
     self.hero = Hero(ball_radius * 2, ball_radius * 2)
     self.hero.body.filled = True
     self.hero.body.fill_color = 'black'
     self.set_hero_position()
     # Default initial velocity for the ball.
     self.__dx = 0
     self.__dy = 0
     # Initialize our mouse listeners.
     onmousemoved(self.set_paddle)
     onmouseclicked(self.game_start)
Esempio n. 20
0
    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.window.add(self.paddle, (self.window.width - self.paddle.width) / 2,
                        (self.window.height - self.paddle.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.window.add(self.ball, (self.window.width - self.ball.width) / 2,
                        (self.window.height - self.ball.height) / 2)

        self.move = False  # Set move so the ball won't be influenced by clicks

        # Default initial velocity for the ball
        self.__dx = random.randint(1, MAX_X_SPEED)
        if random.random() > 0.5:
            self.__dx = -self.__dx
        self.__dy = INITIAL_Y_SPEED
        # Initialize our mouse listeners
        onmouseclicked(self.start)
        onmousemoved(self.paddle_move)
        self.paddle_offset = PADDLE_OFFSET  # Set paddle_offset as an attribute so it can be used in methods
        self.row = brick_rows  # Count the number of rows
        self.col = brick_cols  # Count the number of columns

        # Draw bricks
        y = brick_offset
        for i in range(brick_rows):
            x = 0  # Put position x in for loop to reset the x value at every row
            for j in range(brick_cols):
                self.rect = GRect(brick_width, brick_height)
                self.rect.filled = True
                color = ['red', 'red', 'orange', 'orange', 'yellow', 'yellow', 'green', 'green', 'blue', 'blue']
                self.rect.fill_color = color[i]
                self.window.add(self.rect, x, y)
                x += (brick_width + brick_spacing)
            y += (brick_height + brick_spacing)
Esempio n. 21
0
 def start_game(self, m):
     """
     The game will triggered by this method once player click the mouse.
     :param m: mouse position in (x,y) expression.
     """
     if not self.is_game_over:
         if self.mouse_click:
             print('Game already start.')
         else:
             self.mouse_click = True
             print('Game started')
             onmousemoved(self.__paddle_move)
     else:
         print('There\'s no chance left')
Esempio n. 22
0
 def check_game_start(self, mouse):
     """
     once the user click the mouse, the game will start that the function
     will give the ball velocity to move.
     once the ball is out of the window, the user have to click the mouse again
     to drop the ball.
     """
     if self.ball.x == self.original_x and self.ball.y == self.original_y:
         onmousemoved(self.paddle_move)
         self.window.remove(self.start_label)
         # give the ball velocity
         if self.__dx == 0:
             self.__dy = INITIAL_Y_SPEED
             self.__dx = random.randint(1, MAX_X_SPEED)
             if random.random() > 0.5:
                 self.__dx = -self.__dx
                 print(f'{self.__dy}')
    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)
Esempio n. 24
0
    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 score label
        self.score = 0
        self.score_label = GLabel("Score: " + str(self.score))
        self.score_label.font = "Comic Sans MS-20-bold"
        self.window.add(self.score_label, 0,
                        self.window_height - self.score_label.height)

        # create three hit points
        self.point1 = GOval(ball_radius * 2,
                            ball_radius * 2,
                            x=self.window_width - ball_radius * 3,
                            y=self.window_height - self.window_height * 0.06)
        self.point2 = GOval(ball_radius * 2,
                            ball_radius * 2,
                            x=self.window_width - ball_radius * 6,
                            y=self.window_height - self.window_height * 0.06)
        self.point3 = GOval(ball_radius * 2,
                            ball_radius * 2,
                            x=self.window_width - ball_radius * 9,
                            y=self.window_height - self.window_height * 0.06)
        self.point1.filled = True
        self.point2.filled = True
        self.point3.filled = True
        self.point1.color = "white"
        self.point2.color = "white"
        self.point3.color = "white"
        self.point1.fill_color = "salmon"
        self.point2.fill_color = "salmon"
        self.point3.fill_color = "salmon"
        self.window.add(self.point1)
        self.window.add(self.point2)
        self.window.add(self.point3)

        # create a paddle
        self.paddle = GRect(paddle_width, paddle_height)
        self.paddle.filled = True
        self.window.add(self.paddle,
                        x=(self.window_width - paddle_width) / 2,
                        y=self.window_height - paddle_offset - paddle_height)

        # 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 = random.randint(1, MAX_X_SPEED)
        self.__dy = INITIAL_Y_SPEED
        self.reset_dx()
        # Initialize our mouse listeners.
        self.running = False
        onmouseclicked(self.start_running)
        onmousemoved(self.reset_paddle_position)

        # Draw bricks.
        for i in range(brick_cols):
            x_position = 0
            x_position += (brick_width + brick_spacing) * i
            for j in range(brick_rows):
                y_position = brick_offset
                y_position += (brick_height + brick_spacing) * j
                self.bricks = GRect(brick_width, brick_height)
                # coloring
                self.bricks.filled = True
                self.bricks.color = "white"
                if j == 0 or j == 1:
                    self.bricks.fill_color = "black"
                elif j == 2 or j == 3:
                    self.bricks.fill_color = "navy"
                elif j == 4 or j == 5:
                    self.bricks.fill_color = "steelblue"
                elif j == 6 or j == 7:
                    self.bricks.fill_color = "skyblue"
                else:
                    self.bricks.fill_color = "lightblue"
                self.window.add(self.bricks, x=x_position, y=y_position)
        self.bricks_num = brick_cols * brick_rows
Esempio n. 25
0
    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.__paddle = GRect(paddle_width, paddle_height)
        self.__paddle.filled = True
        self.__window.add(self.__paddle, self.__window.width / 2 - self.__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.__window.add(self.__ball, self.__window.width / 2 - self.__ball.width / 2, self.__window.height / 2 + self.__ball.height / 2)

        # Default initial velocity for the ball
        self.__dx = 0
        self.__dy = 0

        # Initialize our mouse listeners
        self.__restart_game = False
        self.__game_start = False
        onmouseclicked(self.click_event)
        onmousemoved(self.move_event)

        # Draw bricks
        self.__brick_rows = brick_rows
        self.__brick_cols = brick_cols
        self.__brick_width = brick_width
        self.__brick_height = brick_height
        self.__brick_spacing = brick_spacing
        self.__brick_offset = brick_offset
        self.__brick_nums = 0
        self.set_bricks(self.__brick_rows, self.__brick_cols, self.__brick_width, self.__brick_height, self.__brick_spacing, self.__brick_offset)

        # Prepare label
        self.__score = 0
        self.__top_score = 0
        self.__game_end_label = GLabel("GAME OVER")
        self.__game_end_label.font = "Verdana-17"
        self.__game_end_label.color = (150, 0, 0)
        self.__score_label = GLabel("Your Score : " + str(self.__score))
        self.__score_label.font = "Verdana-15"
        self.__score_label.color = (100, 100, 100)
        self.__top_score_label = GLabel("Top Score : " + str(self.__top_score))
        self.__top_score_label.font = "Verdana-15"
        self.__top_score_label.color = (100, 100, 100)
        self.__hint_label = GLabel("Click to Start")
        self.__hint_label.font = "Verdana-15"
        self.__hint_label.color = (100, 100, 100)
        self.__window.add(self.__game_end_label, self.__window.width / 2 - self.__game_end_label.width / 2, 0)
        self.__window.add(self.__score_label, 0, self.__score_label.height)
        self.__window.add(self.__top_score_label, self.__window.width - self.__top_score_label.width, self.__top_score_label.height)
        self.__window.add(self.__hint_label, self.__window.width / 2 - self.__hint_label.width / 2, self.__window.height / 2 + self.__ball.height / 2 + self.__ball.height + self.__hint_label.height * 4)
        self.__life_point_text = ""
        for i in range(3):
            self.__life_point_text += "❤"
        self.__life_point_label = GLabel(self.__life_point_text)
        self.__life_point_label.font = "-22"
        self.__life_point_label.color = (150, 0, 0)
        self.__window.add(self.__life_point_label, self.__window.width - self.__life_point_label.width, self.__window.height)
Esempio n. 26
0
def main():
	window.add(line)
	rect.filled = True
	rect.fill_color = 'blue'
	# onmousedragged(draw)
	onmousemoved(reset_position)
Esempio n. 27
0
    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'):

        self.pw = paddle_width  # Class variable to store paddle width.
        self.ph = paddle_height  # Class variable to store paddle height.
        self.pos = paddle_offset  # Class variable to store paddle offset.
        self.br = ball_radius  # Class variable to store ball radius.
        self.brk_row = brick_rows  # Class variable to store the number of brick rows.
        self.brk_col = brick_cols  # Class variable to store the number of brick columns.
        self.click = 1  # Class variable as a switch to control game status, where 1 equals to game termination.
        self.brick_count = 0  # Class variable to store the number of bricks being eliminated.
        self.score = 0  # Class variable to store the score of the game.

        # 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.wh = window_height  # Class variable to store window height.
        self.ww = window_width  # Class variable to store window width.

        # Create a label to show the score.
        self.label_s = GLabel('Scores: ' + str(self.score),
                              x=2,
                              y=window_height - 2)
        self.label_s.font = '-10'
        self.window.add(self.label_s)

        # Create a paddle.
        self.paddle = GRect(width=paddle_width,
                            height=paddle_height,
                            x=(window_width - paddle_width) / 2,
                            y=window_height - paddle_offset - paddle_height)
        self.paddle.filled = True
        self.paddle.fill_color = 'black'
        self.paddle.color = 'black'
        self.window.add(self.paddle)

        # 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.color = 'black'
        self.set_ball(
        )  # Method to set ball place at initial, details shown as below.
        self.window.add(self.ball)

        # Method to set initial velocity of the ball, details shown as below.
        self.set_ball_velocity()

        # Initialize mouse listeners.
        onmousemoved(self.paddle_location
                     )  # Method to move paddle while moving the mouse.
        onmouseclicked(
            self.game_start
        )  # Method to control game status while clicking the mouse.

        # Draw bricks with different colors, which is determined ny the number of rows.
        for i in range(brick_cols):
            for j in range(brick_rows):
                self.brick = GRect(brick_width,
                                   brick_height,
                                   x=i * (brick_width + brick_spacing),
                                   y=j * (brick_height + brick_spacing) +
                                   BRICK_OFFSET)
                self.brick.filled = True
                if j % 10 == 0 or j % 10 == 1:
                    self.brick.fill_color = 'Red'
                    self.brick.color = 'Red'
                elif j % 10 == 2 or j % 10 == 3:
                    self.brick.fill_color = 'Orange'
                    self.brick.color = 'Orange'
                elif j % 10 == 4 or j % 10 == 5:
                    self.brick.fill_color = 'Yellow'
                    self.brick.color = 'Yellow'
                elif j % 10 == 6 or j % 10 == 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)
 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.game_start = False
     self.window = GWindow(width=window_width,
                           height=window_height,
                           title=title)
     # Create a paddle.
     self.paddle = GRect(width=paddle_width, height=paddle_height)
     self.paddle.filled = True
     self.paddle_offset = paddle_offset
     self.window.add(self.paddle, (window_width - paddle_width) / 2,
                     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.window.add(self.ball, window_width / 2 - ball_radius,
                     window_height / 2 - ball_radius)
     # Default initial velocity for the ball.
     self.__dy = 0
     self.__dx = 0
     # Calculate times of removing brick
     self.count = 0
     # Draw bricks.
     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 j == 0 or j == 1:
                 color = 'red'
             elif j == 2 or j == 3:
                 color = 'gold'
             elif j == 4 or j == 5:
                 color = 'yellow'
             elif j == 6 or j == 7:
                 color = 'green'
             else:
                 color = 'blue'
             self.brick.fill_color = color
             self.brick.color = color
             self.window.add(self.brick, (brick_spacing + brick_width) * i,
                             (brick_spacing + brick_height) * j +
                             brick_offset)
     # Initialize our mouse listeners.
     onmousemoved(self.paddle_move)
     onmouseclicked(self.initialize_velocity)
Esempio n. 29
0
    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'):
        # only three lives
        self.num_lives = NUM_LIVES
        # win img
        self.img = GImage('WIN!!.png')

        # 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)

        # Start button
        self.button_back = GRect(250, 50)
        self.button_back.filled = True
        self.button_back.fill_color = 'darkgrey'
        self.window.add(self.button_back,
                        x=(self.window.width - self.button_back.width) // 2,
                        y=352.5)

        self.button = GRect(250, 50)
        self.button.filled = True
        self.button.fill_color = 'grey'
        self.window.add(self.button,
                        x=(self.window.width - self.button.width) // 2,
                        y=350)

        self.button_word = GLabel('CLICK TO START')
        self.button_word.font = '-30'
        self.window.add(self.button_word,
                        x=(self.window.width - self.button_word.width) // 2,
                        y=350 + self.button_word.height + 10)

        # 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.ball.filled = True
        self.window.add(self.ball)

        # Default initial velocity for the ball.
        self.__dx = self.set_ball_x_velocity()
        self.__dy = INITIAL_Y_SPEED

        # Initialize our mouse listeners.
        onmouseclicked(self.start)
        onmousemoved(self.control_paddle)

        # the switch of the game
        self.start = False

        # Score board.
        self.score = 0
        self.score_label = GLabel('Score: ' + str(self.score))
        self.score_label.font = '-22'
        self.window.add(self.score_label, x=0, y=self.score_label.height + 5)

        # how many lives
        self.life1 = GLabel('❤️')
        self.life1.font = '-20'
        self.window.add(self.life1, self.window.width - self.life1.width,
                        self.life1.height + 7)

        self.life2 = GLabel('❤️')
        self.life2.font = '-20'
        self.window.add(self.life2, self.window.width - self.life2.width * 2,
                        self.life2.height + 7.35)

        self.life3 = GLabel('❤️')
        self.life3.font = '-20'
        self.window.add(self.life3, self.window.width - self.life3.width * 3,
                        self.life3.height + 7.35)

        # Draw bricks.
        x = 0
        # to change row
        space = 0
        self.change = 0
        self.color = 'red'
        # 5 colors
        for i in range(brick_rows // 2):
            # each color 2 rows
            for j in range(2):
                # x bricks
                for k in range(brick_cols):
                    self.brick = GRect(brick_width,
                                       brick_height,
                                       x=0 + x,
                                       y=brick_offset + space)
                    x += (brick_width + brick_spacing)
                    self.brick.filled = True
                    self.brick.fill_color = self.color
                    self.brick.color = self.color
                    self.window.add(self.brick)
                x = 0
                space += (brick_height + brick_spacing)
            self.change_color()
    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'):

        self.__ball_radius = ball_radius
        self.__paddle_width = paddle_width
        self.__paddle_height = paddle_height
        self.__paddle_offset = paddle_offset
        self.__brick_rows = brick_rows
        self.__brick_cols = brick_cols
        self.__brick_width = brick_width
        self.__brick_height = brick_height
        self.__brick_offset = brick_offset
        self.__brick_spacing = brick_spacing

        # 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(self.__paddle_width,
                            self.__paddle_height,
                            x=(self.window.width - paddle_width) / 2,
                            y=(self.window.height - paddle_height -
                               self.__paddle_offset))
        self.paddle.filled = True
        self.window.add(self.paddle)

        # Center a filled ball in the graphical window.
        self.ball = GOval(self.__ball_radius * 2,
                          self.__ball_radius * 2,
                          x=self.window.width / 2 - self.__ball_radius,
                          y=self.window.height / 2 - self.__ball_radius)
        self.ball.filled = True
        self.window.add(self.ball)

        # Default initial velocity for the ball.
        self._dy = INITIAL_Y_SPEED
        self._dx = random.randint(1, MAX_X_SPEED)
        if (random.random() > 0.5):
            self._dx = -self._dx

        # Initialize our mouse listeners.
        onmousemoved(self.paddle_move)
        onmouseclicked(self.ball_start)
        self.start = False

        # Draw bricks
        for i in range(self.__brick_rows):
            for j in range(self.__brick_cols):
                self.bricks = GRect(
                    self.__brick_width,
                    self.__brick_height,
                    x=j * (self.__brick_width + self.__brick_spacing),
                    y=self.__brick_offset + i *
                    (self.__brick_height + self.__brick_spacing))
                self.bricks.filled = True
                if i % 10 == 0 or i % 10 == 1:
                    self.bricks.fill_color = 'red'
                elif i % 10 == 2 or i % 10 == 3:
                    self.bricks.fill_color = 'orange'
                elif i % 10 == 4 or i % 10 == 5:
                    self.bricks.fill_color = 'yellow'
                elif i % 10 == 6 or i % 10 == 7:
                    self.bricks.fill_color = 'green'
                elif i % 10 == 8 or i % 10 == 9:
                    self.bricks.fill_color = 'blue'
                self.window.add(self.bricks)