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:


main.py

 

from functions import *
from mainGame import *

gamestate = 'menu'  

def GameManager(state):

    while True:
        if state == 'menu':

            state = ShowMenu()

        elif state == 'game':

            MainGame()

            state = ShowMenu()

        elif state == 'score':

            ViewHighScores(LoadScoreFileData())

            state = ShowMenu()

        elif state  == 'quit':

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

            break

GameManager(gamestate)

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):
    try:
        scoresFile = open('scores','rb')
    except:
        scoresFile = open('scores','wb+')

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

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