Brick Breaker style game, detects collision based on position, not using the built in collision functions.

#disable walls because this is the advanced version stage.disable_all_walls() #global variables score = 0 rows = 4 columns = 8 bricks = [] #set up sprites player = codesters.Rectangle(0, -220, 80, 15, "red") player.lives = 3 ball = codesters.Circle(0, 0, 20, "blue") #paddle controls def left(): player.move_left(15) stage.event_key("a", left) def right(): player.move_right(15) stage.event_key("d", right) #creates bricks in a grid def create_bricks(): for row in range(rows): for col in range(columns): brick = codesters.Rectangle(col * 60 - 210, row * 30 + 120, 55, 25, "brown") brick.points = random.randint(1, 10) bricks.append(brick) #detects collision with any of the walls by checking position def wall_collision(): if ball.get_x() + 10 >= 240 or ball.get_x() - 10 <= -240: ball.set_x_speed(ball.get_x_speed() * -1) if ball.get_y() + 10 >= 240: ball.set_y_speed(ball.get_y_speed() * -1) #detects collision with any of the bricks by checking position; returns true if a brick was hit, otherwise false. def brick_collision(): global score for brick in bricks: if ball.get_y() + 10 >= brick.get_y() - 15: if ball.get_x() - 10 <= brick.get_x() + 30 and ball.get_x() + 10 >= brick.get_x() - 30: score += brick.points bricks.remove(brick) stage.remove_sprite(brick) ball.set_y_speed(ball.get_y_speed() * -1.05) return True return False #detects collision with the paddle by checking position def paddle_collision(): if ball.get_x() - 10 <= player.get_x() + 40 and ball.get_x() + 10 >= player.get_x() - 40: if ball.get_y() - 10 <= player.get_y() + 7.5: ball.set_y_speed(ball.get_y_speed() * -1.05) #detects collision with the floor by checking position, returns true if game is over, false otherwise. def floor_collision(): #ball has fallen below the paddle, player loses a life and ball resets if ball.get_y() < -230: player.lives -= 1 if player.lives > 0: reset_ball() return False #player has no more lives, end the game by breaking the while loop else: return True #resets the ball's position and speed def reset_ball(): ball.go_to(0,0) stage.wait(3) ball.set_x_speed(random.randint(-5, 5)) ball.set_y_speed(random.randint(-5, 5)) #main function def main(): #score display display = codesters.Text("Score: 0", 0, 235, "red") reset_ball() create_bricks() #main game loop runs while there are any bricks left while len(bricks) > 0: #if true, player has no lives, game is over if floor_collision(): break paddle_collision() wall_collision() #if true, a brick was hit, update score display if brick_collision(): display.set_text("Score: {}".format(score)) #wait so that the screen has a chance to update stage.wait(0.01) ball.set_x_speed(0) ball.set_y_speed(0) #display final score after the game has ended. display.set_text("GAME OVER! Final Score: {}".format(score)) #traditional way of running main function in a python file. if __name__ == '__main__': main()
  • Run Code
  • Show Console
  • Codesters How To (opens in a new tab)