# """
#  Simple graphics demo
 
#  Sample Python/Pygame Programs
#  Simpson College Computer Science
#  http://programarcadegames.com/
#  http://simpson.edu/computer-science/
 
# """
 
# # Import a library of functions called 'pygame'
# import pygame
 
# # Initialize the game engine
# pygame.init()
 
# # Define some colors
# BLACK = (0, 0, 0)
# WHITE = (255, 255, 255)
# BLUE = (0, 0, 255)
# GREEN = (0, 255, 0)
# RED = (255, 0, 0)
 
# PI = 3.141592653
 
# # Set the height and width of the screen
# size = (400, 500)
# screen = pygame.display.set_mode(size)
 
# pygame.display.set_caption("Professor Craven's Cool Game")
 
# # Loop until the user clicks the close button.
# done = False
# clock = pygame.time.Clock()
 
# # Loop as long as done == False
# while not done:
 
#     for event in pygame.event.get():  # User did something
#         if event.type == pygame.QUIT:  # If user clicked close
#             done = True  # Flag that we are done so we exit this loop
 
#     # All drawing code happens after the for loop and but
#     # inside the main while not done loop.
 
#     # Clear the screen and set the screen background
#     screen.fill(WHITE)
 
#     # Draw on the screen a line from (0,0) to (100,100)
#     # 5 pixels wide.
#     pygame.draw.line(screen, GREEN, [0, 0], [100, 100], 5)
 
#     # Draw on the screen several lines from (0,10) to (100,110)
#     # 5 pixels wide using a loop
#     for y_offset in range(0, 100, 10):
#         pygame.draw.line(screen, RED, [0, 10 + y_offset], [100, 110 + y_offset], 5)
 
 
#     # Draw a rectangle
#     pygame.draw.rect(screen, BLACK, [20, 20, 250, 100], 2)
 
#     # Draw an ellipse, using a rectangle as the outside boundaries
#     pygame.draw.ellipse(screen, BLACK, [20, 20, 250, 100], 2)
 
#     # Draw an arc as part of an ellipse.
#     # Use radians to determine what angle to draw.
#     pygame.draw.arc(screen, BLACK, [20, 220, 250, 200], 0, PI / 2, 2)
#     pygame.draw.arc(screen, GREEN, [20, 220, 250, 200], PI / 2, PI, 2)
#     pygame.draw.arc(screen, BLUE, [20, 220, 250, 200], PI, 3 * PI / 2, 2)
#     pygame.draw.arc(screen, RED, [20, 220, 250, 200], 3 * PI / 2, 2 * PI, 2)
 
#     # This draws a triangle using the polygon command
#     pygame.draw.polygon(screen, BLACK, [[100, 100], [0, 200], [200, 200]], 5)
 
#     # Select the font to use, size, bold, italics
#     font = pygame.font.SysFont('Calibri', 25, True, False)
 
#     # Render the text. "True" means anti-aliased text.
#     # Black is the color. This creates an image of the
#     # letters, but does not put it on the screen
#     text = font.render("My text", True, BLACK)
 
#     # Put the image of the text on the screen at 250x250
#     screen.blit(text, [250, 250])
 
#     # Go ahead and update the screen with what we've drawn.
#     # This MUST happen after all the other drawing commands.
#     pygame.display.flip()
 
#     # This limits the while loop to a max of 60 times per second.
#     # Leave this out and we will use all CPU we can.
#     clock.tick(60)
 
# # Be IDLE friendly
# pygame.quit()

# """
#  Simple graphics demo
 
#  Sample Python/Pygame Programs
#  Simpson College Computer Science
#  http://programarcadegames.com/
#  http://simpson.edu/computer-science/
 
# """
 
# # Import a library of functions called 'pygame'
# import pygame
 
# # Initialize the game engine
# pygame.init()
 
# # Define some colors
# BLACK = (0, 0, 0)
# WHITE = (255, 255, 255)
# BLUE = (0, 0, 255)
# GREEN = (0, 255, 0)
# RED = (255, 0, 0)
 
# PI = 3.141592653
 
# # Set the height and width of the screen
# size = (400, 500)
# screen = pygame.display.set_mode(size)
 
# pygame.display.set_caption("Rotate Text")
 
# # Loop until the user clicks the close button.
# done = False
# clock = pygame.time.Clock()
 
# text_rotate_degrees = 0
 
# # Loop as long as done == False
# while not done:
 
#     for event in pygame.event.get():  # User did something
#         if event.type == pygame.QUIT:  # If user clicked close
#             done = True  # Flag that we are done so we exit this loop
 
#     # All drawing code happens after the for loop and but
#     # inside the main while not done loop.
 
#     # Clear the screen and set the screen background
#     screen.fill(WHITE)
 
#     # Draw some borders
#     pygame.draw.line(screen, BLACK, [100,50], [200, 50])
#     pygame.draw.line(screen, BLACK, [100,50], [100, 150])
 
#     # Select the font to use, size, bold, italics
#     font = pygame.font.SysFont('Calibri', 25, True, False)
 
#     # Sideways text
#     text = font.render("Sideways text", True, BLACK)
#     text = pygame.transform.rotate(text, 90)
#     screen.blit(text, [0, 0])
 
#     # Sideways text
#     text = font.render("Upside down text", True, BLACK)
#     text = pygame.transform.rotate(text, 180)
#     screen.blit(text, [30, 0])
 
#     # Flipped text
#     text = font.render("Flipped text", True, BLACK)
#     text = pygame.transform.flip(text, False, True)
#     screen.blit(text, [30, 20])
 
#     # Animated rotation
#     text = font.render("Rotating text", True, BLACK)
#     text = pygame.transform.rotate(text, text_rotate_degrees)
#     text_rotate_degrees += 1
#     screen.blit(text, [100, 50])
 
#     # Go ahead and update the screen with what we've drawn.
#     # This MUST happen after all the other drawing commands.
#     pygame.display.flip()
 
#     # This limits the while loop to a max of 60 times per second.
#     # Leave this out and we will use all CPU we can.
#     clock.tick(60)
 
# # Be IDLE friendly
# pygame.quit()


"""
Sample Python/Pygame Programs
Simpson College Computer Science
http://programarcadegames.com/
http://simpson.edu/computer-science/
 
From:
http://programarcadegames.com/python_examples/f.php?file=maze_runner.py
 
Explanation video: http://youtu.be/5-SbFanyUkQ
 
Part of a series:
http://programarcadegames.com/python_examples/f.php?file=move_with_walls_example.py
http://programarcadegames.com/python_examples/f.php?file=maze_runner.py
http://programarcadegames.com/python_examples/f.php?file=platform_jumper.py
http://programarcadegames.com/python_examples/f.php?file=platform_scroller.py
http://programarcadegames.com/python_examples/f.php?file=platform_moving.py
http://programarcadegames.com/python_examples/sprite_sheets/
"""
import pygame
 
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
BLUE = (0, 0, 255)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
PURPLE = (255, 0, 255)
 
 
class Wall(pygame.sprite.Sprite):
    """This class represents the bar at the bottom that the player controls """
 
    def __init__(self, x, y, width, height, color):
        """ Constructor function """
 
        # Call the parent's constructor
        super().__init__()
 
        # Make a BLUE wall, of the size specified in the parameters
        self.image = pygame.Surface([width, height])
        self.image.fill(color)
 
        # Make our top-left corner the passed-in location.
        self.rect = self.image.get_rect()
        self.rect.y = y
        self.rect.x = x
 
 
class Player(pygame.sprite.Sprite):
    """ This class represents the bar at the bottom that the
    player controls """
 
    # Set speed vector
    change_x = 0
    change_y = 0
 
    def __init__(self, x, y):
        """ Constructor function """
 
        # Call the parent's constructor
        super().__init__()
 
        # Set height, width
        self.image = pygame.Surface([15, 15])
        self.image.fill(WHITE)
 
        # Make our top-left corner the passed-in location.
        self.rect = self.image.get_rect()
        self.rect.y = y
        self.rect.x = x
 
    def changespeed(self, x, y):
        """ Change the speed of the player. Called with a keypress. """
        self.change_x += x
        self.change_y += y
 
    def move(self, walls):
        """ Find a new position for the player """
 
        # Move left/right
        self.rect.x += self.change_x
 
        # Did this update cause us to hit a wall?
        block_hit_list = pygame.sprite.spritecollide(self, walls, False)
        for block in block_hit_list:
            # If we are moving right, set our right side to the left side of
            # the item we hit
            if self.change_x > 0:
                self.rect.right = block.rect.left
            else:
                # Otherwise if we are moving left, do the opposite.
                self.rect.left = block.rect.right
 
        # Move up/down
        self.rect.y += self.change_y
 
        # Check and see if we hit anything
        block_hit_list = pygame.sprite.spritecollide(self, walls, False)
        for block in block_hit_list:
 
            # Reset our position based on the top/bottom of the object.
            if self.change_y > 0:
                self.rect.bottom = block.rect.top
            else:
                self.rect.top = block.rect.bottom
 
 
class Room(object):
    """ Base class for all rooms. """
 
    # Each room has a list of walls, and of enemy sprites.
    wall_list = None
    enemy_sprites = None
 
    def __init__(self):
        """ Constructor, create our lists. """
        self.wall_list = pygame.sprite.Group()
        self.enemy_sprites = pygame.sprite.Group()
 
 
class Room1(Room):
    """This creates all the walls in room 1"""
    def __init__(self):
        super().__init__()
        # Make the walls. (x_pos, y_pos, width, height)
 
        # This is a list of walls. Each is in the form [x, y, width, height]
        walls = [[0, 0, 20, 250, WHITE],
                 [0, 350, 20, 250, WHITE],
                 [780, 0, 20, 250, WHITE],
                 [780, 350, 20, 250, WHITE],
                 [20, 0, 760, 20, WHITE],
                 [20, 580, 760, 20, WHITE],
                 [390, 50, 20, 500, BLUE]
                ]
 
        # Loop through the list. Create the wall, add it to the list
        for item in walls:
            wall = Wall(item[0], item[1], item[2], item[3], item[4])
            self.wall_list.add(wall)
 
 
class Room2(Room):
    """This creates all the walls in room 2"""
    def __init__(self):
        super().__init__()
 
        walls = [[0, 0, 20, 250, RED],
                 [0, 350, 20, 250, RED],
                 [780, 0, 20, 250, RED],
                 [780, 350, 20, 250, RED],
                 [20, 0, 760, 20, RED],
                 [20, 580, 760, 20, RED],
                 [190, 50, 20, 500, GREEN],
                 [590, 50, 20, 500, GREEN]
                ]
 
        for item in walls:
            wall = Wall(item[0], item[1], item[2], item[3], item[4])
            self.wall_list.add(wall)
 
 
class Room3(Room):
    """This creates all the walls in room 3"""
    def __init__(self):
        super().__init__()
 
        walls = [[0, 0, 20, 250, PURPLE],
                 [0, 350, 20, 250, PURPLE],
                 [780, 0, 20, 250, PURPLE],
                 [780, 350, 20, 250, PURPLE],
                 [20, 0, 760, 20, PURPLE],
                 [20, 580, 760, 20, PURPLE]
                ]
 
        for item in walls:
            wall = Wall(item[0], item[1], item[2], item[3], item[4])
            self.wall_list.add(wall)
 
        for x in range(100, 800, 100):
            for y in range(50, 451, 300):
                wall = Wall(x, y, 20, 200, RED)
                self.wall_list.add(wall)
 
        for x in range(150, 700, 100):
            wall = Wall(x, 200, 20, 200, WHITE)
            self.wall_list.add(wall)
 
 
def main():
    """ Main Program """
 
    # Call this function so the Pygame library can initialize itself
    pygame.init()
 
    # Create an 800x600 sized screen
    screen = pygame.display.set_mode([800, 600])
 
    # Set the title of the window
    pygame.display.set_caption('Maze Runner')
 
    # Create the player paddle object
    player = Player(50, 50)
    movingsprites = pygame.sprite.Group()
    movingsprites.add(player)
 
    rooms = []
 
    room = Room1()
    rooms.append(room)
 
    room = Room2()
    rooms.append(room)
 
    room = Room3()
    rooms.append(room)
 
    current_room_no = 0
    current_room = rooms[current_room_no]
 
    clock = pygame.time.Clock()
 
    done = False
 
    while not done:
 
        # --- Event Processing ---
 
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                done = True
 
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT:
                    player.changespeed(-5, 0)
                if event.key == pygame.K_RIGHT:
                    player.changespeed(5, 0)
                if event.key == pygame.K_UP:
                    player.changespeed(0, -5)
                if event.key == pygame.K_DOWN:
                    player.changespeed(0, 5)
 
            if event.type == pygame.KEYUP:
                if event.key == pygame.K_LEFT:
                    player.changespeed(5, 0)
                if event.key == pygame.K_RIGHT:
                    player.changespeed(-5, 0)
                if event.key == pygame.K_UP:
                    player.changespeed(0, 5)
                if event.key == pygame.K_DOWN:
                    player.changespeed(0, -5)
 
        # --- Game Logic ---
 
        player.move(current_room.wall_list)
 
        if player.rect.x < -15:
            if current_room_no == 0:
                current_room_no = 2
                current_room = rooms[current_room_no]
                player.rect.x = 790
            elif current_room_no == 2:
                current_room_no = 1
                current_room = rooms[current_room_no]
                player.rect.x = 790
            else:
                current_room_no = 0
                current_room = rooms[current_room_no]
                player.rect.x = 790
 
        if player.rect.x > 801:
            if current_room_no == 0:
                current_room_no = 1
                current_room = rooms[current_room_no]
                player.rect.x = 0
            elif current_room_no == 1:
                current_room_no = 2
                current_room = rooms[current_room_no]
                player.rect.x = 0
            else:
                current_room_no = 0
                current_room = rooms[current_room_no]
                player.rect.x = 0
 
        # --- Drawing ---
        screen.fill(BLACK)
 
        movingsprites.draw(screen)
        current_room.wall_list.draw(screen)
 
        pygame.display.flip()
 
        clock.tick(60)
 
    pygame.quit()
 
if __name__ == "__main__":
    main()
