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)
def main(): graphics = BreakoutGraphics() # Add animation loop here! lives = NUM_LIVES while True: dx = graphics.get_dx() dy = graphics.get_dy() pause(FRAME_RATE) # Restart the game when the ball is out of window if graphics.all_clear(): graphics.remove_ball() break if graphics.ball_out_of_window(): lives -= 1 if lives > 0: graphics.reset_ball() else: break graphics.ball.move(dx, dy) # Change direction when ball hit the wall if graphics.ball.x <= 0 or graphics.ball.x + graphics.ball.width >= graphics.window.width: graphics.set_dx() if graphics.ball.y <= 0: graphics.set_dy() graphics.check_ball() graphics.game_over()
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 enter_game_page(m): global graphics # set graphics object to global so animation loop can call it. obj = log_in_page.window.get_object_at(m.x, m.y) if obj is log_in_page.play_button or obj is log_in_page.play_label or obj is log_in_page.fb_button \ or obj is log_in_page.fb_label or obj is log_in_page: graphics = BreakoutGraphics() # new window will show at this moment. log_in_page.window.close() # close the login window. graphics.draw_bricks() graphics.add_score_board() graphics.add_accessories( 10 ) # give argument to determine the total number of blue/red blocks will show. graphics.dx_getter() graphics.dy_getter() """ The following part has some bugs I still figuring out, that is, when I use GPolygon to draw a heart shape and add it to window, it seems to conflict to method: window.get_object_at(x,y). Though it's not perfect, I use GOval to represent NUM_LIVES on the right-top window. """ for i in range(NUM_LIVES): heart = graphics.draw_heart_icon() graphics.window.add( heart, graphics.window.width - graphics.heart_shape.width * (i + 1) - 5 * (i + 1), graphics.score_board_label.y - graphics.heart_shape.height)
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)
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
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(): 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)
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()
def main(): graphics = BreakoutGraphics() lives = NUM_LIVES # Add animation loop here while True: if lives > 0 and graphics.bricks_count <= graphics.bricks_amount: if graphics.is_started: graphics.ball.move(graphics.get_ball_dx(), graphics.get_ball_dy()) graphics.obstacles() # check for wall x collision if graphics.ball.x <= 0: new_dx = -1 * graphics.get_ball_dx() graphics.set_ball_dx(new_dx) elif graphics.ball.x >= graphics.window.width - graphics.ball.width: new_dx = -1 * graphics.get_ball_dx() graphics.set_ball_dx(new_dx) # check for wall y collision if graphics.ball.y <= 0: new_dy = -1 * graphics.get_ball_dy() graphics.set_ball_dy(new_dy) elif graphics.ball.y >= graphics.window.height: graphics.is_started = False lives -= 1 graphics.reset_ball() else: graphics.reset_ball() break pause(FRAME_RATE)
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)
def main(): life_count = 0 graphics = BreakoutGraphics() print("TotalLife:" + str(graphics.sum_bricks)) # Add animation loop here! face_vx = face_vy = 1 rate_dx = graphics.getter_dx() rate_dy = graphics.getter_dy() while life_count != NUM_LIVES: # Update graphics.ball.move(rate_dx, rate_dy) graphics.ball_tracker_paddle() # When the ball out of the window, update the number of the life. if graphics.dead == 1: life_count += 1 graphics.dead = 0 # Check graphics.ball_tracker_bricks() # the ball touch the bricks if graphics.remove_succ == 1: face_vy = face_vy * -1 if graphics.ball.x <= 0 or graphics.ball.x >= graphics.window.width - graphics.ball.width: face_vx = face_vx * -1 graphics.remove_succ = 0 # The ball is not touch the bricks else: # Checking if the ball touch the right side or the left side of the window if graphics.ball.x <= 0 or graphics.ball.x >= graphics.window.width - graphics.ball.width: face_vx = face_vx * -1 # Checking if the ball touch the paddle if graphics.paddle_touch == 1: graphics.paddle_touch = 0 face_vy = face_vy * -1 # Checking if the ball touch the top of the window elif graphics.ball.y <= 0: face_vy = face_vy * -1 # Updating the rate of dx and the rate of dy speeds rate_dx = graphics.getter_dx() * face_vx rate_dy = graphics.getter_dy() * face_vy # Checking if the game is over if graphics.bricks_remove == graphics.sum_bricks: print("YOU WIN in breakout.py") break # Pause pause(FRAME_RATE) print("GAME OVER_checking point")
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()
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
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()
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)
def main(): graphics = BreakoutGraphics() # Initialize variables. lives = NUM_LIVES bricks_removed = 0 ignore_paddle = False # Animation loop. while True: if graphics.ball_in_motion is True: # Check window edge collision. if graphics.ball.y + graphics.ball.height >= graphics.window.height: # Lose life when ball exits bottom edge and checks for lose condition. lives -= 1 if lives == 0: break graphics.place_ball() # Reset collision detection with paddle. ignore_paddle = False # Left and right edge detection and fill dx dy values. if graphics.ball.x <= 0 or graphics.ball.x + graphics.ball.width >= graphics.window.width: dx = graphics.set_dx_bounce() else: dx = graphics.get_dx() if graphics.ball.y <= 0: dy = graphics.set_dy_bounce() ignore_paddle = False else: dy = graphics.get_dy() # Check for object collision collision_object = graphics.check_collision() if collision_object is not None: # Bounce off paddle and ignore further detections until variable reset by top edge or brick. if collision_object is graphics.paddle and ignore_paddle is False: dy = graphics.set_dy_bounce() ignore_paddle = True # If brick, bounce, remove brick, reset paddle check, and check for win condition. elif collision_object is not graphics.paddle: dy = graphics.set_dy_bounce() graphics.window.remove(collision_object) ignore_paddle = False bricks_removed += 1 if bricks_removed == graphics.get_brick_count(): break graphics.ball.move(dx, dy) pause(FRAME_RATE) graphics.window.clear() if bricks_removed == graphics.get_brick_count(): graphics.window.add(graphics.label_win) elif lives == 0: graphics.window.add(graphics.label_lose)
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)
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)
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)
def main(): graphics = BreakoutGraphics() lives = NUM_LIVES graphics.set_user_live(lives) graphics.live_update() # Add animation loop here! while True: pause(FRAME_RATE) if graphics.game_switch is True: graphics.ball.move(graphics.get_ball_x(), graphics.get_ball_y()) graphics.obj_detect() if graphics.obj_action() is True: graphics.score_count() if graphics.ball_action() is True: graphics.live_count() if graphics.game_over() is True: break
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())
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
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()
def main(): """ This function plays a breakout game. """ graphics = BreakoutGraphics() lives = NUM_LIVES # player's lives. count_brick = 0 # count how many removed brick. # Animation loop here! while True: pause(FRAME_RATE) dx = graphics.get_dx() dy = graphics.get_dy() graphics.ball.move(dx, dy) obj = graphics.check_for_collisions() # This condition will change the ball direction if the ball touches the paddle. if obj == graphics.paddle and dy >= 0: graphics.set_dy(-dy) # This condition will remove the obj(brick) and change direction. if obj is not graphics.paddle and obj is not None: graphics.set_dy(-dy) graphics.window.remove(obj) count_brick += 1 # If the ball touches the window boundary, the ball will bounce back. if graphics.ball.x <= 0 or graphics.ball.x + graphics.ball.width >= graphics.window.width: graphics.set_dx(-dx) if graphics.ball.y <= 0: graphics.set_dy(-dy) # When the ball out of scope(go deep in the window), condition will reset the ball and minus one lives. if graphics.die(): graphics.window.remove(graphics.ball) lives -= 1 # No lives and End games. if lives == 0: break graphics.start_again() # When you remove all the brick, you win and end the game. if count_brick == graphics.brick_needed_remove(): graphics.window.remove(graphics.ball) break
def main(): rate = FRAME_RATE # To speed up lives = NUM_LIVES # Easy to set lives graphics = BreakoutGraphics() while lives > 0: graphics.check() # Determine the state of the ball and other objects graphics.faster(rate) # Speed up the game if graphics.lose(): # Determine whether to lose graphics.reset() # Reset the position and speed of the ball lives = graphics.lives_time(lives) graphics.print_lives() pause(rate) graphics.end()
def main(): # Add animation loop here! # graphics = BreakoutGraphics(brick_cols=5, brick_rows=4) graphics = BreakoutGraphics() # start backoutgraphics lives = NUM_LIVES # get a variable equal NUM_LIVES while True: if graphics.ball.y + graphics.ball.height >= graphics.window.height: # accidentally fall, lives minus 1 lives -= 1 # live minus 1 graphics.set_ball() # reset the ball back to initial situation if lives == 0: # dead condition - all lives gone graphics.dead() # start the dead method break # stop the while loop if graphics.move_brick == graphics.tot_brick: # win condition - all brick remove graphics.win() # start the win method break # stop the while loop pause(FRAME_RATE) # temporary stop graphics.move_ball() # start the move_ball method graphics.wall_collision() # start the wall_collision method graphics.detect_brick() # start the detect_brick method graphics.detect_paddle() # start the detect_paddle method
def main(): graphics = BreakoutGraphics() # main loop while True: # start and restart page if graphics.get_page() == 0 or graphics.get_page() == 2: graphics.start_page() while True: pause(FRAME_RATE) if graphics.get_page() != 0 or graphics.get_page != 2: break # game page elif graphics.get_page() == 1: graphics.game_reset() graphics.draw_bricks() ball = graphics.get_ball() window = graphics.get_window() while graphics.get_lives() > 0: pause(FRAME_RATE) ball.move(graphics.get_ball_velocity()[0], graphics.get_ball_velocity()[1]) if 0 > ball.x or ball.x + ball.width > window.width: if ball.x < 0: ball.x = 0 elif ball.x + ball.width > window.width: ball.x = window.width - ball.width graphics.set_ball_velocity(x=-1) if 0 > ball.y: ball.y = 0 graphics.set_ball_velocity(y=-1) graphics.detect_collision() graphics.wind() if ball.y > window.height: graphics.set_lives() graphics.game_reset() graphics.draw_bricks() if graphics.check_over(): break graphics.set_page(2)
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)
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)