完全相同的Python代码在不同的计算机上不起作用

发布于 2025-02-07 04:11:43 字数 1864 浏览 1 评论 0原文

我朋友的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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

在风中等你 2025-02-14 04:11:43

Windows和Mac(Unix type OS)在路径中使用相对的斜线单独的文件夹
'/'对于Unix和'\'对于Windows,

您需要检测OS和格式'../graphics/0/enemy/slay/slay/1.png'相应

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文