Sunday, August 19, 2012

mysql : connecting to mysql database from windows command shell

in this code i connect to a mysql database using the command shell and xamp virtual server. Once i have xamp and the path to mysql.exe in my system path I can use:
mysql -h localhost -u oladitan
to connect my localhost database with the user name "oladitan". From here I can perform various operations including database selection and querying.

Tuesday, August 14, 2012

javascript : Crafty : sprite movement with keyboard input

index.html




 
 
 My Crafty Game
 





craftyKeyInput.js

function main() {
 
    //start crafty
    Crafty.init(512, 512);
    //Crafty.canvas();

    //prepare the spritesheet for game use
    Crafty.sprite(64, "images/spriteSheet.png", {
        player: [0, 0]
    });

    //loading screen that displays while assets are being loaded
    Crafty.scene("loading", function () {
        Crafty.load(["images/spriteSheet.png"], function () {
            Crafty.scene("main");
        });

        //white background with some loading text
        Crafty.background("#FFF");
        Crafty.e("2D, DOM, Text").attr({ w: 100, h: 20, x: 150, y: 120 })
                .text("Loading")
                .css({ "text-align": "center" });
    });

    //automatically play the loading scene
    Crafty.scene("loading");  
    
    //
    Crafty.c('Ball', {
        Ball: function() {
                //setup animations
                this.requires("SpriteAnimation")
                .animate("walk_left", 1,0,1)
                .animate("walk_right", 0,0,0)
                .animate("walk_up", 2,0,2)
                .animate("walk_down", 3,0,3)
                //change direction when a direction change event is received
                .bind("NewDirection",
                    function (direction) {
                        if (direction.x < 0) {
                            if (!this.isPlaying("walk_left"))
                                this.stop().animate("walk_left", 10, -1);
                        }
                        if (direction.x > 0) {
                            if (!this.isPlaying("walk_right"))
                                this.stop().animate("walk_right", 10, -1);
                        }
                        if (direction.y < 0) {
                            if (!this.isPlaying("walk_up"))
                                this.stop().animate("walk_up", 10, -1);
                        }
                        if (direction.y > 0) {
                            if (!this.isPlaying("walk_down"))
                                this.stop().animate("walk_down", 10, -1);
                        }
                        if(!direction.x && !direction.y) {
                            this.stop();
                        }
                })
            return this;
        }
    });
    
    Crafty.c("LeftControls", {
        init: function() {
            this.requires('Multiway');
        },
        
        leftControls: function(speed) {
            this.multiway(speed, {W: -90, S: 90, D: 0, A: 180})
            return this;
        }
        
    });

    Crafty.scene("main", function () {
        generateWorld();
        
        //create our player entity with some premade components
        var player1 = Crafty.e("2D, DOM, Ball, player, LeftControls")
                .attr({ x: 256, y: 256, z: 1 })
                .leftControls(1)
                .Ball();
    });
};

style.css

body, html {
 margin:0;
 padding: 0;
 overflow: hidden;
 font-family:Arial;
 font-size:20px;
}

#cr-stage{
 border:2px solid black;
 margin:5px auto;
 color:white
}

Wednesday, August 8, 2012

python 2.7 : pygame 1.9.1 : animating an image in pygame

This is an example of animating an image using pygame:

image.py



Theese lines import the sys and pygame modules if you have python and the compatible pygame installed:

The following lines of code initializes curicial game variables and set prefferences for our loaded images such as replacing a specified color with zero opacity:

the main loop of the game handles animation and refreshing the screen:

Tuesday, August 7, 2012

C# : XNA 4.0: displaying an image to the screen


this code will enable you to display an image using C# XNA 4.0.

Game1.cs


Most of this code is generated by microsoft visual studio when you create your XNA game project in the Game1.cs file. The following includes some code not included in the template that create the final result.
This line of code creates a Texture2D object named "manifestCodeLogo1"

These lines of code change the screen resolution of the application. In this case I have chosen to have a 512x512 resolution application.

this line of code loads the image from this projects Content Library:


this code is responsible for drawing manifestCodeLogo1 and positioning it in the center of the application window:

Sunday, July 29, 2012

c programming : Hello World


This is an example of a c console application written and compiled using visual studio 2010:

HelloWorld.c


this program begins with a call to include the header file resource "stdio.h" in order to enable the compiler to comprehend the syntax used.
the next line of code in this program indicates the begining of the "main" function. every c program needs to have a main function that is automatically the first function called:
line "2" also indicates that this function will return an integer value. the open and closed parentheses with nothing between them indicates that this function does not require values called "arguments" to be initialized. The curly brace on line "3" indicates the beginning of the function body.
the following code prints user defined text to the system console, and then waits for the user to enter a key before exiting:

because this function returns an integer value, line "6" is necessary to satisfy that requirement by returning an integer value of "0".