嘿我正在开发一个关于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))
这就是我怎么称呼它
hero = Sprite(20,400, 'spaceship.bmp')
herobullet = Sprite(0,480, 'bulete.bmp')
enemiebullet = Sprite(0,480, 'bulete.bmp')
更新
这是commad行中出现的错误消息
C:\ Program Files(x86)\ Microsoft Visual Studio 12.0 \ Common7 \ IDE \ Extensions \ Microsoft \ Python Tools for Visual Studio \ 2.0 \ visualstudio_py_debugger.py:720:UnicodeWarning:Unicode等于比较无法将两个参数转换为Unicode - 将它们解释为不相等 if filename == frame.f_code.co_filename或(not bound and breakpoint_path_match(filename,frame.f_code.co_filename)): C:\ Program Files(x86)\ Microsoft Visual Studio 12.0 \ Common7 \ IDE \ Extensions \ Microsoft \ Python Tools for Visual Studio \ 2.0 \ visualstudio_py_util.py:265:RuntimeWarning:use surfarray:找不到名为numpy或Numeric的模块 (ImportError:找不到名为numpy或Numeric的模块) obj_repr = repr(obj) C:\ Program Files(x86)\ Microsoft Visual Studio 12.0 \ Common7 \ IDE \ Extensions \ Microsoft \ Python Tools for Visual Studio \ 2.0 \ visualstudio_py_util.py:265:RuntimeWarning:使用sndarray:找不到名为numpy或Numeric的模块 (ImportError:找不到名为numpy或Numeric的模块) obj_repr = repr(obj) 程序'[6344] python.exe'已退出,代码为-1073741510(0xc000013a)。
这是完整的代码
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')
答案 0 :(得分:2)
首先,请始终包含足够的错误消息,以便我们知道在哪里搜索:)
是一个可能的错误self.bitmap = image.load(name)
如果您已经导入了这样的pygame:
import pygame
这行代码应更改为:
self.bitmap = pygame.image.load(name)
希望有所帮助! 亚历
(编辑)在搜索了一下运行时错误之后,可能的解决方案是下载并安装此模块http://numpy.scipy.org/。
(edit2)在搜索到您之后,我认为您需要检查您的程序是否有任何unicode或非asci字符。他们会看起来像这样的“çö”。看看你是否在任何地方插入了任何这些字符并尝试修复它:)