我的系统在左上 -> 右下轴上对齐时遇到问题。
这是一张照片。
我使用的是 3.11.2 版本的 python。
这是我的代码
import pygame
import sys
import numpy as np
# Initialize Pygame
pygame.init()
# Constants for tile size and screen dimensions
TILE_SIZE = 128
SCREEN_WIDTH, SCREEN_HEIGHT = 1200, 1220
# Calculate the number of tiles that fit on the screen
num_horizontal_tiles = SCREEN_WIDTH // TILE_SIZE * 2
num_vertical_tiles = SCREEN_HEIGHT // TILE_SIZE * 2
# Define colors
WHITE = (255, 255, 255)
# Create the screen
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("Isometric Tile Demo")
# Define an isometric tileset (PNG images)
tile_width = TILE_SIZE
tile_height = TILE_SIZE
# Function to load and draw isometric tiles
def draw_isometric_tile(x, y, image_path):
iso_x = x - y // 2
iso_y = (x + y) // 4 + TILE_SIZE // 4 # Adjust the Y position
tile_image = pygame.image.load(image_path)
tile_image = pygame.transform.scale(tile_image, (tile_width, tile_height))
screen.blit(tile_image, (iso_x, iso_y))
# Terrain generation
terrain = np.random.choice(['grass', 'dirt', 'sand', 'water'], size=(num_horizontal_tiles, num_vertical_tiles), p=[0.6, 0.2, 0.1, 0.1])
# Define rules for terrain generation
terrain_rules = {
'grass': ['dirt'],
'dirt': ['grass', 'sand'],
'sand': ['dirt', 'water'],
'water': ['sand']
}
# Perform cellular automaton steps
for _ in range(5): # You can adjust the number of iterations as needed
new_terrain = terrain.copy()
for x in range(num_horizontal_tiles):
for y in range(num_vertical_tiles):
current_terrain = terrain[x, y]
neighboring_terrains = [terrain[i, j] for i in range(x - 1, x + 2) for j in range(y - 1, y + 2)
if 0 <= i < num_horizontal_tiles and 0 <= j < num_vertical_tiles]
new_terrain[x, y] = np.random.choice(terrain_rules[current_terrain])
terrain = new_terrain
# Define the mapping of terrain types to image paths
terrain_images = {
'grass': "C:/Users/seth/AppData/Local/Programs/Python/Python311/terrain_images/Isometric Grass tile 2.png",
'dirt': "C:/Users/seth/AppData/Local/Programs/Python/Python311/terrain_images/Isometric Dirt tile.png",
'sand': "C:/Users/seth/AppData/Local/Programs/Python/Python311/terrain_images/isometric Sand tile.png",
'water': "C:/Users/seth/AppData/Local/Programs/Python/Python311/terrain_images/Isometric Water tile.png"
}
# Main game loop
running = True
# Initial camera position
camera_x, camera_y = 0, 0
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# Handle camera movement
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
camera_x -= 5
if keys[pygame.K_RIGHT]:
camera_x += 5
if keys[pygame.K_UP]:
camera_y -= 5
if keys[pygame.K_DOWN]:
camera_y += 5
# Clear the screen
screen.fill(WHITE)
# Draw isometric tiles based on camera position
for x in range(num_horizontal_tiles):
for y in range(num_vertical_tiles):
tile_type = terrain[x, y] # Get terrain type at this position
image_path = terrain_images.get(tile_type, "") # Get image path based on terrain type
if image_path:
draw_isometric_tile((x - camera_x) * tile_width + SCREEN_WIDTH // 2,
(y - camera_y) * tile_height + SCREEN_HEIGHT // 2, image_path)
pygame.display.flip()
# Quit Pygame
pygame.quit()
sys.exit()