Creating rock on the screen with Pygame

Starting from this chapter I will no longer posting script on the article but just to let you know what is the progress I have made in this game. This is an open source game so at the end of this project I will upload the game as well as all the source code online for you to download, so don’t worry about it!

Basically what I have accomplished today is to create a RockSprite module and then use this module to create as many as 30 rocks on the screen which will move toward the ships. The player can now use the ship to intercept the rock as mentioned earlier in my previous article.

from game_sprite import GameSprite
from pygame.locals import *
from vector2d import Vector2D

class RockSprite(GameSprite):
    
    # Constructor. Pass in the image file
    # and it's width and height and the position on the spritesheet
    def __init__(self, image, width, height, pos, world):
        # Call the parent class constructor
        GameSprite.__init__(self, image, width, height, pos)
        
        self.world = world
        self.rock_health = 100
        
    def updatePosition(self): 
        self.pos += Vector2D(0.0, 0.1)
        #if the rock get passed the boundary then remove it
        if(self.pos.y > 560):
            self.world.removeRockEntity(self)
        else:
            #else create the new rectangle object of that rock
            self.rect = Rect(self.pos.x - self.width/2, self.pos.y - self.height/2, self.width, self.height) #self.rect will be called by the rock group

After the first ship has intercepted the rock the second ship will intercept it again to destroy it, the next thing I will need to do is to create a state engine for this game, see you tomorrow!