Science and technology

My open supply online game for Open Jam

This 12 months, I joined in on the Open Jam, a “game jam” during which programmers around the globe dedicate a weekend to create open supply video games. The jam is actually an excuse to spend a weekend coding, and nearly all of the video games that come out of the problem are small distractions reasonably than one thing you are prone to play for hours on finish. But they’re enjoyable, numerous, and open supply, and that is a fairly good characteristic listing for a sport.

The sport I submitted is Unveil, a relaxing puzzle sport during which the participant should first uncover the purpose, after which work to realize it with the best variety of factors. Because a part of the sport is the invention course of, I will not reveal any extra in regards to the gameplay than that.

The complete sport is just 338 traces, written in Python utilizing the Pygame module. It’s, in fact, open supply, and a part of it might function a very good introduction to a couple programming ideas that used to confound me (two-dimensional arrays being essentially the most important). For easy sport design, a two-dimensional array may be very helpful as a result of so many enduring video games are constructed on them. You can in all probability consider a number of, though if you do not know what a two-dimensional array is, it’s possible you’ll not understand it.

Arrays in gaming

An array is a group of knowledge. An array will be listed throughout a web page or an X-axis (in mathematical phrases). For occasion:

artichoke, lettuce, carrot, aubergine, potato

An array may be represented as an inventory or a Y-axis:


This is a one-dimensional array. A two-dimensional array extends on each the X-axis and Y-axis.

Here’s a typical two-dimensional array seen on this planet of board video games:

Yes, two-dimensional arrays are used because the board for chess, draughts, noughts and crosses (additionally known as tic-tac-toe), RPG battle maps, minesweeper, Carcassonne, Forbidden Island, and in barely modified kinds, video games like Monopoly and even Ur (actually the oldest sport we all know of).

If you may comfortably create a two-dimensional array, you’ve gotten an amazing begin at programming any variety of video games.

Creating tiles in Pygame

If you are not acquainted with Python, it’s best to take a while to evaluation this Python (and Pygame) introductory series. If you’re feeling assured sufficient to translate code to different libraries, although, there’s nothing particular to Pygame within the “important” elements of this code (the array constructor), so you should use any library or language.

For simplicity, I will name the person squares within the sport board array tiles. To assemble a two-dimensional array, or sport board because the case could also be, you need to have tiles. In object-oriented programming, you take into account every part as a singular object primarily based upon a template (or class, in programming terminology). So, earlier than creating the sport board, you need to first create the infrastructure for the board’s constructing blocks: tiles.

First, arrange a easy Pygame undertaking, making a show (your window into the sport world), a bunch to signify the sport board, and some customary variables:

import pygame

game_world =, 720))
game_board = pygame.sprite.Group()

working = True
black = (zero, zero, zero)
white = (255, 255, 255)
crimson = (245, 22, 22)
world_x = 960
world_y = 720

Next, create a Tile class to determine the template from which every tile will get forged. The first perform initializes a brand new tile when one is created and offers it the required fundamental fields: width, peak, a picture (truly, I simply stuffed it with the colour white), and whether or not or not it is lively. In this case, I take advantage of is_pressed, as if the tile is a button, as a result of that is what it will appear to be when the code is completed: when the consumer clicks a tile, it adjustments colour as if it had been a button being lit up. For different functions, this state needn’t be seen. In chess, for instance, you may as an alternative have a discipline to signify whether or not a tile is occupied and, in that case, by what sort of chess piece.

class Tile(pygame.sprite.Sprite):
    def __init__(self, x, y, w, h, c):
        self.picture = pygame.Surface((w, h))
        self.rect = self.picture.get_rect()
        self.rect.x = x
        self.rect.y = y
        self.is_pressed = False

The second perform is an replace perform. Specifically, it checks whether or not a tile has been clicked by the consumer. This requires mouse coordinates, which you may get later within the code throughout the occasion loop.

For this demonstration, I will make this perform fill the tile with the colour crimson when it is within the is_pressed state and again to white in any other case:

    def was_clicked(self, mouse):
        if self.rect.collidepoint(mouse) and not self.is_pressed:
            self.is_pressed = True
        elif self.rect.collidepoint(mouse) and self.is_pressed:
            self.is_pressed = False
            return False

Main loop

This demo’s primary loop is straightforward. It checks for 2 sorts of enter: a give up sign and a mouse down (click on) occasion. When it detects a mouse click on, it calls the was_clicked perform to react (filling it with crimson or white, relying on its present state).

Finally, the display fills with black, the sport board state is up to date, and the display is redrawn:

holding place for sport board building

whereas working:
    for occasion in pygame.occasion.get():
        if occasion.sort == pygame.QUIT:
            working = False

        elif occasion.sort == pygame.MOUSEBUTTONDOWN:
            for hitbox in game_board:


pygame.give up()

Board building

To construct a two-dimensional array, you need to first resolve what number of tiles you need in each instructions. I will use eight for this instance as a result of that is how a chessboard is constructed, however you may use fewer or extra. You might even settle for arguments at launch to outline the array’s measurement relying on choices, similar to --row and --column:

Because you do not know the dimensions of the board, you need to calculate the width of the rows and columns primarily based on the dimensions of your show. I additionally embrace one pixel of padding between every tile, as a result of, with out a hole, it seems like one large block of colour:

column_width = world_x / columns
row_height = world_y / rows
pad = 1

Laying out tiles throughout the show is straightforward. Of course, this is not the purpose, because it solely attracts alongside the X-axis, but it surely’s a very good begin:

j = zero

whereas j < rows:
    tile = Tile(j * column_width, row_height, column_width - pad, row_height - pad, white)
    j += 1

The thought is that the variable j begins at zero, so the primary tile is positioned from zero to column_width, much less the worth of the padding. Then the variable j is incremented to 1, so the subsequent tile is positioned at 1 occasions the worth of column_width, and so forth.

You can run that code to see the partial success it renders. What this resolution clearly lacks is any consciousness of additional rows.

Use a second counter variable to trace rows:

j = zero
ok = zero

whereas j < rows:
    whereas ok < columns:
        tile = Tile(ok * column_width, j * row_height, column_width - pad, row_height - pad, white)
        ok += 1
    j += 1
    ok = zero

In this code block, which achieves the specified end result, every tile is positioned in an area decided by the present worth of both j or ok.

The ok variable is incremented inside its loop so that every tile is progressively positioned alongside the X-axis.

The j variable is incremented outdoors the nested loop in order that all the pieces will get moved down one row.

The ok variable is then set to zero in order that when the internal loop begins once more, all the pieces is shunted again to the far left of the display.

Easy arrays

Creating a grid can appear mathematically and syntactically intensive, however with this instance plus just a little little bit of thought of what end result you need, you may generate them at will. The solely factor left so that you can do now’s to create a sport round it. That’s what I did, and also you’re welcome to play it by downloading it from its dwelling on or from its supply repository on Enjoy!

Most Popular features the latest multimedia technologies, from live video streaming to audio packages to searchable archives of news features and background information. The site is updated continuously throughout the day.

Copyright © 2017 Breaking Express, Green Media Corporation

To Top