quarta-feira, 19 de agosto de 2009

Introduction to cocos2d

In preparation for the pyweek I'm studying about the cocos2d library. cocos2d is a high-level framework that is good for game development since it provides good abstractions like Scenes, Layers, Sprites and Scene transitions. It gives you some default classes like ColorLayer and TiledLayer (rectangular and hexagonal) and a very good set of samples, but enough talking. I'll show how to initialize the library, create a sprite on the screen and control it with the arrow keys (it allows diagonal movements too :)
First install the cocos2d library (pre-requisites:pyglet 1.1.2 and python setuptools). Just go to the respective pages and follow the instructions. They are pretty clear, but if you need any help don't hesitate to ask.
Lets begin importing all the necessary stuff.

from cocos.director import *
from cocos.scene import *
from cocos.layer import *
from cocos.sprite import *

The director is the singleton class of cocos that controls the flow of your aplication. It can run scenes and change between them (among other tasks). Scenes are different parts of the game and layers are drawable parts of the screen that can be pilled up to form the visuals of the application. A simple example: a game could have 4 scenes (the menu, win, game over and the game itself) and each level could be a layer of the game scene. In our example we will have only one scene with one layer.
So we will create a ColorLayer, a type of layer that has a color background.

class TutorialLayer (ColorLayer):
def __init__(self):
super(TutorialLayer, self).__init__(0, 0, 100, 255)

Now we will instruct the director initialize and run a Scene object. Since there's only one scene in our application and it does not have any logic in it, we won't create a subclass of it, we will simply intanciate a new Scene and add our custom layer to it.

director.init()
director.run(Scene(TutorialLayer()))

Now run this code. You should see a 640x480 blue screen. We will ad now a sprite (a simple image that can do actions live move, rotate, etc) to it. Add this line in the __init__ method of the TutorialLayer (image here):

self.ship = Sprite('ship.png', position=(320,240))
self.add(self.ship)

If you run it you should see a ship in the center of the screen. Now were going to handle key events to finish our example. First we will let cocos know that our layer wants to receive events. To do this just add the following line below the class declaration:
is_event_handler = True
Now that we can receive events, we need to create a listener to the key press and key release functions and a character set to store wich characters are pressed. The event listener for the key press event is defined by the on_key_press(self, key, modifiers) function. The modifiers parameter shows if any modifier key like control or alt is pressed. A similar event is defined for the release of a key. First we will initialize our character pool in the constructor using

self.chars_pressed = set()

Then we add the on_key_press and on_key_release methods:

def on_key_press(self, key, modifiers):
self.chars_pressed.add(key)

def on_key_release(self, key, modifiers):
self.chars_pressed.remove(key)

What we do here is keep track of the currently pressed keys by storing them on a set. Now we just check if the keys are in our set in every frame. To do this we will schedule a function to be called every frame.

self.schedule(self.update)

And we will define the following constants:

LEFT = 65361
UP = 65362
RIGHT = 65363
DOWN = 65364

Now the the update method:

def update(self, dt):
x, y = self.ship.position
if LEFT in self.chars_pressed:
x -= 2
if UP in self.chars_pressed:
y += 2
if RIGHT in self.chars_pressed:
x += 2
if DOWN in self.chars_pressed:
y -= 2
self.ship.position = (x, y)

Notice that the (0,0) point of the screen is the left-bottom one. Now test the program and verify that the ship really moves in every direction, including diagonals. The complete file can be found here.
That's my introduction to cocos2d :) I hope you liked it. See you next time!

quarta-feira, 12 de agosto de 2009

Pyweek 9

Hi!
Do you want to make a game and have one week available? If your answer is YES why don't you enter in the next Pyweek?

The Pyweek is a contest that challenges you to write a game (in Python) from scratch in one week. It is intended to be fun and to drive innovation from game developers. It will be my first time in the contest and I'm really hoping to finish it with something that's fun to play. I'll be using the cocos2d library and I'll post more about it when the competition is nearer. If you have the time and will to make a game happen in one week then join me at 00:00 UTC 2009-08-30 :)

See you there!

segunda-feira, 10 de agosto de 2009

My first completed deal in FGL

Today I completed my first deal in FGL: a primary sponsorship with playtowerdefensegames. Everything went smoothly and I'm really looking forward to make more flash games. Also, I began to work in my new game, but I'm not telling what it's about... yet. I'll post my experiences and achievements in this blog for everyone to know what I'm working into. Also, I'll post some videos of the gameplay once it's close to the real thing :) I hope I'll get more attention to my games this way.

segunda-feira, 3 de agosto de 2009

News and more FGL

Hi!
I'm happy to say that I got a second offer on my game on FlashGameLicense! Now I have a bigger offer and I will explain the Buy It Now and Last Call options of FGL (the things that got me the new bid)

Buy It Now:

This options appears only to more experienced developers OR to games that are waiting for a sponsorhip for some time. It allows you to set a fixed price to your game in a Primary Sponsor that allows Ads and don't have gameplay changes. This way the sponsor can simply buy your game with your price on it, but you have to accept any deals from Buy it Now. This is why this option is dangerous: you can underprice your game and there's no auction system to let anyone give you a higher bid! Use with caution

Last Call:

When you're about to close a deal you should activate the Last Call option. This makes sponsors aware that you are going to sell the game and attracts last minute sponsorships. Your game will have a mark saying that is on Last Call. That means: "If you want to bid, do it NOW!". I think it's always a good idea to activate this option, since my bigger offer came after I did it.

That's it. Later in this week I'll post my delicious bookmarks about flash games development and resources for games. See ya.