在
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