def main(): """ This program simulates a bouncing ball at (START_X, START_Y) that has VX as x velocity and 0 as y velocity. Each bounce reduces y velocity to REDUCE of itself. """ global times,switch,VY ball=GOval(SIZE,SIZE,x=START_X,y=START_Y) ball.filled= True ball.fill_color='black' window.add(ball) switch = False onmouseclicked(turn_on) while True: pause(DELAY) if switch==True: VY+=GRAVITY ball.move(VX,VY) pause(DELAY) if ball.y>window.height: VY=-REDUCE*VY if ball.x>window.width: ball.x=START_X ball.y=START_Y times +=1 if times>=3: break else: pass
def main(): """ This program simulates a bouncing ball at (START_X, START_Y) that has VX as x velocity and 0 as y velocity. Each bounce reduces y velocity to REDUCE of itself. """ onmouseclicked(bouncing)
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.window = GWindow(window_width, window_height, title='Zone Game') # Create zone self.zone = GRect(zone_width, zone_height, x=(window_width - zone_width) / 2, y=(window_height - zone_height) / 2) self.zone.color = 'blue' self.window.add(self.zone) # Create ball and initialize velocity/position self.ball = GOval(2 * ball_radius, 2 * ball_radius) self.ball.filled = True self.ball.fill_color = 'salmon' self.dx = 0 self.dy = 0 self.reset_ball() # Initialize mouse listeners onmouseclicked(self.handle_click)
def main(): """ """ ball.filled = True ball.fill_color = 'black' window.add(ball, START_X - SIZE / 2, START_Y - SIZE / 2) onmouseclicked(start)
def start(): graphics.reset_ball_location() graphics.window.add(life_label, graphics.window.width - life_label.width - 30, life_label.height + 20) graphics.window.add(score_label, 20, score_label.height + 20) onmouseclicked(serve)
def form_line(e): # Form a line from the dot to the coordinate clicked. path = GLine(dot.x+SIZE/2, dot.y+SIZE/2, e.x, e.y) window.add(path) # The dot will be removed when the line is formed. window.remove(dot) onmouseclicked(draw_dot)
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 __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 draw_dot(event): # Draw the first dot at the point clicked. global dot dot.move(event.x-dot.x-dot.width/2, event.y-dot.y-dot.height/2) window.add(dot) # The next click will activate the function: form_line. onmouseclicked(form_line)
def play_again(n): """ After finishing the game, if player click the result label, which show Game over or You win, the login page will show up again and player can start new run of game. :param n: the mouse positional information after game finished. """ global log_in_page, NUM_LIVES if graphics.window.get_object_at(n.x, n.y) is graphics.result_label: NUM_LIVES = 3 log_in_page = Log_in_page() graphics.window.remove(graphics.result_label) graphics.window.close() log_in_page.add_play_button() log_in_page.add_fb_button() log_in_page.solid_bar = GRect(log_in_page.load_bar.width, log_in_page.load_bar.height) log_in_page.loading() log_in_page.window.add(log_in_page.solid_bar, log_in_page.load_bar.x, log_in_page.load_bar.y) log_in_page.load_label.text = '100%' log_in_page.window.add( log_in_page.load_label, log_in_page.load_bar.x + log_in_page.load_bar.width - log_in_page.load_label.width, log_in_page.load_bar.y + log_in_page.load_bar.height + log_in_page.load_label.height + 5) onmouseclicked(animation_loop)
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(): # Add animation loop here! # make paddle and bricks graphics.make_a_paddle() graphics.brick() onmouseclicked(function)
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) self.paddle_offset = PADDLE_OFFSET self.ball_r = BALL_RADIUS # Create a paddle self.paddle = GRect(paddle_width, paddle_height, x=(window_width-paddle_width)/2, y=window_height-paddle_offset-paddle_height) self.paddle.filled = True self.paddle.fill_color = 'blue' self.window.add(self.paddle) # Center a filled ball in the graphical window self.first_ball = GOval(ball_radius*2, ball_radius*2, x=(window_width-ball_radius)/2, y=(window_height-ball_radius)/2) self.first_ball.filled = True self.first_ball.fill_color = 'black' self.window.add(self.first_ball) self.initial_x = (window_width-ball_radius)/2 self.initial_y = (window_height-ball_radius)/2 # Default initial velocity for the ball self.__dx = random.randint(1, MAX_X_SPEED) self.__dy = INITIAL_Y_SPEED if random.random() > 0.5: self.__dx = -self.__dx # Initialize our mouse listeners self.activate = False self.crash_is_paddle = False self.crash_is_brick = False self.point_count = 0 onmousemoved(self.paddle_move) onmouseclicked(self.ball_start) # Draw bricks for y_pos in range(0, (BRICK_HEIGHT+BRICK_SPACING)*BRICK_ROWS-BRICK_SPACING, BRICK_HEIGHT+BRICK_SPACING): for x_pos in range(0, (BRICK_WIDTH+BRICK_SPACING)*BRICK_COLS-BRICK_SPACING, BRICK_WIDTH+BRICK_SPACING): self.brick = GRect(BRICK_WIDTH, BRICK_HEIGHT) self.brick.filled = True if y_pos < BRICK_HEIGHT+BRICK_SPACING * 2 + 1: self.brick.fill_color = 'red' elif y_pos < (BRICK_HEIGHT+BRICK_SPACING) * 4: self.brick.fill_color = 'orange' elif y_pos < (BRICK_HEIGHT+BRICK_SPACING) * 6: self.brick.fill_color = 'yellow' elif y_pos < (BRICK_HEIGHT+BRICK_SPACING) * 8: self.brick.fill_color = 'green' elif y_pos < (BRICK_HEIGHT+BRICK_SPACING) * 10: self.brick.fill_color = 'blue' else: self.brick.fill_color = 'black' self.window.add(self.brick, x=x_pos, y=y_pos)
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 main(): """ This program is the game that the player controls a paddle to rebound the ball. And the ball will break bricks that it hits. :return: """ life_count.text = 'Live: ' + str(life_left) onmouseclicked(click_start)
def main(): """ This program will set attribute to the ball and add ball instance on window, while listening to onmouseclicked and onmousemoved events. """ ball_setting() onmouseclicked(click_event) onmousemoved(move_event)
def main(): """ This program will prepare canvas for drawing circle and line, while listening to onmouseclicked and onmousemove events. """ prepare_canvas() onmouseclicked(click_event) onmousemoved(move_event)
def main(): onmouseclicked(hit) while True: mole=GImage('mole.jpeg') mole_x=random.randint(0,window.width-mole.width) mole_y=random.randint(0,window.height-mole.height) window.add(mole,mole_x,mole_y) pause(DELAY)
def main(): window.add(rect) rect.filled = True rect.color = COLOR rect.fill_color = COLOR onmousemoved(reset_position) onmouseclicked(my_punch) onmousedragged(draw)
def draw_line(event): """ When the user clicks the mouse again, this function will remove the former dot and draw a line. """ line = GLine(dot.x + SIZE / 2, dot.y + SIZE / 2, event.x, event.y) window.add(line) window.remove(dot) onmouseclicked(function)
def function(event): """ When the user clicks the mouse, this function will make a dot at that point. """ global dot dot = GOval(SIZE, SIZE, x=event.x - SIZE / 2, y=event.y - SIZE / 2) window.add(dot) onmouseclicked(draw_line)
def main(): opening() life_ui.font = "-15" score_ui.font = "-15" w.add(life_ui, x=w.width - life_ui.width, y=w.height - life_ui.height) w.add(score_ui, x=0, y=w.height - score_ui.height) onmousemoved(mouse_move) onmouseclicked(main_game)
def main(): """ This program creates lines on an instance of GWindow class. There is a circle indicating the user’s first click. A line appears at the condition where the circle disappears as the user clicks on the canvas for the second time. """ onmouseclicked(draw_line)
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.pof = paddle_offset self.paddle = GRect(paddle_width, paddle_height) self.paddle.filled = True self.window.add(self.paddle, (window_width - self.paddle.width) / 2, window_height - self.pof) # Center a filled ball in the graphical window. self.ball = GOval(2 * ball_radius, 2 * ball_radius) self.ball.filled = True self.window.add(self.ball, self.window.width / 2 - self.ball.width / 2, self.window.height / 2) self.initial_x = self.window.width / 2 - self.ball.width / 2 self.initial_y = self.window.height / 2 # Default initial velocity for the ball. self.__dx = 0 self.__dy = 0 self.set_speed() self.get_dx() self.get_dy() # Initialize our mouse listeners. self.click = False onmousemoved(self.paddle_follow) onmouseclicked(self.start) # Draw bricks. self.br = brick_rows self.bc = brick_cols self.sp = brick_spacing self.of = brick_offset self.set_brick() # self.check() # self.rest_ball() self.total = self.br * self.bc
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'): num_bricks = 100 # 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) self.paddle = GRect(paddle_width, paddle_height, x=(window_width - paddle_width)/2, y = window_height - \ paddle_offset) # initializing the paddle self.paddle.filled = True self.paddle.fill_color = 'black' self.window.add(self.paddle) # initializing the ball self.ball = GOval(width=ball_radius * 2, height=ball_radius * 2, x=window_width / 2 - BALL_RADIUS, y=window_height / 2 - BALL_RADIUS) self.ball.filled = True self.ball.fill_color = 'black' self.window.add(self.ball) # initial velocity self.vx = 0 self.vy = INITIAL_Y_SPEED # draw bricks self.draw_bricks() # number of lives left self.num_lives = 3 # running? big question mark self.running = False # brick count self.brick_count = 100 # mouse listeners onmouseclicked(self.start) onmousemoved(self.move_paddle)
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') # 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.window.add(self.ball) self.ball.filled = True 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 self.__start = False # Initialize our mouse listeners onmousemoved(self.paddle_move) # using own method must add 'self.' in the front onmouseclicked(self.clicked) # Draw bricks color_num = BRICK_COLS/5 for i in range(BRICK_ROWS): for j in range(BRICK_COLS): if j // color_num == 0: color = 'red' elif j // color_num == 1: color = 'orange' elif j // color_num == 2: color = 'yellow' elif j // color_num == 3: color = 'green' else: color = 'blue' bricks = GRect(BRICK_WIDTH, BRICK_HEIGHT) self.window.add(bricks, x=0+i*(BRICK_WIDTH+BRICK_SPACING), y=BRICK_OFFSET+j*(BRICK_HEIGHT+BRICK_SPACING)) bricks.filled = True bricks.fill_color = color self.total_bricks += 1
def build_karel(e): # Build up the robot, Karel. karel_head() karel_eye() karel_neck() karel_body() karel_limb() karel_label() onmouseclicked(move)
def start(self, e): """ Start the game :param e: event :return: None """ onmousemoved(self.paddle_move) onmouseclicked(self.click) self.window.remove(self.icon)
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) label_2 = add_score() graphics.window.add(label_2, x=0, y=graphics.window.height - label_life.height)