I am making a pygame game for a school project, and I have been doing a lot of debugging. I just finished fixing the classes by giving it two positional arguments instead of 1, after that when I tried to run the game it gave me errors on lines 454 and 441, however my code is only around 180 lines long. (My aim with all of this was making another sort of player that the player could move called an enemy) It could all be a problem with my library and not the code itself. I will include the error code below and the code itself.
Error code:
Traceback (most recent call last):
File "C:\Users\(Myname)\PycharmProjects\LearningPython3\venv\lib\site-packages\pygame\sprite.py", line 441, in add
self.add(*sprite)
TypeError: pygame.sprite.AbstractGroup.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\(myname)\PycharmProjects\LearningPython3\App.py", line 147, in <module>
enemy_group.add(enemy_image)
File "C:\Users\(myname)\PycharmProjects\LearningPython3\venv\lib\site-packages\pygame\sprite.py", line 454, in add
sprite.add_internal(self)
AttributeError: 'pygame.Surface' object has no attribute 'add_internal'
The code in parts (Some parts are not included):
# bullet class
class Bullet (pygame.sprite.Sprite):
def __init__(self,pos_x,pos_y):
super().__init__()
self.image = pygame.Surface((50,10))
self.image.fill((255,255,0))
self.rect = self.image.get_rect(center = (pos_x,pos_y))
def update(self):
self.rect.x += 5
#Making the enemy work
enemy_speed_factor = 1.5
class Enemy:
def __init__(self, enemy_image):
"""Initialize the enemy and set its starting position"""
self.screen = screen
#Load the enemy image and get its rect
self.image = pygame.image.load("Enemy4.png")
self.rect = self.image.get_rect()
self.screen_rect = screen.get_rect()
#start each new enemy at the bottom of the screen
self.rect.centerx = self.screen_rect.centerx
self.rect.bottom = self.screen_rect.bottom
#store a decimal value for the enemys x and y center
self.centerx = float(self.rect.centerx)
self.centery = float(self.rect.centery)
#Movement flag
self.moving_right = False
self.moving_left = False
self.moving_down = False
self.moving_up = False
def update(self):
"""Update the enemys position based on the movement flag"""
#Upade the enemy's center value, not the rect.
if self.moving_right and self.rect.right < self.screen_rect.right:
self.centerx += self.ai_settings.enemy_speed_factor
if self.mobing_left and self.rect.left >0:
self.centerx -= self.ai_settings.enemy_speed_factor
if self.moving_down and self.rect.bottom < self.screen_rect.bottom:
self.centery =+ self.ai_settings.enemy_speed_factor
if self.moving_down and self.rect.top > self.screen_rect.top:
self.centery -= self.ai_settings.enemy_speed_factor
#Update rect object from self.center
if self.moving_up or self.moving_down:
self.rect.centery = self.centery
if self.moving_left or self.moving_right:
self.rect.centerx = self.centerx
def blitme(selfself, self):
"""draw the enemy at its current location"""
self.screen.blit(self.image, self.rect)
#making the movements for the enemy
def check_keydown_events(events, enemy):
"""responds to keypresses"""
if event.key == pygame.K_RIGHT:
enemy.moving_right = True
elif event.key == pygame.K_LEFT:
enemy.moving_left = True
elif event.key == pygame.K_DOWN:
enemy.moving_down = True
elif event.key == pygame.K_UP:
enemy.moving_up = True
def check_keyup_evets(event, enemy):
"""responds to key releases"""
if event.key == pygame.K_RIGHT:
enemy.moving_right = False
elif event.key == pygame.K_LEFT:
enemy.moving_left = False
elif event.key == pygame.K_DOWN:
enemy.moving_down = False
elif event.key == pygame.K_UP:
enemy.moving_up = False
def check_keyup_events(event, enemy):
pass
def check_events(enemy):
"""Respond to keypresses and mouse events"""
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
elif event.type == pygame.KEYDOWN:
check_keydown_events(event, enemy)
elif event.type == pygame.KEYUP:
check_keyup_events(event, enemy)
def update_screen(ai_settings, screen, enemy):
"""Update images on the screen and flip to the new screen"""
#Redeaw the screen during each pass through the loop
screen.fill(ai_settings.bg_color)
enemy.blitme()
#Make the most recently drawn screeen visible
pygame.display.flip()
def blitme(self):
self.screen.blit(self)
# enemy
enemy_image = pygame.image.load("Enemy4.png")
enemy = Enemy(enemy_image)
enemy_group = pygame.sprite.Group()
enemy_group.add(enemy)