Python Pygame:使用sprite时出错
嘿,我正在pygame上做一个小游戏项目,但每次我尝试运行代码时,当他开始使用我的精灵时,我都会出错:Python Pygame:使用sprite时出错,python,pygame,sprite,Python,Pygame,Sprite,嘿,我正在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)
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\u py\u debugger.py:720:Unicode equal comparison未能将两个参数转换为Unicode-将它们解释为不相等
如果文件名==frame.f_code.co_文件名或(未绑定且断点路径匹配(文件名,frame.f_code.co_文件名)):
C:\Program Files(x86)\Microsoft Visual Studio 12.0\Common7\IDE\Extensions\Microsoft\Python Tools for Visual Studio\2.0\visualstudio\u py\u util.py:265:运行时警告:使用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\u py\u util.py:265:运行时警告:使用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')
从pygame导入*
随机输入
从太空输入入侵者*
#为敌人和英雄创建精灵
类精灵:
def _init(self、xposition、yposition、name):
self.x=xposition
self.y=y位置
self.bitmap=image.load(名称)
self.bitmap.set_colorkey((0,0,0))
def设置位置(自身、X位置、Y位置):
self.x=xposition
self.y=y位置
def渲染(自):
blit(self.bitmap,(self.x,self.y))
#两个32*32 sprite之间的冲突检测
def相交(o1_x,o1_y,o2_x,o2_y):
如果(o1_x>o2_x-32)和(o1_xo2_y-32)和(o1_y
首先,始终包含适当的错误消息,以便我们知道在何处搜索:)
因此,一个可能的错误可能是
self.bitmap = image.load(name)
如果您已像这样导入pygame:
import pygame
此代码行应更改为:
self.bitmap = pygame.image.load(name)
希望有帮助!
亚历克斯
(编辑)在搜索运行时错误后,可能的解决方案是下载并安装此模块
(edit2)在搜索您的问题后,我认为您需要检查您的程序是否有任何unicode或非asci字符。它们看起来像这样的“çö”。查看您是否错误地插入了这些字符中的任何一个,并尝试修复它:)您得到的错误是什么?您能告诉我们确切的错误吗?如果我说您也可以通过将
import pygame
更改为from pygame import*
来执行此操作,我会像使用from方法一样导入它吗?似乎有一个对你的进口声明提出异议。。你能发布完整的代码吗?我刚安装了numpy,我知道有一个新错误:C:\ProgramFiles(x86)\Microsoft Visual Studio 12.0\Common7\IDE\Extensions\Microsoft\Python Tools for Visual Studio\2.0\visualstudio\u py\u debugger.py:296:UnicodeWarning:Unicode相等比较无法将两个参数转换为Unicode-将它们解释为不相等的处理程序=self.handler\u cache.get(cur\u frame.f\u code.co\u文件名)程序“[7692]python.exe'已退出,代码为-1(0xffffffff)。@FellowRémi在搜索您的问题后,我认为您需要检查您的程序是否有任何unicode或非asci字符。它们看起来像这样的“çö”。查看是否错误地在任何位置插入了这些字符,并尝试修复:)