#incase to manipulate visit www.pygame.org to work and design on games
import sys
import random
import pygame

pygame.init()

WIDTH = 800
HEIGHT = 600

RED = (255,0,0)
BLUE = (0,0,255)
YELLOW = (255,255,0)

BACKGROUND_COLOR = (11,238,207)
Clock = pygame.time.Clock()

player_size = 50
player_pos = [WIDTH/2,HEIGHT-2*player_size]

enemy_size = 50
enemy_pos = [random.randint(0,WIDTH-enemy_size),0]

SPEED = 10
screen = pygame.display.set_mode((WIDTH,HEIGHT))

game_over = False
enemy_list = [enemy_pos]

score = 0
myFont = pygame.font.SysFont("monospace", 30)

def difficulty_level(score, SPEED):
    if score < 20:
        SPEED = 5
    elif score < 50:
        SPEED = 8
    elif score < 80:
        SPEED = 10
    elif score < 100:
        SPEED = 15
    elif score < 200:
        SPEED = 20
    else:
        SPEED = 25
    return SPEED

def drop_enemies(enemy_list):
    delay = random.random()
    if len(enemy_list) < 10 and delay < 0.1:
        x_pos = random.randint(0,WIDTH-enemy_size)
        y_pos = 0
        enemy_list.append([x_pos,y_pos])

def draw_enemies(enemy_list):
    for enemy_pos in enemy_list:
        pygame.draw.rect(screen, BLUE, (enemy_pos[0], enemy_pos[1], enemy_size, enemy_size))

def update_enemy_positions(enemy_list,score):
    for idx,enemy_pos in enumerate(enemy_list):
      if enemy_pos [1] >= 0 and enemy_pos [1] < HEIGHT:
           enemy_pos[1] += SPEED
      else:
          enemy_list.pop(idx)
          score+=1
    return score
def detect_collsion(player_pos,enemy_pos):
    p_x = player_pos[0]
    p_y = player_pos[1]

    e_x = enemy_pos[0]
    e_y = enemy_pos[1]

    if (e_x >= p_x and e_x < (p_x + player_size)) or (p_x >= e_x and p_x < (e_x + enemy_size)):
        if (p_y >= e_y and p_y < (e_y + enemy_size)) or (e_y >= p_y and e_y < (p_y + player_size)):
          return True
    return False

def collision_check(enemy_list,player_pos):
    for enemy_pos in enemy_list:
        if detect_collsion(player_pos, enemy_pos):
            return True
    return False

while not game_over:

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
        if event.type == pygame.KEYDOWN:
            x = player_pos[0]
            y = player_pos[1]
            if event.key == pygame.K_LEFT:
                x-=player_size
            elif event.key == pygame.K_RIGHT:
                x+=player_size

            player_pos = [x,y]
    screen.fill(BACKGROUND_COLOR)
    #if enemy_pos [1] >= 0 and enemy_pos [1] < HEIGHT:
        #enemy_pos[1] += SPEED
    #else:
        #enemy_pos[0] = random.randint(0,WIDTH-enemy_size)
        #enemy_pos[1] = 0

    #if detect_collsion(player_pos,enemy_pos):
        #game_over = True
        #break

    drop_enemies(enemy_list)
    score = update_enemy_positions(enemy_list, score)
    SPEED = difficulty_level(score, SPEED)
    print(score)

    text = "Score:" + str(score)
    label = myFont.render(text, 1, YELLOW)
    screen.blit(label, (WIDTH - 200, HEIGHT - 40))

    if collision_check(enemy_list, player_pos):
        game_over = True
        break

    draw_enemies(enemy_list)


    pygame.draw.rect(screen, RED, (player_pos[0], player_pos[1], player_size, player_size))
    Clock.tick(20)
    pygame.display.update()



#incase to manipulate visit www.pygame.org to work and design on games
import sys
import random
import pygame

pygame.init()

WIDTH = 800
HEIGHT = 600

RED = (255,0,0)
BLUE = (0,0,255)
YELLOW = (255,255,0)

BACKGROUND_COLOR = (11,238,207)
Clock = pygame.time.Clock()

player_size = 50
player_pos = [WIDTH/2,HEIGHT-2*player_size]

enemy_size = 50
enemy_pos = [random.randint(0,WIDTH-enemy_size),0]

SPEED = 10
screen = pygame.display.set_mode((WIDTH,HEIGHT))

game_over = False
enemy_list = [enemy_pos]

score = 0
myFont = pygame.font.SysFont("monospace", 30)

def difficulty_level(score, SPEED):
    if score < 20:
        SPEED = 5
    elif score < 50:
        SPEED = 8
    elif score < 80:
        SPEED = 10
    elif score < 100:
        SPEED = 15
    elif score < 200:
        SPEED = 20
    else:
        SPEED = 25
    return SPEED

def drop_enemies(enemy_list):
    delay = random.random()
    if len(enemy_list) < 10 and delay < 0.1:
        x_pos = random.randint(0,WIDTH-enemy_size)
        y_pos = 0
        enemy_list.append([x_pos,y_pos])

def draw_enemies(enemy_list):
    for enemy_pos in enemy_list:
        pygame.draw.rect(screen, BLUE, (enemy_pos[0], enemy_pos[1], enemy_size, enemy_size))

def update_enemy_positions(enemy_list,score):
    for idx,enemy_pos in enumerate(enemy_list):
      if enemy_pos [1] >= 0 and enemy_pos [1] < HEIGHT:
           enemy_pos[1] += SPEED
      else:
          enemy_list.pop(idx)
          score+=1
    return score
def detect_collsion(player_pos,enemy_pos):
    p_x = player_pos[0]
    p_y = player_pos[1]

    e_x = enemy_pos[0]
    e_y = enemy_pos[1]

    if (e_x >= p_x and e_x < (p_x + player_size)) or (p_x >= e_x and p_x < (e_x + enemy_size)):
        if (p_y >= e_y and p_y < (e_y + enemy_size)) or (e_y >= p_y and e_y < (p_y + player_size)):
          return True
    return False

def collision_check(enemy_list,player_pos):
    for enemy_pos in enemy_list:
        if detect_collsion(player_pos, enemy_pos):
            return True
    return False

while not game_over:

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
        if event.type == pygame.KEYDOWN:
            x = player_pos[0]
            y = player_pos[1]
            if event.key == pygame.K_LEFT:
                x-=player_size
            elif event.key == pygame.K_RIGHT:
                x+=player_size

            player_pos = [x,y]
    screen.fill(BACKGROUND_COLOR)
    #if enemy_pos [1] >= 0 and enemy_pos [1] < HEIGHT:
        #enemy_pos[1] += SPEED
    #else:
        #enemy_pos[0] = random.randint(0,WIDTH-enemy_size)
        #enemy_pos[1] = 0

    #if detect_collsion(player_pos,enemy_pos):
        #game_over = True
        #break

    drop_enemies(enemy_list)
    score = update_enemy_positions(enemy_list, score)
    SPEED = difficulty_level(score, SPEED)
    print(score)

    text = "Score:" + str(score)
    label = myFont.render(text, 1, YELLOW)
    screen.blit(label, (WIDTH - 200, HEIGHT - 40))

    if collision_check(enemy_list, player_pos):
        game_over = True
        break

    draw_enemies(enemy_list)


    pygame.draw.rect(screen, RED, (player_pos[0], player_pos[1], player_size, player_size))
    Clock.tick(20)
    pygame.display.update()



#incase to manipulate visit www.pygame.org to work and design on games #if enemy_pos [1] >= 0 and enemy_pos [1] < HEIGHT: #enemy_pos[1] += SPEED #else: #enemy_pos[0] = random.randint(0,WIDTH-enemy_size) #enemy_pos[1] = 0 #if detect_collsion(player_pos,enemy_pos): #game_over = True #break