map #3

Open
opened 2024-02-12 10:51:53 +00:00 by tmaksimovs · 0 comments
Collaborator

import pygame
import json
import csv

Initialize Pygame

pygame.init()

Load JSON data

with open('data.json') as json_file:
level_data = json.load(json_file)

Load Collisions

collisions = []
with open('Collisions.csv', 'r') as csv_file:
csv_reader = csv.reader(csv_file)
for row in csv_reader:
row_data = []
for value in row:
if value: # Check if the value is not empty
row_data.append(int(value))
else:
row_data.append(0) # Assuming non-numeric or empty values represent empty spaces
collisions.append(row_data)

Set up the display

screen_width = 1920
screen_height = 1015
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption('Your Game Name')

Load images for layers

layers = {}
for layer in level_data["layers"]:
layers[layer] = pygame.image.load(layer).convert_alpha()
layers[layer] = pygame.transform.scale(layers[layer], (screen_width, screen_height))

Create a new surface for collisions with color black

collision_layer = pygame.Surface((screen_width, screen_height), pygame.SRCALPHA)
collision_layer.fill((0, 0, 0, 0)) # Fill the surface with transparent color

Draw rectangles for collision areas

for y, row in enumerate(collisions):
for x, value in enumerate(row):
if value == 1:
rect = pygame.Rect(x * 64, y * 80, 64, 80) # Adjust the rect dimensions based on your collision cell size
pygame.draw.rect(collision_layer, (0, 0, 0), rect)

Load image for player

player_image = pygame.image.load("Player.png").convert_alpha()
player_image = pygame.transform.scale(player_image, (32, 32)) # Adjust player size to fit the new scale

Player properties

player_rect = player_image.get_rect(topleft=(100, 100)) # Starting position of the player

Main game loop

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

# Handle player movement
keys = pygame.key.get_pressed()
if keys[pygame.K_a]:
    if player_rect.left > 0 and collisions[player_rect.centery // 50][player_rect.left // 50 - 1] == 0:
        player_rect.x -= 5
if keys[pygame.K_d]:
    if player_rect.right < screen_width and collisions[player_rect.centery // 50][player_rect.right // 50] == 0:
        player_rect.x += 5
if keys[pygame.K_w]:
    if player_rect.top > 0 and collisions[player_rect.top // 50 - 1][player_rect.centerx // 50] == 0:
        player_rect.y -= 5
if keys[pygame.K_s]:
    if player_rect.bottom < screen_height and collisions[player_rect.bottom // 50][player_rect.centerx // 50] == 0:
        player_rect.y += 5

# Clear the screen
screen.fill(pygame.Color(level_data["bgColor"]))

# Draw layers
for layer_name, layer_image in layers.items():
    screen.blit(layer_image, (0, 0))

# Draw collision layer
screen.blit(collision_layer, (0, 0))

# Draw player
screen.blit(player_image, player_rect.topleft)

# Update the display
pygame.display.flip()

Quit Pygame

pygame.quit()

import pygame import json import csv # Initialize Pygame pygame.init() # Load JSON data with open('data.json') as json_file: level_data = json.load(json_file) # Load Collisions collisions = [] with open('Collisions.csv', 'r') as csv_file: csv_reader = csv.reader(csv_file) for row in csv_reader: row_data = [] for value in row: if value: # Check if the value is not empty row_data.append(int(value)) else: row_data.append(0) # Assuming non-numeric or empty values represent empty spaces collisions.append(row_data) # Set up the display screen_width = 1920 screen_height = 1015 screen = pygame.display.set_mode((screen_width, screen_height)) pygame.display.set_caption('Your Game Name') # Load images for layers layers = {} for layer in level_data["layers"]: layers[layer] = pygame.image.load(layer).convert_alpha() layers[layer] = pygame.transform.scale(layers[layer], (screen_width, screen_height)) # Create a new surface for collisions with color black collision_layer = pygame.Surface((screen_width, screen_height), pygame.SRCALPHA) collision_layer.fill((0, 0, 0, 0)) # Fill the surface with transparent color # Draw rectangles for collision areas for y, row in enumerate(collisions): for x, value in enumerate(row): if value == 1: rect = pygame.Rect(x * 64, y * 80, 64, 80) # Adjust the rect dimensions based on your collision cell size pygame.draw.rect(collision_layer, (0, 0, 0), rect) # Load image for player player_image = pygame.image.load("Player.png").convert_alpha() player_image = pygame.transform.scale(player_image, (32, 32)) # Adjust player size to fit the new scale # Player properties player_rect = player_image.get_rect(topleft=(100, 100)) # Starting position of the player # Main game loop running = True while running: # Handle events for event in pygame.event.get(): if event.type == pygame.QUIT: running = False # Handle player movement keys = pygame.key.get_pressed() if keys[pygame.K_a]: if player_rect.left > 0 and collisions[player_rect.centery // 50][player_rect.left // 50 - 1] == 0: player_rect.x -= 5 if keys[pygame.K_d]: if player_rect.right < screen_width and collisions[player_rect.centery // 50][player_rect.right // 50] == 0: player_rect.x += 5 if keys[pygame.K_w]: if player_rect.top > 0 and collisions[player_rect.top // 50 - 1][player_rect.centerx // 50] == 0: player_rect.y -= 5 if keys[pygame.K_s]: if player_rect.bottom < screen_height and collisions[player_rect.bottom // 50][player_rect.centerx // 50] == 0: player_rect.y += 5 # Clear the screen screen.fill(pygame.Color(level_data["bgColor"])) # Draw layers for layer_name, layer_image in layers.items(): screen.blit(layer_image, (0, 0)) # Draw collision layer screen.blit(collision_layer, (0, 0)) # Draw player screen.blit(player_image, player_rect.topleft) # Update the display pygame.display.flip() # Quit Pygame pygame.quit()
Sign in to join this conversation.
No Label
No Milestone
No project
No Assignees
1 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: gtillers/Game#3
There is no content yet.