Why Is My Pygame Sprite, in a Group, Not Drawn - Attributeerror: 'Group' Object Has No Attribute 'Blitme'

Why is my PyGame Sprite, in a Group, not drawn - AttributeError: 'Group' object has no attribute 'blitme'

The method blitme doesn't exist in pygame.sprite.Group. You cannot invoke a method on a pygame.sprite.Group object, that doesn't exist. But you don't need blitme at all. All you have to do is to invoke 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 for the position.

For instance:

letters.draw()

pygame.sprite.Group.draw() and pygame.sprite.Group.update() are methods which are provided by pygame.sprite.Group.

The former delegates the to the update mehtod of the contained pygame.sprite.Sprites - you have to implement the method. See pygame.sprite.Group.update():

Calls the update() method on all Sprites in the Group [...]

The later 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. [...]

Python is throwing AttributeError: 'Group' object has no attribute 'blitme'

The code is passing the Sprite.Group named aliens (note the "s") as the parameter alien (No "s") into update_screen().

Since alien is a group, it does not have the blitme() function defined.

def update_screen(ai_settings,screen,ship,alien,bullets):
##更新屏幕上的图像,并切换到屏幕
#每次循环时都重绘屏幕
screen.fill(ai_settings.bg_color)
for bullet in bullets:
bullet.draw_bullet() # Should this be bullets.draw(screen) ?

ship.blitme() # Single sprite, this is OK
alien.blitme() # <-- HERE, remove this
aliens.draw(screen) # Draws all the alien sprites too
#让最近绘制的屏幕可见
pygame.display.flip()

I think this function should probably have the parameter renamed alien -> aliens since there is no aliens parameter passed, yet it's referenced.

def update_screen(ai_settings,screen,ship,aliens,bullets):
##更新屏幕上的图像,并切换到屏幕
#每次循环时都重绘屏幕
screen.fill(ai_settings.bg_color)
bullets.draw(screen) # Should this be bullets.draw(screen) ?
ship.blitme()
aliens.draw(screen)
#让最近绘制的屏幕可见
pygame.display.flip()

AttributeError: 'Group' object has no attribute 'update_bullets'

In your traceback error it says:

Traceback (most recent call last):
File "D:/snake/venv/Alien_Invasion/main_game.py", line 2, in <module>
from pygame import Group
importError: cannot import name 'Group' from 'pygame' (D:\snake\venv\lib\site-packages\pygame\__init__.py)

Which says there should be this line:

from pygame import Group

on line 2 of your main_game.py module. However in the code you have included, that is not there:

main_game.py

import pygame

from settings import Settings
from ship import Ship

Are you sure you are showing he correct code?

Ignoring the code and just looking at the error message it shows that you had this line:

from pygame import Group

There is no pygame.Group which is why you are getting the error telling you that. I think you are looking for pygame.sprite.Group

Edit

Now that we have the right code and the right question :-)

Sprite groups have some built-in methods which can be seen in the docs here. One of these is update() which will then call the update() function in all the sprites in the group. Your problem is that you named your sprite function update_bullets() not just update() and then you tried to call update_bullets() on the group and the group does not have that method.

So to fix your code you have to rename your Bullet method to just update() and then call the sprite.Group method update() to have it called on all the bullets in the group.

You have the same issue with your draw_bullet() method but you worked around it by iterating over the group and calling your method. That works, but you should correct it to take advantage of the builtin group method. You need to change it to just draw() and then you can call it on all the bullets by just calling bullets.draw(). As I said, in your code now, because you are not using the name that the sprite.Group knows how to find, you are iterating over the group. If you rename it, you can then replace this:

for bullet in bullets.sprites():
bullet.draw_bullet()

with just:

bullets.draw()

In alien invation pygame why this error shows 'pygame.Rect' object has no attribute 'get_rect' ?

screen is a pygame.Rect object which as stated has no get_rect method. You need to pass a Surface object into screen not a Rect object.

See:

https://www.pygame.org/docs/ref/display.html#pygame.display.set_mode

AttributeError: 'Group' object has no attribute '_check_edges' (based off the game project in Python Crash Course,2ndEdition(EricMatthes)

Here's your error:

AttributeError: 'Group' object has no attribute '_check_edges'

When you see an error like this, it's telling you that the object you're using to call _check_edges() doesn't have access to that method. It's also telling you that the object you're using is a Group.

So let's look at the code that's calling _check_edges():

def _scarfy_edge(self):
for scarfy in self.scarfys.sprites():
if self.scarfys._check_edges():
self._respond
break

You have self.scarfys._check_edges(). But self.scarfys is a Pygame Group object; it's not a Scarfy. I think this is a typo, and you meant to write self.scarfy._check_edges().

Typos are simple mistakes, but their effects in a larger project can be hard to untangle.



Related Topics



Leave a reply



Submit