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

Username:   Password: 
 RegisterRegister   
 [Tutorial] 2-D mapping I (eg. Tetris)
Index -> Programming, Turing -> Turing Tutorials
Goto page Previous  1, 2
View previous topic Printable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
DemonZ




PostPosted: Sat Nov 18, 2006 7:42 pm   Post subject: (No subject)

yes thats it thanks i want it to go from tile to tile. can u show me how u do that?
Sponsor
Sponsor
Sponsor
sponsor
ericfourfour




PostPosted: Sat Nov 18, 2006 7:57 pm   Post subject: (No subject)

It is not that hard to do. I basically outlined it in my previous post. It should go like this though.

code:
loop
    for every entity
        if entity is not in motion
            allow entity to choose direction (make sure they can walk there too)
            if the entity will move
                activate animation for that entity corresponding to the direction
                block of tiles the entity is moving to and from
                disable movement for this entity
end loop

I think that is everything. I'm in a rush!
CodeMonkey2000




PostPosted: Sat Nov 18, 2006 10:27 pm   Post subject: (No subject)

Confused now i have a problem with collision.
i ont think i did this right
heres my code:

code:

setscreen ("graphics:100;200,offscreenonly")
var Space : array 0 .. 9, 0 .. 19 of int
var tilex : flexible array 0 .. 0 of int
var tiley : flexible array 0 .. 0 of int
var key : array char of boolean
var player :
    record
        x, y : int
    end record

player.x := 0
player.y := 0

var p : int := 0
for x : 0 .. 9
    for y : 0 .. 19
        Space (x, y) := 0
        drawfillbox (10 * x, y * 10, 10 + 10 * x, y * 10 + 10, Space (x, y))
        p += 1
    end for
end for

var Piece : array 1 .. 3 of array 0 .. 10, 0 .. 20 of int

var fileName : string := "map.t"
var fileNo : int := 0
var e : int
open : fileNo, fileName, get
for decreasing y : 19 .. 0
    for x : 0 .. 9
        get : fileNo, Space (x, y)

        if Space (x, y) > 0 then
            for t : 0 .. 10
                new tilex, upper (tilex) + 1
                e := upper (tilex)
                tilex (e) := 10 * x + t

                new tiley, upper (tiley) + 1
                e := upper (tiley)
                tiley (e) := 10 * y + t
            end for
        end if

    end for
end for
close : fileNo

proc redraw
    for y : 0 .. 19
        for x : 0 .. 9
            if Space (x, y) = 0 then
                drawfillbox (10 * x, y * 10, 10 + 10 * x, y * 10 + 10, black)
            elsif Space (x, y) = 1 then
                drawfillbox (10 * x, y * 10, 10 + 10 * x, y * 10 + 10, green)
            elsif Space (x, y) = 2 then
                drawfillbox (10 * x, y * 10, 10 + 10 * x, y * 10 + 10, red)
            elsif Space (x, y) = 3 then
                drawfillbox (10 * x, y * 10, 10 + 10 * x, y * 10 + 10, blue)
            end if
        end for
    end for
end redraw

fcn collide (x, y : int) : boolean
    var go : boolean := true

    for tx : 1 .. upper (tilex)
        if x = tilex (tx) then
            for ty : 1 .. upper (tiley)
                if y = tilex (ty) then
                    go := false
                end if
            end for
        end if
    end for

    result go
end collide
loop
    Input.KeyDown (key)

    redraw
    if key ('w') and collide (player.x, player.y + 5) then
        player.y += 5
    end if
    if key ('s') and collide (player.x, player.y - 5) then
        player.y -= 5
    end if
    if key ('d') and collide (player.x + 5, player.y) then
        player.x += 5
    end if
    if key ('a') and collide (player.x - 5, player.y) then
        player.x -= 5
    end if
    drawfillbox (player.x, player.y, player.x + 10, player.y + 10, yellow)
    View.Update
    Time.DelaySinceLast (50)
end loop

DemonZ




PostPosted: Sat Nov 18, 2006 10:52 pm   Post subject: (No subject)

ok thanks alot im gonna test this theory now (not really a theory more like a solution!)
ZeroPaladn




PostPosted: Mon Nov 20, 2006 1:31 pm   Post subject: (No subject)

Nice tutorial! Didn't know you could make an array of an array Laughing . It was a litle hard to read though.
ericfourfour




PostPosted: Mon Nov 20, 2006 3:31 pm   Post subject: (No subject)

When you first start 2D mapping I would recommend using 2D arrays. Once you are more experienced a 1D array will be easier to work with (have you ever tried working with a 2D flexible array?). There are however a few things you should know before working with a 1D array.

(This is with a 1D array with the index starting at 0)

The upper bound of the array.
code:
height * width - 1


Converting coordinates to an index.
code:
x + y * width


Converting an index to coordinates.
code:
x: index mod width
y: (index - xFormula) / width
CodeMonkey2000




PostPosted: Tue Nov 21, 2006 6:51 pm   Post subject: (No subject)

theres no such thing as a 2d flexible array yet in turing. it says "feature not available. unable to allocate memory-sorry for any inconvieance"
ericfourfour




PostPosted: Tue Nov 21, 2006 8:22 pm   Post subject: (No subject)

That is exactly my point. The 2D flexible array does work however. You can set the size of the second dimension once but after that you cannot change to a different size. The first dimension can be changed as often as you like. This can get tedious. That is why a 1D array is easier.
Sponsor
Sponsor
Sponsor
sponsor
CodeMonkey2000




PostPosted: Tue Nov 21, 2006 10:20 pm   Post subject: (No subject)

aww ok i get it. array 1..5,1..5 has 25 elements in it anyway so just declare 1..25. 2d arrays are just ways of organizing data
ericfourfour




PostPosted: Tue Nov 21, 2006 11:03 pm   Post subject: (No subject)

Yes, that is it exactly. There is one other thing though. You should be careful if the arrays are going to start at 1. You will have to add 1 to the formulae I previously mentioned to get the proper result.
Display posts from previous:   
   Index -> Programming, Turing -> Turing Tutorials
View previous topic Tell A FriendPrintable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 2 of 2  [ 25 Posts ]
Goto page Previous  1, 2
Jump to:   


Style:  
Search: