예제 #1
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)
예제 #2
0
def main():
    graphics = BreakoutGraphics()
    # Add animation loop here!

    while True:
        # graphics.click_to_start(graphics.mouse_click)
        if graphics.mouse_click == 1:  # 如果滑鼠開關打開了
            while True:
                graphics.ball_move()  # 球就開始移動
                pause(FRAME_RATE)
                if graphics.ball.y + graphics.ball.height >= graphics.window.height:
                    graphics.times += 1  # 死了一次
                    break
                p1 = graphics.ball_touch()  # 在移動的過程中偵測是否撞到物體
                if graphics.ball.y <= graphics.window.height / 2:  # 當球在視窗上半部移動時
                    if p1 is not None:  # 撞到了物體
                        graphics.window.remove(p1)  # 移除掉物體
                        graphics.set_dy()  # 球反彈
                else:
                    if p1 is not None:  # 撞到了物體
                        graphics.set_dy()  # 球反彈
            graphics.window.remove(graphics.ball)  # 球重新從原始位置開始
            graphics.window.add(
                graphics.ball,
                x=(graphics.window.width - graphics.ball.width) / 2,
                y=(graphics.window.height - graphics.ball.height) / 2)
            graphics.mouse_click -= 1  # 開關回到初始值(0)
        pause(FRAME_RATE)
예제 #3
0
def main():
    graphics = BreakoutGraphics()

    dx = graphics.get_x_speed()
    dy = graphics.get_y_speed()

    lives = NUM_LIVES

    # Add animation loop here!
    while True:
        pause(FRAME_RATE)

        if graphics.start and lives > 0 and graphics.count > 0:
            graphics.ball.move(dx, dy)

            if graphics.ball.y >= graphics.window.height:
                lives -= 1
                graphics.reset_ball()
            else:
                # ball touches paddle or bricks
                if graphics.get_obj():
                    if graphics.ball.y < graphics.window.height / 2:  # bricks
                        dy = -dy
                    else:  # paddle
                        if dy > 0:  # ensure won’t stick on the paddle
                            dy = -dy
                # ball touches the window
                if graphics.ball.x <= 0 or graphics.ball.x + graphics.ball.width >= graphics.window.width:
                    dx = -dx
                if graphics.ball.y <= 0:
                    dy = -dy

            # update ball velocity
            if graphics.score > 0 and graphics.score % 200 == 0:
                dy += 0.1
                graphics.update_dy(dy)

        elif lives <= 0:
            # game over
            label = GLabel('Game Over', x=0, y=graphics.window.height / 2)
            label.font = '-48-bold'
            label.color = 'red'
            graphics.window.add(label)
            graphics.window.remove(graphics.ball)
            break

        elif graphics.count <= 0:
            # you win
            label = GLabel('You Win!!!', x=0, y=graphics.window.height / 2)
            label.font = '-48-bold'
            label.color = 'forestgreen'
            graphics.window.add(label)
            graphics.window.remove(graphics.ball)
            break

    # label animation
    label_dx = 1
    while label.x <= graphics.window.width / 2 - 120:
        label.move(label_dx, 0)
        pause(10)
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
예제 #5
0
def main():
    graphics = BreakoutGraphics()

    init_dx = abs(graphics.get_ball_dx())
    init_dy = abs(graphics.get_ball_dy())
    acc = [[init_dx + 1, init_dx + 2, init_dx + 3, init_dx + 4],
           [init_dy + 1, init_dy + 2, init_dy + 3, init_dy + 4]]

    # init remained life
    while True:
        if graphics.ball_active:

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

            # move cannon/flame
            if graphics.cannon_appear:
                graphics.cannon_icon.move(graphics.cannon_icon_dx,
                                          graphics.cannon_icon_dy)
                graphics.cannon_icon_background.move(graphics.cannon_icon_dx,
                                                     graphics.cannon_icon_dy)
            # move ball_size_inc icon
            if graphics.ball_size_inc_appear:
                graphics.ball_size_inc_icon.move(graphics.cannon_icon_dx,
                                                 graphics.cannon_icon_dy)
                graphics.ball_size_inc_icon_background.move(
                    graphics.cannon_icon_dx, graphics.cannon_icon_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

            # ball accelerated when game goes on for a while
            if 45 < graphics.remained_bricks <= 47:
                ball_acc_handler(graphics, acc, 0)
            if 40 < graphics.remained_bricks <= 42:
                ball_acc_handler(graphics, acc, 1)
            if 35 < graphics.remained_bricks <= 37:
                ball_acc_handler(graphics, acc, 2)
            if 30 < graphics.remained_bricks <= 32:
                ball_acc_handler(graphics, acc, 3)

        pause(FRAME_RATE)
    game_over_handler(graphics)
예제 #6
0
def main():
    graphics = BreakoutGraphics()
    FRAME_RATE = 1000 / 120  # 120 frames per second.
    while graphics.num_lives != 0 and graphics.brick_count != 0:
        if graphics.running:
            graphics.move_ball()
            graphics.handle_collisions()
        pause(FRAME_RATE)
    graphics.game_over()
예제 #7
0
def main():
    graphics = BreakoutGraphics()

    # Add animation loop here!
    while True:
        graphics.bounce()
        x_speed = graphics.get_dx()
        y_speed = graphics.get_dy()
        graphics.ball.move(x_speed, y_speed)
        pause(FRAME_RATE)
        graphics.restart()
예제 #8
0
def main():
    graphics = BreakoutGraphics()
    # Add animation loop here!
    while True:
        if graphics.start_or_not is True:
            graphics.check_for_collision()
            graphics.reset_ball()
            graphics.ball.move(graphics.get_dx(), graphics.get_dy())
        if graphics.lives_left == 0:
            break
        if graphics.numbers_of_brick == 0:
            break
        pause(FRAME_RATE)
예제 #9
0
def main():
    graphics = BreakoutGraphics()
    dx = graphics.get_x_velocity()
    dy = graphics.get_y_velocity()
    life_times = NUM_LIVES
    while True:  # wait for user to click to start the game
        if graphics.has_click is True:
            while True:
                collisions = graphics.check_for_collision()
                if collisions == 1:
                    dy = -dy
                if collisions == 2:
                    if dy > 0:
                        dy = -dy
                    else:
                        dy = dy
                if graphics.collision_times >= graphics.win_points:  # when all bricks removed, stop the game
                    break
                if graphics.ball.x <= 0 or graphics.ball.x + graphics.ball.width >= graphics.window.width:
                    dx = -dx  # for the boundary conditions, when ball collide the wall,
                if graphics.ball.y <= 0:  # ball need to bounce back
                    dy = -dy
                if graphics.ball.y + graphics.ball.height >= graphics.window.height:
                    life_times -= 1  # when ball touch the bottom of window, lives minus one,
                    graphics.reset_ball()  # and the game need to be restart
                    break
                graphics.ball.move(dx, dy)
                pause(FRAME_RATE)
            graphics.has_click = False
        elif graphics.collision_times >= graphics.win_points:  # when player remove all bricks
            graphics.window.remove(graphics.ball)
            graphics.window.remove(graphics.paddle)
            graphics.window.add(
                graphics.label_2,
                x=(graphics.window.width - graphics.label_2.width) / 2,
                y=(graphics.window.height - graphics.label_2.height))
            print('You win')
            break
        elif life_times <= 0:
            graphics.window.remove(graphics.ball)
            graphics.window.remove(graphics.paddle)
            graphics.window.add(
                graphics.label_1,
                x=(graphics.window.width - graphics.label_1.width) / 2,
                y=(graphics.window.height - graphics.label_1.height))
            print('You lose')
            break
        else:
            pass
        pause(FRAME_RATE)
예제 #10
0
def main():
    graphics = BreakoutGraphics()

    dx = graphics.get_x_speed()
    dy = graphics.get_y_speed()

    lives = NUM_LIVES

    # Add animation loop here!
    while True:
        pause(FRAME_RATE)
        if graphics.start and lives > 0 and graphics.count > 0:
            graphics.ball.move(dx, dy)

            if graphics.ball.y + graphics.ball.height >= graphics.window.height:
                lives -= 1
                graphics.reset_ball()
            else:
                if graphics.get_obj():
                    dy = -dy
                if graphics.ball.x <= 0 or graphics.ball.x + graphics.ball.width >= graphics.window.width:
                    dx = -dx
                if graphics.ball.y <= 0:
                    dy = -dy

        elif lives <= 0:
            # game over
            label = GLabel('Game Over', x=0, y=graphics.window.height / 2)
            label.font = '-48-bold'
            label.color = 'red'
            graphics.window.add(label)
            graphics.window.remove(graphics.ball)
            break
        elif graphics.count <= 0:
            # you win
            label = GLabel('You Win!!!', x=0, y=graphics.window.height / 2)
            label.font = '-48-bold'
            label.color = 'forestgreen'
            graphics.window.add(label)
            graphics.window.remove(graphics.ball)
            break

    # move the label
    dx = 1
    while label.x <= graphics.window.width / 2 - 120:
        label.move(dx, 0)
        pause(10)
예제 #11
0
def main():
    """
    this project plays breakout
    """
    global NUM_LIVES
    graphics = BreakoutGraphics()
    score = 0  # the score you have
    score2 = 0
    delay = 0  # the speed you have
    win = 1000
    # Add animation loop here!
    while NUM_LIVES > 0:  # if your lives > 0 you die
        if graphics.get_game_state(
        ):  # if true ( you are playing the game now )
            dx = graphics.get_dx()  # get dx
            dy = graphics.get_dy()  # get dy
            NUM_LIVES, score, delay, score2, win = graphics.bounce_ball(
                dx, dy, NUM_LIVES, score, delay, score2)  # bouncing the ball
        pause(FRAME_RATE + delay + 20)  # the speed of ball bouncing
        if score2 == win:  # if you break all of the bricks
            break
    graphics.remove_all(score)  # show you win or lose
예제 #12
0
def main():
    global dx, dy
    graphics = BreakoutGraphics()
    lives = NUM_LIVES
    dx, dy = graphics.getter()  # getter設計用來 return coder端 隱藏的dx,dy
    while True:
        pause(FRAME_RATE)
        if graphics.switch:

            if graphics.ball_under_paddle():
                lives -= 1
                if lives >= 0:
                    graphics.reset_ball()
                else:
                    break
            graphics.ball.move(dx, dy)
            if graphics.ball.x <= 0 or graphics.ball.x + graphics.ball.width >= graphics.window.width:
                dx = -dx
            if graphics.ball.y <= 0:  #故意撞到底面不反彈
                dy = -dy
            check(graphics)
def main():
    graphics = BreakoutGraphics()
    lives = NUM_LIVES
    # Add animation loop here!
    dy = graphics.get_dy()
    dx = graphics.get_dx()
    box = graphics.row * graphics.col  # Count how many bricks
    while True:
        pause(FRAME_RATE)
        if graphics.move and lives > 0:  # If move is true and there are remaining lives
            graphics.ball.move(dx, dy)
            if graphics.ball.x <= 0 or graphics.ball.x >= graphics.window.width - graphics.ball.width:
                dx *= -1
            if graphics.ball.y <= 0:
                dy *= -1
            if graphics.ball.y >= graphics.window.height - graphics.ball.height:  # If user doesn't pick up the ball
                lives -= 1
                graphics.window.remove(
                    graphics.ball)  # Remove the falling ball
                graphics.move = False  # Turn off the move
            obj = graphics.check_collisions(
            )  # Whether the ball collides the objects
            if obj == graphics.paddle:  # If the ball collides the paddle, the ball bounces back
                dy = -dy
            # If the ball collides bricks, the ball bounces back and the bricks are removed
            elif obj != graphics.paddle and obj is not None:
                dy = -dy
                graphics.window.remove(obj)
                box -= 1  # If the bricked are removed, the game ends
        elif lives < 0:  # User loses
            break
        elif box == 0:  # User wins
            break
        else:  # If user haven't started the game
            graphics.window.add(
                graphics.ball,
                (graphics.window.width - graphics.ball.width) / 2,
                (graphics.window.height - graphics.ball.height) / 2)
        pause(FRAME_RATE)
예제 #14
0
def main():
    global NUM_LIVES

    graphics = BreakoutGraphics()
    # bricks_num is the number of existing bricks  in the game
    bricks_num = 100

    # Add animation loop here!
    while True:
        dx = graphics.dx_get()
        dy = graphics.dy_get()
        graphics.ball.x = (graphics.window.width - graphics.ball.width) / 2
        graphics.ball.y = (graphics.window.height - graphics.ball.height) / 2

        while True:
            if dx == 0 and dy == 0:
                dx = graphics.dx_get()
                dy = graphics.dy_get()
                pause(FRAME_RATE)
            else:
                graphics.ball.move(dx, dy)

                # when ball is touching the paddle, it will just work the function of "check_object".
                # And when the ball is touching the other thing, it means the ball touched the bricks.
                # It will remove the brick and bricks_num subtract one.

                if graphics.window.get_object_at(graphics.ball.x,
                                                 graphics.ball.y) is not None:
                    if graphics.paddle.x <= graphics.ball.x <= graphics.paddle.x + graphics.paddle.width and graphics.paddle.y <= graphics.ball.y <= graphics.paddle.y + graphics.paddle.height:
                        dy = abs(dy)
                    else:
                        graphics.window.remove(
                            graphics.window.get_object_at(
                                graphics.ball.x, graphics.ball.y))
                        bricks_num -= 1
                    dy = -dy
                elif graphics.window.get_object_at(
                        graphics.ball.x,
                        graphics.ball.y + graphics.ball.height) is not None:
                    if graphics.paddle.x <= graphics.ball.x <= graphics.paddle.x + graphics.paddle.width and graphics.paddle.y <= graphics.ball.y + graphics.ball.height <= graphics.paddle.y + graphics.paddle.height:
                        dy = abs(dy)
                    else:
                        graphics.window.remove(
                            graphics.window.get_object_at(
                                graphics.ball.x,
                                graphics.ball.y + graphics.ball.height))
                        bricks_num -= 1
                    dy = -dy
                elif graphics.window.get_object_at(
                        graphics.ball.x + graphics.ball.width,
                        graphics.ball.y) is not None:
                    if graphics.paddle.x <= graphics.ball.x + graphics.ball.width <= graphics.paddle.x + graphics.paddle.width and graphics.paddle.y <= graphics.ball.y <= graphics.paddle.y + graphics.paddle.height:
                        dy = abs(dy)
                    else:
                        graphics.window.remove(
                            graphics.window.get_object_at(
                                graphics.ball.x + graphics.ball.width,
                                graphics.ball.y))
                        bricks_num -= 1
                    dy = -dy
                elif graphics.window.get_object_at(
                        graphics.ball.x + graphics.ball.width,
                        graphics.ball.y + graphics.ball.height) is not None:
                    if graphics.paddle.x <= graphics.ball.x + graphics.ball.width <= graphics.paddle.x + graphics.paddle.width and graphics.paddle.y <= graphics.ball.y + graphics.ball.height <= graphics.paddle.y + graphics.paddle.height:
                        dy = abs(dy)
                    else:
                        graphics.window.remove(
                            graphics.window.get_object_at(
                                graphics.ball.x + graphics.ball.width,
                                graphics.ball.y + graphics.ball.height))
                        bricks_num -= 1
                    dy = -dy

                # when the ball touches the bounder of the window,
                # it will change the velocity except the bottom of the window.
                if graphics.ball.x <= 0 or graphics.ball.x >= graphics.window.width - graphics.ball.width:
                    dx = -dx
                if graphics.ball.y <= 0:
                    dy = -dy

                # 1. bricks_num =0 means winning, so we have bto break.
                # 2. When ball is touching the bottom of the window, NUM_LIVES will minus one and break.
                if bricks_num == 0:
                    break
                elif graphics.ball.y + graphics.ball.height >= graphics.window.height:
                    NUM_LIVES -= 1
                    graphics.trigger = 0

                    break

                pause(FRAME_RATE)

        # check the winning condition and losing condition
        if NUM_LIVES == 0:
            graphics.window.remove(graphics.ball)
            graphics.window.add(graphics.game_over)
            break
        elif bricks_num == 0:
            graphics.window.remove(graphics.ball)
            graphics.window.add(graphics.win)
            break
예제 #15
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()
예제 #16
0
stanCode Breakout Project
Adapted from Eric Roberts's Breakout by
Sonja Johnson-Yu, Kylie Jue, Nick Bowman,
and Jerry Liao

YOUR DESCRIPTION HERE
"""
from campy.gui.events.mouse import onmouseclicked, onmousemoved
from campy.gui.events.timer import pause
from campy.graphics.gobjects import GOval, GRect, GLabel
from breakoutgraphics import BreakoutGraphics
import random

FRAME_RATE = 1000 / 40  # 120 frames per second.
NUM_LIVES = 3
graphics = BreakoutGraphics(brick_cols=10, brick_rows=10)
SCORE = 0
p = graphics.paddle()
w = graphics.window
ball = graphics.ball()
vx = graphics.get_dx()
vy = graphics.get_dy()
brick_o = graphics.brick()
COUNT = 0
life_left = NUM_LIVES
score_count = SCORE
life_ui = GLabel(f"Life: {life_left}")
score_ui = GLabel(f"Score: {score_count}")
WIN_SCORE = graphics.brick_count() + 7
laser1 = graphics.laser1(ball)
laser2 = graphics.laser2(ball)
예제 #17
0
def main():
    """
    You have the three lives in this game.
    You only can enter the game when you click the mouse at the first time.
    When the paddle doesn't catch the ball, you will lose one life, reset the place of the ball and the mouse data.
    When the ball collides the the boundaries, it will change the speed.
    When the bricks are removed over, the game is in the end and you win!
    When the number of lives reaches zero, the game is over and you lose!
    """
    graphics = BreakoutGraphics()
    life = NUM_LIVES
    while True:
        if life > 0 or graphics.how_many_bricks > 0:
            if graphics.count == 1:
                graphics.set_ball_velocity()
                while True:
                    graphics.ball.move(graphics.get_x_velocity(),
                                       graphics.get_y_velocity())
                    graphics.collision_and_bounce()
                    if graphics.ball.x <= 0 or graphics.ball.x + graphics.ball.width >= graphics.window.width:
                        graphics.set_x_velocity()
                    if graphics.ball.y <= 0 or graphics.ball.y + graphics.ball.height >= graphics.window.height:
                        graphics.set_y_velocity()
                    if graphics.ball.y >= graphics.window.height - graphics.ball.height:
                        graphics.remove_ball()
                        life -= 1
                        graphics.window.add(graphics.ball,
                                            x=graphics.get_ball_start_x(),
                                            y=graphics.get_ball_start_y())
                        graphics.count = 0
                        break
                    if graphics.how_many_bricks == 0:
                        break
                    pause(FRAME_RATE)
                if graphics.how_many_bricks == 0:
                    break
                if life == 0:
                    break
        else:
            break
        pause(FRAME_RATE)
    graphics.window.add(graphics.ball,
                        x=graphics.get_ball_start_x(),
                        y=graphics.get_ball_start_y())
예제 #18
0
"""
stanCode Breakout Project
Adapted from Eric Roberts's Breakout by
Sonja Johnson-Yu, Kylie Jue, Nick Bowman,
and Jerry Liao

"""

from campy.gui.events.timer import pause
from breakoutgraphics import BreakoutGraphics
from campy.gui.events.mouse import onmouseclicked, onmousemoved

FRAME_RATE = 1000 / 60 # 120 frames per second.
NUM_LIVES = 3

graphics = BreakoutGraphics()


def main():
    """
    Breakout game, users can move paddle to make the ball rebound and break bricks, when all bricks are cleared,
    users win the game
    """
    # use chk to trigger the game, if chk = 1, game starts
    while True:
        chk = graphics.get_chk()
        if chk == 1:
            bouncing()
        pause(FRAME_RATE)

예제 #19
0
def main():
    graphics = BreakoutGraphics()
    # Add animation loop here!


    window = graphics.window

    dx = graphics.get_dx()
    dy = graphics.get_dy()
    ball = graphics.ball
    paddle = graphics.paddle
    paddle_height = graphics.get_brick_height()
    ball_radius = graphics.get_ball_radius()
    brick_cols = graphics.get_brick_cols()
    brick_rows = graphics.get_brick_rows()
    brick_spacing = graphics.get_brick_spacing()
    brick_offset = graphics.get_brick_offset()
    brick_width = graphics.get_brick_width()
    brick_height = graphics.get_brick_height()
    remain_live = NUM_LIVES
    live_label = GLabel('Life:' + str(remain_live))
    window.add(live_label, x=0, y=brick_offset/2)

    brick_left = brick_cols * brick_rows


    while True:
        # game main animation
        ball_upper_left = window.get_object_at(ball.x, ball.y)
        ball_upper_right = window.get_object_at(ball.x + 2 * ball_radius, ball.y)
        ball_lower_left = window.get_object_at(ball.x, ball.y + 2 * ball_radius)
        ball_lower_right = window.get_object_at(ball.x + 2 * ball_radius, ball.y + 2 * ball_radius)
        if brick_left == 0:
            window.remove(ball)
            you_win = GLabel('YOU WIN')
            you_win.color = 'Navy'
            window.add(you_win, x=(window.width-you_win.width)/2, y=(window.height-you_win.height)/2)
            break
        elif remain_live == 0:
            window.remove(ball)
            you_win = GLabel('YOU LOSE')
            you_win.color = 'Red'
            window.add(you_win, x=(window.width-you_win.width)/2, y=(window.height-you_win.height)/2)
            break
        elif graphics.start is True and remain_live > 0:
            ball.move(dx, dy)
            if ball.y + 2 * ball_radius >= window.height:
                remain_live -= 1
                live_label.text = 'Life:' + str(remain_live)
                window.add(ball, x=window.width / 2 - ball_radius, y=window.height / 2 - ball_radius)
                graphics.start = False
            elif ball.x <= 0 or ball.x + 2 * ball_radius >= window.width:
                dx = -dx
            elif ball.y <= 0:
                dy = -dy
            elif ball_lower_right is paddle and dy >= 0:
                dy = -dy
            elif ball_lower_right is paddle and dy >= 0:
                dy = -dy
            elif ball_upper_left is not None and ball_upper_left is not paddle and ball_upper_left is not live_label:
                window.remove(ball_upper_left)
                brick_left -= 1
                dy = -dy
            elif ball_upper_right is not None and ball_upper_right is not paddle and ball_upper_right is not live_label:
                window.remove(ball_upper_right)
                brick_left -= 1
                dy = -dy
            elif ball_lower_right is not None and ball_lower_right is not paddle and ball_lower_right is not live_label:
                window.remove(ball_lower_right)
                brick_left -= 1
                dx = -dx
                dy = -dy
            elif ball_lower_left is not None and ball_lower_left is not paddle and ball_lower_left is not live_label:
                window.remove(ball_lower_left)
                brick_left -= 1
                dx = -dx
                dy = -dy
        pause(FRAME_RATE)
예제 #20
0
def main():
    graphics = BreakoutGraphics()
    lives = NUM_LIVES
    bricks = 100

    while True:
        if graphics.out_of_zone():
            graphics.remove_ball()
            lives -= 1
            if lives > 0:
                graphics.reset_ball()
            elif lives == 0:
                graphics.win_or_lose(False)
                break
        if graphics.brick_nums == 0:
            graphics.win_or_lose(True)
            break
        graphics.move_ball()
        graphics.handle_wall_collisions()
        graphics.handle_obj_collisions()
        pause(FRAME_RATE)
예제 #21
0
def main():
    """
    This program show the detail condition for break out game and could be divided to five steps:

    First, check the lives that player have.
    Second, check the total bricks left on the window.
    Third, set the collision condition with wall.
    Forth, set the collision condition with bricks and paddle.
    Finally, set the result that ball move below the paddle.
    """
    graphics = BreakoutGraphics()

    # Add animation loop here!
    lives = NUM_LIVES
    while True:
        if lives > 0:
            graphics.ball.move(graphics.get_dx(), graphics.get_dy())
            if graphics.total_bricks > 0:

                # Collision with wall
                if graphics.ball.x <= 0 or graphics.ball.x + graphics.ball.width >= graphics.window.width:
                    graphics.set_dx(-graphics.get_dx())
                if graphics.ball.y <= 0 or graphics.ball.y + graphics.ball.height >= graphics.window.height:
                    graphics.set_dy(-graphics.get_dy())

                # Collision with bricks or paddle
                if graphics.check_for_collisions() is True:
                    # Collision with paddle
                    if graphics.check_object_type() is True:
                        graphics.set_dy(-graphics.get_dy())
                        # Promise the ball will not stuck in the paddle.
                        # I discuss this idea with classmate 李佳謙
                        if graphics.get_dy() > 0:
                            graphics.set_dy(-graphics.get_dy())

                    # Collision with bricks
                    elif graphics.check_object_type() is False:
                        graphics.window.remove(graphics.obj)
                        graphics.total_bricks -= 1
                        graphics.set_dx_collision(graphics.get_dx())
                        graphics.set_dy(-graphics.get_dy())

                # The condition that lives will decrease.
                if graphics.ball.y + graphics.ball.height >= graphics.window.height:
                    lives -= 1
                    graphics.window.remove(graphics.ball)
                    graphics.reset_ball()
                pause(FRAME_RATE)
            # Clear all bricks
            else:
                graphics.game_win()
                break
        # The lives is zero and player is lose.
        else:
            graphics.game_over()
            break
예제 #22
0
TODO: this is a break out game, you have 3 healths, you need to control
      your mouse to let the paddle move to win the game. if you break 100
      bricks (score = 100), you will win the game.
"""

from campy.gui.events.timer import pause
from breakoutgraphics import BreakoutGraphics
from campy.graphics.gobjects import GLabel
from campy.gui.events.mouse import onmouseclicked

FRAME_RATE = 1000 / 120 # 120 frames per second.
NUM_LIVES = 3
score = 0
score_label = GLabel('Score:'+ str(score), y=30)
graphics = BreakoutGraphics()
is_playing = False
hp = NUM_LIVES
dx = graphics.get_vx()
dy = graphics.get_vy()
time = 0   # how long did you play


def main():
    print(f'Health:{hp}')
    onmouseclicked(play)
    score_label.font = '-25'
    graphics.window.add(score_label)


예제 #23
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)
예제 #24
0
def main():
    lives = NUM_LIVES
    graphics = BreakoutGraphics()

    # Animation
    while True:
        pause(FRAME_RATE)
        graphics.move_ball()
        graphics.wall_collisions()
        graphics.ball_collisions()
        if graphics.outside_window():
            graphics.reset_ball()
            lives -= 1
        # conditions for the end of the game.
        if lives == 0:
            graphics.game_over()
            break
        elif graphics.end_game():
            break
예제 #25
0
File: breakout.py
Name: Elsa
---------------------------
This program plays a game called "Break out"
in which players rebounding the ball to break bricks
to gain scores.
"""

from campy.gui.events.timer import pause
from breakoutgraphics import BreakoutGraphics
from campy.gui.events.mouse import onmouseclicked

FRAME_RATE = 1000 / 120  # 120 frames per second.
NUM_LIVES = 3

graphics = BreakoutGraphics()
vx = graphics.get_vx()
vy = graphics.get_vy()
INITIAL = graphics.get_vy(
)  # the solution from tutors that solve the problem that the ball sticks at the paddle
ball = graphics.ball
window = graphics.window
life_left = NUM_LIVES
life_count = graphics.life_count

# Add animation loop here!


def main():
    """
    This program is the game that the player controls a paddle to rebound the ball.
예제 #26
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)
예제 #27
0
File: breakous.py
Name: Isabelle
"""

from campy.gui.events.timer import pause
from breakoutgraphics import BreakoutGraphics
from campy.gui.events.mouse import onmouseclicked
from campy.graphics.gobjects import GLabel
import random

# Constant
FRAME_RATE = 1000 / 120  # 120 frames per second.
NUM_LIVES = 3  # how many lives player has

# Global
graphics = BreakoutGraphics()
dx = graphics.get_dx()  # horizontal velocity
dy = graphics.get_dy()  # vertical velocity
lives = NUM_LIVES  # the number of lives
score = 0  # score the player get
y_speed = -7.0  # vertical speed which will be changed with score increasing
label_life = GLabel('LIFE:' + str(lives))  # lives on label
label_score = GLabel('SCORE: ' + str(score))  # score on label
img_speed = 1.0  # images' vertical velocity


def main():
    onmouseclicked(start_the_game)
    label_1 = add_life()
    graphics.window.add(label_1, x=graphics.window.width - label_life.width, \
                        y=graphics.window.height - label_life.height)
예제 #28
0
def main():
    graphics = BreakoutGraphics()
예제 #29
0
def main():
    graphics = BreakoutGraphics()
    # Add animation loop here!
    lives = NUM_LIVES
    end_game = False
    dx = graphics.get_dx()
    dy = graphics.get_dy()
    while True:
        pause(FRAME_RATE)
        if graphics.activate is True:  # if player mouseclick, start game.
            graphics.first_ball.move(dx, dy)
            if graphics.first_ball.x <= 0 or graphics.first_ball.x + graphics.first_ball.width > graphics.window.width:
                dx = -dx
            if graphics.first_ball.y <= 0:
                dy *= -1
                graphics.verse_y(
                )  # because the wall reflect event didn't return to class. I have to manually edit it.
            if graphics.conflict() is True:  # if there is a conflict
                graphics.crash(
                )  # to judge is paddle or brick. if it's a brick, crush it!
                graphics.reflect()  # to reflect
                dy = graphics.get_dy()  # new dy
            if graphics.win_game_judge():  # to judge if player wins.
                print('WIN GAME!')
                graphics.activate = False
                graphics.reset_ball()
                graphics.win_game_label()
                break
            if graphics.ball_go_down(
            ):  # if the ball fall, lives is decreased.
                lives = lives - 1
                if lives > 0:
                    graphics.reset_ball()
                    dx = graphics.get_dx()
                    graphics.activate = False  # stop the game, player need to click mouse to keep playing.
                else:  # player just lose the game
                    graphics.end_game_symbol()
                    graphics.activate = False
                    graphics.reset_ball()
                    break
예제 #30
0
def main():
    """
    A breakout game, user can click mouse to start,and move the mouse to control the paddle,
    using the paddle to bounce the ball for hitting bricks.
    """
    graphics = BreakoutGraphics()
    life = NUM_LIVES

    # Add animation loop here!
    while life > 0:
        dx = graphics.get_dx()
        dy = graphics.get_dy()
        pause(FRAME_RATE)
        graphics.ball.move(dx, dy)
        graphics.window_check()
        graphics.check_remove()
        life -= graphics.dead()
        if graphics.ball.y >= graphics.window.height:
            graphics.restart()