Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
5.8 kB
0
Indexable
Never
import pygame
import random
import math

pygame.init()

width = 800
height = 600

black = (0, 0, 0)
white = (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("Havadan Kare Vurma Oyunu")

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_speed = 1.0

game_over = False

score = 0
level = 1









class enemy(object):
    def __init__(self, x, y, width, height):
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        # Burada düşmana rastgele bir renk atıyoruz
        self.color = (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))

    def draw(self, win):
        # Burada düşmanı atanan renkle çiziyoruz
        pygame.draw.rect(win, self.color, (self.x, self.y, self.width, self.height))




# Düşman listesi oluştur
enemy_list = []
# Düşman sayısı belirle
enemy_count = 10
# Her düşman için bir nesne yarat ve listeye ekle
for i in range(enemy_count):
    # Düşmanın konumunu ve boyutunu rastgele belirle
    x = random.randint(0, display_width - enemy_width)
    y = random.randint(0, display_height - enemy_height)
    width = enemy_width
    height = enemy_height
    # Düşman nesnesini yarat
    enemy_obj = enemy(x, y, width, height)
    # Listeye ekle
    enemy_list.append(enemy_obj)













font = pygame.font.Font('freesansbold.ttf', 20)

def show_score_level(score, level):
    score_text = font.render("Puan: " + str(score), True, skorrenk)
    level_text = font.render("Seviye: " + str(level), True, seviyerenk)
    win.blit(score_text, (10, 10))
    win.blit(level_text, (10, 40))

# Düşmanları çiz
def draw_enemies(enemy_list):
    for enemy_obj in enemy_list:
        # Her düşmanın draw metodunu çağır
        enemy_obj.draw(win)

def draw_bullets(bullet_list):
    for bullet in bullet_list:
        pygame.draw.circle(win, bullet_color, (bullet[0], bullet[1]), bullet_radius)

def detect_collision(enemy_pos, bullet):
    e_x = enemy_pos[0]
    e_y = enemy_pos[1]
    b_x = bullet[0]
    b_y = bullet[1]

    if (b_x >= e_x and b_x <= (e_x + enemy_size)) and (b_y >= e_y and b_y <= (e_y + enemy_size)):
        return True
    return False

def calculate_angle(player_pos, target_pos):
    delta_x = target_pos[0] - player_pos[0]
    delta_y = target_pos[1] - player_pos[1]
    angle = math.atan2(delta_y, delta_x)
    return angle

cross_pos = [0, 0]

bullet_delay = 0
bullet_delay_max = 15

paused = False

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.key == pygame.K_ESCAPE:
                paused = True

    if paused:
        keys = pygame.key.get_pressed()
        if keys[pygame.K_RETURN]:
            paused = False
        elif keys[pygame.K_q]:
            game_over = True

    if not paused:
        keys = pygame.key.get_pressed()
        if keys[pygame.K_LEFT]:
            player_pos[0] -= 5
        if keys[pygame.K_RIGHT]:
            player_pos[0] += 5
        if keys[pygame.K_UP]:
            player_pos[1] -= 5
        if keys[pygame.K_DOWN]:
            player_pos[1] += 5

        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.copy()
                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)

        # Düşmanları hareket ettir
        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)

        # Yeni düşman ekleme
        if len(enemy_list) < 10:
            x_pos = random.randint(0, width-enemy_size)
            y_pos = 0
            enemy_list.append([x_pos, y_pos])

        # Mermileri hareket ettir
        for bullet in bullet_list:
            bullet[0] += bullet[2]
            bullet[1] += bullet[3]

        # Mermi - düşman çarpışmasını kontrol et
        collided_enemies = []
        for enemy_pos in enemy_list:
            for bullet in bullet_list:
                if detect_collision(enemy_pos, (bullet[0], bullet[1])):
                    collided_enemies.append(enemy_pos)
                    bullet_list.remove(bullet)
                    score += 1
                    enemy_speed += 0.020

        # Çarpışan düşmanları kaldır
        for enemy_pos in collided_enemies:
            enemy_list.remove(enemy_pos)

        if score < 10:
            level = 1
        elif score < 20:
            level = 2
        elif score < 30:
            level = 3
        else:
            level = 4

        draw_enemies(enemy_list)
        show_score_level(score, level)
        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()