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

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:

Classes

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

Files

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 *

seed()

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!")
else:
   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.