Skip to main content

Posts

Crafty JS Tutorial (DX Ball / Breakout)

Introduction: This Crafty JS tutorial is part of Alkottab Studio's internship program, to take developers with no previous game development experience in the quickest way to production phase. Prerequisites: Previous development experience. The very basics of HTML/CSS/Javascript. Finishing the first part of the Crafty tutorial on its website. [ How Crafty works ,  Download and Setup ] Section 1: Game description: A ball continuously moving and hitting walls and bricks. Bricks are destroyed and points are collected when hit by the ball. User controls a bat and moves it right and left to prevent the ball from hitting the ground. If the ball hits the ground, a life is lost. Section 2: File Structure: Before starting the tutorial, it is better to explain how the files are arranged: index file : links to JS files, game div, some simple CSS. js directory: Crafty file and game file. assets directory: images and spritesheets used. Section 3: Coding consi...

Facebook Scores API with Koala Gem

Here is a code snippet for a piece of code that I found a lot of people asking about on the web. Since Koala gem does not have an explicit API for Facebook Scores API , some people wonder why it isn't there. Actually, there is a generic call that can do the job just fine: After getting your Koala gem to work: check this URL for more about Creating a Facebook Rails app with Koala gem . Read the set of scores for a user and their friends Facebook Scores API states that: "You can read the set of scores for a user and their friends for your app by issuing an HTTP GET request to /APP_ID/scores with the user access_token for that app." So this can be done with the 'get_object()' method, api = Koala::Facebook::API.new(session[:access_token]) scores = api.get_object(APP_ID + "/scores") Create or update a score for a user: Facebook Scores API states that: "You can post a score for a user by issuing an HTTP POST request to /USER_ID/...

Qt: Save QPainter Output in an SVG or Image File

To save what you paint in a Qt window to a file (SVG or image) is an easy and straightforward piece of code. You create a QSvgGenerator or a QImage object. Some initialization. Paint into the object (treat it as an IODevice) And  we are done. Suppose that this is what I draw in my paint event: void MainWindow::paintEvent(QPaintEvent *) { QPainter painter; painter.begin(this); painter.setRenderHint(QPainter::Antialiasing); paint(painter); painter.end(); } void MainWindow::paint(QPainter &painter) { painter.setClipRect(QRect(0, 0, 200, 200)); painter.setPen(Qt::NoPen); painter.fillRect(QRect(0, 0, 200, 200), Qt::gray); painter.setPen(QPen(Qt::white, 4, Qt::DashLine)); painter.drawLine(QLine(0, 35, 200, 35)); painter.drawLine(QLine(0, 165, 200, 165)); } If I want to save it into an SVG file, it would be something like this void MainWindow::saveSvg() { QString path = QFileDialog::getSaveFileName(this, tr("Sa...

Resizable Google Chart

To resize a Google chart (pie chart, area chart, geo chart ...) is a matter of a few lines of code after refactoring the given example in the Google Charts Gallery. The keyword is: window resize. Just keep your data in a javascript variable and put the Google Chart code in a function to be called in each onResize event. That way the chart will be redraw to fit in the new size of the chart div, and it will look like it is resizing / scaling. Note that you may want to set a min/max width/height for the size of the chart (css on the div can do the job just fine). Performance? Just fine. Unless you have large data and many details to draw, then it may be a headache to draw from the beginning at each resize (you can limit the chart resize frequency with javascript by a delay or timer). Here is a complete example: <html> <head> <script type="text/javascript" src="https://www.google.com/jsapi"></script> <script type="tex...

Camera Drag and Zoom with Mouse in Unity 3D

The script is easy and descriptive, it is the result of some searching and modification. Press the left mouse button and drag to move, and use mouse scroll wheel to zoom in and out. The zooming code can work for both orthogonal and perspective but I've been using and testing it in orthogonal mode.  Modify the public values to fit in your game. ** Just drag the script to your camera and it will work! ** C# code [CameraDragZoom.cs] using UnityEngine; using System.Collections; public class CameraDragZoom : MonoBehaviour { public float dragSpeed = -10; public int minX = -892; public int maxX = 1111; public int minZ = -880; public int maxZ = 1145; public int bottomMargin = 80; // if you have some icons at the bottom (like an RPG game) this will help preventing the drag action at the bottom public float orthZoomStep = 10.0f; public int orthZoomMaxSize = 500; public int orthZoomMinSize = 300; private bool orthograp...

How to Install Twitter Bootstrap in Rails

To install Twitter Bootstrap in a Rails application, there are several ways and gems. Here I prefer the working and straight-forward one. 1- Inside " group :assets " add: gem 'bootstrap-sass' 2- Go to " app\assets\stylesheets " directory and create a file named " bootstrap_and_overrides.css.scss " 3- Inside the previous file, add: @import "bootstrap"; @import "bootstrap-responsive"; 4- Inside " app\assets\javascripts\application.js " file, add: //= require bootstrap 5- Then in the terminal, do " bundle install " and your Rails application with be ready for Twitter Bootstrap tags.

Unity3D: Rotate Object to Face Another

I've been working on porting an HTML5 demo to Unity3D, when I stopped for more than a day just to rotate a cannon towards a point! I have to admit that Unity3D is great, but it can turn simple tasks into complex ones for no reason. Anyways, after some time with Quaternion class and Lerp function and searching for online solutions, I decided to go back to basics. The problem with Lerp is that it forces rotation in a certain time. So the cannon will rotate 30 degrees in -say- 5 seconds, and will also rotate 60 degrees in 5 seconds, which is not logical. And to solve this I have to do some extra math. So what did I do? I just rotated with a certain amount of degrees per second (using rotate() function) , and checked if the angle between my cannon and the target is within the allowed range to shoot (with a cross product of two vectors). That's it! The code works as follows: - If cannon is in attack mode, rotate towards target point with a fixed speed. - The cannon object h...

Link Preview using Rails, AJAX, and Nokogiri Gem

I was trying to make some working code to preview link content like Facebook, Google Plus, LinkedIn .. etc. And I have to say, it is not that easy to get it to perfection. Here are some notes before starting: The happy scenario is to find ready tags inside the HEAD tag of the HTML file. Some times they are there, a lot of times they are not there. In video sharing websites like Youtube or Vimeo, or other news websites who care about these details, you will find meta data to save the day. For example: <meta property="og:url" content="http://www.youtube.com/watch?v=c6nzShZQBLQ"> <meta property="og:title" content="Kinetic Scrolling Example [Arabic] [Qt]"> <meta property="og:description" content="Visit my blog entry for more info, and the complete example: http://3adly.blogspot.com/2010/11/qt-kinetic-scrolling-ariyas-example.html Example uploaded on M..."> <meta property="og:type" conten...

Deploying Rails Application on Windows Azure (Ubuntu VM)

In the last weeks, I've been exploring my options for hosting a Ruby on Rails application. An important step was Windows Azure. I registered for a 90-day trial to give it a try. One of the great services Microsoft has offered was the Ubuntu virtual machine ( :D ). So this tutorial can be useful even with a normal Ubuntu machine (except the Endpoints part) since I'm treating it like an Ubuntu machine regardless of the Azure service it is hosted on. In this tutorial, I will create a new Ubuntu 12.04 LTS virtual machine, install Ruby ( 1.9.3 ) and Rails ( 3.2.9), install Passenger gem, install Apache server and connect it to Rails application. And finally, some important notes about getting your project up and running. Create an Ubuntu 12.04 LTS VM: - In you portal, from the bottom right corner click: New -> Compute -> Virtual Machine -> From Gallery. - Scroll the list to Ubuntu Server 12.04 LTS and click it. - Fill the required data about the VM name, the us...

Ruby on Rails Facebook Application using Koala Gem

Here is a another Facebook application example ( check the python example ), but this time it is with Ruby on Rails using Koala gem. The great thing about Koala is how it is easy integrated and straight forward. Here are the steps from the very beginning: 1- Create a new Ruby on Rails project: rails new rorApp 2- Delete ./public/index.html file as it is not needed. 3- In Gemfile add: gem 'koala', '1.3.0' 4- In ./config/initializers folder, add a constants.rb file with the following data: APP_ID= '123456789' # please change! APP_SECRET= '1b2n3n5n6n7m8m9n9m0m' # please change! SITE_URL = 'http://localhost:3000/' # please change! Where APP_ID and APP_SECRET are the values you have from the Facebook application you create in the developers section, and the SITE_URL is the root URL of your application website ('http://localhost:3000/' if our case of testing locally) 5- In ./config/routes.rb, add the following routes: root...