0
votes

import pygame
screen = pygame.display.set_mode((800, 600))

bullet_group = pygame.sprite.Group()

while gameRun:

clock.tick(FPS)

draw_bg()

enemy.draw()
enemy.update_animation()

player.update_animation()
player.draw()
player.move(move_left, move_right)

bullet_group.update()
bullet_group.draw(screen)

if player.alive: 
    if shoot:
        bullet = Bullet(player.rect.centerx, player.rect.centery, player.direction, bullerRect)
        bullet_group.add(bullet)
    if player.in_air:
        player.update_action(2)
    elif move_right or move_left:
        player.update_action(1)# 1 is run
    else:
        player.update_action(0)# 0 is idle

for event in pygame.event.get():
    if event.type == pygame.QUIT:
        gameRun = False
    
    if event.type == pygame.KEYDOWN:
        if event.key == pygame.K_a:
            move_left = True
        if event.key == pygame.K_d:
            move_right = True
        if event.key == pygame.K_SPACE:
            shoot = True
        if event.key == pygame.K_w and player.alive:
            player.jump = True
    if event.type == pygame.KEYUP:
        if event.key == pygame.K_a:
            move_left = False
        if event.key == pygame.K_d:
            move_right = False
        if event.key == pygame.K_SPACE:
            shoot = False

pygame.display.update()

Error:

Traceback (most recent call last):

File "c:\Users\gopal\Desktop\Misc Projects\main.py", line 136, in

  bullet_group.draw(SCREEN)

File "C:\Users\gopal\AppData\Local\Programs\Python\Python39\lib\site-packages\pygame\sprite.py", line 546, in draw

 surface.blits((spr.image, spr.rect) for spr in sprites)

TypeError: Source objects must be a surface

1
We can't answer the question because the Bullet class is missing from your code. Show the Bullet class. - Rabbid76

1 Answers

0
votes

The problem isn't in the code in the question. The problem is related to the Bullet class.

pygame.sprite.Group.draw()is a methods which are provided by pygame.sprite.Group.It uses the image and rect attributes of the contained pygame.sprite.Sprites to draw the objects — you have to ensure that the pygame.sprite.Sprites have the required attributes. See pygame.sprite.Group.draw():

Draws the contained Sprites to the Surface argument. This uses the Sprite.image attribute for the source surface, and Sprite.rect. [...]

Therefore self.image must be a pygame.Surface object. Use pygame.image.load to load an image from a file:

self.image = pygame.image.load("your-image-filepath.png")