Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Game Engine problem (based off of the tutorial)
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
rcbhs




PostPosted: Tue Jan 06, 2009 12:42 pm   Post subject: Game Engine problem (based off of the tutorial)

Soooo, I am trying to expand the Game Engne tutorial to work with my own game. Now a quick explination is...I want to make it so I can place objects as sprites instead of pictures. I am not exactly sure how to do this. Where would I put the Sprite.New, Sprite.SetHeight, Sprite.SetLocation, etc... I am really confused here.

MageObjects.t:

code:


class Player
    inherit Mage

    stepAmount := 4

    body procedure DrawObject ()
        Pic.Draw (PicId, posX, posY, picMerge)
    end DrawObject

    body procedure Update (Keys : array char of boolean)

        if (Keys (KEY_DOWN_ARROW)) then
            Move (0, -stepAmount, true)
        end if
        if (Keys (KEY_UP_ARROW)) then
            Move (0, stepAmount, true)
        end if
        if (Keys (KEY_LEFT_ARROW)) then
            Move (-stepAmount, 0, true)
        end if
        if (Keys (KEY_RIGHT_ARROW)) then
            Move (stepAmount, 0, true)
        end if
    end Update
end Player


mageGame.t (driver):

code:

setscreen ("graphics:640;480,offscreenonly")

include "mageEngine.t"
include "mageObjects.t"

var Engine : pointer to GameEngine
new Engine

Engine -> Create (3)

var Player_ : pointer to Player
new Player_
Engine -> AddObject (Player_) -> Create (".\\Mage\\BlackMageDownArrow.gif" ,320 ,60)
Engine -> Run ()


mageEngine.t:

code:

class Mage
    export Create, DrawObject, Update,
        posX, posY, stepAmount, Direction

    var PicId : int
    var ImageName : string
    var posX : int := 400
    var posY : int := 300
    var stepAmount : int
    var Direction : int := 0
    var mageSpriteUp, mageSpriteDown, mageSpriteLeft : int
    var magePicUp, magePicDown, magePicLeft, magePicRight : int


    procedure Create (img : string, posx, posy : int)
        ImageName := img
        posX := posx
        posY := posy
        PicId := Pic.FileNew (ImageName)
    end Create

    procedure Move (x, y : int, relative : boolean)
        if (relative) then
            posX += x
            posY += y
        else
            posX := x
            posY := y
        end if
    end Move

    procedure MoveDir ()
        Move (round (stepAmount * cosd (Direction)), round (stepAmount * sind (Direction)), true)
    end MoveDir

    deferred procedure DrawObject ()

    deferred procedure Update (Keys : array char of boolean)
end Mage


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


class GameEngine
    import Mage, Input
    export Create, Run, AddObject

    var Entities : flexible array 0 .. 1 of pointer to Mage
    var Keys : array char of boolean
    var Fps : int
    var FpsMilli : int

    var lastTime : int
    var thisTime : int

    procedure Create (fps : int)
        Fps := fps
        FpsMilli := 100 div Fps
        lastTime := Time.Elapsed
    end Create

    procedure Run ()
        loop
            thisTime := Time.Elapsed

            if (thisTime - lastTime >= FpsMilli) then
                lastTime := thisTime
                cls
                Input.KeyDown (Keys)
                if (Keys (KEY_ESC)) then
                    return
                end if

                if (upper (Entities) - 1 > 0) then
                    for i : 0 .. upper (Entities) - 2
                        Entities (i) -> Update (Keys)
                        Entities (i) -> DrawObject ()
                    end for
                end if

                View.Update ()
            end if
        end loop
    end Run


    function AddObject (obj : pointer to Mage) : pointer to Mage
        Entities (upper (Entities) - 1) := obj
        new Entities, upper (Entities) + 1
        result obj
    end AddObject
end GameEngine


Well you don't have the files so it won't work, however that is the code there. So my problem is, instead of a picture I want that to be a sprite (so I can do some movement animation. Also, what if I don't want 8 point movement? Just 4 point? What do I change there?)
Sponsor
Sponsor
Sponsor
sponsor
DemonWasp




PostPosted: Tue Jan 06, 2009 1:41 pm   Post subject: RE:Game Engine problem (based off of the tutorial)

Well, there are a few strategies:

Simple, Easy Strategy:
Each player has their own image / sprite. In the class, add a method called "setup" (these are technically called "constructors", but I don't think Turing has those); that's where you would do your Pic.New and your Sprite.New calls. You should also add a "destroy" method (destructor) which calls Sprite.Free and Pic.Free.

Then, in move, you'd do the Sprite.SetLocation.

Harder, More Efficient Strategy:
A lot of your players may be using the same image (particularly if you want to use things like projectiles). It would be really cumbersome to load the same image over and over, so why not make a "library" of images. A library looks like this:

code:

class ImageLibrary

    function GetImage ( filename : string ) : int
        % If we haven't loaded filename yet, load the picture, and store it in a list of filename-picture ID pairs.
        % If we HAVE loaded it already, just return the ID, instead of loading the picture again.
    end GetImage

end ImageLibrary


In this case, Pic.New is within ImageLibrary.GetImage, while Sprite.New is still in a constructor ("setup") method and Sprite.Free is in a destructor ("destroy") method. Pic.Free should only be called in ImageLibrary.Destroy, which is only called to clean up after your game.

This strategy is somewhat more difficult, but should save you headaches later with running out of the number of images you can load and performance issues.

4 versus 8-point movement
The solution to this is somewhere between the Update() and Move() procedures, you need to limit the entities to 4-point movement.
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 2 Posts ]
Jump to:   


Style:  
Search: