miércoles, 10 de diciembre de 2014

Picking up from where we left off

I haven't been able to continue with my game making journey for some months. The reason behind this is that I was accepted in a PhD program in physics! I have been studying hard this whole semester, and though I like the idea of writing a few posts here, it really wasn't an option. On the bright side, what I've learned can help me become a better programmer, especially the math related stuff. Now that the semester is over, I plan to finish the first graphical game, taking into account the suggestions I received on the last post. I'm currently using a laptop running Lubuntu, and I haven't been able to get Python and pygame running correctly. Once I get this done, I'll start with the game again. During the semester, I had to write a program, and I used C++ and SFML for it. Now that I revisited C++ I feel like sticking to it, but the blog has already started out with Python. If anyone reads this, let me know if you'd like to see the change to SFML or at least hear some more about it. See you on the next post!

lunes, 7 de julio de 2014

First game with graphics, part 3

After an unplanned abscence, finally I'll show you how to control an image on screen with the mouse.

This example is actually very similar to the last one. I added a generous amount of whitespace in order to clearly read each function and each class.

Almost a month!

I can't believe it's been almost a month since my last post. I've been very busy at work lately, and I haven't been able to continue playing around with pygame. But soon enough I'll continue making my first game with graphics. I actually have some code ready, but I have to take some time to comment on it, or else there would be little point in posting it.

lunes, 9 de junio de 2014

First Game with Graphics, part 2

On the last post we managed to draw an image to the screen, and now we are going to make a program that allows us to move that image.

First of all, since our program is going to become a little longer, we must give it a little more structure. It's time to divide the program into classes, and give each piece a specific job. This will make it much easier to modify parts of the program without affecting others. Let's start by drawing the same image to the screen, but this time with classes

jueves, 5 de junio de 2014

Must-watch video!!!

As you may know, I'm no expert programmer. What I add to this blog is whatever I'm learning at the moment. As I got more and more into pygame I found this video, and it's very, very helpful. This guy really knows what he's talking about. Enjoy!

miércoles, 4 de junio de 2014

First Game with Graphics

We are done with console programs. The fact is that we want to make video games, and while making console programs is a great exercise, they require a lot of effort that we could just as easily spend doing graphical games. Let's get started!

Installing pygame

First of all, there seems to be a bit of a version gap between python and pygame. I had Python 3.4 64-bit installed, but pygame can only work with python 3.2 32 bit. I uninstalled 3.4 first, and then downloaded 3.2. Here's where you can get python: Python's Homepage. Pygame can be downloaded here: Pygame's Homepage,

When downloading pygame make sure to notice the version number. The file you're looking for is pygame-1.9.2a0.win32-py3.2.msi.

Install python first, then pygame.

lunes, 2 de junio de 2014

Guess the number, part 5

Finally, the high score system works, and the program now runs from start to finish without trouble, and has all the features that we set as goals in the beginning. Let's take a look at the different files:



from functions import *
from mainGame import *

gamestate = 'menu'  

def GameManager(state):

    while True:
        if state == 'menu':

            state = ShowMenu()

        elif state == 'game':


            state = ShowMenu()

        elif state == 'score':


            state = ShowMenu()

        elif state  == 'quit':

            print("\nSee you soon!\n")



This file is the one from which we are going to access the program. It imports all the functions we're going to need, and serves as a sort of 'hub' where the program returns to receive new instructions. This while loop is the main game loop.

viernes, 30 de mayo de 2014

Guess the number, part 4

Almost there!

The high score system I've been trying to implement has proven to be quite an interesting exercise. I thought I had an issue with pickle, and kept running into different errors. Turns out that the true problem lied with the file input/output part of the code.

I came up with this function:

def CheckHighScores(score):
        scoresFile = open('scores','rb')
        scoresFile = open('scores','wb+')

    if not scoresFile.read(1):
        scoresList = []
        scoresList = pickle.load(scoresFile)

    if not scoresList:
        for counter,i in enumerate(scoresList):
            if counter == 3:
            if score >= i.score:

lunes, 26 de mayo de 2014

Guess the number, part 3

I've been trying to create a high score system for the game, but it has proven to be quite challenging! At first I thought I'd use a dictionary for the players' name and score, but after a while I noticed that a) I wouldn't be able to sort it and b) printing key and value doesn't feel intuitive. So I went down a very interesting road, involving the following concepts:


After some though, I decided that a player class would be the way to go. It simply contains two attributes, name and score, and both are generated right when an object is instanced:

class player:
    def __init__(self,name_arg,score_arg):
        self.name = name_arg
        self.score = score_arg


This was all new to me! I had never dumped binary data into a file before. The whole point is to store a list of player objects in a file in order to be able to access this information in different playing sessions. I did this through the 'pickle' module. It works something like this:

viernes, 23 de mayo de 2014

Guess the number, part 2

In this post I'm going to add more functionalty and structure to the game. If you ever downloaded an open source game's code in order to see how it was made, you surely saw that it's not in a single file. A game's code is usually spread across multiple files and directories. This helps a lot because it keeps things organized.

I'm not an expert programmer (hell, I'll be glad if somebody considered me even a novice programmer), so I'm not sure if I'm doing this right, but this is how I broke down the game:

- A main file, which handles the main game loop and calls all of the main functions.

- A file with the functions, besides the main game.

- A file with the actual game function.

This results in small files with readable code in each one. Here's how it worked out:

jueves, 22 de mayo de 2014

Guess the number, Part 1

For my first program, I'm going to do a "Guess the number" game. It's one of the most recommended first-game programs out there, so it feels like a good place to start.

Here's the most basic version:

from random import *


secretNumber = randint(1,10)

print("Welcome! I'm thinking of a number between 1 and 10. Can you guess what it is?")

answer = int(input("Your guess: "))

if answer == secretNumber:
   print("You are correct!")
   print("I'm sorry, that's not correct.")
   print("The secret number was: " + str(secretNumber))

First of all, instead of import random, I prefer to do from random import *, so I can use seed() instead of random.seed() and randint() instead of random.randint().

Starting out

I've always wanted to make my own video games. For a long time, it just seemed like an impossible task, someting that only a few chosen people could undertake. When in my teens, I discovered modding, and designed a few levels for Doom, Quake and Starcraft, and I was satisfied with those. Further along I found out about RPG Maker (back then in it's '95 version, unofficially translated by a group called Kanjihack, if I recall correctly). I never finished a game, but I had a lot of fun with it, and understood a little more about the mechanics of game making.