Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
7.4 kB
1
Indexable
Never
import pygame
import random
import math
import os

pygame.init()

width = 800
height = 600

black = (0, 0, 0)
white = (255, 255, 255)
coinrenk = (255, 255, 255)
red = (255, 0, 0)

skorrenk = (122, 211, 214)
seviyerenk = (255, 255, 0)

win = pygame.display.set_mode((width, height))
pygame.display.set_caption("Rain of Bullets")

clock = pygame.time.Clock()

player_size = 30
player_pos = [width / 2, height - 2 * player_size]

bullet_radius = 5
bullet_speed = 12
bullet_color = red
bullet_list = []

enemy_size = 20
enemy_pos = [random.randint(0, width - enemy_size), 0]
enemy_list = [enemy_pos]
enemy_colors = [(random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))]

enemy_speed = 1.0
max_enemy_speed = 2.0

game_over = False

score = 0
level = 1
coin = 0

font = pygame.font.SysFont('Roboto-Regular.ttf', 30)

menu_options = ["Oyna", "Karakter", "Ayarlar"]
selected_option = 0
show_menu = True

menu_font = pygame.font.SysFont('Roboto-Regular.ttf', 36)
button_width = 200
button_height = 50
button_margin = 20

def show_score_level_coin(score, level, coin):
    score_text = font.render("Puan: " + str(score), True, skorrenk)
    level_text = font.render("Seviye: " + str(level), True, seviyerenk)
    coin_text = font.render("Coin: " + str(coin), True, coinrenk)
    win.blit(score_text, (10, 10))
    win.blit(level_text, (10, 40))
    win.blit(coin_text, (10, 70))

def show_coin_text():
    global coin_text

    if show_menu:
        coin_text = font.render("Coin: " + str(coin), True, coinrenk)
    else:
        coin_text = None

def draw_menu_buttons():
    button_x = (width - button_width) // 2
    button_y = (height - len(menu_options) * (button_height + button_margin)) // 2

    for i, option in enumerate(menu_options):
        if i == selected_option:
            color = red
        else:
            color = white

        pygame.draw.rect(win, color, (button_x, button_y, button_width, button_height))
        text = menu_font.render(option, True, black)
        text_rect = text.get_rect(center=(button_x + button_width / 2, button_y + button_height / 2))
        win.blit(text, text_rect)

        # Add coin text to the menu buttons
        if i == 0:
            win.blit(coin_text, coin_text_rect)

        button_y += button_height + button_margin

while not game_over:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            game_over = True

        if event.type == pygame.KEYDOWN:
         if event.type == pygame.KEYDOWN:
            if show_menu:
                if event.key == pygame.K_UP:
                    selected_option = (selected_option - 1) % len(menu_options)
                elif event.key == pygame.K_DOWN:
                    selected_option = (selected_option + 1) % len(menu_options)
                elif event.key == pygame.K_RETURN:
                    if selected_option == 0:
                        show_menu = False
                    elif selected_option == 1:
                        pass
                    elif selected_option == 2:
                        pass
            else:
                if event.key == pygame.K_ESCAPE:
                    show_menu = True

        if event.type == pygame.MOUSEBUTTONDOWN:
            if show_menu:
                mouse_pos = pygame.mouse.get_pos()
                button_x = (width - button_width) // 2
                button_y = (height - len(menu_options) * (button_height + button_margin)) // 2

                for i in range(len(menu_options)):
                    if button_x <= mouse_pos[0] <= button_x + button_width and button_y <= mouse_pos[1] <= button_y + button_height:
                        selected_option = i
                        if selected_option == 0:
                            show_menu = False
                        elif selected_option == 1:
                            pass
                        elif selected_option == 2:
                            pass

                    button_y += button_height + button_margin

    if show_menu:
        win.fill(black)
        draw_menu_buttons()
        pygame.display.update()
        continue

    if not paused:
        if pygame.mouse.get_pressed()[0]:
            bullet_delay += 1
            if bullet_delay >= bullet_delay_max:
                cross_pos = pygame.mouse.get_pos()
                bullet_pos = [player_pos[0] + player_size / 2, player_pos[1] + player_size / 2]
                angle = calculate_angle(player_pos, cross_pos)
                bullet_vel = [bullet_speed * math.cos(angle), bullet_speed * math.sin(angle)]
                bullet_list.append([bullet_pos[0], bullet_pos[1], bullet_vel[0], bullet_vel[1]])
                bullet_delay = 0

        win.fill(black)

        collided_enemies = []
        for enemy_idx, enemy_pos in enumerate(enemy_list):
            for bullet in bullet_list:
                if detect_collision(enemy_pos, (bullet[0], bullet[1])):
                    collided_enemies.append(enemy_idx)
                    bullet_list.remove(bullet)
                    score += 1
                    enemy_speed += 0.020
                    if enemy_speed > max_enemy_speed:
                        enemy_speed = max_enemy_speed
                    enemy_colors[enemy_idx] = (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))
                    hit_sound.play()

        for enemy_idx in sorted(collided_enemies, reverse=True):
            enemy_list.pop(enemy_idx)
            enemy_colors.pop(enemy_idx)

        for idx, enemy_pos in enumerate(enemy_list):
            if enemy_pos[1] >= 0 and enemy_pos[1] < height:
                enemy_pos[1] += enemy_speed
            else:
                enemy_list.pop(idx)
                enemy_colors.pop(idx)
                score -= 1
                if score < 0:
                    score = 0

        if len(enemy_list) < 10:
            x_pos = random.randint(0, width - enemy_size)
            y_pos = 0
        if len(enemy_list) < 10:
            x_pos = random.randint(0, width - enemy_size)
            y_pos = 0
            enemy_list.append([x_pos, y_pos])
            enemy_colors.append((random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)))

        for bullet in bullet_list:
            bullet[0] += bullet[2]
            bullet[1] += bullet[3]

        if score < 10:
            level = 1
            enemy_speed = 1.0
            max_enemy_speed = 2.0
        elif score < 20:
            level = 2
            enemy_speed = 1.2
            max_enemy_speed = 2.3
        elif score < 30:
            level = 3
            enemy_speed = 1.5
            max_enemy_speed = 2.6
        elif score < 40:
            level = 4
            enemy_speed = 1.7
            max_enemy_speed = 3.0    
        else:
            level = 5
            enemy_speed = 2.0
            max_enemy_speed = 3.2

        win.fill(black)

        draw_enemies(enemy_list)
        show_score_level_coin(score, level, coin)
        draw_bullets(bullet_list)

        pygame.draw.rect(win, white, (player_pos[0], player_pos[1], player_size, player_size))

    clock.tick(60)

    pygame.display.update()

pygame.quit()