Added a world boundary on the map so that the player can’t walk out of it.
layouts = {
'boundary': import_csv_layout('map//map_FloorBlocks.csv'), <<Looking at this
'grass': import_csv_layout('map//map_Grass.csv'),
'object': import_csv_layout('map//map_Objects.csv')
This the csv directory of where the floor blocks (the barrier) of the world map. I made a function in another file (support.py) that has all the stuff to make it work.
support.py
def import_csv_layout(path):
terrain_map = []
with open(path) as level_map:
layout = reader(level_map, delimiter = ',')
for row in layout:
terrain_map.append(list(row))
return terrain_map
This code reads the csv file, then in level.py it draws it on the map and places the blocks around the edge of it.
world barrier shown.
world barrier hidden ( I just took off self.visible_sprites).
Grass
Added plants and collision on them!!!!
layouts = {
'boundary': import_csv_layout('map//map_FloorBlocks.csv'),
This one >>'grass': import_csv_layout('map//map_Grass.csv'),
'object': import_csv_layout('map//map_Objects.csv')
same thing as boundary, but has a graphics directory AND a csv (as does object).
The code creates where the camera will be “placed” on the screen. It then puts an offset – what kind of angle or area the camera – will be “placed”.
RESULT:
This only offsets the camera, but doesn’t make the camera move with the player… yet.
Added an overlap for collisions (and also made the camera move with the player).
changes the size of the rectangle (the collision) and makes it smaller or larger. The parameters “x” and “y” are the numbers that will increase or decrease the size of the hitbox.
“x” will be zero and “y” will be a certain number (-26 to be specific)
Also replaced (almost) every rect with hitbox.
Also changed the for loop in YSortCameraGroup class so that we have overlap on ALL rocks.
BEFORE:
for sprite in self.sprites():
offset_pos = sprite.rect.topleft - self.offset
self.display_surface.blit(sprite.image, offset_pos)
AFTER:
for sprite in sorted(self.sprites(),
key = lambda sprite: sprite.rect.centery):
offset_pos = sprite.rect.topleft - self.offset
self.display_surface.blit(sprite.image, offset_pos
RESULT:
Character overlaping behind the rock.
Character overlaping in front of the rock.
NOTE: I will add a short clip of the camera following the player, just not now (can’t screen record if the settings are blocked ˙◠˙).
PUTTING THE FLOOR MAP ON THE SCREEN (and putting the player in the middle of the map)!!!
The collision is currently NOT working (she keeps teleporting when you hit left). I’m trying to figure out the issue and it might be something about the vector class. I might try to figure it out at home.
UPDATE: I found out what was the problem:
I changed the “self” in the for loop and if statement to “sprite”. Self was what was making the player character teleport from one side of the screen to the other and then made the character disappear out of nowhere. Also the vector wasn’t the problem. It’s supposed to be there because we’re making a 2D game and it needs that for math or whatever (in depth info is on the pygame website Pygame Math)
Making a python program of a Zelda type game!!!! (tutorial by Clear Code)
‘main.py’
import pygame, sys
from settings import *
class Game:
def __init__(self):
pygame.init()
self.screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption('Zelda meets Dark Souls')
self.clock = pygame.time.Clock()
def run(self):
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
self.screen.fill('black')
pygame.display.update()
self.clock.tick(FPS)
if __name__ =='__main__':
game = Game()
game.run()
This is the main file (as it says in the name) that runs the game. It takes the parameters from both settings.py and debug.py. Basically the brain of the program.
import pygame
from settings import *
from tile import Tile
from player import Player
class Level:
def __init(self):
# Get the display surface
self.display_surface = pygame.display.get_surface()
'''Sprite group setup'''
self.visible_sprites = pygame.sprite.Group()
self.obsracles_sprites = pygame.sprite.Group()
# sprite setup
self.create_map()
def create_map(self):
for row_index,row in enumerate(WORLD_MAP):
for col_index,col in enumerate(row):
x = col_index * TILESIZE
y = row_index * TILESIZE
if col == 'x':
Tile((x,y),[self.visible_sprites])
def run (self):
'''Update and draw the game'''
self.visible_sprites.draw(self.display_surface)
Basically the core of this program. Will have all the sprites for the level (player.py and tile.py are important too, but if the levels don’t work, nor will the other files…)