Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
3.6 kB
4
Indexable
Never
import pygame
import sys
import random

# Ekran boyutu
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600

# Renkler
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)

# Karakter sınıfı
class Character(pygame.sprite.Sprite):
    def __init__(self, x, y, image_file):
        super().__init__()
        self.image = pygame.image.load(image_file).convert_alpha()
        self.rect = self.image.get_rect()
        self.rect.x = x
        self.rect.y = y

    def update(self):
        self.rect.x += self.speed_x
        self.rect.y += self.speed_y

        if self.rect.x < 0 or self.rect.x > SCREEN_WIDTH - self.rect.width:
            self.speed_x = -self.speed_x
        if self.rect.y < 0 or self.rect.y > SCREEN_HEIGHT - self.rect.height:
            self.speed_y = -self.speed_y

# Düşman sınıfı
class Enemy(Character):
    def __init__(self, x, y, image_file):
        super().__init__(x, y, image_file)
        self.speed_x = random.choice([-2, 2])
        self.speed_y = random.choice([-2, 2])

# Oyuncu sınıfı
class Player(Character):
    def __init__(self, x, y, image_file):
        super().__init__(x, y, image_file)
        self.speed_x = 0
        self.speed_y = 0

    def update(self):
        keys = pygame.key.get_pressed()
        if keys[pygame.K_w]:
            self.speed_y = -2
        elif keys[pygame.K_s]:
            self.speed_y = 2
        else:
            self.speed_y = 0

        if keys[pygame.K_a]:
            self.speed_x = -2
        elif keys[pygame.K_d]:
            self.speed_x = 2
        else:
            self.speed_x = 0

        super().update()

# Oyun başlat
pygame.init()
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("Basit RPG Oyunu")

# Karakterler
player_images = ["player1.png", "player2.png", "player3.png"]
enemy_image = "enemy.png"

player = None

running = True
game_over = False
enemy = Enemy(random.randint(0, SCREEN_WIDTH - 50), random.randint(0, SCREEN_HEIGHT - 50), enemy_image)

while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    if not game_over:
        if player is None:
            screen.fill(WHITE)

            for i, image in enumerate(player_images):
                img = pygame.image.load(image).convert_alpha()
                screen.blit(img, (i * (SCREEN_WIDTH // len(player_images)), SCREEN_HEIGHT // 2))

            pygame.display.flip()

            mouse_clicked = pygame.mouse.get_pressed()
            if mouse_clicked[0]:
                x, y = pygame.mouse.get_pos()
                for i in range(len(player_images)):
                    if i * (SCREEN_WIDTH // len(player_images)) < x < (i + 1) * (SCREEN_WIDTH // len(player_images)):
                        player = Player(SCREEN_WIDTH // 2, SCREEN_HEIGHT // 2, player_images[i])
                        break

        else:
            screen.fill(WHITE)

            all_sprites = pygame.sprite.Group()
            all_sprites.add(player)
            all_sprites.add(enemy)

            all_sprites.update()
            all_sprites.draw(screen)

            pygame.display.flip()

            if pygame.sprite.collide_rect(player, enemy):
                screen.fill(WHITE)
                font = pygame.font.Font(None, 36)
                text = font.render("Öldün!", True, BLACK)
                screen.blit(text, (SCREEN_WIDTH // 2 - text.get_width() // 2, SCREEN_HEIGHT // 2 - text.get_height() // 2))
                pygame.display.flip()
                pygame.time.wait(2000)
                game_over = True
                break

pygame.quit()
sys.exit()