蒟蒻蒻蒻 2022-07-21 14:35 采纳率: 57.1%
浏览 37
已结题

pygame 碰撞报错

import pygame as pg
from pygame.locals import *
from random import randint

height = 400
weigh = 600
goods_h = 100
goods_w = goods_h*0.92
roles_h = 50
roles_w = roles_h
red_w = 50
red_h = red_w*1.4
yb_h = 50
yb_w = yb_h*1.72

pg.init()

object_group = pg.sprite.Group()
def change(roles_address:str,w:'int > 0',h:'int > 0'):
         role = roles_address
         roles = pg.image.load(role)
         roles = pg.transform.smoothscale(roles,(w,h))
         pos = roles.get_rect()
         if roles_address != 'pygame图片素材\背景.jpg' or roles_address != 'pygame图片素材\财神爷.png':
                  object_group.add(roles)
         return roles

size = pg.display.set_mode([weigh,height])
back = change('pygame图片素材\背景.jpg',weigh,height)
coin = change('pygame图片素材\金币.png',roles_h,roles_w)
goods = change('pygame图片素材\财神爷.png',goods_h,goods_w)
red = change('pygame图片素材\红包.png',red_h,red_w)
yb = change('pygame图片素材\元宝.png',yb_h,yb_w)


font = pg.font.SysFont('twcencondensed',50)
score = 0

pos_r = coin.get_rect()
pos_r.x = randint(0,weigh - roles_h)
pos_r.top = randint(-(2*roles_w),-roles_w)

pos_g = goods.get_rect()
pos_g.left = (weigh - goods_w)/2
pos_g.bottom = height

pos_d = red.get_rect()
pos_d.x = randint(0,weigh - red_h)
pos_d.top = randint(-(2*red_w),-red_w)

pos_y = yb.get_rect()
pos_y.x = randint(0,weigh - yb_h)
pos_y.top = randint(-(2*yb_w),-yb_w)

speed_r = [0,randint(3,6)]
speed_d = [0,randint(3,6)]

while pg.out():
         text = font.render("Score:" + str(score),True,(255,255,255))
         
         size.blit(back,(0,0))
         size.blit(text,(10,10))
         size.blit(goods,pos_g)
         size.blit(coin,pos_r)
         size.blit(red,pos_d)
         
         pos_r = pos_r.move(speed_r)
         #碰撞
         if pg.sprite.spritecollide(goods,object_group,False,None):
                  print(pg.sprite.spritecollide(goods,object_group,False,None))

         key = pg.key.get_pressed()
         if key[K_a] and pos_g.left >= 0:pos_g.x -= 5
         if key[K_d] and pos_g.right <= weigh:pos_g.x += 5

         pg.display.update()
         time.sleep(0.05)

中的#碰撞 报错
报错信息:

Traceback (most recent call last):
  File "C:\Users\Administrator\AppData\Roaming\Python\Python38\site-packages\pygame\sprite.py", line 449, in add
    self.add(*sprite)
TypeError: add() argument after * must be an iterable, not pygame.Surface

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:/Users/Administrator/Desktop/python/python(窗口)/pygame/游戏_得分.py", line 29, in <module>
    back = change('pygame图片素材\背景.jpg',weigh,height)
  File "C:/Users/Administrator/Desktop/python/python(窗口)/pygame/游戏_得分.py", line 25, in change
    object_group.add(roles)
  File "C:\Users\Administrator\AppData\Roaming\Python\Python38\site-packages\pygame\sprite.py", line 462, in add
    sprite.add_internal(self)
AttributeError: 'pygame.Surface' object has no attribute 'add_internal'

我想将精灵和精灵组进行碰撞判断
注:pygame库被作者修改过,增加了退出函数并将解码方式改为UTF-8

  • 写回答

2条回答 默认 最新

  • 请叫我问哥 Python领域新星创作者 2022-07-21 17:19
    关注

    你的roles变量是一个Surface,要想加到组里的话需要定义成Sprite对象

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 7月31日
  • 已采纳回答 7月23日
  • 创建了问题 7月21日

悬赏问题

  • ¥20 keepalive配置业务服务双机单活的方法。业务服务一定是要双机单活的方式
  • ¥50 关于多次提交POST数据后,无法获取到POST数据参数的问题
  • ¥15 win10,这种情况怎么办
  • ¥15 如何在配置使用Prettier的VSCode中通过Better Align插件来对齐等式?(相关搜索:格式化)
  • ¥100 在连接内网VPN时,如何同时保持互联网连接
  • ¥15 MATLAB中使用parfor,矩阵Removal的有效索引在parfor循环中受限制
  • ¥20 Win 10 LTSC 1809版本如何无损提升到20H1版本
  • ¥50 win10 LTSC 虚拟键盘不弹出
  • ¥30 微信小程序请求失败,网页能正常带锁访问
  • ¥15 Matlab求解微分方程,如何用fish2d进行预优?