| import pygame
|
| import sys
|
| import random
|
|
|
|
|
| pygame.init()
|
|
|
|
|
| WIDTH, HEIGHT = 854, 480
|
| screen = pygame.display.set_mode((WIDTH, HEIGHT))
|
| pygame.display.set_caption("Rcrafty 1.0")
|
|
|
|
|
| WHITE = (255, 255, 255)
|
| BLACK = (0, 0, 0)
|
| GRAY = (128, 128, 128)
|
| BROWN = (139, 69, 19)
|
| BLUE = (0, 0, 255)
|
| STONE_COLOR = (100, 100, 100)
|
| MENU_BACKGROUND = (150, 75, 0)
|
|
|
|
|
| BLOCK_SIZE = 50
|
|
|
|
|
| PLAYER_SIZE = 50
|
| player_pos = [WIDTH // 2, HEIGHT // 2]
|
| player_speed = 200
|
|
|
|
|
| camera_offset = [0, 0]
|
|
|
|
|
| world_blocks = {}
|
| GROUND_HEIGHT = 10
|
| GROUND_DEPTH = 5
|
| WATER_LEVEL = GROUND_HEIGHT + 2
|
|
|
|
|
| font = pygame.font.Font(None, 36)
|
|
|
|
|
| running = True
|
| menu = True
|
| loading = False
|
|
|
|
|
| clock = pygame.time.Clock()
|
|
|
|
|
| inventory_slots = 5
|
| slot_width = 80
|
| slot_margin = 10
|
| inventory = {"dirt": 0, "stone": 0}
|
| selected_block = "dirt"
|
|
|
|
|
| def generate_world():
|
| global world_blocks
|
| for x in range(-50, 50):
|
| for y in range(GROUND_HEIGHT + GROUND_DEPTH * 10):
|
| if y > GROUND_HEIGHT:
|
| world_blocks[(x, y)] = BROWN
|
| elif y == GROUND_HEIGHT:
|
| if random.random() < 0.2:
|
| world_blocks[(x, y)] = STONE_COLOR
|
| else:
|
| world_blocks[(x, y)] = BROWN
|
| elif y == WATER_LEVEL:
|
| world_blocks[(x, y)] = BLUE
|
|
|
|
|
| def draw_world():
|
|
|
| for (block_x, block_y), color in world_blocks.items():
|
|
|
| draw_x = block_x * BLOCK_SIZE + camera_offset[0]
|
| draw_y = block_y * BLOCK_SIZE + camera_offset[1]
|
| pygame.draw.rect(screen, color, (draw_x, draw_y, BLOCK_SIZE, BLOCK_SIZE))
|
| pygame.draw.rect(screen, BLACK, (draw_x, draw_y, BLOCK_SIZE, BLOCK_SIZE), 1)
|
|
|
|
|
| def draw_player():
|
|
|
| body_rect = pygame.Rect(player_pos[0] - PLAYER_SIZE // 2, player_pos[1] - PLAYER_SIZE // 4, PLAYER_SIZE, PLAYER_SIZE)
|
| pygame.draw.rect(screen, GRAY, body_rect)
|
|
|
| head_center = (player_pos[0], player_pos[1] - PLAYER_SIZE // 2)
|
| pygame.draw.circle(screen, GRAY, head_center, PLAYER_SIZE // 4)
|
|
|
|
|
| def draw_inventory():
|
| inventory_width = inventory_slots * slot_width + (inventory_slots - 1) * slot_margin
|
| start_x = (WIDTH - inventory_width) // 2
|
| y = HEIGHT - 100
|
| for i, (block_type, count) in enumerate(inventory.items()):
|
| slot_x = start_x + i * (slot_width + slot_margin)
|
| pygame.draw.rect(screen, WHITE, (slot_x, y, slot_width, slot_width), 2)
|
| block_color = BROWN if block_type == "dirt" else STONE_COLOR
|
| pygame.draw.rect(screen, block_color, (slot_x + 5, y + 5, slot_width - 10, slot_width - 10))
|
| if count > 0:
|
| text = font.render(str(count), True, BLACK)
|
| screen.blit(text, (slot_x + 5, y + 5))
|
|
|
|
|
| def add_to_inventory(block_type):
|
| if block_type == BROWN:
|
| inventory["dirt"] += 1
|
| elif block_type == STONE_COLOR:
|
| inventory["stone"] += 1
|
|
|
|
|
| def place_block(block_type, pos):
|
| if block_type == "dirt" and inventory["dirt"] > 0:
|
| world_blocks[pos] = BROWN
|
| inventory["dirt"] -= 1
|
| elif block_type == "stone" and inventory["stone"] > 0:
|
| world_blocks[pos] = STONE_COLOR
|
| inventory["stone"] -= 1
|
|
|
|
|
|
|
| def check_collision(player_rect):
|
|
|
| for (block_x, block_y), color in world_blocks.items():
|
| block_rect = pygame.Rect(block_x * BLOCK_SIZE + camera_offset[0], block_y * BLOCK_SIZE + camera_offset[1], BLOCK_SIZE, BLOCK_SIZE)
|
| if player_rect.colliderect(block_rect):
|
| return True
|
| return False
|
|
|
|
|
| while running:
|
| dt = clock.tick(1440) / 1000.0
|
|
|
|
|
| for event in pygame.event.get():
|
| if event.type == pygame.QUIT:
|
| running = False
|
| if event.type == pygame.MOUSEBUTTONDOWN:
|
| if menu:
|
| menu = False
|
| loading = True
|
| else:
|
|
|
| mouse_pos = pygame.mouse.get_pos()
|
| world_x = (mouse_pos[0] - camera_offset[0]) // BLOCK_SIZE
|
| world_y = (mouse_pos[1] - camera_offset[1]) // BLOCK_SIZE
|
| block_coords = (world_x, world_y)
|
| if event.button == 1:
|
| if block_coords in world_blocks:
|
| block_type = world_blocks.pop(block_coords)
|
| add_to_inventory(block_type)
|
| elif event.button == 3:
|
| place_block(selected_block, block_coords)
|
|
|
| if event.type == pygame.KEYDOWN:
|
| if event.key == pygame.K_1:
|
| selected_block = "dirt"
|
| elif event.key == pygame.K_2:
|
| selected_block = "stone"
|
|
|
|
|
| keys = pygame.key.get_pressed()
|
| if not menu and not loading:
|
| new_x = player_pos[0]
|
| new_y = player_pos[1]
|
|
|
| if keys[pygame.K_w]:
|
| new_y -= player_speed * dt
|
| if keys[pygame.K_s]:
|
| new_y += player_speed * dt
|
| if keys[pygame.K_a]:
|
| new_x -= player_speed * dt
|
| if keys[pygame.K_d]:
|
| new_x += player_speed * dt
|
|
|
|
|
| player_rect = pygame.Rect(new_x - PLAYER_SIZE // 2, new_y - PLAYER_SIZE // 2, PLAYER_SIZE, PLAYER_SIZE)
|
|
|
|
|
| if not check_collision(player_rect):
|
| player_pos[0] = new_x
|
| player_pos[1] = new_y
|
|
|
|
|
| if not menu and not loading:
|
| if keys[pygame.K_w]:
|
| camera_offset[1] += player_speed * dt
|
| if keys[pygame.K_s]:
|
| camera_offset[1] -= player_speed * dt
|
| if keys[pygame.K_a]:
|
| camera_offset[0] += player_speed * dt
|
| if keys[pygame.K_d]:
|
| camera_offset[0] -= player_speed * dt
|
|
|
|
|
| if menu:
|
| screen.fill(MENU_BACKGROUND)
|
| text = font.render("Single Play", True, WHITE)
|
| rect = text.get_rect(center=(WIDTH // 2, HEIGHT // 2))
|
| pygame.draw.rect(screen, BLACK, rect.inflate(20, 20))
|
| screen.blit(text, rect)
|
| elif loading:
|
| screen.fill(BLACK)
|
| text = font.render("Loading world...", True, WHITE)
|
| rect = text.get_rect(center=(WIDTH // 2, HEIGHT // 2))
|
| screen.blit(text, rect)
|
| pygame.display.flip()
|
| pygame.time.wait(4000)
|
| generate_world()
|
| loading = False
|
| else:
|
| screen.fill(WHITE)
|
| draw_world()
|
| draw_player()
|
| draw_inventory()
|
| text = font.render("Rcrafty 1.0", True, BLACK)
|
| fps_text = font.render(f"FPS: {int(clock.get_fps())}", True, BLACK)
|
| screen.blit(text, (10, 10))
|
| screen.blit(fps_text, (10, 40))
|
|
|
| pygame.display.flip()
|
|
|