本文介绍了* 后的 Add() 参数必须是可迭代的,而不是 int Pygame的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我是 pygame 的新手,我正在尝试编写一个 shmup 游戏.但是我总是遇到这个错误:

Hello I am new to pygame and I am trying to write a shmup game.However I am always having this error:

TypeError: * 之后的 add() 参数必须是可迭代的,而不是 intself.add(*group)

这是错误的回溯:

文件C:/Users/Pygame/game.py",第 195 行,在player.shoot()

文件C:/Users/Pygame/game.py",第 78 行,在拍摄中bullet = Bullets(self.rect.center,self.angle)

File "C:/Users/Pygame/game.py", line 78, in shoot bullet = Bullets(self.rect.center,self.angle)

文件C:/Users/Pygame/game.py",第 124 行,initsuper(Bullets,self).init(pos,angle)

File "C:/Users/Pygame/game.py", line 124, in init super(Bullets,self).init(pos,angle)

这是我到目前为止编写的代码,它运行良好,但是当用户想要射击时会引发错误.

This is the code I have written so far, it works well however when the user wants to shoot the error is being raised.

import os
import pygame
import random
import math

WIDTH = 480
HEIGHT = 600
FPS = 60

#colors:
WHITE = (255,255,255)
BLACK = (0,0,0)
GREEN = (0,250,0)
RED = (255,0,0)
BLUE = (0,0,255)
YELLOW = (255,255,0)

#setup assets
game_folder = os.path.dirname("C:/Users/PygameP/")
img_folder = os.path.join(game_folder,"img")

#intialise pygame
pygame.init()
pygame.mixer.init()
screen = pygame.display.set_mode((WIDTH,HEIGHT))
clock = pygame.time.Clock()

class Player(pygame.sprite.Sprite):
    def __init__(self):
            pygame.sprite.Sprite.__init__(self)
            self.image = pygame.Surface((50,40))
            self.image.fill(GREEN)
            self.rect = self.image.get_rect()
            self.rect.centerx = WIDTH/2
            self.rect.bottom = HEIGHT-10
            #controls the speed
            self.angle = 0
            self.orig_image = self.image
            #self.rect = self.image.get_rect(center=pos)

    def update(self):

        keystate = pygame.key.get_pressed()
        if keystate[pygame.K_LEFT]:
            self.angle -= 5
            self.rotate()
        if keystate[pygame.K_RIGHT]:
            self.angle += 5
            self.rotate()

    def rotate(self):
        self.image = pygame.transform.rotozoom(self.orig_image, self.angle, 1)
        self.rect = self.image.get_rect(center=self.rect.center)


    def shoot(self):
        bullet = Bullets(self.rect.center,self.angle)
        all_sprites.add(bullet)
        bullets.add(bullet)

class Mob(pygame.sprite.Sprite):
    def __init__(self):
        super(Mob,self).__init__()        
        self.image = pygame.Surface((30,40))
        self.image = meteor_img
        self.image = pygame.transform.scale(meteor_img,(50,38))
        self.image.set_colorkey(BLACK)
        self.rect = self.image.get_rect()
        self.radius = int(self.rect.width/2)
        self.rect.x = random.randrange(0,WIDTH - self.rect.width)
        self.rect.y = random.randrange(-100,-40)
        self.speedy = random.randrange(1,8)

    #updating the position of the sprite
    def update(self):
        self.rect.y += self.speedy

        if self.rect.top > HEIGHT + 10:
            self.rect.x = random.randrange(0,WIDTH - self.rect.width)
            self.rect.y = random.randrange(-100,-40)
            self.speedy = random.randrange(1,8)

class Bullets(pygame.sprite.Sprite):
    def __init__(self,pos,angle):

        super(Bullets,self).__init__(pos,angle)        
        # Rotate the image.
        self.image = pygame.Surface((10,20))
        self.image = bullet_img
        self.image = pygame.transform.scale(bullet_img,(50,38))
        self.image = pygame.transform.rotate(bullet_img, angle)
        self.rect = self.image.get_rect()

        speed = 5
        self.velocity_x = math.cos(math.radians(-angle))*speed
        self.velocity_y = math.sin(math.radians(-angle))*speed
        #store the actual position
        self.pos = list(pos)


    def update(self):
        self.pos[0] += self.velocity_x
        self.pos[1] += self.velocity_y
        self.rect.center = self.pos
        if self.rect.bottom <0:
            self.kill()


#load all game graphics
background = pygame.image.load(os.path.join(img_folder,"background.png")).convert()
background_rect = background.get_rect()

player_img = pygame.image.load(os.path.join(img_folder,"arrow.png")).convert()

bullet_img = pygame.image.load(os.path.join(img_folder,"bullet.png")).convert()

meteor_img = pygame.image.load(os.path.join(img_folder,"m.png")).convert()


#creating a group to store sprites to make it easier to deal with them
#every sprite we make goes to this group
all_sprites = pygame.sprite.Group()

mobs = pygame.sprite.Group()

bullets = pygame.sprite.Group()
player = Player()
all_sprites.add(player)

for i in range(8):
    m = Mob()
    all_sprites.add(m)
    mobs.add(m)

running = True
while running:
    clock.tick(FPS)

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_SPACE:
                player.shoot()

    #Update
    all_sprites.update()

    #checking if a bullet hits a mob
    hits = pygame.sprite.groupcollide(mobs,bullets,True,True)
    for hit in hits:
        m = Mob()
        all_sprites.add(m)
        mobs.add(m)
    hits = pygame.sprite.spritecollide(player,mobs, False,pygame.sprite.collide_circle)


    #drawing the new sprites here
    screen.fill(BLACK)
    #show the background image
    screen.blit(background,background_rect)
    all_sprites.draw(screen)
    pygame.display.flip()

pygame.quit()

有什么意见吗?

推荐答案

您正在将 posangle 传递给 __init__pygame.sprite.Sprite 的方法在这里,

You're passing the pos and the angle to the __init__ method of pygame.sprite.Sprite here,

super(Bullets,self).__init__(pos,angle)

但是您只能传递将添加此精灵实例的精灵组.所以只需删除这些参数:

but you can only pass sprite groups to which this sprite instance will be added. So just remove those arguments:

super(Bullets,self).__init__()

这篇关于* 后的 Add() 参数必须是可迭代的,而不是 int Pygame的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 16:16