完全相同的Python代码在不同的计算机上不起作用
我朋友的Windows计算机完全相同的代码与我的MacBook无法使用,反之亦然。我们俩都会遇到相同的错误,“ typeError:'nontype'对象是不可订阅的”,当我们开始导入PNG文件时,问题开始。否则普通代码而无需文件目录正常工作。
这是我们的瓷砖模块,
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
这是我们的敌人模块
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()
,这是我们第一次编码游戏,因此确实会很感激。谢谢。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Windows和Mac(Unix type OS)在路径中使用相对的斜线单独的文件夹
'/'对于Unix和'\'对于Windows,
您需要检测OS和格式'../graphics/0/enemy/slay/slay/1.png'相应
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