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 label_for(name, rank, color): if rank > 0: label = GLabel('{} {}'.format(name, rank)) else: label = GLabel('{} *'.format(name)) label.setColor(color) return label
def main_menu(self, dx=0, dy=0): self.__menu_code = 0 # 建立邊界區 self.boundary_zone() # 大標體 self.__obj_main_txt_1 = GLabel('Breakout clone', x=200 + self.__boundary_x + dx, y=200 + self.__boundary_y + dy) self.__obj_main_txt_1.font = f'Times New Roman-100' self.window.add(self.__obj_main_txt_1) # 中標題 self.__obj_main_txt_2 = GLabel('Maker : Tsai NoNo', x=200 + self.__boundary_x + dx, y=300 + self.__boundary_y + dy) self.__obj_main_txt_2.font = f'Times New Roman-50' self.__obj_main_txt_2.color = "gray" self.window.add(self.__obj_main_txt_2) # 開始 self.__obj_main_txt_start = GLabel('Start', x=200 + self.__boundary_x + dx, y=400 + self.__boundary_y + dy) self.__obj_main_txt_start.font = f'Times New Roman-50' self.__obj_main_txt_start.color = "dimgray" self.window.add(self.__obj_main_txt_start) self.main_code_update()
def __init__(self, ball_radius=BALL_RADIUS, paddle_width=PADDLE_WIDTH, paddle_height=PADDLE_HEIGHT, paddle_offset=PADDLE_OFFSET, brick_rows=BRICK_ROWS, brick_cols=BRICK_COLS, brick_width=BRICK_WIDTH, brick_height=BRICK_HEIGHT, brick_offset=BRICK_OFFSET, brick_spacing=BRICK_SPACING, title='Breakout', __dy=INITIAL_Y_SPEED): # Create a graphical window, with some extra space. self.window_width = brick_cols * (brick_width + brick_spacing) - brick_spacing self.window_height = brick_offset + 3 * (brick_rows * (brick_height + brick_spacing) - brick_spacing) self.window = GWindow(width=self.window_width, height=self.window_height, title=title) # Create a paddle. self.paddle = GRect(width=paddle_width, height=paddle_height, x=(self.window_width-paddle_width)/2, y=(self.window_height-paddle_offset-paddle_height)) self.paddle.filled = True self.paddle.color = '#513743' self.paddle.fill_color = '#513743' self.window.add(self.paddle) self.life_label = GLabel('● ● ●') self.life_label.font = '-15' self.life_label.color = '#c53d43' self.score_label = GLabel('score: 0') self.score_label.font = '-15' self.score_label.color = '#455765' self.brick_rows = BRICK_ROWS self.brick_cols = BRICK_COLS # Initialize our mouse listeners. onmousemoved(self.reset_paddle_location) # Draw bricks. self.create_bricks()
def pilot(self): # Instructions line_1 = GLabel('Welcome to my Breakout Game!') line_1.font = 'Courier-12-bold' line_2 = GLabel('Your mission is to get the highest score.') line_2.font = 'Courier-12-bold' line_3 = GLabel('No matter how you get it >.^') line_3.font = 'Courier-12-bold' self.window.add(line_1, x=(self.window.width - line_1.width) / 2, y=self.window.height - 40) self.window.add(line_2, x=(self.window.width - line_2.width) / 2, y=self.window.height - 20) self.window.add(line_3, x=(self.window.width - line_3.width) / 2, y=self.window.height) # Animation while True: # Update line_1.move(0, -5) line_2.move(0, -5) line_3.move(0, -5) # Check if line_1.y <= self.window.height / 2: break # Pause pause(100) pause(1000) self.window.remove(line_1) self.window.remove(line_2) self.window.remove(line_3) pause(1000)
def create_beeper(e): # create 4 beepers size = 50 for i in (1, 3, 7, 9): beeper = GOval(size, size, x=i * 50 - size / 2, y=400 - size / 2) beeper.filled = True beeper.fill_color = 'blue' window.add(beeper) label1 = GLabel('001', x=50 - size / 2 + 9, y=400 - size / 2 + 37) label2 = GLabel('101', x=150 - size / 2 + 9, y=400 - size / 2 + 37) label3 = GLabel('201', x=350 - size / 2 + 9, y=400 - size / 2 + 37) label4 = GLabel('202', x=450 - size / 2 + 9, y=400 - size / 2 + 37) label1.font = '-15' label2.font = '-15' label3.font = '-15' label4.font = '-15' label1.color = 'white' label2.color = 'white' label3.color = 'white' label4.color = 'white' window.add(label1) window.add(label2) window.add(label3) window.add(label4) onmouseclicked(build_karel)
def opening(): black_cover = GRect(w.width, w.height, x=0, y=0) black_cover.filled = True black_cover.fill_color = "black" w.add(black_cover) far = GLabel("A long time ago in a galaxy far, \nfar away...") far.color = "blue" far.font = "-20" w.add(far, x=w.width / 2 - far.width / 2 + 60, y=w.height / 2) pause(1200) w.remove(far) main_title = GLabel("STAR\nWARS") main_title.color = "yellow" for i in range(20): size = (160 // (int(i) + 1)) size = -size main_title.font = str(size) w.add(main_title, x=w.width / 2 - main_title.width / 4, y=w.height / 2) pause(FRAME_RATE * 6) w.remove(main_title) opening_crawl = GLabel("It is a period of civil war\n" "Princess Leia races home abroad\n" "her spaceship but was later\n" "captured by Empires's agents\n" "Use your lightsaber to destroy\n" "the force field to save the\n" "princess") opening_crawl.color = "yellow" opening_crawl.font = "-15" w.add(opening_crawl, x=w.width / 2 - 130, y=w.height + 1) for i in range(50): opening_crawl.move(0, -10) pause(FRAME_RATE * 6) w.remove(black_cover) w.remove(opening_crawl)
def __build_txt(self, dx=0, dy=0): font = 'Times New Roman' # 開始 start_game start_game = GLabel('Start', x=200 + self.__boundary_x + dx, y=400 + self.__boundary_y + dy) start_game.font = f'{font}-50' start_game.color = "dimgray" self.window.add(start_game) self.obj_txt['start'] = start_game # 大標體 tital tital = GLabel('Breakout clone', x=200 + self.__boundary_x + dx, y=200 + self.__boundary_y + dy) tital.font = f'{font}-100' tital.color = "darkgray" self.window.add(tital) self.obj_txt['tital'] = tital # 中標題 subtitle subtitle = GLabel('Maker : Tsai NoNo', x=200 + self.__boundary_x + dx, y=300 + self.__boundary_y + dy) subtitle.font = f'{font}-50' subtitle.color = "gray" self.window.add(subtitle) self.obj_txt['subtitle'] = subtitle return 'self.obj_txt is ok'
def __init__(self, ball_radius=BALL_RADIUS, paddle_width=PADDLE_WIDTH, paddle_height=PADDLE_HEIGHT, paddle_offset=PADDLE_OFFSET, brick_rows=BRICK_ROWS, brick_cols=BRICK_COLS, brick_width=BRICK_WIDTH, brick_height=BRICK_HEIGHT, brick_offset=BRICK_OFFSET, brick_spacing=BRICK_SPACING, title='Breakout'): # Create a graphical window, with some extra space window_width = brick_cols * (brick_width + brick_spacing) - brick_spacing window_height = brick_offset + 3 * (brick_rows * (brick_height + brick_spacing) - brick_spacing) self.window = GWindow(width=window_width, height=window_height, title='Breakout') # Element score label self.score = 0 self.score_label = GLabel('Score: ' + str(self.score)) # Element for game winning self.win_show = GLabel('Create by Mike Lin', x=100, y=100) self.win_show2 = GLabel('March 20 stanCode SC101', x=135, y=115) # Create a paddle self.paddle = GRect(paddle_width, paddle_height, x=(window_width - paddle_width) / 2, y=window_height - paddle_offset) self.window.add(self.paddle) self.paddle.filled = True # Center a filled ball in the graphical window self.ball = GOval(2 * ball_radius, 2 * ball_radius, x=window_width / 2 - ball_radius, y=window_height / 2 - ball_radius) self.ball_x = window_width / 2 - ball_radius self.ball_y = window_height / 2 - ball_radius # Default initial velocity for the ball self.__dx = 0 self.__dy = 0 # Other variables self.total_bricks = 0 # Initialize our mouse listeners onmousemoved( self.paddle_move) # using own method must add 'self.' in the front onmouseclicked(self.clicked) self.draw_bricks_and_ball()
def __init__(self, ball_radius=BALL_RADIUS, paddle_width=PADDLE_WIDTH, paddle_height=PADDLE_HEIGHT, paddle_offset=PADDLE_OFFSET, brick_rows=BRICK_ROWS, brick_cols=BRICK_COLS, brick_width=BRICK_WIDTH, brick_height=BRICK_HEIGHT, brick_offset=BRICK_OFFSET, brick_spacing=BRICK_SPACING, title='Breakout'): # Create a graphical window, with some extra space. self.window_width = brick_cols * (brick_width + brick_spacing) - brick_spacing self.window_height = brick_offset + 3 * ( brick_rows * (brick_height + brick_spacing) - brick_spacing) self.window = GWindow(width=self.window_width, height=self.window_height, title=title) # Create a paddle. self.pw = paddle_width self.ph = paddle_height self.paddle = GRect(self.pw, self.ph, x=(self.window_width - self.pw) / 2, y=self.window_height - paddle_offset) self.paddle.filled = True self.window.add(self.paddle) # Center a filled ball in the graphical window. self.bra = ball_radius * 2 self.ball = GOval(self.bra, self.bra) self.ball.filled = True self.window.add(self.ball, (self.window_width - self.bra) / 2, (self.window_height - self.bra) / 2) # Default initial velocity for the ball. self.__dx = 0 self.__dy = 0 # Initialize our mouse listeners. onmousemoved(self.paddle_move) onmouseclicked(self.ball_move) # Draw bricks. self.build_bricks() self.score = 0 self.score_text = GLabel('Score: ' + str(self.score)) self.score_text.font = '-15-bold' self.window.add(self.score_text, 0, self.window_height) self.lives_text = GLabel('Lives: ' + str(3)) self.lives_text.font = '-15-bold' self.window.add(self.lives_text, self.window_width - 80, self.window_height)
def game_over(self): g_over = GLabel('GAME OVER!') g_over.font = 'Courier-25' g_over = GLabel('GAME OVER!', self.__window.width / 2 - g_over.width / 2, self.__window.height / 2) g_over.font = 'Courier-25' self.__window.add(g_over)
def main(): global graphics, life_cnt graphics.window.add(graphics.score, 250, graphics.score.height + 3) graphics.window.add(graphics.life, 0, graphics.life.height + 3) graphics.window.add(graphics.life1, graphics.life.width + 3, 3) graphics.window.add(graphics.life2, graphics.life.width + 6 + graphics.life1.width, 3) graphics.window.add(graphics.life3, graphics.life.width + 9 + graphics.life1.width * 2, 3) # Add animation loop here! while True: pause(FRAME_RATE) dx = graphics.get_dx() dy = graphics.get_dy() graphics.ball.move(dx, dy) # Check wall graphics.check_wall() # Check four points graphics.check_pts() # Life cnt if graphics.ball.y > graphics.window.height: life_cnt -= 1 graphics.life.text = 'LIVES:' graphics.lives = life_cnt if life_cnt == 2: graphics.window.remove(graphics.life1) elif life_cnt == 1: graphics.window.remove(graphics.life2) elif life_cnt == 0: graphics.window.remove(graphics.life3) 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) dx = 0 dy = 0 graphics.set2_dx(dx) graphics.set2_dy(dy) if life_cnt == 0: graphics.window.remove(graphics.ball) oh_no = GLabel("NO MORE LIVES! :'( ") oh_no.font = 'Helvetica-25-italic' graphics.window.add(oh_no, x=(graphics.window.width - oh_no.width) / 2, y=(graphics.window.height - oh_no.height) / 2) break elif graphics.score_cnt == graphics.brick_rows * graphics.brick_cols: graphics.window.remove(graphics.ball) oh_no = GLabel("YOU WIN! :D ") oh_no.font = 'Helvetica-25-italic' graphics.window.add(oh_no, x=(graphics.window.width - oh_no.width) / 2, y=(graphics.window.height - oh_no.height) / 2) break
def probe_obj(self): """ This function will detect any collision between the ball with other objects. :return: True, if the ball bump into the paddle or the bricks. """ # The variable 'obj' will change to the next corner if the checked None. obj1 = self.window.get_object_at(self.ball.x, self.ball.y) obj2 = self.window.get_object_at(self.ball.x + self.ball.width, self.ball.y) obj3 = self.window.get_object_at(self.ball.x, self.ball.y + self.ball.height) obj4 = self.window.get_object_at(self.ball.x + self.ball.width, self.ball.y + self.ball.height) point = obj1 if point is None or point is self.name and point is self.score_label and point is self.lives_label: point = obj2 if point is None or point is self.name and point is self.score_label and point is self.lives_label: point = obj3 if point is None or point is self.name and point is self.score_label and point is self.lives_label: point = obj4 if point is None or point is self.name and point is self.score_label and point is self.lives_label: pass if point is self.paddle: # To avoid the ball from sticking on the paddle, I move the ball above the paddle. self.ball.move(0, -self.paddle_height - self.ball.height) # When the ball touches the paddle, the paddle will change its color to the ball's color. self.paddle.color = self.ball.color self.paddle.fill_color = self.ball.fill_color return True if point is not self.paddle and point is not None and point is not self.name\ and point is not self.score_label and point is not self.lives_label: # Every time when the ball bump into a brick, the score will reset. self.window.remove(point) self.brick_left -= 1 # The formula of score counting. self.score = int((self.brick_rows * self.brick_cols - self.brick_left) ** 1.5) if self.brick_left == (self.brick_rows * self.brick_cols) * 1/2: self.lives += 1 self.window.remove(self.lives_label) self.lives_label = GLabel('Lives: ' + str(self.lives)) self.lives_label.font = '-15-bold' self.window.add(self.lives_label, self.window_width-100, self.window_height-5) self.window.remove(self.score_label) self.score_label = GLabel('Score: ' + str(self.score)) self.score_label.font = '-15-bold' self.window.add(self.score_label, 5, self.window.height) # The color of the ball depends on how many bricks are left. if self.brick_rows * self.brick_cols * 3/5 < self.brick_left <= self.brick_rows * self.brick_cols * 4/5: self.ball.color = 'palegreen' self.ball.fill_color = 'palegreen' elif self.brick_rows * self.brick_cols * 2/5 < self.brick_left <= self.brick_rows * self.brick_cols * 3/5: self.ball.color = 'lightyellow' self.ball.fill_color = 'lightyellow' elif self.brick_rows * self.brick_cols * 1/5 < self.brick_left <= self.brick_rows * self.brick_cols * 2/5: self.ball.color = 'peachpuff' self.ball.fill_color = 'peachpuff' # More difficult to see the ball. elif self.brick_left <= self.brick_rows * self.brick_cols * 1/5: self.ball.color = 'black' self.ball.fill_color = 'black' return True
def end_game_symbol(self): # show the end game symbol self.lose_label = GLabel('YOU DIE', x=self.window.width*0.4, y=self.window.height/2-10) self.lose_label.font = 'Courier-13' self.lose_label.color = 'red' self.window.add(self.lose_label) self.lose_count = GLabel('Score : '+str(self.point_count), x=self.window.width*0.4, y=self.window.height/2 - 50) self.lose_count.font = 'Courier-13' self.lose_count.color = 'red' self.window.add(self.lose_count)
def win_game(self): if self.__left_bricks <= 0: you_win = GLabel('YOU WIN!') you_win.font = 'Courier-25' you_win = GLabel('YOU WIN!', self.__window.width / 2 - you_win.width / 2, self.__window.height / 2) you_win.font = 'Courier-25' self.__window.add(you_win) return True
def __init__(self, window_object): self.window = window_object # initialize bar self.slide_bar = GRect(WINDOW_DIM - 2 * BAR_OFFSET, BAR_WIDTH, x=BAR_OFFSET, y=WINDOW_DIM - BAR_OFFSET) self.slide_bar.filled = True self.slide_bar.fill_color = 'darksage' self.window.add(self.slide_bar) # initialize orb self.slider_orb = GOval(BALL_RADIUS * 2, BALL_RADIUS * 2, x=WINDOW_DIM - 2 * BAR_OFFSET, y=WINDOW_DIM - BAR_OFFSET - BALL_RADIUS / 2) self.slider_orb.filled = True self.slider_orb.fill_color = 'mediumturquoise' self.window.add(self.slider_orb) # initialize blank fractal self.fractal = GImage.from_file('white_600_400.png') # initialize description label self.description_1 = GLabel("Click to start animation.", 40, 40) self.description_1.x = self.window.width / 2 - self.description_1.\ width / 2 self.description_1.y = self.window.height - 2*self.description_1.\ height + 5 self.window.add(self.description_1) self.description_2 = GLabel( "Move the slider in order to see the progression of the Julia set fractal pattern.", 40, 40) self.description_2.x = self.window.width / 2 - self.description_2.width / 2 self.description_2.y = self.window.height - self.description_2.height + 5 self.window.add(self.description_2) # initialize title self.title = GImage.from_file('title.PNG') self.title.x = (self.window.width - self.title.width) / 2 self.title.y = 10 self.window.add(self.title) # initialize slider ratio (to measure progression) self.slider_ratio = 0 # add mouse listeners onmouseclicked(self.start_sequence) onmousemoved(self.zoom_fractal) # initialize run statement self.running = False
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 __init__(self, bar_width=BAR_WIDTH, bar_height=BAR_HEIGHT, win_width=WIN_WIDTH, win_height=WIN_HEIGHT, play_button_width=PLAY_BUTTON_WIDTH, play_button_height=PLAY_BUTTON_HEIGHT, fb_button_width=WIN_WIDTH * 0.6, fb_button_height=FB_BUTTON_HEIGHT): self.window = GWindow(win_width, win_height, title='Welcome') self.load_bar = GRect(bar_width, bar_height) self.load_label = GLabel('0%') self.solid_bar = GRect(0, bar_height) self.play_button = GRect(play_button_width, play_button_height) self.play_label = GLabel('Start New Game') self.fb_button = GRect(fb_button_width, fb_button_height) self.fb_label = GLabel('continue with facebook')
def words_label(): """ This function will write the label of Bonjour Paris. """ # paris label label2 = GLabel('PARIS', x=90, y=450) label2.font = '-80' label2.color = 'white' window.add(label2) # Bonjour label label3 = GLabel('BONJOUR', x=40, y=340) label3.font = '-70' label3.color = 'white' window.add(label3)
def __init__(self, ball_radius=BALL_RADIUS, paddle_width=PADDLE_WIDTH, paddle_height=PADDLE_HEIGHT, paddle_offset=PADDLE_OFFSET, brick_rows=BRICK_ROWS, brick_cols=BRICK_COLS, brick_width=BRICK_WIDTH, brick_height=BRICK_HEIGHT, brick_offset=BRICK_OFFSET, brick_spacing=BRICK_SPACING, title='Breakout'): # Create a graphical window, with some extra space. window_width = brick_cols * (brick_width + brick_spacing) - brick_spacing window_height = brick_offset + 3 * (brick_rows * (brick_height + brick_spacing) - brick_spacing) self.window = GWindow(width=window_width, height=window_height, title=title) # Create a paddle. self.paddle = GRect(paddle_width, paddle_height) self.window.add(self.paddle, (self.window.width - paddle_width) / 2, self.window.height - paddle_offset - paddle_height) self.paddle.filled = True self.paddle.fill_color = 'blue' self.paddle.color = 'blue' # Center a filled ball in the graphical window. self.ball = GOval(ball_radius * 2, ball_radius * 2) self.ball.filled = True self.reset_ball() # Default initial velocity for the ball. self.__dx = 0 self.__dy = 0 self.switch = False # Initialize our mouse listeners. onmouseclicked(self.game_start) onmousemoved(self.move_paddle) # Draw bricks. by = 0 for i in range(brick_rows): bx = 0 for j in range(brick_cols): self.brick = GRect(brick_width, brick_height) self.brick.filled = True if i < brick_cols / 5: self.brick.fill_color = 'red' elif brick_cols / 5 <= i < brick_cols / 5 * 2: self.brick.fill_color = 'orange' elif brick_cols / 5 * 2 <= i < brick_cols / 5 * 3: self.brick.fill_color = 'yellow' elif brick_cols / 5 * 3 <= i < brick_cols / 5 * 4: self.brick.fill_color = 'green' else: self.brick.fill_color = 'blue' self.window.add(self.brick, bx, brick_offset+by) bx += brick_width + brick_spacing by += brick_height + brick_spacing # scoreboard self.earned_score = 0 self.total_score = brick_cols * brick_rows self.scoreboard = GLabel(f'score: {self.earned_score}/{self.total_score}', x=10, y=30) self.scoreboard.font = 'courier-20' self.window.add(self.scoreboard)
def main(): bg = GRect(950, 600) window.add(bg) bg.color = 'lightblue' bg.fill_color = 'lightblue' ryan() tube() peach() title = GLabel('KAKAO FRIENDS') title.color = 'cornflowerblue' title.font = 'Verdana-50-bold' window.add(title, x=255, y=500) title2 = GLabel('KAKAO FRIENDS') title2.color = 'ivory' title2.font = 'Verdana-50-bold' window.add(title2, x=251, y=500)
def loading(self): """ the loading page setup """ self.loading_text.font = "-70" self.loading_text.x = (self.window.width - self.loading_text.width) // 2 self.loading_text.y = self.window.height // 3 self.window.add(self.loading_text) self.progress_bar.font = "-40" self.progress_bar.x = (self.window.width - self.progress_bar.width) // 2 self.progress_bar.y = self.loading_text.y + self.loading_text.height self.window.add(self.progress_bar) self.menu_text.x = (self.window.width - self.menu_text.width) // 2 self.menu_text.y = self.progress_bar.y + int(self.progress_bar.height*1.2) self.window.add(self.menu_text) for num in range(len(GIFT_COLOR)): self.example_gift[num] = GRect(self.gift_size, self.gift_size) color_set(self.example_gift[num], GIFT_COLOR[num][0]) self.example_gift[num].x = self.window.width//2 - self.gift_size*3 self.example_gift[num].y = self.menu_text.y + int(self.gift_size*1.5*(num+0.7)) - self.gift_size//3 self.gift_explanation[num] = GLabel(GIFT_COLOR[num][1]) self.gift_explanation[num].x = self.window.width//2 - self.gift_size*1 self.gift_explanation[num].y = self.menu_text.y + int(self.gift_size*1.5*(num+0.7)) + self.gift_explanation[num].height self.window.add(self.example_gift[num]) self.window.add(self.gift_explanation[num]) for tick in range(6): self.single_bar(["▏", "▎", "▍", "▌", "▋", "▊", "▉", "█"], tick)
def make_title(): """ this code can make a title on the window """ title = GLabel('Similarity.py') title.font = '-70' window.add(title, 50, 150)
def setup_label(self): # self.score = 0 # 為什麼要有這一行?? # print(self.score) self.label = GLabel('Scores:' + str(self.score)) # 跨method 所以要再上面寫 self.label.color = 'black' self.label.font = 'Courier-12-bold' self.window.add(self.label, x=10, y=30)
def game_over(self): game_over = GLabel('Game Over') game_over.color = 'tomato' game_over.font = 'Courier-30-bold' self.window.add(game_over, x=(self.window.width - game_over.width) / 2, y=self.window.height / 2)
def congrats(self): label_win = GLabel('Congratulations!') label_win.color = 'navy' label_win.font = 'Courier-30-bold' self.window.add(label_win, x=(self.window.width - label_win.width) / 2, y=self.window.height / 2)
def __init__(self, window_width=WINDOW_WIDTH, window_height=WINDOW_HEIGHT, zone_width=ZONE_WIDTH, zone_height=ZONE_HEIGHT, ball_radius=BALL_RADIUS): # Create window self.w = GWindow(width=window_width, height=window_height, title='Zone_Game') # Create zone self.zone = GRect(zone_width, zone_height, x=(self.w.width-zone_width)/2, y=(self.w.height-zone_height)/2) self.zone.color = 'blue' self.w.add(self.zone) # Create ball and initialize velocity/position self.ball = GOval(ball_radius*2, ball_radius*2) self.ball.filled = True self.dx = 0 self.dy = 0 self.lives = 3 self.reset_ball() # Lives Word self.lives_word = GLabel('Lives: ' + str(self.lives), x=WINDOW_WIDTH * 0.02, y=WINDOW_HEIGHT * 0.1) self.lives_word.font = 'Courier-25-bold' self.w.add(self.lives_word) # Initialize mouse listeners onmouseclicked(self.handle_click)
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)
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 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