0

The exact same code from my friend's windows computer does not work with my macbook and vice versa. We both get the same error, "TypeError: 'NoneType' object is not subscriptable" and the problem starts when we start importing png files. Otherwise normal code without needing the file directory works fine.

This was our tile module

import pygame
from support import import_folder


class Tile(pygame.sprite.Sprite):
    def __init__(self, size, x, y):
        super().__init__()
        self.image = pygame.Surface((size, size))
        self.rect = self.image.get_rect(topleft=(x, y))


def update(self, shift):
    self.rect.x += shift


class StaticTile(Tile):
    def __init__(self, size, x, y, surface):
        super().__init__(size, x, y)
        self.image = surface

class AnimatedTile(Tile):
    def __init__(self, size, x, y, path):
        super().__init__(size, x, y)
        self.frames = import_folder(path)
        self.frame_index = 0
        self.image = self.frames[self.frame_index]

    def animate(self):
        self.frame_index += 0.15
        if self.frame_index >= len(self.frames):
            self.frame_index = 0
        self.image = self.frames[int(self.frame_index)]

    def update(self, shift):
        self.animate()
        self.rect.x += shift

This was our enemy module

import pygame
from tiles import AnimatedTile
from random import randint


class Enemy(AnimatedTile):
    def __init__(self, size, x, y):
        super().__init__(size, x, y, '../graphics/0/enemy/slay/1.png')
        self.rect.y += size - self.image.get_size()[1]
        self.speed = randint(3, 5)

    def move(self):
        self.rect.x += self.speed

    def reverse_image(self):
        if self.speed > 0:
            self.image = pygame.transform.flip(self.image, True, False)

    def reverse(self):
        self.speed *= -1

    def update(self, shift):
        self.rect.x += shift
        self.animate()
        self.move()
        self.reverse_image()

This is our first time coding a game so some help will really be appreciated. Thank you.

Pinnipeds
  • 1
  • 1
  • There could be a lot of reasons behind it, for example different python versions. Different library versions, different operational systems. If you are going to share your code and build a project, i recommed using either a docker or vcs with venvs – INGl0R1AM0R1 Jun 12 '22 at 15:33

1 Answers1

0

Windows and Mac(unix type os's) separate folders in path using opposite slashes '/' for unix and '\' for Windows

you will need to detect os and format '../graphics/0/enemy/slay/1.png' accordingly

import os
if os.name == 'nt':
   # Windows
else:
   # non Windows