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 start_page(self): """ This method draw the start page and restart page. """ if self._page == 0: check = self._window.get_object_at(self._window.width // 2, self._window.height // 2) if check is None: progress_rate_board = GLabel(f'Loading...0%') progress_rate_board.font = 'Chalkduster-15' self._window.add( progress_rate_board, (self._window.width - progress_rate_board.width) // 2, (self._window.height + PROGRESS_BAR_SIZE) // 2 + progress_rate_board.height + LABEL_SPACING) pause_time = 300 for i in range(10): color = COLOR_LIST[i % len(COLOR_LIST)] progress_bar = GRect(PROGRESS_BAR_SIZE * (i + 1), PROGRESS_BAR_SIZE) progress_bar.filled = True progress_bar.fill_color = color progress_bar.color = color self._window.add( progress_bar, self._window.width // 2 - PROGRESS_BAR_SIZE * 5, self._window.height // 2 - PROGRESS_BAR_SIZE // 2) progress_rate_board.text = f'Loading...{10*(i+1)}' pause(pause_time) pause_time += 100 self._window.clear() self.draw_bricks() self._start_label.text = f'Click to start' self._window.add( self._start_label, (self._window.width - self._start_label.width) // 2, (self._window.height + self._start_label.height) // 2) elif self._page == 2: self._window.clear() # self.draw_bricks() self._start_label.text = f'Click to restart' self._window.add( self._start_label, (self._window.width - self._start_label.width) // 2, (self._window.height + self._start_label.height) // 2) highscore_board = GLabel(f'High score: {self._highscore}') highscore_board.font = 'Chalkduster-60' highscore_board.color = 'navy' self._window.add( highscore_board, (self._window.width - highscore_board.width) // 2, self._start_label.y - self._start_label.height - LABEL_SPACING * 3)
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)