TypeError:* 之后的 pygame.sprite.Sprite.add() 参数必须是可迭代的,而不是 int。无法修复此错误

问题描述 投票:0回答:0

嗨,我收到这个错误。一切正常,但是当我在 main.py 中调用 hunter_fires 并在运行程序后尝试按向上键时,这就是我收到错误的时候。请帮忙,谢谢。 我猜问题出在与 Bullet_Animate.py 类相关的 hunter.py hunter_fires() 方法

------------------------main.py-------------------- ----------------------

import pygame
import random
from TileMapping import TileMapping
from Diamond_Maker import Diamond_Maker
from Game_portals import Game_portals
from GameController import Game_handler
from Hunter import Hunter
 
#Initializing variable for using 2D vectors
vector = pygame.math.Vector2
 
#Initializing pygame
pygame.init()
 
#window display is being set
#tile size is 32 x 32
#1280/32 = 40 tiles wide
#736/32 = 21 tiles high
WIN_WIDTH = 1280
WIN_HEIGHT = 672
display_surface = pygame.display.set_mode((WIN_WIDTH, WIN_HEIGHT))
pygame.display.set_caption("Gremlin Knight")
 
#Setting seconds and clock
SECONDS = 60    #Frames per second
clock = pygame.time.Clock()
 
 
 
#Create the tile map
#0 -> no tile, 1 -> dirt, 2-5 -> platforms, 6 -> diamond maker, 7-8 -> portals, 9 -> player
#21 rows and 40 columns
game_tile_map = [
    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    [7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 0],
    [4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5, 0, 0, 0, 0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 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
]
 
#Creating groups
game_tile_group = pygame.sprite.Group()
game_platform_group = pygame.sprite.Group()
 
hunter_group = pygame.sprite.Group()
bullet_group = pygame.sprite.Group()
 
gremlins_group = pygame.sprite.Group()
 
game_portal_group = pygame.sprite.Group()
 
diamond_group = pygame.sprite.Group()
#diamond maker will be added to the game tile group. it is just a class with some animation funtions
#so doesnot need a group
 
#making Tile objects from the game_tile_map
#Looping thrpugh the 21 lists (rows) in the tile map(r moves us down the map)
for r in range(len(game_tile_map)):
    # Loop through the 40 elements in a given list (cols) (c moves us across the map)
    for c in range(len(game_tile_map[r])):
        # Dirt tiles
        if game_tile_map[r][c] == 1:
            TileMapping(c * 32, r * 32, 1, game_tile_group)  #1 is an image int. for y-val is r as it moves us down the map
            #Platform tiles
        elif game_tile_map[r][c] == 2:
            TileMapping(c * 32, r * 32, 2, game_tile_group, game_platform_group)
        elif game_tile_map[r][c] == 3:
            TileMapping(c * 32, r * 32, 3, game_tile_group, game_platform_group)
        elif game_tile_map[r][c] == 4:
            TileMapping(c * 32, r * 32, 3, game_tile_group, game_platform_group)
        elif game_tile_map[r][c] == 5:
            TileMapping(c * 32, r * 32, 5, game_tile_group, game_platform_group)
        #Diamond maker
        elif game_tile_map[r][c] == 6:
            Diamond_Maker(c * 32, r * 32, game_tile_group)
        #Portals
        elif game_tile_map[r][c] == 7:
            Game_portals(c * 32, r * 32, "green", game_portal_group)
        elif game_tile_map[r][c] == 8:
            Game_portals(c * 32, r * 32, "purple", game_portal_group)
        #Our Hunter
        elif game_tile_map[r][c] == 9:
            hunter_obj = Hunter(c * 32 - 32, r * 32 + 32, game_platform_group, game_portal_group, bullet_group) #ddd 32 to push it down
            hunter_group.add(hunter_obj)
 
 
#Loading a background image
background_pic = pygame.transform.scale(pygame.image.load("images/background.png"), (1280, 736))
background_rect = background_pic.get_rect()
background_rect.topleft = (0, 0)
 
#Now i create a gameController object
game_control = Game_handler()
 
#--------------GAME LOOP-------------------------
gameOn = True
while gameOn:
    #checking if the user wants to quit
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            gameOn = False
        if event.type == pygame.KEYDOWN:
            #Hunter wants to jump
            if event.key == pygame.K_SPACE:
                hunter_obj.hunter_jump()
            #Hunter wants to fire
            if event.key == pygame.K_UP:
                hunter_obj.hunter_fires()
 
    #background blit
    display_surface.blit(background_pic, background_rect)
 
    #tile is drawn here and diamond maker is updates
    game_tile_group.update()
    game_tile_group.draw(display_surface)
 
    #Drawing and updating portal groups
    game_portal_group.update()
    game_portal_group.draw(display_surface)
 
    #Draw hunter group and update
    hunter_group.update()
    hunter_group.draw(display_surface)
 
    #Updating the game and drawing
    game_control.update()
    game_control.draw()
 
    #Updating the bullet group and drawing it
    bullet_group.update()
    bullet_group.draw(display_surface)
 
 
    # This will update the display window and tick the clock
    pygame.display.update()
    clock.tick(SECONDS)
 
# The game ends
pygame.quit()
 
 
 
--------------------------------Hunter.py--------------------------------------------
#This class will inherit from sprite class
#This class is the hunter that the user controls
import pygame.sprite
import pygame
 
 
#Initializing variable for using 2D vectors
vector = pygame.math.Vector2
 
#window display is being set
WIN_WIDTH = 1280
WIN_HEIGHT = 672
 
class Hunter(pygame.sprite.Sprite):
    def __init__(self, xcor, ycor, g_platform, g_portal, bullet_group):
        """Initialize the player"""
        super().__init__()
 
        #Declaring constant variables that will eventually affect the gameplay of the Hunter
        self.ACCELERATION_HORIZONTAL = 2 #how fast hunter moves when we press a key
        self.FRICTION_HORIZONTAL = 0.15 #SLIPPERY slide motion
        self.ACCELERATION_VERTICAL = 0.8  # serves as Gravity. when we move off of a plaform, we gonna move, fall and accelarate under force of gravity
        self.SPEED_JUMPING_VERTICAL = 18  # Determines how high the Hunter can jump
        self.BEGINNING_HEALTH = 100
        '''Speed is the time rate at which an object is moving along a path,
           while velocity is the rate and direction of an object's movement.'''
 
        # Empty sprite lists to hold Animation frames for Hunter movements
        self.spritesList_move_right = []
        self.spritesList_move_left = []
        self.spritesList_idle_right = []
        self.spritesList_idle_left = []
        self.spritesList_jump_right = []
        self.spritesList_jump_left = []
        self.spritesList_attack_right = []
        self.spritesList_attack_left = []
 
        #Now uploading the moving animation images to the game display
        self.spritesList_move_right.append(
            pygame.transform.scale(pygame.image.load("images/player/run/Run (1).png"), (64, 64))) #original img too big 508 pixels
        self.spritesList_move_right.append(
            pygame.transform.scale(pygame.image.load("images/player/run/Run (2).png"), (64, 64)))
        self.spritesList_move_right.append(
            pygame.transform.scale(pygame.image.load("images/player/run/Run (3).png"), (64, 64)))
        self.spritesList_move_right.append(
            pygame.transform.scale(pygame.image.load("images/player/run/Run (4).png"), (64, 64)))
        self.spritesList_move_right.append(
            pygame.transform.scale(pygame.image.load("images/player/run/Run (5).png"), (64, 64)))
        self.spritesList_move_right.append(
            pygame.transform.scale(pygame.image.load("images/player/run/Run (6).png"), (64, 64)))
        self.spritesList_move_right.append(
            pygame.transform.scale(pygame.image.load("images/player/run/Run (7).png"), (64, 64)))
        self.spritesList_move_right.append(
            pygame.transform.scale(pygame.image.load("images/player/run/Run (8).png"), (64, 64)))
        self.spritesList_move_right.append(
            pygame.transform.scale(pygame.image.load("images/player/run/Run (9).png"), (64, 64)))
        self.spritesList_move_right.append(
            pygame.transform.scale(pygame.image.load("images/player/run/Run (10).png"), (64, 64)))
        for sprite in self.spritesList_move_right:
            self.spritesList_move_left.append(pygame.transform.flip(sprite, True, False))
 
        # #Idle images loaded for the game display
        self.spritesList_idle_right.append(
            pygame.transform.scale(pygame.image.load("images/player/idle/Idle (1).png"), (64, 64)))
        self.spritesList_idle_right.append(
            pygame.transform.scale(pygame.image.load("images/player/idle/Idle (2).png"), (64, 64)))
        self.spritesList_idle_right.append(
            pygame.transform.scale(pygame.image.load("images/player/idle/Idle (3).png"), (64, 64)))
        self.spritesList_idle_right.append(
            pygame.transform.scale(pygame.image.load("images/player/idle/Idle (4).png"), (64, 64)))
        self.spritesList_idle_right.append(
            pygame.transform.scale(pygame.image.load("images/player/idle/Idle (5).png"), (64, 64)))
        self.spritesList_idle_right.append(
            pygame.transform.scale(pygame.image.load("images/player/idle/Idle (6).png"), (64, 64)))
        self.spritesList_idle_right.append(
            pygame.transform.scale(pygame.image.load("images/player/idle/Idle (7).png"), (64, 64)))
        self.spritesList_idle_right.append(
            pygame.transform.scale(pygame.image.load("images/player/idle/Idle (8).png"), (64, 64)))
        self.spritesList_idle_right.append(
            pygame.transform.scale(pygame.image.load("images/player/idle/Idle (9).png"), (64, 64)))
        self.spritesList_idle_right.append(
            pygame.transform.scale(pygame.image.load("images/player/idle/Idle (10).png"), (64, 64)))
        for sprite in self.spritesList_idle_right:
            self.spritesList_idle_left.append(pygame.transform.flip(sprite, True, False))
 
        # Jump animation images loaded
        self.spritesList_jump_right.append(
            pygame.transform.scale(pygame.image.load("images/player/jump/Jump (1).png"), (64, 64)))
        self.spritesList_jump_right.append(
            pygame.transform.scale(pygame.image.load("images/player/jump/Jump (2).png"), (64, 64)))
        self.spritesList_jump_right.append(
            pygame.transform.scale(pygame.image.load("images/player/jump/Jump (3).png"), (64, 64)))
        self.spritesList_jump_right.append(
            pygame.transform.scale(pygame.image.load("images/player/jump/Jump (4).png"), (64, 64)))
        self.spritesList_jump_right.append(
            pygame.transform.scale(pygame.image.load("images/player/jump/Jump (5).png"), (64, 64)))
        self.spritesList_jump_right.append(
            pygame.transform.scale(pygame.image.load("images/player/jump/Jump (6).png"), (64, 64)))
        self.spritesList_jump_right.append(
            pygame.transform.scale(pygame.image.load("images/player/jump/Jump (7).png"), (64, 64)))
        self.spritesList_jump_right.append(
            pygame.transform.scale(pygame.image.load("images/player/jump/Jump (8).png"), (64, 64)))
        self.spritesList_jump_right.append(
            pygame.transform.scale(pygame.image.load("images/player/jump/Jump (9).png"), (64, 64)))
        self.spritesList_jump_right.append(
            pygame.transform.scale(pygame.image.load("images/player/jump/Jump (10).png"), (64, 64)))
        for sprite in self.spritesList_jump_right:
            self.spritesList_jump_left.append(pygame.transform.flip(sprite, True, False))
 
        # Attack animation image loaded
        self.spritesList_attack_right.append(
            pygame.transform.scale(pygame.image.load("images/player/attack/Attack (1).png"), (64, 64)))
        self.spritesList_attack_right.append(
            pygame.transform.scale(pygame.image.load("images/player/attack/Attack (2).png"), (64, 64)))
        self.spritesList_attack_right.append(
            pygame.transform.scale(pygame.image.load("images/player/attack/Attack (3).png"), (64, 64)))
        self.spritesList_attack_right.append(
            pygame.transform.scale(pygame.image.load("images/player/attack/Attack (4).png"), (64, 64)))
        self.spritesList_attack_right.append(
            pygame.transform.scale(pygame.image.load("images/player/attack/Attack (5).png"), (64, 64)))
        self.spritesList_attack_right.append(
            pygame.transform.scale(pygame.image.load("images/player/attack/Attack (6).png"), (64, 64)))
        self.spritesList_attack_right.append(
            pygame.transform.scale(pygame.image.load("images/player/attack/Attack (7).png"), (64, 64)))
        self.spritesList_attack_right.append(
            pygame.transform.scale(pygame.image.load("images/player/attack/Attack (8).png"), (64, 64)))
        self.spritesList_attack_right.append(
            pygame.transform.scale(pygame.image.load("images/player/attack/Attack (9).png"), (64, 64)))
        self.spritesList_attack_right.append(
            pygame.transform.scale(pygame.image.load("images/player/attack/Attack (10).png"), (64, 64)))
        for sprite in self.spritesList_attack_right:
            self.spritesList_attack_left.append(pygame.transform.flip(sprite, True, False))
 
        # Load image and get rect
        self.current_sprite = 0
        self.image = self.spritesList_idle_right[self.current_sprite]
        self.rect = self.image.get_rect()
        self.rect.bottomleft = (xcor, ycor)
 
        # #Attaching the sprite groups to hunter objects
        self.g_platform = g_platform
        self.g_portal = g_portal
        self.bullet_group = bullet_group
 
        #idle is fine. when hunter not moving
        #move is pressing a key to move right or left
        #for attack/jump, when I press a key the animation should work even when i am not pressing the key
        #declaring Booleans to check this animation working
        self.jumping_animation = False
        self.shooting_animation = False
 
        #Sounds associated with the hunter
        #Sounds loaded
        self.jumping_audio = pygame.mixer.Sound("sounds/jump_sound.wav")
        self.attack_audio = pygame.mixer.Sound("sounds/slash_sound.wav")
        self.collide_portal_audio = pygame.mixer.Sound("sounds/portal_sound.wav")
        self.hunter_hit_audio = pygame.mixer.Sound("sounds/player_hit.wav")
 
        #To help us with motion, declaring Kinematics vectors
        #Kinematics is the study of motion of a system of bodies
        #without directly considering the forces or potential fields affecting the motion.
        self.hunter_position = vector(xcor, ycor)
        self.hunter_velocity = vector(0, 0)
        # in x direction i dont wanna accelerate unless press a key
        #in y dir, i want vertical acceleration be due to gravity
        self.hunter_acceleration = vector(0, self.ACCELERATION_VERTICAL)
 
        # Set intial player values
        self.hunter_health = self.BEGINNING_HEALTH
        self.starting_xcor = xcor
        self.starting_ycor = ycor
 
    def update(self):
        """Update the player"""
        self.move()
        self.check_hunter_collisions()
        self.check_hunter_animations()
 
 
    def move(self):
        #The acceleration vector is set
        #acceleration will trigger the movements
        # 0 bcoz initially hunter dont move unless a key is pressed
        # vertical_acc to be present in Y direction due to gravity
        self.hunter_acceleration = vector(0, self.ACCELERATION_VERTICAL)
 
        #Now check if User pressing a key, the x-component of the acceleration to be non-Zero
        keys = pygame.key.get_pressed()
        if keys[pygame.K_LEFT]:
            self.hunter_acceleration.x = -1 * self.ACCELERATION_HORIZONTAL #.x -> as we changing x-component, -1 bcoz moving left
            self.hunter_action_animation(self.spritesList_move_left, 0.5)
        elif keys[pygame.K_RIGHT]:
            self.hunter_acceleration.x = self.ACCELERATION_HORIZONTAL
            self.hunter_action_animation(self.spritesList_move_right, 0.5)
        else:
            if self.hunter_velocity.x > 0:
                self.hunter_action_animation(self.spritesList_idle_right, .5)   # velocity > 0 i'm facing right
            else:
                self.hunter_action_animation(self.spritesList_idle_left, .5)
 
 
        # Calculate new kinematics values: (4, 1) + (2, 8) = (6, 9) based on the updated acceleration
        self.hunter_acceleration.x -= self.hunter_velocity.x * self.FRICTION_HORIZONTAL #more friction, faster we go. thats multiplying by x-cor of our velocity
 
        self.hunter_velocity += self.hunter_acceleration
        self.hunter_position += self.hunter_velocity + (0.5*self.hunter_acceleration)
 
        # Update rect based on kinematic calculations and add wrap around movement
        # so we run off the right, we come over on the left
        if self.hunter_position.x < 0: #we crossed left side of the screen, should come out of the right side of the screen
            self.hunter_position.x = WIN_WIDTH
        elif self.hunter_position.x > WIN_WIDTH:
            self.hunter_position.x = 0
 
        self.rect.bottomleft = self.hunter_position
 
 
    def check_hunter_collisions(self):
        """Check for collisions with platforms and portals"""
 
        # Collision check between player and platforms when falling
        if self.hunter_velocity.y > 0:       # >0 we moving down
            #we can collide with lot of platforms, so putting all platforms collided with in a list
            collided_platforms = pygame.sprite.spritecollide(self, self.g_platform, False)
            if collided_platforms:
                self.hunter_position.y = collided_platforms[0].rect.top + 1
                self.hunter_velocity.y = 0 #ensures we stop moving down
 
        # Collision check between player and platform if jumping up
        if self.hunter_velocity.y < 0:        #>0 we moving up
            collided_platforms = pygame.sprite.spritecollide(self, self.g_platform, False)
            if collided_platforms:
                self.hunter_velocity.y = 0 #do not move up
                #if velocity slow, hunter may land on platform
                #if velocity fast, hunter could be inside the platform
                while pygame.sprite.spritecollide(self, self.g_platform, False):
                    self.hunter_position.y += 1    #pushed hunter down until there is no contact between hunter and paltform
                    self.rect.bottomleft = self.hunter_position
 
        # Collision check for portals
        if pygame.sprite.spritecollide(self, self.g_portal, False):
            self.collide_portal_audio.play()
            # Determine which portal hunter is moving to
            # Left and right
            if self.hunter_position.x > WIN_WIDTH // 2:  # > means hunter on right, will come out of left
                self.hunter_position.x = 86 # 0 puts in a loop
            else:   #else Hunter on left side, will come out of right
                self.hunter_position.x = WIN_WIDTH - 150
 
            # Top and bottom
            if self.hunter_position.y > WIN_HEIGHT // 2:   # > means we are bottom of the screen, hunter will be warped bottom of the screen
                self.hunter_position.y = 64
            else:
                self.hunter_position.y = WIN_HEIGHT - 132
 
            self.rect.bottomleft = self.hunter_position
 
 
    #Checks if the Hunter's jump/fire animation works
    def check_hunter_animations(self):
        #hunter's jump is animated
        if self.jumping_animation:
            if self.hunter_velocity.x > 0:
                self.hunter_action_animation(self.spritesList_jump_right, .1)
            else:
                self.hunter_action_animation(self.spritesList_jump_left, .1)
 
        #Animate the player attack
        if self.shooting_animation:
            if self.hunter_velocity.x > 0:
                self.hunter_action_animation(self.spritesList_attack_right, .25)
            else:
                self.hunter_action_animation(self.spritesList_attack_left, .25)
 
    #Hunter jumps upward if on a platform
    def hunter_jump(self):
        # Only jump if on a platform
        if pygame.sprite.spritecollide(self, self.g_platform, False):
            self.jumping_audio.play()
            # -1 because hunter moves up
            self.hunter_velocity.y = -1 * self.SPEED_JUMPING_VERTICAL   #this launches the hunter in the air
            self.jumping_animation = True
 
    #fires Bullet from a sword
    def hunter_fires(self):
        from Bullet_Animate import Bullet_Animate
 
        self.attack_audio.play()
        Bullet_Animate(self.rect.centerx, self.rect.centery, self.bullet_group, self)
        self.shooting_animation = True
 
    #This method resets the hunter's position
    def hunter_position_reset(self):
        #self.hunter_velocity = vector(0, 0)
        self.hunter_position = vector(self.starting_xcor, self.starting_ycor)
        self.rect.bottomleft = self.hunter_position
 
    #This method animates the HUnter's actions
    def hunter_action_animation(self, sprite_list, hunter_speed):
        if self.current_sprite < len(sprite_list) -1:
            self.current_sprite += hunter_speed
        else:
            self.current_sprite = 0
            #this ends the endless jump animation ends
            if self.jumping_animation:
                self.jumping_animation = False
            #End the attack animation
            if self.shooting_animation:
                self.shooting_animation = False
 
        self.image = sprite_list[int(self.current_sprite)]
 
 
-----------------------------Bullet_Animate.py------------------------------------------------
#This class will inherit from Sprite class too
#I want to keep things in groups and collide with things
 
#Bullet is a projectile launched by the hunter
 
import pygame.sprite
 
class Bullet_Animate(pygame.sprite.Sprite):
 
    #Bullet being initialized
    #hunter param --> to get position of the hunter and determine how far the bullet should travel
    def __int__(self, x_Val, y_val, bullet_group, hunter):       #x, y val comes from the hunter. The bullet will not be drawn on tile map
        super().__init__()
 
        # Set constant variables
        self.BULLET_VELOCITY = 20 # 20 pixels per loop in left/right direction.
        self.BULLET_RANGE = 500  #when I create a bullet and moves 500 pixels, it destroys itself after 500 pixels
 
        # Load image and get rect
        if hunter.hunter_velocity.x > 0:     # > 0 is slash facing left
            self.image = pygame.transform.scale(pygame.image.load("images/player/shoot.png"), (32, 32))
        else:
            self.image = pygame.transform.scale(
                pygame.transform.flip(pygame.image.load("images/player/shoot.png"), True, False), (32, 32))
            self.BULLET_VELOCITY = -1 * self.BULLET_VELOCITY   #when facing left, we need to negate our velocity
 
        self.rect = self.image.get_rect()
        self.rect.center = (x_Val, y_val)
 
        self.starting_xCor = x_Val
 
        bullet_group.add(self) #adding current bullet to Bullet group
 
    #As i will move the bullet so the update method handles this
    def update(self):
        self.rect.x += self.BULLET_VELOCITY       #neg or pos depending on Hunter's movement
 
        # if bullet > RANGE, then it is killed
        #when fired to left, abs is negative.
        if abs(self.rect.x - self.starting_xCor) > self.BULLET_RANGE:   #using abs for absolute val
            self.kill()

I wrote the code and expecting to solve an error
python java error-handling data-science computer-science
© www.soinside.com 2019 - 2024. All rights reserved.