Sprite Group will not blit [duplicate] - python

I am a beginner for pygame and I am imitating the game code "alien invasion" in the book "Python Crash Course" to program a game named "Alphabet zoo". In this game, different letters fall down from the top of the screen after a time interval and the letter will vanish while you strike the corresponding key on the keyboard. The x position of each letter is random and the falling speed will accelerate as the game progress. Game will end under a certain condition(e.g. screen height is occupied by letters). This seems a great chanllenge for me. During the first stage, my codes are simplified for same letters 'A' rather than varied letters. They are as the following:
alphabet_zoo.py
import sys
import pygame
from pygame.locals import *
from settings import Settings
from letter import Letter
import game_functions as gf
from pygame.sprite import Group
def run_game():
pygame.init()
az_settings =Settings()
screen = pygame.display.set_mode((0,0), RESIZABLE)
pygame.display.set_caption("Alphabet Zoo")
letter = Letter(az_settings, screen)
letters = Group()
while True:
gf.check_events(az_settings, screen, letters)
letters.update()
gf.update_screen(az_settings, screen, letters)
run_game()
settings.py
class Settings():
def __init__(self):
self.bg_color = (0, 0, 0)
self.letter_speed_factor = 10.5
game_functions.py
import sys
import pygame
from letter import Letter
def letter_generator(az_settings, screen, letters, lag_time):
new_letter = Letter(az_settings, screen)
letters.add(new_letter)
def check_events(az_settings, screen, letters):
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_a:
print('a')
def update_screen(az_settings, screen, letters):
screen.fill(az_settings.bg_color)
letters.blitme()
letters.update()
pygame.display.flip()
letter.py
import pygame
import random
from pygame.sprite import Sprite
class Letter(Sprite):
def __init__(self, az_settings, screen):
super().__init__()
self.screen = screen
self.az_settings = az_settings
self.image = pygame.image.load('images/A.png')
self.rect = self.image.get_rect()
self.screen_rect = screen.get_rect()
self.rect.centerx = random.randint(0, self.screen_rect.right)
self.rect.top = self.screen_rect.top
self.center = float(self.rect.centerx)
def blitme(self):
self.screen.blit(self.image, self.rect)
def update(self):
if self.rect.bottom < self.screen_rect.bottom:
self.rect.centery += self.az_settings.letter_speed_factor
I think I should use sprite in the codes. Unfortunately, the program hints " AttributeError: 'Group' object has no attribute 'blitme'" after running. I would appreciate a lot if you help me with the problem.

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

Related

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

I am a beginner for pygame and I am imitating the game code "alien invasion" in the book "Python Crash Course" to program a game named "Alphabet zoo". In this game, different letters fall down from the top of the screen after a time interval and the letter will vanish while you strike the corresponding key on the keyboard. The x position of each letter is random and the falling speed will accelerate as the game progress. Game will end under a certain condition(e.g. screen height is occupied by letters). This seems a great chanllenge for me. During the first stage, my codes are simplified for same letters 'A' rather than varied letters. They are as the following:
alphabet_zoo.py
import sys
import pygame
from pygame.locals import *
from settings import Settings
from letter import Letter
import game_functions as gf
from pygame.sprite import Group
def run_game():
pygame.init()
az_settings =Settings()
screen = pygame.display.set_mode((0,0), RESIZABLE)
pygame.display.set_caption("Alphabet Zoo")
letter = Letter(az_settings, screen)
letters = Group()
while True:
gf.check_events(az_settings, screen, letters)
letters.update()
gf.update_screen(az_settings, screen, letters)
run_game()
settings.py
class Settings():
def __init__(self):
self.bg_color = (0, 0, 0)
self.letter_speed_factor = 10.5
game_functions.py
import sys
import pygame
from letter import Letter
def letter_generator(az_settings, screen, letters, lag_time):
new_letter = Letter(az_settings, screen)
letters.add(new_letter)
def check_events(az_settings, screen, letters):
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_a:
print('a')
def update_screen(az_settings, screen, letters):
screen.fill(az_settings.bg_color)
letters.blitme()
letters.update()
pygame.display.flip()
letter.py
import pygame
import random
from pygame.sprite import Sprite
class Letter(Sprite):
def __init__(self, az_settings, screen):
super().__init__()
self.screen = screen
self.az_settings = az_settings
self.image = pygame.image.load('images/A.png')
self.rect = self.image.get_rect()
self.screen_rect = screen.get_rect()
self.rect.centerx = random.randint(0, self.screen_rect.right)
self.rect.top = self.screen_rect.top
self.center = float(self.rect.centerx)
def blitme(self):
self.screen.blit(self.image, self.rect)
def update(self):
if self.rect.bottom < self.screen_rect.bottom:
self.rect.centery += self.az_settings.letter_speed_factor
I think I should use sprite in the codes. Unfortunately, the program hints " AttributeError: 'Group' object has no attribute 'blitme'" after running. I would appreciate a lot if you help me with the problem.
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. [...]

I am having trouble getting this ship in pygame to move right and left with KEYUP and KEYDOWN presses

At the moment, I am learning from the Python Crash Course Intro Book By Eric Matthes. I am on the section about making a game with Pygame. The second they added keyup and keydown my code stopped letting me move the ship right and left. Beforehand I was able to move the ship without the additional functions.
I have tried looking on the web. I have tried messing around with the code to fit the situation. I, however, have not tried another IDE. I apologize in advance if this is not properly formatted, I am a noob to this website. Thanks!
===================
alien_invaion.py
import pygame
from settings import Settings
from ship import Ship
import game_functions as gf
def run_game():
# Initialize pygame, settings, and screen object.
pygame.init()
ai_settings = Settings()
screen = pygame.display.set_mode(
(ai_settings.screen_width, ai_settings. screen_height))
pygame.display.set_caption("Alien Invasion")
# Make a ship.
ship = Ship(ai_settings, screen)
# Start the main loop of the game.
while True:
gf.check_events(ship)
ship.update
gf.update_screen(ai_settings, screen, ship)
run_game()
ship.py
import pygame
class Ship():
def __init__(self, ai_settings, screen):
"""Initialize the ship and set its starting position."""
self.screen = screen
self.ai_settings = ai_settings
# Load the ship image and get is rect.
self.image = pygame.image.load('images/ship.bmp')
self.rect = self.image.get_rect()
self.screen_rect = screen.get_rect()
# Start each new ship at the bottom center of the screen.
self.rect.centerx = self.screen_rect.centerx
self.rect.bottom = self.screen_rect.bottom
# Store a decimal value for the ship's center.
# Movement flags
self.moving_right = False
def update(self):
"""Update the ship's position based on the movement flag."""
# Update the ship's center value, not rect.
if self.moving_right:
self.rect.centerx += 1
# Update rect object from self.center.
def blitme(self):
"""Draw the ship at its current location.""
self.screen.blit(self.image, self.rect)
settings.py
class Settings():
"""A class to store all settings for Alien Invasion"""
def __init__(self):
"""Initialize the game's settings."""
# Screen Settings
self.screen_width = 1200
self.screen_height = 800
self.bg_color = (0, 23, 233)
# Ship settings
game_functions.py
import sys
import pygame
def check_events(ship):
"""respond to keypresss's and mouse events."""
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT:
ship.moving_right = True
elif event.type == pygame.KEYUP:
if event.key == pygame.K_RIGHT:
ship.moving_right = False
def update_screen(ai_settings, screen, ship):
"""Update images on the screen and flip to the new screen."""
# Redraw the screen during each pass through the loop.
screen.fill(ai_settings.bg_color)
ship.blitme()
# Make the most recently drawn screen visible.
pygame.display.flip()
No error messages... I expect the ship to be able to move when I hold the left and right keys down and the ship to stop moving when I release said keys.
You missed the parentheses when the update method is called:
ship.update
ship.update()

Pygame Pong paddles not showing up

I'm working on a Ping Pong game in Pygame. Having some trouble getting the paddles to display on screen within a class. I have a feeling either my constructor init method is incorrect (although not throwing up any errors) or the display colour is overwriting the paddles.
Here's my code.
Program.py
import sys
import pygame
from settings import Settings
from paddles import Paddles
import game_functions as gf
def run_game():
# Initialise pygame, settings and screen object.
pygame.init()
ai_settings = Settings()
screen = pygame.display.set_mode((ai_settings.screen_width,ai_settings.screen_height))
pygame.display.set_caption('Ping Pong')
# Start the main loop for the game.
while True:
gf.check_events()
gf.update_screen(ai_settings,screen)
#Make paddles
paddles = Paddles(screen)
run_game()
paddles.py
import pygame
import sys
class Paddles():
def __init__(self, screen):
self.screen = screen
self.paddle_l = pygame.draw.rect(screen, (255, 255, 255), [15, 250, 10, 100])
self.paddle_r = pygame.draw.rect(screen, (255, 255, 255), [780, 250, 10, 100])
def paddles(self):
pass
settings.py
class Settings():
"""A class to store all settings for Ping Pong"""
def __init__(self):
"""Initialise the game's settings."""
# Screen settings
self.screen_width = 800
self.screen_height = 600
self.bg_colour = (0,0,0)
game_functions.py
import sys
import pygame
def check_events():
"""Respond to keypresses and mouse events."""
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
def update_screen(ai_settings, screen):
"""Update images on the screen and flip to the new screen."""
# Redraw the screen during each pass through the loop.
screen.fill(ai_settings.bg_colour)
pygame.display.flip()
Like I said I'm not getting any errors, the game window just opens to a black background. The code worked when I put it inside a normal function but not a class.
What am I doing wrong? How do I get the paddles to display?
Is there a reason you're setting paddles outside of the "main loop?"
Moving paddles into your update_screen method gets them showing, at the very least. However, this is newing up a new Paddles object each time update_screen is called.
def update_screen(ai_settings, screen):
"""Update images on the screen and flip to the new screen."""
# Redraw the screen during each pass through the loop.
screen.fill(ai_settings.bg_colour)
paddles = Paddles(screen)
pygame.display.flip()

Identify individual sprite from group in Pygame

Original Post:
With pygame is it possible to identify a random sprite from a Group?
I'm trying to learn Python and have been trying to enhance the Alien Invasion program. for the aliens themselves an alien with the alien class and a group is created from this with 4 rows of 8 aliens.
I'd like to periodically have a random alien fly down to the bottom of the screen. Is it possible to do this with a group or do I have to come up with some other means of creating my fleet if I want to have this functionality?
I've came across a few cases where other people seem to have been trying something similar but there isn't any information stating if they were successful or not.
Update:
I've looked into this a little further. I tried creating an alien_attack function in game_functions.py. It was as follows:
def alien_attack(aliens):
for alien in aliens:
alien.y += alien.ai_settings.alien_speed_factor
alien.rect.y = alien.y
I called it from the while loop in alien_invasion.py with gf.alien_attack(aliens). Unfortunately this caused 3 rows to vanish and one row to attack in the manner I desire except that the whole row did this instead of an individual sprite.
I also tried changing aliens = Group() to aliens = GroupSingle() in alien_attack.py. This caused the game to start with only one sprite on the screen. It attacked in the manner I desire but I'd like all the other sprites to appear also but not attack. How is this done?
You can pick a random sprite by calling random.choice(sprite_group.sprites()) (sprites() returns a list of the sprites in the group). Assign this sprite to a variable and do whatever you want with it.
Here's a minimal example in which I just draw an orange rect over the selected sprite and call its move_down method (press R to select another random sprite).
import random
import pygame as pg
class Entity(pg.sprite.Sprite):
def __init__(self, pos):
super().__init__()
self.image = pg.Surface((30, 30))
self.image.fill(pg.Color('dodgerblue1'))
self.rect = self.image.get_rect(center=pos)
def move_down(self):
self.rect.y += 2
def main():
pg.init()
screen = pg.display.set_mode((640, 480))
clock = pg.time.Clock()
all_sprites = pg.sprite.Group()
for _ in range(20):
pos = random.randrange(630), random.randrange(470)
all_sprites.add(Entity(pos))
# Select a random sprite from the all_sprites group.
selected_sprite = random.choice(all_sprites.sprites())
done = False
while not done:
for event in pg.event.get():
if event.type == pg.QUIT:
done = True
elif event.type == pg.KEYDOWN:
if event.key == pg.K_r:
selected_sprite = random.choice(all_sprites.sprites())
all_sprites.update()
# Use the selected sprite in the game loop.
selected_sprite.move_down()
screen.fill((30, 30, 30))
all_sprites.draw(screen)
# Draw a rect over the selected sprite.
pg.draw.rect(screen, (255, 128, 0), selected_sprite.rect, 2)
pg.display.flip()
clock.tick(30)
if __name__ == '__main__':
main()
pg.quit()

About releasing memory when using pygame and Python

For example, a projectile flies off screen, does the program still compute its location, speed, etc.?
If so, how to release it?
import pygame
from pygame.locals import *
from sys import exit
pygame.init()
screen = pygame.display.set_mode((640, 480), 0, 32)
background = pygame.image.load(background_image_filename).convert()
sprite = pygame.image.load(sprite_image_filename)
x = 0.
while True:
for event in pygame.event.get():
if event.type == QUIT:
exit()
screen.blit(background, (0,0))
screen.blit(sprite, (x, 100))
x+= 10.
pygame.display.update()
Yes, the location, speed, etc. still have to be computed, otherwise no object that is off-screen could ever enter the screen area again. Pygame is smart enough not to attempt to render these objects.
It's usually advisable to use pygame sprites and sprite groups which allow you to remove sprites simply by calling self.kill(). You could also use lists or sets to store your objects, but then you have to write a bit more code yourself.
So I'd first define a pygame.Rect (the game_area) with the size of your screen or a bit larger (in the example below I use a smaller one). Rects have a contains method that you can use to check if your sprite's rect is inside the game_area rect. If the sprite is outside, just call self.kill() and pygame will remove the sprite from all associated sprite groups.
import random
import pygame as pg
from pygame.math import Vector2
class Projectile(pg.sprite.Sprite):
def __init__(self, pos, game_area):
super().__init__()
self.image = pg.Surface((5, 5))
self.image.fill(pg.Color('aquamarine2'))
self.rect = self.image.get_rect(center=pos)
self.vel = Vector2(2, 0).rotate(random.randrange(360))
self.pos = Vector2(pos)
self.game_area = game_area
def update(self):
self.pos += self.vel
self.rect.center = self.pos
if not self.game_area.contains(self.rect):
self.kill()
def main():
screen = pg.display.set_mode((640, 480))
game_area = pg.Rect(60, 60, 520, 360)
game_area_color = pg.Color('aquamarine2')
clock = pg.time.Clock()
all_sprites = pg.sprite.Group(Projectile(game_area.center, game_area))
done = False
while not done:
for event in pg.event.get():
if event.type == pg.QUIT:
done = True
all_sprites.add(Projectile(game_area.center, game_area))
all_sprites.update()
screen.fill((30, 30, 30))
all_sprites.draw(screen)
pg.draw.rect(screen, game_area_color, game_area, 2)
pg.display.flip()
clock.tick(60)
if __name__ == '__main__':
pg.init()
main()
pg.quit()
Yes it does, but since you have only a single projectile (incremented using x), you can easily choose what to do using a few if statements. The process becomes harder when there are multiple projectiles (which you need to store in a container), you should apply this.
Here is an example
for projectile in projectile_list:
# Check if the position is inside the screen
if 0 < projectile.x < WIDTH and 0 < projectile.y < HEIGHT:
# Do position operations
This way, you only process what is required. You can apply similar method to remove unused projectiles from the list or whatever container you are using.

Categories