def main_game(e): # run this when user click global COUNT, life_ui, score_ui, vx, vy, score_count, life_left, ball # create life count ui if is_at_original_point(): # check if the game started if life_left > 0 and score_count < WIN_SCORE: while ball.y < w.height: # game loop: keep the game going if ball doesn't fall out of the border if score_count < WIN_SCORE/2: ball.move(vx, vy) pause(FRAME_RATE) check_if_ball_hit_sth() else: boss_fight() break w.remove(score_ui) score_ui = update_score() life_left -= 1 w.remove(ball) if score_count >= WIN_SCORE: over = GLabel("YOU WON") over.font = "-60" w.add(over, w.width / 2 - over.width / 2, w.height / 2) elif life_left <= 0: game_over() else: ball = graphics.ball() w.remove(life_ui) life_ui = update_life() else: pass
def 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 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 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 make_title(): """ this code can make a title on the window """ title = GLabel('Similarity.py') title.font = '-70' window.add(title, 50, 150)
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 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_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'): # 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 __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 game_over(self): """ One of the conditions for game over. When all chances were used out. """ self.reset_ball() lose = GLabel('You lose!') lose.font = '-70-bold' self.window.add(lose, (self.window.width - lose.width) / 2, 500)
def game_over(self): """ When lives are used over, the game is over! """ game_over = GLabel('Game over :(') game_over.font = '-40-bold' game_over.color = 'gray' self.window.add(game_over, (self.window.width/2)-(game_over.width/2), self.window.height/2)
def gameover(self): over = GLabel('Game Over!') over.font = '-50' over.color = 'red' self.window.add(over, x=(self.window.width - over.width) / 2, y=self.window.height / 2) self.window.remove(self.ball)
def main(): # background background_color = ['ivory', 'lightblue', 'skyblue'] for i in range(0, 3): background = GRect(window.width, window.height / 3, x=0, y=window.height / 3 * i) background.filled = True background.fill_color = '' + background_color[i] background.color = '' + background_color[i] window.add(background) # pyramid pyramid_color = [ 'saddlebrown', 'darkgoldenrod', 'goldenrod', 'orange', 'gold', 'palegoldenrod', 'lemonchiffon' ] for i in range(0, 7): # pyramid Grect pyramid = GRect(FIRST_X - REDUCE * i, FIRST_Y, x=5 + REDUCE / 2 * i, y=window.height - FIRST_Y * (1 + i)) pyramid.filled = True pyramid.fill_color = '' + pyramid_color[i] window.add(pyramid) # pyramid GLabel # pyramid_label = GLabel('STAGE '+str(i+1), x=260, y=window.height - FIRST_Y * i -10) # pyramid_label.font = '-24' # pyramid_label.color = 'darkgrey' # window.add(pyramid_label) # flag flagstaff = GRect(5, 100, x=280, y=50) flagstaff.filled = True flagstaff.fill_color = 'brown' window.add(flagstaff) flag = GRect(100, 40, x=285, y=50) flag.filled = True flag.fill_color = 'silver' window.add(flag) flag_label = GLabel('GOAL!!!', x=290, y=85) flag_label.font = '-26' flag_label.color = 'red' window.add(flag_label) # sun sun() # robot robot_face() robot_body()
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 build_start_sign(): """ This function creates a start sign with GLabel :return: object, sign """ sign = GLabel('Click Anywhere !!!', x=START_X + 40, y=START_Y + 10) sign.color = 'dimgrey' sign.font = 'Times New Roman-15-italic-bold' return sign
def set_end_massage(self, massage): """ This method can show massage on center of graphic at the end of game. :param massage: str, massage want to show. """ massage_label = GLabel(massage) massage_label.font = '-40' self.window.add(massage_label, x=(self.window.width - massage_label.width) / 2, y=(self.window.height - massage_label.height) / 2)
def label_win_the_game(): """ Show "You Win!" as user wins the game :return: gobject """ label = GLabel('You Win!') label.font = '-50' label.color = 'skyblue' return label
def label_gameover(): """ show "Game over!" as game ends :return: gobject, show player the game is over """ label = GLabel('Game over!') label.font = '-50' label.color = 'skyblue' return label
def textbox(dx=0, dy=0): """ Function: draw text Principle: use label :param dx: x-axis displacement :param dy: y-axis displacement """ txt = GLabel('保夾500元', x=dx + 130, y=dy + 160) txt.font = 'Arial-55' window.add(txt)
def win(self): """ This method shows an image and label if the user wins the game by clearing all the bricks """ if self.score == 100: win = GLabel('WIN!!') win.font = 'Times-50' self.window.add(win, 50, 235) self.window.add(self.img, 10, 25) self.window.remove(self.ball)
def build_special_stop_sign(): """ This function creates a special stop sign with GLabel :return: object, sign """ random_x = random.randint(0, 600) random_y = random.randint(30, 500) sign = GLabel(random_word(), x=random_x, y=random_y) sign.color = random_color() sign.font = 'Times New Roman-20-bold' return sign
def build_stop_sign(): """ This function creates a stop sign with GLabel :return: object, sign """ random_x = random.randint(0, 640) random_y = random.randint(30, 500) sign = GLabel('Stop Clicking !!! ', x=random_x, y=random_y) sign.color = 'firebrick' sign.font = 'Times New Roman-15-bold' return sign
def fail_game2(self): fail_frame = GRect(450, 80) fail_frame.filled = True fail_frame.fill_color = 'white' fail_frame.color = 'white' self.window.add(fail_frame, (self.window.width - fail_frame.width) / 2, (self.window.height - fail_frame.height) / 2) failed = GLabel('Game Over') failed.font = '-60-bold' failed.color = 'black' self.window.add(failed, fail_frame.x + 70, fail_frame.y + 75)
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 finish_game(self): finish_frame = GRect(450, 80) finish_frame.filled = True finish_frame.fill_color = 'white' finish_frame.color = 'white' self.window.add(finish_frame, (self.window.width - finish_frame.width) / 2, (self.window.height - finish_frame.height) / 2) finish = GLabel('Congratulation!!! You win!') finish.font = '-35-bold' finish.color = 'red' self.window.add(finish, finish_frame.x + 10, finish_frame.y + 65)
def show_damage_number(self, damage): """ This method can show damage number on the graphic :param damage: int, damage number """ damage_label = GLabel(f'-{damage}') damage_label.color = 'red' damage_label.font = f'-{damage}' self.window.add(damage_label, x=self.hero.body.x + self.hero.body.width, y=self.hero.body.y + self.hero.body.height) pause(80) self.window.remove(damage_label)
def win(self): self.window.clear() win_background = GRect(self.window.width, self.window.height) win_background.filled = True win_background.color = '#e5abbe' win_background.fill_color = '#e5abbe' win_label = GLabel('WIN') win_label.font = '-50' win_label.color = '#fdeff2' self.window.add(win_background) self.window.add(win_label, self.window.width/2-win_label.width/2, self.window.height/2.5) self.window.add(self.score_label, (self.window_width-self.score_label.width)/2, win_label.y+50)
def lose(self): self.window.clear() lose_background = GRect(self.window.width, self.window.height) lose_background.filled = True lose_background.color = '#a6a5c4' lose_background.fill_color = '#a6a5c4' lose_label = GLabel('GAME OVER') lose_label.font = '-50' lose_label.color = '#e7e7eb' self.window.add(lose_background) self.window.add(lose_label, self.window.width/2-lose_label.width/2, self.window.height/2.5) self.window.add(self.score_label, (self.window_width-self.score_label.width)/2, lose_label.y+50)