Create the World module for Rock Sweeper

Welcome to the second chapter of the Rock Sweeper project, a game which creates with Pygame. In this article we will create a World module which will later be used to manage game entities such as spaceship and rock as well as render the background image of the game.

We will use NetBeans IDE 8.1 to create this game, first open up your NetBeans IDE if you already have one or else you can refer to my previous article on how to install the NetBeans IDE on your computer. Start a new project and call it Rock Sweeper.

pygame's project
pygame’s project

Create an empty module under the same folder as the existing module and named it World. Below is the script for the World module which includes those use to blit the background.

from pygame.locals import *

class World(object):
    def __init__(self, maintile, background_coordinate, screen_size):
        self.spritesheet = maintile
        #the rectangle object uses in clipping the background area
        self.clip_rect = Rect(background_coordinate, screen_size)
        self.spritesheet.set_clip(self.clip_rect) # clip a portion of the spritesheet with that rectangle object
        self.background = self.spritesheet.subsurface(self.spritesheet.get_clip()) #create the background surface 
    def render(self, surface):
        surface.blit(self.background, (0,0))

In the main module we will call the World module to render the background within the while loop.

#!/usr/bin/env python

import pygame
from pygame.locals import *
from sys import exit
from world import World

maintile = 'maintile.png' #spritesheet file

SCREEN_SIZE = (550, 550) 


screen = pygame.display.set_mode(SCREEN_SIZE, 0, 32)
pygame.display.set_caption("Rock Sweeper")

spritesheet = pygame.image.load(maintile).convert_alpha() #spritesheet surface object

pygame.display.set_icon(spritesheet) #use the entire spritesheet surface as the window icon

world = World(spritesheet, BACKGROUND_COORDINATE, SCREEN_SIZE)

while True:
    for event in pygame.event.get():
        if event.type == QUIT:
    world.render(screen) #render the background

If you run the main module above then you will see the below outcome.

blit background
blit background

The next chapter we will create game entities such as spaceship and rock so make sure you continue reading the tutorial!

Leave a Reply

Your email address will not be published. Required fields are marked *