【飞机大战】pygame中精灵组使用add方法添加敌机对象报错
# 添加小型敌机def Add_SEneies(group1, group2, num):
for i in range(num):
e1 = SmallEnemy(bgSize)
group1.add(e1)
group2.add(e1)
# 添加中型敌机
def Add_MEneies(group1, group2, num):
for i in range(num):
e2 = MidEnemy(bgSize)
group1.add(e2)
group2.add(e2)
# 添加大型敌机
def Add_BEneies(group1, group2, num):
for i in range(num):
e3 = BigEnemy(bgSize)
group1.add(e3)
group2.add(e3)
def main():
clock = pygame.time.Clock()
# 类的调用部分
plane = Plane(bgSize)
# 变量设置部分
delay = 100# 延时,用于图片的切换
switch_image = True# 图片切换标志符
bg_y = 0
# 敌机设置及添加
Enemies = pygame.sprite.Group()
SEneies = pygame.sprite.Group()
MEneies = pygame.sprite.Group()
BEneies = pygame.sprite.Group()
Add_SEneies(SEneies, Enemies, 10)
Add_MEneies(MEneies, Enemies, 5)
Add_BEneies(BEneies, Enemies, 2)
while True:
screen.blit(bgPic, (0, bg_y))
if pygame.mixer.music.get_busy() == False:
pygame.mixer.music.play(-1)
#英雄机加载部分
if not (delay % 5):
switch_image = not switch_image
if Plane.alive:
if switch_image:
screen.blit(plane.image1, plane.rect)
else:
screen.blit(plane.image2, plane.rect)
else:
if not(delay % 3):
screen.blit(plane.Plane_Hit_images, plane.rect)
if me_index == 0:
Plane.reset()
# 生成大型敌机
for bememy in BEneies:
bememy.move()
if switch_image:
screen.blit(bememy.image1, bememy.rect)
else:
screen.blit(bememy.image2, bememy.rect)
if bememy.rect.bottom == -50:
music_enemy3_down.play()
#
# # 生成中型敌机
# for mememy in MEneies:
# mememy.move()
# screen.blit(mememy.image, mememy.rect)
#
# # 生成小型敌机
# for sememy in SEneies:
# sememy.move()
# screen.blit(sememy.image, sememy.rect)
为什么为报:
self.add(*group)
TypeError: add() argument after * must be an iterable, not int
这个错呢哪位大神遇见过这样的问题,烦请指点下,到底是什么问题?
完整错误信息呢?貌似group的实参是个int 无符号整形 发表于 2018-8-18 17:48
完整错误信息呢?貌似group的实参是个int
D:\Python\Python36\python.exe E:/Python项目/PlaneWars/Main.py
Traceback (most recent call last):
File "E:/Python项目/PlaneWars/Main.py", line 210, in <module>
main()
File "E:/Python项目/PlaneWars/Main.py", line 111, in main
Add_SEneies(SEneies, Enemies, 10)
File "E:/Python项目/PlaneWars/Main.py", line 77, in Add_SEneies
e1 = SmallEnemy(bgSize)
File "D:\Python\Python36\lib\site-packages\pygame\sprite.py", line 124, in __init__
self.add(*groups)
File "D:\Python\Python36\lib\site-packages\pygame\sprite.py", line 142, in add
self.add(*group)
File "D:\Python\Python36\lib\site-packages\pygame\sprite.py", line 142, in add
self.add(*group)
TypeError: add() argument after * must be an iterable, not int 把你这个Enemy类贴出来或者 贴出SmallEnemy这个类看下 Legend丶Hu 发表于 2018-8-19 00:36
把你这个Enemy类贴出来或者 贴出SmallEnemy这个类看下
import pygame
from random import *
class SmallEnemy(pygame.sprite.Sprite):
def __int__(self, back_size):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load("images/enemy0.png").convert_alpha()
self.rect = self.image.get_rect()
self.width = back_size
self.height = back_size
self.rect.left = randint(0, self.width - self.rect.width - 120)
self.rect.top = randint(-2 * self.height, 0)
self.speed = 2
self.alive = True # 是否存活
self.mask = pygame.mask.from_surface(self.image)# 检测非透明部分
def move(self):
if self.rect.top < self.height:
self.rect.top += self.speed
else:
self.reset()
def reset(self):
self.alive = True # 是否存活
self.rect.left = randint(0, self.width - self.rect.width - 120)
self.rect.top = randint(-2 * self.height, 0)
class MidEnemy(pygame.sprite.Sprite):
# life = 5
def __int__(self, back_size):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load("images/enemy1.png").convert_alpha()
self.rect = self.image.get_rect()
self.MidEnemy_Hit_images = []
self.MidEnemy_Hit_images.extend([
pygame.image.load("images/enemy1_down1.png").convert_alpha(),
pygame.image.load("images/enemy1_down2.png").convert_alpha(),
pygame.image.load("images/enemy1_down3.png").convert_alpha(),
pygame.image.load("images/enemy1_down4.png").convert_alpha()])
self.width = back_size
self.height = back_size
self.rect.left = randint(0, self.width - self.rect.width - 120)
self.rect.top = randint(-5 * self.height, -self.height)
self.speed = 1
self.alive = True # 是否存活
self.mask = pygame.mask.from_surface(self.image)# 检测非透明部分
def move(self):
if self.rect.top < self.height:
self.rect.top += self.speed
else:
self.reset()
def reset(self):
self.alive = True # 是否存活
self.rect.left = randint(0, self.width - self.rect.width - 120)
self.rect.top = randint(-5 * self.height, -self.height)
class BigEnemy(pygame.sprite.Sprite):
# life = 10
def __int__(self, back_size):
pygame.sprite.Sprite.__init__(self)
self.image1 = pygame.image.load("images/enemy2.png").convert_alpha()
self.image2 = pygame.image.load("images/enemy2_n2.png").convert_alpha()
self.rect = self.image1.get_rect()
self.BigEnemy_Hit_images = []
self.BigEnemy_Hit_images.extend([
pygame.image.load("images/enemy2_down1.png").convert_alpha(),
pygame.image.load("images/enemy2_down2.png").convert_alpha(),
pygame.image.load("images/enemy2_down3.png").convert_alpha(),
pygame.image.load("images/enemy2_down4.png").convert_alpha(),
pygame.image.load("images/enemy2_down5.png").convert_alpha(),
pygame.image.load("images/enemy2_down6.png").convert_alpha()])
self.width = back_size
self.height = back_size
self.rect.left = randint(0, self.width - self.rect.width - 120)
self.rect.top = randint(-10 * self.height, -2 * self.height)
self.speed = 1
self.alive = True # 是否存活
self.mask = pygame.mask.from_surface(self.image1)# 检测非透明部分
def move(self):
if self.rect.top < self.height:
self.rect.top += self.speed
else:
self.reset()
def reset(self):
self.alive = True # 是否存活
self.rect.left = randint(0, self.width - self.rect.width - 120)
self.rect.top = randint(-10 * self.height, -2 * self.height)
如果你是和小甲鱼的视频一样分多个文件写这个游戏那么def add_small_enemies(group1,group2,num):
for i in range(num):
e1 = enemy.SmallEmeny(bg_size)
group1.add(e1)
group2.add(e1)
def add_mid_enemies(group1,group2,num):
for i in range(num):
e2 = enemy.MidEmeny(bg_size)
group1.add(e2)
group2.add(e2)
def add_big_enemies(group1,group2,num):
for i in range(num):
e3 = enemy.BigEmeny(bg_size)
group1.add(e3)
group2.add(e3) Legend丶Hu 发表于 2018-8-19 14:45
如果你是和小甲鱼的视频一样分多个文件写这个游戏那么
是啊就是通过面向对象的思想,按照类型分成多个文件写的 C:\\User\XJ\Desktop\1.png 看下这个 Legend丶Hu 发表于 2018-8-19 21:50
看下这个
红框的是MidEnemy类的文件名(模块名), 但是我是用from 模块名 import * 啊,同时,我也尝试了直接导入Enemy(也就是各类敌机类所在的文件),但是就是报add方法存在问题 Legend丶Hu 发表于 2018-8-19 21:47
您的意思是图片要使用绝对路径是吗?尝试过了还是不行, 再把main函数给贴出来吧bgSize这一块发出来看下 blning 发表于 2018-8-19 22:41
您的意思是图片要使用绝对路径是吗?尝试过了还是不行,
你先把bgSize这变量在main.py中有关的代码发出来,粘贴复制就好。我来看看类型 请问最后是怎么解决的?super 精灵类的__iniit__方法 是有参数的 同求
页:
[1]