Python (pygame) using the sprites and classes to make clones of images
Question: 
    import pygame
    import time
    import random #Loads pygame and clock and random function
    
    pygame.init() #Intiates pygame
    
    display_width = 1440
    display_height = 900
    
    gameDisplay = pygame.display.set_mode((display_width,display_height)) 
    clock = pygame.time.Clock() #Starts auto clock updater
    AstImg = pygame.image.load('Images\Ast.gif') #Asteroid image
    
    def asteroid(x,y): #Function for asteroid display
         gameDisplay.blit(AstImg,(x,y))
    
    def game():
         background_image = pygame.image.load("Images/Space.jpg").convert() 
         e = 1
         x = 2500
         y = 2500
         ax = 2500
         ay = 2500
         bx = 2500
         by = 2500
         cx = 2500
         cy = 2500
         dx = 2500
         dy = 2500
    
        while e == 1:
            gameDisplay.blit(background_image, [0, 0]) 
    
            asteroid(x,y)
            asteroid(ax,ay)
            asteroid(bx,by)
            asteroid(cx,cy)
            asteroid(dx,dy)
            if x == 2500:
               x = display_width
               y = random.randrange(60,display_height - 60)
           x += -2.5
           if ax == 2500:
              ax = display_width
              ay = random.randrange(60,display_height - 60)
           ax += -2.5
           if bx == 2500:
              bx = display_width
              by = random.randrange(60,display_height - 60)
           bx += -2.5
           if cx == 2500:
              cx = display_width
              cy = random.randrange(60,display_height - 60)
           cx += -2.5
           if dx == 2500:
              dx = display_width
               dy = random.randrange(60,display_height - 60)
           dx += -2.5
    
           pygame.display.update()
           clock.tick(120) #FPS
    game()
    

I am trying to make this pygame code more efficient using sprites can someone
show me how to do this so i can spawn more asteroids I will eventually need
quite a few asteroids at once and this wont quite work well for it thanks


Answer: It looks like you're going to want to use classes to make your code more
compact. Essentially, a class is a collection of variables and functions that
may be used to define an object, such as an asteroid. Here is an example of a
very simple asteroid class:

    
    
    class Asteroid(pygame.sprite.Sprite):
    
        def __init__(self,x,y,image):
            pygame.sprite.Sprite.__init__(self)
            self.image = image
            self.rect.x = x
            self.rect.y = y
    
        def update(self):
            # insert movement code here
    

Now, mutliple instances of the asteroid class may be created. This would be
done like so:

    
    
    ast1 = Asteroid(given_x,given_y,AstImg)
    ast2 = Asteroid(given_x,given_y,AstImg)
    ast3 = Asteroid(given_x,given_y,AstImg)
    ast4 = Asteroid(given_x,given_y,AstImg)
    ast5 = Asteroid(given_x,given_y,AstImg)
    

Even better would be to make a `for` loop which would create however many
asteroids you'd like and even randomize starting x and y values:

    
    
    spriteList = pygame.sprite.Group()
    for i in range(12):
        ast = Asteroid(random.randrange(1,1440),random.randrange(1,900),AstImg)
        ast.add(spriteList) # then in your while loop write spriteList.draw(gameDisplay)
                            # and spriteList.update()
    

I recommend looking further into Pygame sprite classes and how they work.
[Here](http://programarcadegames.com/index.php?chapter=introduction_to_sprites)
is a link a that may help.

