Archive for the code Category

Automating Game Events

Posted in code with tags , , , , on May 19, 2009 by Tau

One of the major things that’s been bothering me about the code as I look it over is the amount of hardcoded strings, and the requirement for data files to have a certain format. Previously, the events listed in the maps had to have two arguments because when the list of events was created in memory, both arg1 and arg2 had to be set (there would be an error that the input array was out of range without them) which lead to lots of NULLs in the map files.

I’ve altered this. Now the arguments for game events are filled into an array, rather than private variables, so that any number of arguments may be given. This is also advantageous as future event classes may need more than two arguments.

This being an RPG, there’s going to be a need to automate events; those wonderful parts of the game where the story runs off and the player has to do nothing but press ‘continue’ when they’ve finished reading. So I’ve started a class to manage this. All it does is execute an event, and then wait a given time before executing the next one. This is done with the pygame.time.delay() function. Something like:

for i in range(len(events)):

Although, it’s not finished yet. All the current events have player cancelling built in (like a conversation won’t end until you press ‘q’) which I’ll keep for the time being. Some people read slower than others..

I expect some form of this will be used for the non-player movement on screen eg. village people wandering around. That may be the next area of work for me, although I have started to think about the battle handling. Clearly though, an intensive graphics session is needed.

Screen Messages

Posted in code with tags , , , , , on December 21, 2008 by Tau

I’ve just finished the classes for printing messages onto the screen. It’s basically the same as the dialogue classes, but requiring fewer variables: the text is read in from a file given as the argument of the event type “message” and blitted to screen.

Coming back to the code after quite a while has made me realise how much I shouldve commented the first time around. I’m going to go through all the files and properly document them, drawing up a diagram of where all the classes and functions lead.

Apache, MySQL and PHP

Posted in code with tags , , , on September 1, 2008 by Tau

It is my eventual goal that this blog, with additional elements, be hosted by myself as a fully functional website. As with the game itself, this comes not from a belief that the traffic will warrant it, but rather from a desire to know how it is done, and as a creative enterprise.

To that end I have been attempting to install and link together Apache, MySQL and PHP on my Mac. Now I know Macs come with this pre-installed, but I thought I may as well get the latest releases. Here began a labour of Hercules.

I was aware of the existence of packages like MAMP which bunble Apache, MySQL and PHP together, providing you with the directory htdocs and db for the MySQL databases, along with all the MySQL scripts. It comes with a nice GUI for turning the servers on and off and a web interface for creating SQL tables and the like. Well, I thought, that’s just for people who don’t know what they’re doing (people, it turns out, like me). So after downloading the latest releases I set about building them. Apache went quite smoothly, and I was running HTML documents on localhost through firefox in no-time. PHP took a little more time, as I ultimately had to amend Apache’s configuration document httpd.conf so it knew what to do with .php files. (Note: I really wish I could go through everything in detail, and I wished others had done during the many hours of googling, but I have of course forgotten).

MySQL just wouldn’t work. It constantly timed out due to some problem with the pid file, and despite plenty of modifying of my.cnf or changing of permissions, nothing seemed to work. Strangely, it did briefly. The server didn’t time out and I could run the mysql script, although it wouldn’t connect via PHP in a webpage. So I restarted my computer, and the nothing worked. Not MySQL, not PHP, not Apache.

Finally, after numerous re-installs and re-builds, I gave up and used MAMP, which runs perfectly. When I build a server to host a site for real, I’m sure I’ll deal with this again, but for the moment it’s simply about learning the tricks of the trade.

Isometric Solid Objects

Posted in code with tags , , , , , on August 24, 2008 by Tau

Now I had succeded in making the objects solid, although this worked only so long as said object was supposed to be a rectangle (because the method used the collide() function, checking to see whether the character’s and map object’s rectangles collided). However, with an isometric map the objects are far from recatngular; they’re rather pointy.

Now my plan to solve this is to create another type of input file, for each and every image. This would contain the name of the image file, plus a list of co-ordinates that map out the solid regions of the image. When the map loads, all of these could be fed into an array of solid areas, and the character’s position checked against these whenever it moves.

The code is nearly complete, although so far unchecked. I’ve found debugging tends to scale exponentially with lines of code.

Dialogue – On the Screen

Posted in code with tags , , , , on August 18, 2008 by Tau

Well, kind of. That which used to print in the terminal now appears as ugly text in an ugly blue box on the screen, and disappears when the conversation finishes. I moved the construction of the screen window to an external class that as a Get Screen function, which can be called by any other class, and thus drawn onto by any other class. It means all sorts of goodies can now be drawn whilst the main loop is running, such as dialogue, messages and menus. That said, my code is still ugly as sin.

I just need to create an image for the dialogue box to make it a little prettier, and it’ll be done, aside from writing the code for conversations generating gamekeys, by which I mean, for example, speaking with a NPC and getting some information from them allows you to proceed to a new area or whatever.

Next I think a few more events, such as finding items and attribute changes, and then really get going with the sprites, at least in order to finish this village and work towards some form of simple, wander around demo.

Dialogue Update

Posted in code with tags , , , , on August 17, 2008 by Tau

So I’m trying to get the dialogue to print out on the screen rather than in the terminal. Currently I’m doing this by sending the background all the way through a few functions to the dialogue class, where what is usually just printed is made into a new surface and blitted onto the background. This works fine.

However, the display won’t redraw in the dialogue class, only in the mainloop, meaning you see nothing until the conversation is over, and it all appears at once. Marvellous, if you’re psychic.

I really can’t see why this is. If the dialogue class can get information from he keyboard via pygame, and blit onto an existing surface, why can’t it simply redraw the screen? What’s so special about the main game loop? Is it because this is where the screen is created?

I wish I had a Magic 8 Ball.

Solid Map Objects

Posted in code with tags , , , , on August 3, 2008 by Tau

The last thing left on my list before being happy with the movement drawing was the characters interaction with the map objects. Previously, the Character Sprite was drawn after the Map Objects and so would run straight over them. Not Good. So what I’ve done is added another attribute to the input file “Solid”, if this is the case, and if a movement in a given direction would cause the character’s Rect would collide with the Map Object’s Rect, the movement function isn’t called. Thus making the object appear solid.

canmove = True
if movekey[K_LEFT]:
for image in self.thismap.grids:
if image.getSolid():
if image.rect.collidepoint(self.char.rect.centerx – 10,            self.char.rect.centery):
canmove = False
if canmove:

Next, I think, I’d like to develop the Character. It may just be a matter of an arbitrary event that gives +10 HP or whatever, and stores for further use.

I’ve also renamed the main file from gametest to nsf0.1, and will keep versions from now on. I’m tempted to use CVS, but it’s just another thing to learn, and my learning list is a little full right now.