Python Pygame精灵碰撞:对象没有属性';精灵&x27;
我正在用PythonPyGame制作一个游戏,制作了一个移动的背景,一个跳跃的精灵作为玩家,以及产生障碍物。现在我正在研究玩家和障碍物之间的碰撞问题,并且已经编写了一些代码,但最终出现了一个错误:Python Pygame精灵碰撞:对象没有属性';精灵&x27;,python,pygame,sprite,collision,Python,Pygame,Sprite,Collision,我正在用PythonPyGame制作一个游戏,制作了一个移动的背景,一个跳跃的精灵作为玩家,以及产生障碍物。现在我正在研究玩家和障碍物之间的碰撞问题,并且已经编写了一些代码,但最终出现了一个错误: collided = pygame.sprite.spritecollide(player, obsticles, True) File "C:\Users\Kasutaja\AppData\Roaming\Python\Python27\site packages\pygame
collided = pygame.sprite.spritecollide(player, obsticles, True)
File "C:\Users\Kasutaja\AppData\Roaming\Python\Python27\site
packages\pygame\sprite.py", line 1514, in spritecollide
for s in group.sprites():
AttributeError: 'Obsticles' object has no attribute 'sprites'
我不知道,为什么那个班不是一个精灵,以及如何使它成为一个精灵。我希望在修正了这个错误后,碰撞能起作用
玩家形象
障碍物意象
背景图像
导入pygame,随机
pygame.init()
W、 H=800600
HW,HH=W/2,H/2
面积=W*H
bg=pygame.image.load('Linn.png')
bg=pygame.transform.scale(bg,(800600))
DS=pygame.display.set_模式((W,L))
clock=pygame.time.clock()
职业玩家(pygame.sprite.sprite):
定义初始值(self、x、y、py、paat、veerg、rida):
超级(玩家,自我)。\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu()
self.x=x
self.y=y
自我跳跃=错误
self.platform_y=py
自速度指数=0
self.paat=pygame.image.load('STlaev.png')。convert_alpha()
self.paat=pygame.transform.scale(self.paat,(64,64))
self.rect=self.paat.get_rect()
self.veerg=veerg
self.rida=rida
self.kokku=veerg*rida
self.rect=self.paat.get_rect()
W=self.veergL=self.rect.width/veerg
H=self.weegK=self.rect.height/rida
HW,HH=self.veergKeskel=(W/2,H/2)
self.veerg=list([(索引%veerg*W,int(索引/veerg)*H,W,H)用于范围内的索引(self.kokku)])
self.handle=list([#pildi paigutamise voimalikud PositionId
(0,0),(-HW,0),(-W,0),
(0,-HH),(-HW,-HH),(-W,-HH),
(0,-W),(-HW,-H),(-W,-H),]
self.mask=pygame.mask.from_surface(self.paat)
def do_跳线(自):
全球速度
如果自我跳跃:
self.y+=速度[自速度指数]
自速度指数+=1
如果self.velocity_index>=len(速度)-1:
self.velocity_index=len(velocity)-1
如果self.y>self.platform\u y:
self.y=self.platform\u y
自我跳跃=错误
自速度指数=0
def绘图(自、DS、veergindex、x、y、句柄=0):
blit(self.paat,(self.x+self.handle[handle][0],self.y+self.handle[handle][1]),self.veerg[veergindex])
def do(自我):
self.do_jumpt()
p、 提款(DS,指数%p.kokku,300300,0)
def更新(自我):
self.rect.topleft=self.x,self.y
p=Player(310200200,'STlaev.png',4,1)#Mangija Algkordinadid,huppe
korgus,pilt,雪碧valik
速度=列表([(i/2.0)-14表示i在范围(0,50)])35; Huppe ulatus
指数=3
def键(播放器):
keys=pygame.key.get_pressed()
如果键[pygame.K_SPACE]或键[pygame.K_UP]和player.jumping==False:
player.jumping=正确
职业障碍(pygame.sprite.sprite):
定义初始值(自、x、y、img、宽度、高度):
超级(障碍,自我)。\uuuu init\uuuuu()
self.img=pygame.image.load('box.png').convert()
self.img=pygame.transform.scale(self.img,(64,64))
self.rect=self.img.get_rect()
self.x=x
self.y=y
self.width=宽度
自我高度=高度
self.hitbox=(x,y,宽度,高度)
self.mask=pygame.mask.from_surface(self.img)
def牵引(自,DS):
DS.blit(self.img,(self.x,self.y))
def更新(自我):
self.rect.topleft=self.x,self.y
def redrawWindow():
对于对象中的i:
i、 抽签(DS)
pygame.time.set_定时器(pygame.USEREVENT+2,random.choice([2000,3000,1000,4000,0]))
对象=[]
“精灵”
sprites=pygame.sprite.Group()
player=player(310200200,'STlaev.png',4,1)
障碍物=障碍物(832300,'box.png',64,64)
所有精灵=pygame.sprite.Group(玩家、障碍物)
ob=pygame.sprite.Group(障碍物)
x=0
尽管如此:
“游戏循环”
对于pygame.event.get()中的事件:
如果event.type==pygame.QUIT:
pygame.quit()
退出
如果event.type==pygame.USEREVENT+2:
r=随机。随机范围(0,2)
如果r==0:
附加对象(Obsticles(832300,64,64))
''障碍物速度和删除''
对于对象中的i:
i、 x-=5#障碍物的速度
如果i.x<-64:#从窗口删除障碍物
objects.pop(objects.index(i))
“背景运动”
back\u x=x%bg.get\u rect().width
blit(bg,(back\x-bg.get\rect().width,0))
如果返回x
collide=pygame.sprite.spritecollide(玩家,障碍物,真)
事实上,您使用的是对象obsticles
,而不是精灵组ob
。因此,要解决此问题,请执行以下操作:
collide=pygame.sprite.spritecollide(player,ob,True)
import pygame, random
pygame.init()
W, H = 800,600
HW, HH = W/2,H/2
AREA = W * H
bg = pygame.image.load('Linn.png')
bg = pygame.transform.scale(bg, (800, 600))
DS = pygame.display.set_mode((W,L))
clock = pygame.time.Clock()
class Player(pygame.sprite.Sprite):
def __init__(self, x, y, py, paat, veerg, rida):
super(Player,self).__init__()
self.x = x
self.y = y
self.jumping = False
self.platform_y = py
self.velocity_index = 0
self.paat = pygame.image.load('STlaev.png').convert_alpha()
self.paat = pygame.transform.scale(self.paat,(64,64))
self.rect = self.paat.get_rect()
self.veerg = veerg
self.rida = rida
self.kokku = veerg * rida
self.rect = self.paat.get_rect()
W = self.veergL = self.rect.width/veerg
H = self.weegK = self.rect.height/rida
HW,HH = self.veergKeskel = (W/2,H/2)
self.veerg = list([(index % veerg * W, int(index/veerg) * H,W,H )for index in range(self.kokku)])
self.handle = list([ #pildi paigutamise voimalikud positsioonid
(0, 0), (-HW, 0), (-W, 0),
(0, -HH), (-HW, -HH), (-W, -HH),
(0, -W), (-HW, -H), (-W, -H),])
self.mask = pygame.mask.from_surface(self.paat)
def do_jumpt(self):
global velocity
if self.jumping:
self.y += velocity[self.velocity_index]
self.velocity_index += 1
if self.velocity_index >= len(velocity) - 1:
self.velocity_index = len(velocity) - 1
if self.y > self.platform_y:
self.y = self.platform_y
self.jumping = False
self.velocity_index = 0
def draw(self, DS,veergindex,x,y,handle=0):
DS.blit(self.paat,(self.x+self.handle[handle][0], self.y + self.handle[handle][1]),self.veerg[veergindex])
def do(self):
self.do_jumpt()
p.draw(DS,index%p.kokku,300,300,0)
def update(self):
self.rect.topleft = self.x, self.y
p = Player(310, 200, 200, 'STlaev.png', 4, 1) #Mangija algkordinaadid, huppe
korgus, pilt, sprite valik
velocity = list([(i/ 2.0)-14 for i in range (0,50)]) #Huppe ulatus
index = 3
def keys(player):
keys = pygame.key.get_pressed()
if keys[pygame.K_SPACE] or keys[pygame.K_UP] and player.jumping == False:
player.jumping = True
class Obsticles(pygame.sprite.Sprite):
def __init__(self, x, y, img, width, height):
super(Obsticles,self).__init__()
self.img = pygame.image.load('box.png').convert()
self.img = pygame.transform.scale(self.img, (64,64))
self.rect = self.img.get_rect()
self.x = x
self.y = y
self.width = width
self.height = height
self.hitbox = (x,y,width,height)
self.mask = pygame.mask.from_surface(self.img)
def draw(self, DS):
DS.blit(self.img, (self.x, self.y))
def update(self):
self.rect.topleft = self.x, self.y
def redrawWindow():
for i in objects:
i.draw(DS)
pygame.time.set_timer(pygame.USEREVENT+2, random.choice([2000, 3000, 1000, 4000,0]))
objects = []
'''Sprites'''
sprites = pygame.sprite.Group()
player = Player(310, 200, 200, 'STlaev.png', 4, 1)
obsticles = Obsticles(832,300,'box.png',64,64)
all_sprites = pygame.sprite.Group(player,obsticles)
ob = pygame.sprite.Group(obsticles)
x=0
while True:
'''Game loop'''
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
if event.type == pygame.USEREVENT+2:
r = random.randrange(0,2)
if r == 0:
objects.append(Obsticles(832,300,64,64))
'''Obsticle speed and deleting'''
for i in objects:
i.x -= 5 #the speed of the obsticle
if i.x < -64: #deleting obsticle from the window
objects.pop(objects.index(i))
'''Background movement'''
back_x = x % bg.get_rect().width
DS.blit(bg, (back_x - bg.get_rect().width, 0))
if back_x < W:
DS.blit(bg, (back_x, 0))
x -= 1
'''Sprites'''
all_sprites.update()
collided = pygame.sprite.spritecollide(player,obsticles ,True)
for i in collided:
print('Collision.')
'''Fuctions'''
keys(p)
p.do()
index+=1
redrawWindow()
pygame.display.update()
clock.tick(60)
pygame.quit()
quit()