Untitled

 avatar
unknown
plain_text
2 years ago
8.7 kB
1
Indexable
import pygame
import random

# Game initialization
pygame.init()

# Game window dimensions
WINDOW_WIDTH = 800
WINDOW_HEIGHT = 600

# Player dimensions
PLAYER_WIDTH = 32
PLAYER_HEIGHT = 32
PLAYER_SPEED = 5

# Dragon dimensions
DRAGON_WIDTH = 64
DRAGON_HEIGHT = 64
DRAGON_SPEED = 5
DRAGON_FIRE_COOLDOWN = 60
DRAGON_FIRE_DAMAGE = 2

# Chest dimensions
CHEST_WIDTH = 64
CHEST_HEIGHT = 64

# Hotbar dimensions
HOTBAR_WIDTH = 500
HOTBAR_HEIGHT = 50
SLOT_SIZE = 50
SLOT_PADDING = 5

# Health bar dimensions
HEALTHBAR_WIDTH = 500
HEALTHBAR_HEIGHT = 50
HEART_SIZE = 30
HEART_PADDING = 5

# Crosshair dimensions
CROSSHAIR_SIZE = 20
CROSSHAIR_COLOR = (255, 0, 0)

# Time settings
DAY_LENGTH = 360  # Time in seconds for a full day
NIGHT_LENGTH = 60  # Time in seconds for a full night
SLEEP_DURATION = 5  # Time in seconds for sleeping

# Create the game window
window = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
pygame.display.set_caption("Dragon World")

# Load images
background_day_image = pygame.image.load("day_background.jpg")
background_night_image = pygame.image.load("night_background.jpg")
water_image = pygame.image.load("water.png")
sand_image = pygame.image.load("sand.png")
palm_tree_image = pygame.image.load("palm_tree.png")
chest_image = pygame.image.load("chest.png")
heart_image = pygame.image.load("heart.png")
pickaxe_image = pygame.image.load("pickaxe.png")
bed_image = pygame.image.load("bed.png")

# Player object
player_x = WINDOW_WIDTH // 2
player_y = WINDOW_HEIGHT // 2

# Dragon object
dragon_x = random.randint(0, WINDOW_WIDTH - DRAGON_WIDTH)
dragon_y = random.randint(0, WINDOW_HEIGHT - DRAGON_HEIGHT)
dragon_cooldown = 0
dragon_following = False
dragon_befriended = False

# Chest object
chest_x = random.randint(100, WINDOW_WIDTH - CHEST_WIDTH - 100)
chest_y = random.randint(100, WINDOW_HEIGHT - CHEST_HEIGHT - 100)
is_chest_open = False

# Hotbar
hotbar_x = (WINDOW_WIDTH - HOTBAR_WIDTH) // 2
hotbar_y = WINDOW_HEIGHT - HOTBAR_HEIGHT - 10
inventory_slots = [None] * 10
current_slot = 0
holding_pickaxe = False

# Health bar
healthbar_x = (WINDOW_WIDTH - HEALTHBAR_WIDTH) // 2
healthbar_y = hotbar_y - HEALTHBAR_HEIGHT - 10
max_health = 10
current_health = max_health

# Crosshair position
crosshair_x = WINDOW_WIDTH // 2 - CROSSHAIR_SIZE // 2
crosshair_y = WINDOW_HEIGHT // 2 - CROSSHAIR_SIZE // 2

# Game state
game_state = "main_menu"

# Time variables
time_elapsed = 0
is_daytime = True
is_sleeping = False
sleep_start_time = 0

# Game loop
running = True
clock = pygame.time.Clock()

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

        # Check mouse click
        if event.type == pygame.MOUSEBUTTONDOWN:
            if game_state == "main_menu":
                mouse_pos = pygame.mouse.get_pos()
                if play_button_rect.collidepoint(mouse_pos):
                    game_state = "playing"

            elif game_state == "playing" and is_chest_open:
                mouse_pos = pygame.mouse.get_pos()
                if inventory_slot_rect.collidepoint(mouse_pos):
                    # Perform item placement logic here
                    if holding_pickaxe:
                        inventory_slots[current_slot] = "pickaxe"
                        holding_pickaxe = False
                    else:
                        inventory_slots[current_slot] = "bed"

            elif game_state == "playing" and not is_chest_open:
                mouse_pos = pygame.mouse.get_pos()
                if sand_rect.collidepoint(mouse_pos):
                    if holding_pickaxe and current_slot == 9:
                        bed_x = crosshair_x
                        bed_y = crosshair_y
                        holding_pickaxe = False
                        inventory_slots[current_slot] = None

        # Check for 'E' key press
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_e:
                if game_state == "playing":
                    if player_rect.colliderect(chest_rect):
                        is_chest_open = True

            # Check for number key press (1-9)
            elif event.key in [pygame.K_1, pygame.K_2, pygame.K_3, pygame.K_4, pygame.K_5, pygame.K_6,
                               pygame.K_7, pygame.K_8, pygame.K_9]:
                if game_state == "playing":
                    current_slot = event.key - pygame.K_1  # Map key to slot index

            # Check for 'x' key press
            elif event.key == pygame.K_x:
                if game_state == "playing":
                    holding_pickaxe = not holding_pickaxe

        # Check for right mouse button press
        if event.type == pygame.MOUSEBUTTONDOWN:
            if event.button == 3:  # Right mouse button
                if game_state == "playing":
                    pygame.mouse.set_visible(False)  # Hide mouse cursor

        # Check for right mouse button release
        if event.type == pygame.MOUSEBUTTONUP:
            if event.button == 3:  # Right mouse button
                if game_state == "playing":
                    pygame.mouse.set_visible(True)  # Show mouse cursor

    # Player movement controls
    keys = pygame.key.get_pressed()

    if game_state == "playing":
        if keys[pygame.K_w]:
            player_y -= PLAYER_SPEED
        if keys[pygame.K_s]:
            player_y += PLAYER_SPEED
        if keys[pygame.K_a]:
            player_x -= PLAYER_SPEED
        if keys[pygame.K_d]:
            player_x += PLAYER_SPEED

    # Dragon behavior
    if dragon_following:
        # Dragon fire attack
        if dragon_cooldown <= 0:
            dragon_cooldown = DRAGON_FIRE_COOLDOWN
            # Perform fire attack logic here
            current_health -= DRAGON_FIRE_DAMAGE

    # Update game logic
    dragon_cooldown -= 1

    # Update time
    time_elapsed += 1

    # Day and night cycle
    if is_daytime and time_elapsed >= DAY_LENGTH:
        is_daytime = False
        time_elapsed = 0
    elif not is_daytime and time_elapsed >= NIGHT_LENGTH:
        is_daytime = True
        time_elapsed = 0

    # Sleeping mechanic
    if is_sleeping:
        if pygame.time.get_ticks() - sleep_start_time >= SLEEP_DURATION * 1000:
            is_daytime = True
            is_sleeping = False

    # Render graphics
    if is_daytime:
        window.blit(background_day_image, (0, 0))  # Draw day background image
    else:
        window.blit(background_night_image, (0, 0))  # Draw night background image

    # Draw water
    for x in range(0, WINDOW_WIDTH, water_image.get_width()):
        for y in range(0, WINDOW_HEIGHT, water_image.get_height()):
            window.blit(water_image, (x, y))

    # Draw sand
    for x in range(0, WINDOW_WIDTH, sand_image.get_width()):
        for y in range(0, WINDOW_HEIGHT, sand_image.get_height()):
            window.blit(sand_image, (x, y))

    # Draw palm trees
    palm_tree_width = palm_tree_image.get_width()
    palm_tree_height = palm_tree_image.get_height()
    window.blit(palm_tree_image, (100, 300))
    window.blit(palm_tree_image, (400, 200))

    # Draw player
    player_rect = pygame.Rect(player_x, player_y, PLAYER_WIDTH, PLAYER_HEIGHT)
    pygame.draw.rect(window, (255, 0, 0), player_rect)

    # Draw dragon
    dragon_rect = pygame.Rect(dragon_x, dragon_y, DRAGON_WIDTH, DRAGON_HEIGHT)
    pygame.draw.rect(window, (0, 255, 0), dragon_rect)

    # Draw chest
    chest_rect = pygame.Rect(chest_x, chest_y, CHEST_WIDTH, CHEST_HEIGHT)
    window.blit(chest_image, (chest_x, chest_y))

    # Draw hotbar
    pygame.draw.rect(window, (0, 0, 0), (hotbar_x, hotbar_y, HOTBAR_WIDTH, HOTBAR_HEIGHT))
    for i in range(len(inventory_slots)):
        slot_x = hotbar_x + i * (SLOT_SIZE + SLOT_PADDING)
        slot_y = hotbar_y + SLOT_PADDING
        slot_rect = pygame.Rect(slot_x, slot_y, SLOT_SIZE, SLOT_SIZE)
        pygame.draw.rect(window, (255, 255, 255), slot_rect)
        if inventory_slots[i] == "pickaxe":
            window.blit(pickaxe_image, (slot_x, slot_y))
        elif inventory_slots[i] == "bed":
            window.blit(bed_image, (slot_x, slot_y))

    # Draw health bar
    pygame.draw.rect(window, (0, 0, 0), (healthbar_x, healthbar_y, HEALTHBAR_WIDTH, HEALTHBAR_HEIGHT))
    for i in range(current_health):
        heart_x = healthbar_x + i * (HEART_SIZE + HEART_PADDING)
        heart_y = healthbar_y + HEART_PADDING
        window.blit(heart_image, (heart_x, heart_y))

    # Draw crosshair
    crosshair_rect = pygame.Rect(crosshair_x, crosshair_y, CROSSHAIR_SIZE, CROSSHAIR_SIZE)
    pygame.draw.rect(window, CROSSHAIR_COLOR, crosshair_rect)

    pygame.display.flip()
    clock.tick(60)

# Quit the game
pygame.quit()
Editor is loading...