August 2014

Archive for August 2014

Easy way to test a tiled and animated background sprite

I'm working in my game, it's going be awesome. Ok, so while working in it, I need a fast way to test my animated background sprites. I use Aseprite for drawing - it's great, and if you use Ubuntu, you can  sudo apt-get install aseprite. Go to the website for more info on other OS: http://www.aseprite.org/

Let's try an example:

save this water sprite! I made it myself! :D (that's why it's ugly..)

Aseprite can export your sprite sheet to gif easily:
file->import sprite sheet
Choose your file, and set width and height to the sprite size (mine is 32 x 32 pixels)
Now in frame->play animation you can see your animation is working!
Now in file->save as... select .gif and we have a beautiful animated gif.


This is the gif!

Ok, problem is it's difficult to have an idea if the water will turn out ok because we haven't seem it animated AND tiled. So how can we do this? I was thinking about it and decided to code an easy solution to the viewing problem! Let's make and html!

 <html>  
 <head>  
 <title>Animated Background Tile Tester</title>  
 <style TYPE="text/css">  
 <!-- body {  
 background-image: url("animateWater1.gif");  
 background-size: 64px 64px;  
 image-rendering: -moz-crisp-edges;  
 image-rendering: -o-crisp-edges;  
 image-rendering: -webkit-optimize-contrast;  
 -ms-interpolation-mode: nearest-neighbor;  
 } -->  
 </style>  
 </head>  
 <body></body>   
 </html>  

Ok, so this is a very simple html for testing sprite. Let's break it down:
background-image is where the filename of your image goes.
background-size is the size. I'm using it to double the scale of my gif, because my game will use double pixel scale.
image-rendering lines and -ms-interpolation are forcing use nearest-neighbor for aliasing option - like no anti-aliasing.


I couldn't find a way to embedded the example above in this blog post, if you know how to do the Xzibit way and put a html page inside a html page, please tell me.

Nearest-neighbor doesn't work

Ok, I've only tested in Firefox 31.0. I know this example doesn't work in Chrome. I'm using because I had to disable caching - I use SSD and there is a lot of people on the web saying that caching is bad for it. Don't know if it's true, but with no cache it's better for web development.

I can't resize Aseprite on Ubuntu!!!

Yeah, unfortunately this is true for the Aseprite in the repository. But there is one way out. Close it. In your home folder, find the .asepriterc file and open it. Find the lines that look like this:

 ...  
 [GfxMode]  
 Maximized = no  
 Width = 1800  
 Height = 960  
 ...  

And change the width height to one of your choice, and then reopen Aseprite. It's not the best solution, but it's easy and works! There are other solutions in the web, Google is your friend.



A tilemap in PyQt for a bigger game project



I wanted to get back into game development, as a hobby, for some time now. I have some idea of what I want: a simple 2d rpg game, like Pokemon in Game Boy. So for starts, I needed a way to draw tilemaps, a good editor, something I could customize for my needs.

But I really couldn't find anything as easy to use as I wanted so I decided to write my own. I opted to use Python and PyQt - and I don't know much about any of them, so I had a slow start. I'm reading about PyQt for two days in all my free time.

Until now I just have a window showing a hardcoded tilemap on screen. I'm sharing it, even unfinished, since I had a really hard time figuring out the steps to do this.

Since python cares about identation, I'm sharing a link: http://pastebin.com/d1FGDCJf

 #!/usr/bin/env python  
 # display a tiled image from tileset with PyQt  
 import sys  
 from PIL import Image  
 from PIL.ImageQt import ImageQt  
 from PyQt4 import QtGui, QtCore  
 from PyQt4.QtGui import QImage  
 from numpy import ndarray  
 # Simple background, will use open from a file in future  
 background =   [[12, 2,12, 2, 1, 2, 3, 3, 1, 1],  
      [ 1, 1, 7, 8, 5, 6, 7, 8,12, 3],  
      [ 1, 3, 1, 3,12,10,10, 1,12,12],  
      [ 2,12, 0, 4,10, 3,12, 2,12,12],  
      [12,12, 1, 1,10, 3,12, 2,12, 1],  
      [12,12,12, 0,10, 2, 1,12, 1,12],  
      [ 3,12, 3,12, 0, 2, 2,12,12, 3],  
      [ 1,12, 1,12, 1, 1,12,12, 3,12],  
      [ 3,12, 0,12,12,12,12,12, 3, 3],  
      [12, 3, 1, 2, 3,12,12,12, 1,12]]  
 # This will have the tileset  
 tileset = []   
 # last time I was writing this save function!  
 def save():  
   f = open( "map.txt" , "wb" )  
   f.write( "background :   [" )  
   for i in range(len(background) ):  
         f.write( "[" )  
     for j in range(len(background[0])):  
       f.write( str(background[j][i]) )  
       f.write( "," ) if j != len(background[0])-1 else (f.write( "]," ) if i != len(background)-1 else f.write( "]" ))  
     f.write( "\n" ) if i != len(background)-1 else f.write( "]" )  
   f.close()  
 class MyImage(QtGui.QWidget):  
   def __init__(self, parent, width, height):  
     QtGui.QWidget.__init__(self, parent)  
     BOX_SIZE = 32  
     image_file = Image.open("simpletile.png")  
     self.setWindowTitle("View tiled background")  
     # get tileset file and split it in images that can be pointed through array  
     if image_file.size[0] % BOX_SIZE == 0 and image_file.size[1] % BOX_SIZE ==0 :  
       currentx = 0  
       currenty = 0  
       tilei = 0  
       while currenty < image_file.size[1]:  
         while currentx < image_file.size[0]:  
           print currentx,",",currenty  
           tileset.append( image_file.crop((currentx,currenty,currentx + BOX_SIZE, currenty + BOX_SIZE)) )  
           tilei += 1  
           currentx += BOX_SIZE  
         currenty += BOX_SIZE  
         currentx = 0  
     # get the background numbers and use to get the tiles    
     for i in range(len(background) ):  
       for j in range(len(background[0])):  
         image = ImageQt( tileset[ background[j][i] ] )  
         pixmap = QtGui.QPixmap.fromImage(image)  
         image = QtGui.QPixmap(pixmap)  
         label = QtGui.QLabel(self)  
         label.setGeometry(i*BOX_SIZE+10, j*BOX_SIZE+10, BOX_SIZE, BOX_SIZE)  
         label.setPixmap(image)  
 save()  
 app = QtGui.QApplication(sys.argv)  
 width = 320  
 height = 320  
 w = MyImage(None, width, height)  
 w.setGeometry(100, 100, width+20, height+20)  
 w.show()  
 app.exec_()  

Powered by Blogger.