Ejemplo n.º 1
0
def sierpinski_triangle(order, length, upper_left_x, upper_left_y):
    """
	:param order:
	:param length:
	:param upper_left_x:
	:param upper_left_y:
	:return:
	"""
    if order == 0:
        return
    else:
        tri1 = GLine(upper_left_x, upper_left_y, upper_left_x + length,
                     upper_left_y)
        tri1.color = 'mediumblue'
        tri2 = GLine(upper_left_x, upper_left_y, upper_left_x + length / 2,
                     upper_left_y + length * t_h)
        tri2.color = 'steelblue'
        tri3 = GLine(upper_left_x + length, upper_left_y,
                     upper_left_x + length / 2, upper_left_y + length * t_h)
        tri3.color = 'dodgerblue'
        window.add(tri1)
        window.add(tri2)
        window.add(tri3)

        # Upper left triangle.
        sierpinski_triangle(order - 1, length / 2, upper_left_x, upper_left_y)
        # Upper right triangle.
        sierpinski_triangle(order - 1, length / 2, upper_left_x + length / 2,
                            upper_left_y)
        # Middle bottom triangle.
        sierpinski_triangle(order - 1, length / 2, upper_left_x + length / 4,
                            upper_left_y + length * t_h / 2)
        pause(PAUSE)
Ejemplo n.º 2
0
def sierpinski_triangle(order, length, upper_left_x, upper_left_y):
    """
	:param order: control how many times of loop
	:param length: the length of the first triangle
	:param upper_left_x: the upper left x coordinate
	:param upper_left_y: the upper left y coordinate
	:return: nothing
	"""
    if order == 0:
        return
    else:
        # draw line to form a triangle
        line1 = GLine(upper_left_x, upper_left_y, upper_left_x + length,
                      upper_left_y)
        line2 = GLine(upper_left_x, upper_left_y, upper_left_x + 0.5 * length,
                      upper_left_y + 0.866 * length)
        line3 = GLine(upper_left_x + length, upper_left_y,
                      upper_left_x + length * 0.5,
                      upper_left_y + 0.866 * length)

        pause(20)
        # add line on window
        window.add(line1)
        window.add(line2)
        window.add(line3)

        # enter recursion
        # one at left, one at right, and one at the bottom
        sierpinski_triangle(order - 1, length / 2, upper_left_x, upper_left_y)
        sierpinski_triangle(order - 1, length / 2, upper_left_x + 0.5 * length,
                            upper_left_y)
        sierpinski_triangle(order - 1, length / 2,
                            upper_left_x + 0.25 * length,
                            upper_left_y + 0.433 * length)
Ejemplo n.º 3
0
def sierpinski_triangle(order, length, upper_left_x, upper_left_y):
    """
	This function recursively prints a certain order of Sierpinski triangle.
	:param order: Controls the order of Sierpinski triangle.
	:param length: Controls the length of Sierpinski triangle.
	:param upper_left_x: The x coordinate of the upper left vertex of a triangle.
	:param upper_left_y: The y coordinate of the upper left vertex of a triangle.
	"""
    pause(10)
    # Draw three line to create a triangle each time.
    line1 = GLine(upper_left_x, upper_left_y, upper_left_x + length,
                  upper_left_y)
    line2 = GLine(upper_left_x, upper_left_y, upper_left_x + length * 0.5,
                  upper_left_y + length * 0.866)
    line3 = GLine(upper_left_x + length, upper_left_y,
                  upper_left_x + length * 0.5, upper_left_y + length * 0.866)
    # Add lines to window.
    window.add(line1)
    window.add(line2)
    window.add(line3)
    # Pass when order reduce to 1.
    if order == 1:
        return
    else:
        # Execute three recursion respectively.
        sierpinski_triangle(order - 1, length * 0.5, upper_left_x,
                            upper_left_y)
        sierpinski_triangle(order - 1, length * 0.5,
                            upper_left_x + length * 0.5, upper_left_y)
        sierpinski_triangle(order - 1, length * 0.5,
                            upper_left_x + length * 0.25,
                            upper_left_y + length * 0.433)
Ejemplo n.º 4
0
def bounce(event):
    global times, vy, started
    while times < 3 and started is True:
        vy = vy + GRAVITY
        ball.move(VX, vy)
        if ball.y + SIZE >= window.height:
            """
            When the ball drops to the floor, it will bounce back with opposite
            direction and the velocity will reduce.
            """
            vy = -vy * REDUCE
            if vy > 0:
                vy = -vy
        if ball.x + SIZE >= window.width:
            """
            When the ball reaches the right-side bound, it means one round has been done.
            The ball will be put back to the starting place, 
            and the button will be removed to detect the further clicks.
            """
            ball.x = START_X
            ball.y = START_Y
            times += 1
            vy = 0
            started = False
        pause(DELAY)
Ejemplo n.º 5
0
def main():
    graphics = BreakoutGraphics()
    live = NUM_LIVES
    score = graphics.scores

    # Add animation loop here!
    while True:

        pause(FRAME_RATE)
        if graphics.ball.y + graphics.ball.height >= graphics.window.height:
            live -= 1
            if live > 0:
                graphics.reset_ball()
            else:
                graphics.gameover()
                break

        dx = graphics.get_vx()
        dy = graphics.get_vy()
        graphics.ball.move(dx, dy)
        graphics.sensor()
        # graphics.box_sensor()
        # graphics.drop_a_gift()
        # graphics.box.move(0,5)
        graphics.end()

        if score == 3:
            graphics.window.add()

        if graphics.ball.x <= 0 or graphics.ball.x + graphics.ball.width >= graphics.window.width:
            graphics.reverse_vx()
        if graphics.ball.y <= 0:
            graphics.reverse_vy()
Ejemplo n.º 6
0
    def loading(self):

        self.download_label = GLabel('downloading')
        self.solid_bar.color = 'black'
        self.solid_bar.filled = True
        self.solid_bar.fill_color = 'black'

        self.window.add(self.load_bar, (self.window.width - self.load_bar.width) / 2,
                        self.window.height * 0.7)

        while self.solid_bar.width < self.load_bar.width:
            self.window.remove(self.solid_bar)
            self.window.remove(self.load_label)
            self.solid_bar.width = self.solid_bar.width + 10
            if (bar_ratio := (self.solid_bar.width / self.load_bar.width) * 100) < 100:
                self.load_label.text = f'{int(bar_ratio)} %'
            else:
                self.load_label.text = f'100%'

            self.window.add(self.solid_bar, self.load_bar.x, self.load_bar.y)
            self.window.add(self.load_label, self.load_bar.x + self.load_bar.width - self.load_label.width,
                            self.load_bar.y + self.load_bar.height + self.load_label.height + 5)

            if self.solid_bar.width < self.load_bar.width * 0.33:
                self.download_label.text = 'downloading....'
            elif self.load_bar.width * 0.25 <= self.solid_bar.width < self.load_bar.width * 0.66:
                self.download_label.text = 'downloading........'
            elif self.load_bar.width * 0.5 <= self.solid_bar.width < self.load_bar.width * 0.99:
                self.download_label.text = 'downloading............'

            self.window.add(self.download_label, self.load_bar.x,
                            self.load_bar.y + self.load_bar.height + self.load_label.height + 5)

            pause(100)
            self.window.remove(self.download_label)
Ejemplo n.º 7
0
def bouncing_ball(mouse):
    """
    This function simulates the bouncing process

    :param mouse:
    """
    global count, lock

    # the initial condition of the ball when clicking the mouse
    vy = 0

    if lock and count <= 3:
        lock = False  # used to close the mouse-clicked function

        # move the ball
        while True:
            ball.move(VX, vy)
            pause(DELAY)

            # ball is falling
            vy += GRAVITY

            # ball is outside of the window
            if ball.x > window.width:
                count += 1
                setup_ball()
                lock = True  # used to open the mouse-clicked function
                break

            # ball is bouncing
            if ball.y > (window.height - SIZE) and vy > 0:
                vy *= -REDUCE
def bouncing(mouse):
    """
    when you click, the code will be launched.
    this simulate the world gravity.
    """
    global is_moving
    global click_time
    if not is_moving and click_time > 0:
        click_time -= 1
        is_moving = True
        window.add(ball, x=START_X, y=START_Y)
        s = 0
        vy = GRAVITY
        vx = VX
        while True:
            ball.move(vx, vy)
            if ball.x + ball.width >= window.width:
                reset()
                break
            vy += GRAVITY
            if ball.y + ball.height >= window.height:
                vy = -REDUCE * vy
            pause(DELAY)
            s += DELAY
        is_moving = False
Ejemplo n.º 9
0
def handle_click(mouse):
    graphics.set_ball_velocity()
    dx = graphics.get_dx()
    dy = graphics.get_dy()
    global game_status
    global brick_cnt
    global lives
    if game_status == 0 and lives > 0 and brick_cnt > 0:
        # Add animation loop here
        while True:
            pause(FRAME_RATE)
            if brick_cnt == 0:
                graphics.reset_ball_position()
                break
            graphics.ball.move(dx, dy)
            game_status = 1
            if graphics.is_collide_paddle():
                if dy > 0:
                    dy = -dy
            if graphics.is_collide_brick():
                dy = -dy
                brick_cnt -= 1
            if graphics.ball.x <= 0 or graphics.ball.x >= graphics.window.width - graphics.ball.width:
                dx = -dx
            if graphics.ball.y <= 0:
                dy = -dy
            if graphics.ball.y >= graphics.window.height - graphics.ball.height:
                game_status = 0
                lives -= 1
                graphics.reset_ball_position()
                break
Ejemplo n.º 10
0
def fall(c, v):
    """
    :param c: OVal, the ball will fall down until touch the ground
    :param v: int, to make the ball fall faster
    """
    c.move(VX, GRAVITY + GRAVITY * v)
    pause(DELAY)
Ejemplo n.º 11
0
def main():
    """
    set up the score label and how many life dose the user have.
    """
    graphics = BreakoutGraphics()
    life = NUM_LIVES
    score_label = GLabel('Score: ' + str(graphics.score) + ' and live: ' + str(life))
    score_label.font = '-20'
    graphics.window.add(score_label, 0, graphics.window.height)
    switch = 1  # a switch that prevents user to click while the ball is moving

    # Add animation loop here!
    while switch == 1 and life >= 0:
        # renew the score and life every time
        score_label.text = 'Score: ' + str(graphics.score) + ' and life: ' + str(life)
        # let the ball moves
        graphics.ball.move(graphics.get_vx(), graphics.get_vy())
        # check whether ball touches objects or not
        graphics.object_check()
        # when touching the bricks, the bricks should be eliminated
        graphics.eliminate_brick()
        # when touching the edge of the window, the ball should reflect
        graphics.edge_check()
        # when the ball touches to the bottom of the window, switch = -1 and the life should minus one
        switch = graphics.check_dead()
        if switch == -1:
            life -= 1
            switch = 1
            graphics.ball.x = (graphics.window.width - graphics.ball.width) / 2
            graphics.ball.y = (graphics.window.height - graphics.ball.height) / 2
            graphics.ball_restart()
        # if all the bricks are eliminated, the game should be ended
        if graphics.score == graphics.max_score:
            break
        pause(FRAME_RATE)  # prevent the ball move too fast
def fall(mouse):
    global times, locked
    vy = 0  # vy means the moved vertical values, starts with zero
    if times < 3:  # when ball jumps out of the window below three times
        if locked:
            while True:
                locked = False  # turn off the switch
                # ball starts moving
                ball.move(VX, vy)

                # check the position of ball
                if ball.y >= window.height:  # when ball is below window, it bounces back.
                    vy *= -REDUCE  # each bounce reduces y velocity to REDUCE of itself.
                if ball.x >= window.width:  # when ball jumps out of the window,
                    times += 1  # add up one time
                    break  # jump out of the while True area

                pause(DELAY)
                vy += GRAVITY
            window.add(ball, START_X,
                       START_Y)  # ball go back to the original position
            locked = True  # turn on the switch so that we can keep
    else:  # when ball jumps out of the window more than three times
        window.add(
            ball, START_X, START_Y
        )  # a bouncing ball go back to original position  at (START_X, START_Y)
        ball.move(0, 0)  # the ball won't move anymore
Ejemplo n.º 13
0
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)
Ejemplo n.º 14
0
def bounce(_):
    """
    This function will be activate after a clicked by users, and the
    simulation of a bouncing ball starts. Users can only try three
    simulations and any click will not affect the process.
    """
    global count
    # Only three tries are available
    if check(count):
        # Click only works when the ball is at the starting point.
        if ball.x == START_X and ball.y == START_Y:
            # Set the velocity at perpendicular direction as zero.
            vy = 0
            while True:
                ball.move(VX, vy)
                vy += GRAVITY
                # When the ball reach the floor, the vertical speed is reversed.
                if ball.y >= window.height:
                    vy *= -REDUCE
                pause(DELAY)
                # Count simulations
                if ball.x >= window.width:
                    window.remove(ball)
                    count += 1
                    break
            # Reset the ball
            window.add(ball, START_X, START_Y)
Ejemplo n.º 15
0
def bouncing():
    """
    The ball bouncing animation, counting number of lives as well
    """
    global NUM_LIVES
    # getting velocity of bounce
    dy = graphics.get_dy()
    dx = graphics.get_dx()
    # checking whether the game can resume or not
    if NUM_LIVES > 0 and graphics.num_bricks > 0:
        # when ball is above the ground, keep bouncing
        while graphics.ball.y + graphics.ball.height < graphics.window.height:
            graphics.ball.move(dx, dy)
            # when ball touches the frame, changes to the opposite direction
            if graphics.ball.x < 0 or graphics.ball.x + graphics.ball.width > graphics.window.width:
                dx = -dx
            elif graphics.ball.y < 0:
                dy = -dy
            # when the ball is in the frame, check if the ball touches objects
            else:
                result = graphics.check_collision()
                # if the ball touches the pad or brick, rebound
                if result == 'pad' or result == 'brick':
                    dy = -dy
                # if the ball does not collide with object, do nothing
                else:
                    pass
            pause(FRAME_RATE)
        # when ball falls to the ground, NUM_LIVE minus 1 and start the next round
        NUM_LIVES -= 1
        print('Remaining Lives:'+str(NUM_LIVES))
        graphics.window.remove(graphics.ball)
        pause(500)
        # place ball at the center, starting next round
        graphics.reset_ball()
Ejemplo n.º 16
0
def main():
    """
    The 'time' number is exploited to control while loop in this function.
    """
    time = 0
    set_background()
    draw_cloud(10)
    bird = Bird()
    sentence_count = 0
    sentence = ['Hellooooooooo, every one.', 'Wow, It\'s a nice day!', 'I wanna learn something NEW.',
                'Is this class SC101?', 'Hmm......, so', 'How can I master in Py?', 'Aha, Let me ask Jerry!',
                '😍😍😁😁🤪🤪']

    while True:
        time += 1
        if time < len(sentence)*20 + 1 and time % 20 == 0:

            bird.clear_speak()
            bird.words = sentence[sentence_count]
            print(time, sentence[sentence_count])
            bird.speak()
            sentence_count += 1

        elif time == len(sentence)*20 + 1:
            time = 0
            sentence_count = 0
        else:
            move_cloud()
            bird.chang_pose()
            pause(DELAY - 175)
            bird.remove_pose()
Ejemplo n.º 17
0
def sierpinski_triangle(order, length, upper_left_x, upper_left_y):
    """
	:param order: the order of Sierpinski Triangle
	:param length: length of order 1 Sierpinski Triangle
	:param upper_left_x: upper left x coordinate of order 1 Sierpinski Triangle
	:param upper_left_y: upper left y coordinate of order 1 Sierpinski Triangle
	:return: Sierpinski Triangle drawn by the recursive function
	"""
    pause(50)
    if order == 0:
        return
    else:
        # upper left
        sierpinski_triangle(order - 1, length / 2, upper_left_x, upper_left_y)

        # upper right
        sierpinski_triangle(order - 1, length / 2, upper_left_x + 0.5 * length,
                            upper_left_y)

        # lower
        sierpinski_triangle(order - 1, length / 2,
                            upper_left_x + 0.25 * length,
                            upper_left_y + 0.433 * length)

        S1 = GLine(upper_left_x, upper_left_y, upper_left_x + length,
                   upper_left_y)
        window.add(S1)
        S2 = GLine(upper_left_x, upper_left_y, upper_left_x + 0.5 * length,
                   upper_left_y + 0.866 * length)
        window.add(S2)
        S3 = GLine(upper_left_x + 0.5 * length, upper_left_y + 0.866 * length,
                   upper_left_x + length, upper_left_y)
        window.add(S3)
Ejemplo n.º 18
0
def main_game(e):
    # run this when user click
    global COUNT, life_ui, score_ui, vx, vy, score_count, life_left, ball
    # create life count ui
    if is_at_original_point():
        # check if the game started
        if life_left > 0 and score_count < WIN_SCORE:
            while ball.y < w.height:
                # game loop: keep the game going if ball doesn't fall out of the border
                if score_count < WIN_SCORE/2:
                    ball.move(vx, vy)
                    pause(FRAME_RATE)
                    check_if_ball_hit_sth()
                else:
                    boss_fight()
                    break
                w.remove(score_ui)
                score_ui = update_score()
            life_left -= 1
            w.remove(ball)
            if score_count >= WIN_SCORE:
                over = GLabel("YOU WON")
                over.font = "-60"
                w.add(over, w.width / 2 - over.width / 2, w.height / 2)
            elif life_left <= 0:
                game_over()
            else:
                ball = graphics.ball()
                w.remove(life_ui)
                life_ui = update_life()
    else:
        pass
def main():
    """
    This program creates the Breakout Game.
    A ball will be bouncing around the GWindow and
    bounce back if it hits the walls, paddles or the bricks.
    Bricks are removed if hit by the ball.
    Player can use the paddle to prevent ball from falling out of
    the bottom window by moving the mouse.
    The game ends if all the bricks are removed or
    the ball falls out of the bottom wall for over NUM_LIVES.
    """
    graphics = BreakoutGraphics()

    # Add animation loop here!
    lives = NUM_LIVES
    while True:
        pause(FRAME_RATE)
        while graphics.mouse_lock is True:
            if graphics.ball_out():
                lives -= 1
                if lives > 0:
                    graphics.reset_ball()
                    break
                elif lives <= 0:
                    break
            if graphics.brick_left == 0:
                break
            graphics.move_ball()
            graphics.handle_collision()
            pause(FRAME_RATE)
        if graphics.brick_left == 0:
            break
Ejemplo n.º 20
0
def bouncing(mouse):
    global x, a
    # mouse click will start bouncing.
    # if x == 3, mouse click can't start bouncing
    if 0 < mouse.x < 800 and x != 3 and a == 0:
        # vy stands for vertical velocity.
        vy = 0
        # when a == 1, the bouncing won't be impacted by mouse click
        a = 1
        # ball still within window
        while ball.x <= 800:
            vy += GRAVITY
            ball.move(VX, vy)
            # the ball reaches to the bottom of window
            if ball.y + SIZE >= 500:
                # the ball will be slowed and changed its bouncing direction after reaching to the bottom
                vy = -vy * REDUCE
                # bouncing speed reaches to 0
                while vy <= 0:
                    ball.move(VX, vy)
                    vy += GRAVITY
                    pause(DELAY)
            pause(DELAY)
        # after finishing bouncing, the ball goes back to the original position
        ball.x = START_X
        ball.y = START_Y
        x += 1
        a = 0
Ejemplo n.º 21
0
def main():
    lives = NUM_LIVES
    graphics = BreakoutGraphics()
    __dx = graphics.get_dx()
    __dy = graphics.get_dy()

    # Add animation loop here!
    while lives != 0:
        if graphics.click:
            graphics.oval.move(__dx, __dy)
        if graphics.oval.x <= 0 or graphics.oval.x + graphics.oval.width >= graphics.window.width:
            __dx = -__dx
        elif graphics.oval.y <= 0 or graphics.oval.y + graphics.oval.height >= graphics.window.height:
            __dy = -__dy

        if graphics.check_obj() is graphics.paddle:
            __dy = -graphics.get_dy()
        elif graphics.check_obj(
        ) is not graphics.paddle and graphics.check_obj() is not None:
            __dy = -__dy
            graphics.window.remove(graphics.check_obj())
            graphics.num_brick -= 1
        elif graphics.window.height <= graphics.oval.y + graphics.oval.height:
            graphics.reset_oval()
            graphics.click = False
            lives -= 1
        elif graphics.num_brick == 0:
            break

        if lives == 0:
            break
        pause(FRAME_RATE)
Ejemplo n.º 22
0
def start(event):
    """
    :param event: mouse event
    """
    global TURN
    global TIME
    if TURN and TIME != 3:
        TIME += 1
        score_label.text = 'chance:' + str(3 - TIME)
        TURN = False  # the circle is moving
        window.add(circle)
        speed_y = GRAVITY
        while circle.x + circle.width < window.width:
            while circle.y + circle.height <= window.height and circle.x + circle.width < window.width:
                circle.move(VX, speed_y)
                speed_y += GRAVITY
                pause(DELAY)
                print(speed_y)
            if circle.y + circle.height <= window.height:
                speed_y = -speed_y * REDUCE
            else:
                speed_y = -speed_y
            circle.y = window.height - circle.height
        TURN = True
        circle.x = START_X
        circle.y = START_Y
Ejemplo n.º 23
0
def main():
    graphics = BreakoutGraphics()
    # init remained life
    graphics.remained_life = NUM_LIVES
    while True:
        if graphics.ball_active:

            # move ball
            graphics.ball.move(graphics.get_ball_dx(), graphics.get_ball_dy())

            # check for collision and handler ball's direction
            collisions_handler(graphics)

            # ball should withinR window boundary, and would end game if it's out of window's low bound
            boundary_check(graphics)
            if not graphics.ball_active:
                if graphics.remained_life == 0:
                    break
                else:
                    graphics.reset_ball_position()
                    graphics.init_ball_velocity()
            # if no bricks remaining, gave over
            if graphics.remained_bricks == 0:
                break

        pause(FRAME_RATE)
Ejemplo n.º 24
0
    def start_game(self, event):
        click = self.__window.get_object_at(event.x, event.y)
        b = self.__grt_button
        grt = self.__grt_wd2

        # While the mouse click on the button, start the game.
        if click == b or click == grt and b.x <= event.x <= b.x + b.width and b.y <= event.y <= b.y + b.height:
            self.__window.remove(self.__grt_wd1)  # Remove the greeting words.
            self.__window.remove(self.__grt_wd2)  # Remove the greeting words.
            self.__window.remove(self.__grt_button)  # Remove the start button.
            self.__window.remove(
                self.__grt_bg)  # Remove the greeting background.
            self.switch = True  # Turn on the switch.

            # Set the ball on the start position, in the middle of the window.
            self.__window.add(
                self.__ball,
                x=self.__window.width / 2 - self.__ball.width / 2,
                y=self.__window.height / 2 - self.__ball.height / 2)
            self.__window.add(self.__paddle)  # Add the paddle to the window.
            self.__draw_bricks()  # Add the bricks to the window.
            self.__window.add(
                self.__scoreboard)  # Add the scoreboard to the window.

            pause(300)  # Delay time serving a ball.
Ejemplo n.º 25
0
def click_start(event):
    """
        Resets the ball if the ball fall out of the bottom
        Input:
            event (GMouseEvent) : mouse clicked event
        """
    global vy, vx, life_count, life_left
    if ball.x == (window.width - ball.width) / 2 and ball.y == (
            window.height - ball.height) / 2 and life_left > 0:
        while True:
            ball.move(vx, vy)
            pause(FRAME_RATE)
            if graphics.wall_rebound_y():
                vy *= -1
            if graphics.wall_rebound_x():
                vx *= -1
            if graphics.detect():
                vy *= -1
                graphics.remove_brick()
            if graphics.detect_paddle():
                vy = -INITIAL  # the solution from tutors that solve the problem that the ball sticks at the paddle
            if graphics.out_of_bottom():
                ball.x = (window.width - ball.width) / 2
                ball.y = (window.height - ball.height) / 2
                life_left -= 1
                life_count.text = 'Live: ' + str(life_left)
                break
            if graphics.total_brick == 0:
                graphics.win_the_game()
                break
    if life_left == 0:  # if player lost 3 lives
        graphics.game_over()
Ejemplo n.º 26
0
def ball_bouncing():
    bounce_x = VX
    bounce_y = GRAVITY
    fall = 0
    count = 0
    global mouse_lock
    mouse_lock = 1
    while True:
        if ball.x <= window.width and ball.y + bounce_y < window.height:
            ball.move(bounce_x, bounce_y)
            pause(DELAY)
            fall += bounce_y
            bounce_y += GRAVITY
        elif ball.y + bounce_y >= window.height:
            bounce_y = -bounce_y
            bounce_y *= REDUCE
            ball.move(bounce_x, bounce_y)
            pause(DELAY)
            bounce_y += GRAVITY
        elif ball.x >= window.width:
            window.remove(ball)
            window.add(ball, START_X, START_Y)
            count += 1
            if count == 3:
                count = 0
                mouse_lock = 0
                break
Ejemplo n.º 27
0
def bouncing(m):
    global START_X, START_Y, VX, GRAVITY, DELAY, click, Time
    if Time > 0:
        # remove the ball at START_X and START_Y
        window.remove(ball)
        while click is True:  # The ball will not be influenced by click as moving
            # creating the ball at the next moving place
            ball2 = GOval(SIZE, SIZE, x=START_X + VX, y=START_Y + GRAVITY)
            ball2.filled = True
            window.add(ball2)
            # the primary down speed
            gravity = 0
            while True:  # the process of moving
                click = False
                gravity = gravity + GRAVITY
                ball2.move(VX, gravity)
                if ball2.y >= window.height:
                    gravity = -gravity
                    gravity = gravity * REDUCE
                    ball2.move(VX, gravity)
                pause(DELAY)
                if ball2.x > window.width:
                    break
            # back to the primary place
            window.add(ball)
            # count the time
            Time -= 1
        click = True
Ejemplo n.º 28
0
def sierpinski_triangle(order, length, upper_left_x, upper_left_y):
    """
	:param order: For how many times we are gonna do the recursions.
	:param length: Equilateral triangle side length
	:param upper_left_x: The x coordinate of the top left vertex of the triangles
	:param upper_left_y: The y coordinate of the top left vertex of the triangles
	:return:
	"""
    if order == 0:
        return
    else:
        line1 = GLine(upper_left_x, upper_left_y, upper_left_x + length,
                      upper_left_y)
        line2 = GLine(upper_left_x, upper_left_y, upper_left_x + 0.5 * length,
                      upper_left_y + 0.866 * length)
        line3 = GLine(upper_left_x + length, upper_left_y,
                      upper_left_x + 0.5 * length,
                      upper_left_y + 0.866 * length)
        window.add(line1)
        window.add(line2)
        window.add(line3)
        pause(10)

        # Left Triangle
        sierpinski_triangle(order - 1, 0.5 * length, upper_left_x,
                            upper_left_y)

        # Right Triangle
        sierpinski_triangle(order - 1, 0.5 * length,
                            upper_left_x + 0.5 * length, upper_left_y)

        # Middle Triangle
        sierpinski_triangle(order - 1, 0.5 * length,
                            upper_left_x + 0.25 * length,
                            upper_left_y + 0.433 * length)
Ejemplo n.º 29
0
def main():
    graphics = BreakoutGraphics()
    lives = NUM_LIVES
    constant_dy = graphics.get_constant_dy()
    # This condition can control when the game will stop. (die or win)
    while lives > 0 and graphics.brick_amount > 0:
        dx = graphics.get_dx()
        dy = graphics.get_dy()
        graphics.ball.move(dx, dy)
        pause(FRAME_RATE)
        # If the ball touch the left or right wall, it will bounce.
        if graphics.ball.x < 0 or graphics.ball.x + graphics.ball.width > graphics.window_width:
            graphics.set_dx(-dx)
        # If the ball touch the top wall, it will bounce.
        elif graphics.ball.y < 0:
            graphics.set_dy(-dy)
        # If the ball touch the ground, lives-1 and the ball will be reset.
        elif graphics.ball.y + graphics.ball.height > graphics.window_height:
            lives -= 1
            graphics.reset_ball()
        # If the ball touch the brick, the brick will be removed and the ball will bounce.
        elif graphics.ball.y + graphics.ball.height < graphics.paddle.y:
            graphics.check_touch_obj()
        # If the ball touch the paddle, the ball will bounce.
        elif graphics.check_touch_paddle():
            graphics.set_dy(constant_dy)
Ejemplo n.º 30
0
def move(mouse):
    global v0, ball, count, switch
    # run only when switch is open.
    if switch == 0:
        # lives of ball
        if count > 0:
            # close switch
            switch = 1
            while True:
                v0 += GRAVITY
                ball.move(VX, v0)
                # check for bounce.
                if ball.y + ball.height >= window.height:
                    v0 = -v0*REDUCE
                    ball.move(VX, v0)
                # check for collision.
                if ball.x > window.width:
                    ball.x = START_X
                    ball.y = START_Y
                    break
                pause(DELAY)
            # time +1 when animation is over.
            count -= 1
            # open the switch when animation is over.
            switch = 0