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

Username:   Password: 
 RegisterRegister   
 2D Scrolling game
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Badsniper




PostPosted: Fri May 13, 2011 10:15 pm   Post subject: 2D Scrolling game

So I am (attempting to) make a 2D "Side scroller", adn I want to make my levels fairly long. However when I make a picture that is 6000 by 2500 pixels, it's TOO big, and makes my program lag really badly at school (at home it works fine.)

Is there any way that I can Keep the size of the picture, but make it not lag?

Here's my movement code to look through (it IS 270 some lines because it works with certain slopes) :

Turing:

var font1 : int := Font.New ("Arial:24")
var font2 : int := Font.New ("Arial:10")
var letter : string (1)
var level : int := 1
var x : int
var y : int
var jv : int := 15
var key : array char of boolean
var back : int := Pic.FileNew ("LEVEL" + intstr (level) + ".jpg")
var fall, jump, horizontal, done : boolean := false
var Possible_Jump : boolean := true
var grav : real := 1
var del : int
var x1, y1, oldx, oldy, dx, dy : int

x1 := 0
y1 := 0
var picscroll := Pic.Width (back)
var picscroll2 := Pic.Height (back)

proc movement

    back := Pic.FileNew ("level" + intstr (level) + ".jpg")
    picscroll := Pic.Width (back)
    picscroll2 := Pic.Height (back)
    Font.Draw ("LEVEL " + intstr (level), maxx div 2 - 50, maxy div 2, font1, black)
    View.UpdateArea (0, 0, maxx, maxy)
    delay (3000)

    x := 100
    y := 50
    jv := 15
    x1 := 0
    y1 := 0
    fall := false
    jump := false
    done := false

    loop

        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%        SCROLLING        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

        if x1 + picscroll > maxx and x > 5 * maxx div 8 then
            x1 -= dx
            x -= dx
        elsif x1 < 0 and x < 3 * maxx div 8 then
            x1 -= dx
            x -= dx
        end if

        if y1 + picscroll2 > maxy and y > 5 * maxy div 8 then
            y1 -= dy
            y -= dy
        elsif y1 < 0 and y < 3 * maxy div 8 then
            y1 -= dy
            y -= dy
        end if

        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%      INITIALIZE      %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

        del := 0
        Possible_Jump := true
        horizontal := false
        dx := 0
        dy := 0
        oldx := x
        oldy := y

        Pic.Draw (back, x1, y1, picMerge)
        Input.KeyDown (key)

        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   MOVE TO THE RIGHT   %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

        if key ('d') and whatdotcolour (x + 5, y - 5) ~= 7 and whatdotcolour (x + 5, y) ~= 7 and jump = false and fall = false then
            x += 2
            dx += 2
            horizontal := true
            for a : 1 .. 13
                if whatdotcolour (x, y - a - 5) = 7 then
                    y -= a
                    dy -= a
                    exit
                end if
            end for
        elsif key ('d') and whatdotcolour (x + 5, y + 5) ~= 7 and jump = false and fall = false then
            x += 2
            dx += 2
            horizontal := true
            for decreasing a : 8 .. 1
                if whatdotcolour (x, (y + a) - 5) = 7 then
                    y += a
                    dy += a
                    exit
                end if
            end for
        end if

        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   MOVE TO THE LEFT   %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

        if key ('a') and whatdotcolour (x - 5, y - 5) ~= 7 and whatdotcolour (x - 5, y) ~= 7 and jump = false and fall = false then
            x -= 2
            dx -= 2
            horizontal := true
            for a : 1 .. 13
                if whatdotcolour (x, y - a - 5) = 7 then
                    y -= a
                    dy -= a
                    exit
                end if
            end for
        elsif key ('a') and whatdotcolour (x - 5, y + 5) ~= 7 and whatdotcolour (x - 3, y - 5) = 7 and jump = false and fall = false then
            x -= 2
            dx -= 2
            horizontal := true
            for decreasing a : 8 .. 1
                if whatdotcolour (x, (y + a) - 5) = 7 then
                    y += a
                    dy += a
                    exit
                end if
            end for
        end if

        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%    POSSIBLE JUMP     %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

        for a : 1 .. 10
            if whatdotcolour (x, y + 5 + a) = 7 then
                Possible_Jump := false
            end if
        end for

        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%         JUMP         %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

        if key ('w') and whatdotcolour (x, y - 5) = 7 and jump = false and whatdotcolour (x, y + 5) ~= 7 and Possible_Jump then
            if key ('d') and whatdotcolour (x + 5, y) ~= 7 and whatdotcolour (x + 6, y) ~= 7 and whatdotcolour (x + 7, y) ~= 7 and horizontal = false then
                x += 3
                dx += 3
            end if
            if key ('a') and whatdotcolour (x - 5, y) ~= 7 and whatdotcolour (x - 6, y) ~= 7 and whatdotcolour (x - 7, y) ~= 7 and horizontal = false then
                x -= 3
                dx -= 3
            end if
            y += jv
            dy += jv
            jump := true
            del := 16
            for a : y - jv .. y
                if whatdotcolour (x, a + 5) = 7 then
                    jump := false
                    jv := 15
                    dy := (dy - jv) + (a - y)
                    y := a
                    exit
                end if
            end for
        elsif jump = true and whatdotcolour (x, y + 5) ~= 7 then
            if key ('d') and whatdotcolour (x + 5, y) ~= 7 and whatdotcolour (x + 6, y) ~= 7 and whatdotcolour (x + 7, y) ~= 7 and horizontal = false then
                x += 3
                dx += 3
            end if
            if key ('a') and whatdotcolour (x - 5, y) ~= 7 and whatdotcolour (x - 6, y) ~= 7 and whatdotcolour (x - 7, y) ~= 7 and horizontal = false then
                x -= 3
                dx -= 3
            end if
            y += jv
            dy += jv
            for a : y - jv .. y
                if whatdotcolour (x, a + 5) = 7 then
                    jump := false
                    dy := (dy - jv) + (a - y)
                    jv := 15
                    y := a
                    exit
                end if
            end for
            jv -= 1
            del := 16
            if jv < 2 then
                jump := false
                jv := 15
            end if
        end if

        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%         FALL        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

        if whatdotcolour (x, y - 5) ~= 7 and fall = false then
            grav := 1
            y -= grav div 1
            dy -= grav div 1
            if key ('d') and whatdotcolour (x + 5, y) ~= 7 and whatdotcolour (x + 6, y) ~= 7 and whatdotcolour (x + 7, y) ~= 7 and horizontal = false then
                x += 3
                dx += 3
            end if
            if key ('a') and whatdotcolour (x - 5, y) ~= 7 and whatdotcolour (x - 6, y) ~= 7 and whatdotcolour (x - 7, y) ~= 7 and horizontal = false then
                x -= 3
                dx -= 3
            end if
            grav += 0.5
            fall := true
            del := 16
        elsif fall = true then
            y -= grav div 1
            dy -= grav div 1
            for decreasing a : y + grav div 1 .. y
                if whatdotcolour (x, a - 5) = 7 then
                    dy := a - (y + grav div 1)
                    fall := false
                    grav := 1
                    y := a
                    exit
                end if
            end for
            if grav < 12 then
                grav += 0.5
            else
                grav := 13
            end if
            if key ('d') and whatdotcolour (x + 5, y) ~= 7 and whatdotcolour (x + 6, y) ~= 7 and whatdotcolour (x + 7, y) ~= 7 and horizontal = false then
                x += 3
                dx += 3
            end if
            if key ('a') and whatdotcolour (x - 5, y) ~= 7 and whatdotcolour (x - 6, y) ~= 7 and whatdotcolour (x - 7, y) ~= 7 and horizontal = false then
                x -= 3
                dx -= 3
            end if
            del := 16
        end if

        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%    OTHER THINGS      %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

        if whatdotcolour (x, y - 5) = 7 then
            grav := 1
            fall := false
            jump := false
            jv := 15
        end if

        if key ('p') then
            drawfillbox (maxx div 2 - 100, maxy div 2 - 100, maxx div 2 + 100, maxy div 2 + 100, 9)
            locatexy (maxx div 2 - 50, maxy div 2 - 50)
            drawfilloval (x, y, 3, 3, 3)
            Font.Draw ("P A U S E D", maxx div 2 - 85, maxy div 2, font1, black)
            Font.Draw ("press any key to resume", maxx div 2 - 67, maxy div 2 - 75, font2, black)
            View.UpdateArea (0, 0, maxx, maxy)
            getch (letter)
            delay (100)
        end if

        if key (KEY_ESC) then
            cls
            Font.Draw ("You have quit the game, and will be returned to the menu", maxx div 2 - 165, maxy div 2, font2, black)
            View.UpdateArea (0, 0, maxx, maxy)
            delay (2000)
            done := true
            exit
        end if

        dx := x - oldx
        dy := y - oldy

        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%    VARIABLE TRACE    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

        exit when whatdotcolour (x, y) = 145
        drawfilloval (x, y, 3, 3, 3)
        put dx, " ", dy
        put x, " ", y
        View.UpdateArea (0, 0, maxx, maxy)
        delay (del)
        cls

    end loop

end movement
[/syntax]
Sponsor
Sponsor
Sponsor
sponsor
mirhagk




PostPosted: Fri May 13, 2011 10:31 pm   Post subject: RE:2D Scrolling game

simply cut your picture up, and only load in and show the pics you need at that time
Badsniper




PostPosted: Fri May 13, 2011 10:50 pm   Post subject: Re: 2D Scrolling game

But won't that be worse because then I would be loading 2 pictures at once when you reach a certain point?
RandomLetters




PostPosted: Fri May 13, 2011 11:01 pm   Post subject: RE:2D Scrolling game

Discard the old pictures when you no longer need them.

2 quarters of a picture is much better than 1 giant picture.
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  [ 4 Posts ]
Jump to:   


Style:  
Search: