I figured this would be perfect avenue of learning for me, since I love games and you can easily see how changes in code interact with the game.
In this tutorial https://www.youtube.com/watch?v=wJMDh9QGRgs&t=3966s I encountered a problem that I just can't seem to be able to figure out myself. No error messages. It just loads the map wrong.
This is how it should about look like. Placement in mine is a bit different.
This is how the level_0_terrain.csv where it should load the positions looks like:
The tutorial uses multiple different files to build the game. Since I have no idea what went wrong here are all files:
main
import pygame, sys
from settings import *
from level import level
from game_data import level_0
pygame.init()
screen = pygame.display.set_mode((screen_width,screen_height))
clock = pygame.time.Clock()
level = level(level_0,screen)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
screen.fill("black")
level.run()
pygame.display.update()
clock.tick(60)
settings
vertical_tile_number = 11
tile_size = 64
screen_height = vertical_tile_number * tile_size
screen_width = 1200
level
My main suspect where things go wrong. Haven't been able to figure out what is the issue.
import pygame
from support import import_csv_layout
from settings import tile_size
from tiles import Tile
class level:
def __init__(self,level_data,surface):
self.display_surface = surface
terrain_layout = import_csv_layout(level_data["terrain"])
# print(terrain_layout)
self.terrain_sprites = self.create_tile_group(terrain_layout,"terrain")
def create_tile_group(self,layout,type):
sprite_group = pygame.sprite.Group()
for row_index, row in enumerate(layout):
for col_index,val in enumerate (row):
if val != "-1":
x = col_index * tile_size
y = col_index * tile_size
if type == "terrain":
sprite = Tile(tile_size,x,y)
sprite_group.add(sprite)
return sprite_group
def run(self):
# Run this entire game / level
self.terrain_sprites.draw(self.display_surface)
support
Here I could not load the file without direct path. Not sure if related but at least if I use print it seems to read level_0_terrain.csv correctly
from csv import reader
def import_csv_layout(path):
terrain_map = []
# with open(path) as map:
with open(r"C:\Users\XXXX\Python games\Mariolike\levels\0\level_0_terrain.csv") as map:
#with open(r"C:/Users/XXXX/Python games/Mariolike/levels/0/level_0_terrain.csv") as map:
level = reader(map,delimiter = ",")
for row in level:
# print(row)
terrain_map.append(list(row))
return terrain_map
tiles
import pygame
class Tile(pygame.sprite.Sprite):
def __init__(self,size,x,y):
super().__init__()
self.image = pygame.Surface((size,size))
self.image.fill("grey")
self.rect = self.image.get_rect(topleft = (x,y))
Game_data
level_0 = {
"terrain":"..levels/0/level_0_terrain.csv",
"coins":"..levels/0/level_0_coins.csv",
"fg palms": "..levels/0/level_0_fg_palms.csv",
"bg palms": "..levels/0/level_0_bg_palms.csv",
"crates": "..levels/0/level_0_crates.csv",
"enemies": "..levels/0/level_0_enemies.csv",
"constraints": "..levels/0/level_0_constraints.csv",
"player": "..levels/0/level_0_player.csv",
"grass": "..levels/0/level_0_grass.csv"}