# 1) try different backgrounds for the stage.  Try replacing
# "space" with: "winter", "space", "water", or "city"
stage.set_background("space")
# 2) choose different colors for the paddle, ball and blocks
paddle_color = "lightblue"
ball_color = "red"
block_color = "yellow"
# 3) make the game harder by making the paddle and blocks smaller
# or make the game easier by making the paddle and block larger
paddle_width = 100
ball_size = 10
block_width = 40
block_height = 10
# 4) make the game harder by making the ball faster and paddle slower
# or make the game easier by making the ball slower and paddle faster
ball_speed = 5
paddle_speed = 5
# 5) change the number of blocks in each row and how many rows of blocks
blocks_per_row = 6
number_of_rows = 4
block_y_spaces = 30
# -----------------------------------------------------------------
# this function prints GAME OVER when the ball goes off the bottom
stage.disable_floor()
bottom = codesters.Rectangle(0, -260, 500, 5, None)
def hits_bottom():
    # 6) Change the message that prints when the player loses the game
    game_over_message = codesters.Text("GAME OVER!", 0, 0, "red")
    game_over_message.set_size(3)
bottom.event_collision(hits_bottom)
# -----------------------------------------------------------------
# these nested for loops create the blocks at the top of the screen
# it requires block_width, block_height, and block_color from above
# it also requires number_of_rows and blocks_per_row from above
import random
first_block_x = -250 + 30 + block_width/2.
block_x_spaces = (500 - 2 * 30 - block_width)/(blocks_per_row-1)
blocks = []
y = 225
for r in range(number_of_rows):
    x = first_block_x
    for b in range(blocks_per_row):
        block = codesters.Rectangle(x, y, block_width, block_height, block_color)
        block.is_goal()
        blocks.append(block)
        x += block_x_spaces
        rand_pop = random.randint(1,10)
        if rand_pop > 10: # try making this number smaller
            stage.remove_sprite(block)
    y -= block_y_spaces
# -----------------------------------------------------------------
# this block of code puts the paddle and the ball on the screen
# then the ball is set in motion
paddle = codesters.Rectangle(0, -210, paddle_width, 10, paddle_color)
ball = codesters.Circle(0, 0, ball_size, ball_color)
ball.set_velx(ball_speed*.4)
ball.set_vely(ball_speed*.9)
# -----------------------------------------------------------------
# these two functions enable the paddle to move left and right
# they use the paddle speed set above
def left():
    paddle.set_x(paddle.x() - 5 * paddle_speed)
stage.event_left_key(left)
def right():
    paddle.set_x(paddle.x() + 5 * paddle_speed)
stage.event_right_key(right)
# -----------------------------------------------------------------
# this collision function runs when the ball hits the paddle
# there is randomness added to how the ball bounces
def collision_paddle(i):
    x_dir = random.randint(0,1)
    if x_dir == 0:
        x_dir = -1
    x_chance = random.randint(3,5)/10.
    y_chance = random.randint(8,12)/10.
    ball.set_velx(ball_speed*x_chance*x_dir)
    ball.set_vely(ball_speed*y_chance)
paddle.event_collision(collision_paddle)
# -----------------------------------------------------------------
# this function manages events when the ball hits a block
# first, the player gets a point
# then, the block is removed form the screen
# finally, the ball reverses its y velocity
score_text = codesters.Text('0', -225, -220, "yellow")
points = 0
def collision_block(i):
    global points
    points += 1
    score_text.set_text(str(points))
    block = stage.get_sprite_by_index(i)
    stage.remove_sprite(block)
    ball.set_vely(-1* ball.vely())
ball.event_collision_goal(collision_block)
                                     
                                    
                                        
    
  
  
  - 
    
    
        Run Code
    
  
- 
        
    
        
        
    
      
- 
    
    
    
    
      Stop Running Code
    
  
- 
    
  
  
  
    Show Chart
  
  
- 
    
  
  
  
    Show Console
  
  
- 
    
    
    
    
      Codesters How To (opens in a new tab)