Pygame:使用spri时出错

2024-09-28 17:20:57 发布

您现在位置:Python中文网/ 问答频道 /正文

嘿,我正在做一个关于pygame的小游戏项目,但是每次我试图运行我的代码时,当他使用我的精灵时,我都会遇到一个错误:

class Sprite:
    def _init_(self, xposition, yposition, name):
        self.x = xposition
        self.y = yposition
        self.bitmap = image.load(name)
        self.bitmap.set_colorkey ((0,0,0))
    def set_position(self, xposition, yposition):
        self.x = xposition
        self.y = yposition
    def render(self):
        screen.blit(self.bitmap, (self.x, self.y))

我这样称呼它

^{pr2}$

更新

下面是commad行中出现的错误消息

C:\Program Files(x86)\Microsoft Visual Studio 12.0\Common7\IDE\Extensions\Microsoft\Python Tools for Visual Studio\2.0\visualstudio_调试器。py:720:UnicodeWarning:Unicode equal comparison未能将这两个参数转换为Unicode-将它们解释为不相等 如果文件名==frame.f_code.co_文件名或(未绑定且断点路径匹配(文件名,frame.f_code.co_文件名)): C: \程序文件(x86)\Microsoft Visual Studio 12.0\Common7\IDE\Extensions\Microsoft\Python Tools for Visual Studio\2.0\visualstudio_实用py:265:RuntimeWarning:使用surfarray:未找到名为numpy或Numeric的模块 (ImportError:找不到名为numpy或Numeric的模块) obj_repr=代表(obj) C: \程序文件(x86)\Microsoft Visual Studio 12.0\Common7\IDE\Extensions\Microsoft\Python Tools for Visual Studio\2.0\visualstudio_实用py:265:RuntimeWarning:use sndarray:未找到名为numpy或Numeric的模块 (ImportError:找不到名为numpy或Numeric的模块) obj_repr=代表(obj) 程序“[6344]python.exe'已退出,代码为-1073741510(0xc00013a)。在

这是完整的代码

from pygame import *
import random
from Space_invader import *

#create the sprite for both enemy and hero
class Sprite:
    def _init_(self, xposition, yposition, name):
        self.x = xposition
        self.y = yposition
        self.bitmap = image.load(name)
        self.bitmap.set_colorkey ((0,0,0))
    def set_position(self, xposition, yposition):
        self.x = xposition
        self.y = yposition
    def render(self):
        screen.blit(self.bitmap, (self.x, self.y))

# colision detection betwee two 32*32 sprite
def Intersect(o1_x, o1_y, o2_x, o2_y):
    if (o1_x > o2_x - 32) and (o1_x < o2_x + 32) and (o1_y > o2_y - 32) and (o1_y < o2_y + 32):
                return 1
    else:
                return 0 

#initialise pygame 
init()
screen = display.set_mode((640,480))
key.set_repeat(1,1)     #make sure you can press the same key more then one and that there is a delay between each action
display.set_caption('UON Invader') #set windows name
background = image.load('background.png') #load background picture
hero = Sprite(20,400, 'spaceship.bmp')
herobullet = Sprite(0,480, 'bulete.bmp')
enemiebullet = Sprite(0,480, 'bulete.bmp')

Tags: andnameselfdefloadmicrosoftstudiovisual
1条回答
网友
1楼 · 发布于 2024-09-28 17:20:57

首先,请始终包含适当的错误消息,以便我们知道搜索位置:) 所以一个可能的错误是

self.bitmap = image.load(name)

如果您导入了这样的pygame:

^{pr2}$

这行代码应该改为:

self.bitmap = pygame.image.load(name)

希望有帮助! 亚历克斯

(编辑)在搜索运行时错误之后,一个可能的解决方案是下载并安装这个模块http://numpy.scipy.org/。在

(edit2)在搜索您的问题后,我认为您需要检查您的程序是否有任何unicode或非asci字符。它们看起来像这样。查看是否在任何地方错误地插入了这些字符,并尝试修复它:)

相关问题 更多 >