Untitled
unknown
plain_text
2 years ago
2.8 kB
5
Indexable
import pygame
# Initialize Pygame
pygame.init()
# Set up the game window
window_width = 800
window_height = 600
window = pygame.display.set_mode((window_width, window_height))
pygame.display.set_caption("Pixel Art Game")
# Define colors
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
# Set the size of each pixel
pixel_size = 10
# Calculate the number of pixels in each direction
pixels_x = window_width // pixel_size
pixels_y = window_height // pixel_size
# Create a 2D array to store the pixel colors
pixels = [[WHITE for _ in range(pixels_x)] for _ in range(pixels_y)]
# Define element colors
TREE_COLOR = (34, 139, 34)
CLOUD_COLOR = (240, 240, 240)
MOUNTAIN_COLOR = (169, 169, 169)
# Define the element sizes
TREE_SIZE = 3
CLOUD_SIZE = 5
MOUNTAIN_SIZE = 8
# Game loop
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# Handle mouse click events
if event.type == pygame.MOUSEBUTTONDOWN:
# Get the position of the mouse click
mouse_x, mouse_y = pygame.mouse.get_pos()
# Calculate the grid position of the clicked pixel
pixel_x = mouse_x // pixel_size
pixel_y = mouse_y // pixel_size
# Draw a tree
if event.button == 1:
for y in range(-TREE_SIZE, TREE_SIZE + 1):
for x in range(-TREE_SIZE, TREE_SIZE + 1):
if 0 <= pixel_x + x < pixels_x and 0 <= pixel_y + y < pixels_y:
pixels[pixel_y + y][pixel_x + x] = TREE_COLOR
# Draw a cloud
elif event.button == 2:
for y in range(-CLOUD_SIZE, CLOUD_SIZE + 1):
for x in range(-CLOUD_SIZE, CLOUD_SIZE + 1):
if 0 <= pixel_x + x < pixels_x and 0 <= pixel_y + y < pixels_y:
pixels[pixel_y + y][pixel_x + x] = CLOUD_COLOR
# Draw a mountain
elif event.button == 3:
for y in range(-MOUNTAIN_SIZE, MOUNTAIN_SIZE + 1):
for x in range(-MOUNTAIN_SIZE, MOUNTAIN_SIZE + 1):
if 0 <= pixel_x + x < pixels_x and 0 <= pixel_y + y < pixels_y:
pixels[pixel_y + y][pixel_x + x] = MOUNTAIN_COLOR
# Clear the screen
window.fill(BLACK)
# Draw the pixels
for y in range(pixels_y):
for x in range(pixels_x):
pygame.draw.rect(window, pixels[y][x], (x * pixel_size, y * pixel_size, pixel_size, pixel_size))
# Update the display
pygame.display.update()
# Quit the game
pygame.quit()
Editor is loading...