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

Username:   Password: 
 RegisterRegister   
 My Game Crashes When I Move My Character About 95 Times! How To Fix!
Index -> Programming, Turing -> Turing Submissions
View previous topic Printable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
master-awesome




PostPosted: Wed Dec 24, 2014 4:00 pm   Post subject: My Game Crashes When I Move My Character About 95 Times! How To Fix!

I was hoping that when my game restarts, this would get rid of the problem, but it is still giving me the same error. How do I fix this?


Game Test.zip
 Description:

Download
 Filename:  Game Test.zip
 Filesize:  8.96 MB
 Downloaded:  185 Time(s)

Sponsor
Sponsor
Sponsor
sponsor
Zren




PostPosted: Wed Dec 24, 2014 5:07 pm   Post subject: RE:My Game Crashes When I Move My Character About 95 Times! How To Fix!

Since others are lazy, I'll just paste your code here.

Turing:

% Name: Master-Awesome
% Updated Date: 12/23/2014

import GUI
View.Set ("graphics:720;580")

% Declare Variables
var picID : int
var x : int := 1
var y : int := 36
var dx : int := 600
var dy : int := 36
var xm : int := 720
var ym : int := 360
var dxm : int := 720
var dym : int := 200
var key : string (1)
var mlives : int := 3
var dmlives : int := 9
var steps : int := 94
var fireblastx : int := 600
var fireblasty : int := 80
var fireblastshape : int := 30
var gravity : int := 20
var ground : int := 36
var jump : int := 200

procedure background
    % Background
    GUI.SetBackgroundColour (7)
    picID := Pic.FileNew ("Dark-Castle.jpg")
    Pic.Draw (picID, 215, 370, picCopy)
    colourback (20)
    locate (35, 1)
    put ""
    colourback (20)
    locate (36, 1)
    put ""
    drawfillbox (1, 34, 720, 360, 7)

    % Mario
    drawfillbox (x, y, 720, 200, 7)
    picID := Pic.FileNew ("Mario/Mario Walking Original/Walking-Mario-1.jpg")
    Pic.Draw (picID, x, y, picCopy)

    % Display Lives
    % Mario Lives
    colour (0)
    colourback (7)
    locate (2, 8)
    put "Mario = ", mlives
    picID := Pic.FileNew ("Mario/Icon/Mario-icon.jpg")
    Pic.Draw (picID, 20, 400, picCopy)

    % Dark Mario Lives
    colour (0)
    locate (2, 70)
    put "Dark Mario = ", dmlives
    picID := Pic.FileNew ("Dark Mario/Icon/Mario-icon.jpg")
    Pic.Draw (picID, 540, 400, picCopy)

    % Display Steps
    colour (0)
    colourback (20)
    locate (35, 35)
    put "Mario Steps = ", steps
end background

% When Game Is Over, This Is Used To Restart

procedure Try_Again
    steps := 94
    mlives := 3
    dmlives := 9
    background
    drawfillbox (1, 34, 720, 360, 7)
    loop
        getch (key)
        if ord (key) = 205 then % right
            % Steps
            steps := steps - 1
            cls
            background
            % Controls
            drawfillbox (x, y, xm, ym, 7)
            x := x + 40
            picID := Pic.FileNew ("Mario/Mario Walking Original/Walking-Mario-2.jpg")
            Pic.Draw (picID, x, y, picCopy)
            delay (50)
            picID := Pic.FileNew ("Mario/Mario Walking Original/Walking-Mario-3.jpg")
            Pic.Draw (picID, x, y, picCopy)
            delay (50)
            picID := Pic.FileNew ("Mario/Mario Walking Original/Walking-Mario-1.jpg")
            Pic.Draw (picID, x, y, picCopy)
            delay (50)
            % Dark Mario
            drawfillbox (dx, dy, dxm, dym, 7)
            randint (dx, 1, 600)
            picID := Pic.FileNew ("Dark Mario/Mario Walking Opposite Direction/Mario-Walking-Opposite-Direction-2.jpg")
            Pic.Draw (picID, dx, dy, picCopy)
            delay (50)
            picID := Pic.FileNew ("Dark Mario/Mario Walking Opposite Direction/Mario-Walking-Opposite-Direction-3.jpg")
            Pic.Draw (picID, dx, dy, picCopy)
            delay (50)
            picID := Pic.FileNew ("Dark Mario/Mario Walking Opposite Direction/Mario-Walking-Opposite-Direction-1.jpg")
            Pic.Draw (picID, dx, dy, picCopy)
            delay (50)
        elsif ord (key) = 203 then % left
            % Steps
            steps := steps - 1
            cls
            background
            % Controls
            drawfillbox (x, y, xm, ym, 7)
            x := x - 40
            picID := Pic.FileNew ("Mario/Mario Walking Opposite Direction/Mario-Walking-Opposite-Direction-2.jpg")
            Pic.Draw (picID, x, y, picCopy)
            delay (50)
            picID := Pic.FileNew ("Mario/Mario Walking Opposite Direction/Mario-Walking-Opposite-Direction-3.jpg")
            Pic.Draw (picID, x, y, picCopy)
            delay (50)
            picID := Pic.FileNew ("Mario/Mario Walking Opposite Direction/Mario-Walking-Opposite-Direction-1.jpg")
            Pic.Draw (picID, x, y, picCopy)
            % Dark Mario
            drawfillbox (dx, dy, dxm, dym, 7)
            randint (dx, 1, 600)
            picID := Pic.FileNew ("Dark Mario/Mario Walking Opposite Direction/Mario-Walking-Opposite-Direction-2.jpg")
            Pic.Draw (picID, dx, dy, picCopy)
            delay (50)
            picID := Pic.FileNew ("Dark Mario/Mario Walking Opposite Direction/Mario-Walking-Opposite-Direction-3.jpg")
            Pic.Draw (picID, dx, dy, picCopy)
            delay (50)
            picID := Pic.FileNew ("Dark Mario/Mario Walking Opposite Direction/Mario-Walking-Opposite-Direction-1.jpg")
            Pic.Draw (picID, dx, dy, picCopy)
            delay (50)
        elsif ord (key) = 32 then    % Space To Shoot
            drawfilloval (fireblastx, fireblasty, fireblastshape, fireblastshape, 7)
            fireblastx := fireblastx - 10
            drawfilloval (fireblastx, fireblasty, fireblastshape, fireblastshape, 41)
            % Controls
            drawfillbox (x, y, xm, ym, 7)
            % Apply Gravity For Character Fall
            y -= gravity
            if y < ground then
                y := ground
            end if
            picID := Pic.FileNew ("Mario/Mario Walking Original/Walking-Mario-1.jpg")
            Pic.Draw (picID, x, y, picCopy)
            delay (50)
        elsif ord (key) = 208 then   % Down To Reset Shoot Position
            drawfilloval (fireblastx, fireblasty, fireblastshape, fireblastshape, 7)
            fireblastx := 600
            drawfilloval (fireblastx, fireblasty, fireblastshape, fireblastshape, 41)
        elsif ord (key) = 200 and y = ground then  % Up To Jump
            % Steps
            steps := steps - 1
            cls
            background
            % Controls
            drawfillbox (x, y, xm, ym, 7)
            y := jump
            picID := Pic.FileNew ("Mario/Mario Walking Original/Walking-Mario-2.jpg")
            Pic.Draw (picID, x, y, picCopy)
            delay (50)
            picID := Pic.FileNew ("Mario/Mario Walking Original/Walking-Mario-3.jpg")
            Pic.Draw (picID, x, y, picCopy)
            delay (50)
            picID := Pic.FileNew ("Mario/Mario Walking Original/Walking-Mario-1.jpg")
            Pic.Draw (picID, x, y, picCopy)
            delay (50)
            % Dark Mario
            drawfillbox (dx, dy, dxm, dym, 7)
            randint (dx, 1, 600)
            picID := Pic.FileNew ("Dark Mario/Mario Walking Opposite Direction/Mario-Walking-Opposite-Direction-2.jpg")
            Pic.Draw (picID, dx, dy, picCopy)
            delay (50)
            picID := Pic.FileNew ("Dark Mario/Mario Walking Opposite Direction/Mario-Walking-Opposite-Direction-3.jpg")
            Pic.Draw (picID, dx, dy, picCopy)
            delay (50)
            picID := Pic.FileNew ("Dark Mario/Mario Walking Opposite Direction/Mario-Walking-Opposite-Direction-1.jpg")
            Pic.Draw (picID, dx, dy, picCopy)
        end if

        % Display Fire Blast
        drawfilloval (fireblastx, fireblasty, fireblastshape, fireblastshape, 7)
        fireblastx := fireblastx - 10
        drawfilloval (fireblastx, fireblasty, fireblastshape, fireblastshape, 41)

        % Apply Gravity For Character Fall
        y -= gravity
        if y < ground then
            y := ground
        end if

        % Lives
        if x = dx and y = dy then
            mlives := mlives - 1
            steps := steps - 1
            cls
            background
        elsif fireblastx < dx then
            dmlives := dmlives - 1
            delay (20)
            cls
            background
            fireblastx := 600
        end if
        % Game Over
        if mlives <= 0 then
            cls
            GUI.SetBackgroundColour (7)
            picID := Pic.FileNew ("Game-Over.jpg")
            Pic.Draw (picID, 126, 260, picCopy)
            % Buttons
            var game_over : int := GUI.CreateButton (320, 200, 0, "Try Again", Try_Again)
            % Button Press (The Source Of The Stupid Code That Pops Up Right After
            % You Close The Program Because Of The Version Of Windows Being The
            % Latest Which I Hope Turing Fixes In 4.1.2)
            loop
                exit when GUI.ProcessEvent
            end loop
        end if
    end loop
end Try_Again

% Original Game

background
loop
    getch (key)
    if ord (key) = 205 then     % right
        % Steps
        steps := steps - 1
        cls
        background
        % Controls
        drawfillbox (x, y, xm, ym, 7)
        x := x + 40
        picID := Pic.FileNew ("Mario/Mario Walking Original/Walking-Mario-2.jpg")
        Pic.Draw (picID, x, y, picCopy)
        delay (50)
        picID := Pic.FileNew ("Mario/Mario Walking Original/Walking-Mario-3.jpg")
        Pic.Draw (picID, x, y, picCopy)
        delay (50)
        picID := Pic.FileNew ("Mario/Mario Walking Original/Walking-Mario-1.jpg")
        Pic.Draw (picID, x, y, picCopy)
        delay (50)
        % Dark Mario
        drawfillbox (dx, dy, dxm, dym, 7)
        randint (dx, 1, 600)
        picID := Pic.FileNew ("Dark Mario/Mario Walking Opposite Direction/Mario-Walking-Opposite-Direction-2.jpg")
        Pic.Draw (picID, dx, dy, picCopy)
        delay (50)
        picID := Pic.FileNew ("Dark Mario/Mario Walking Opposite Direction/Mario-Walking-Opposite-Direction-3.jpg")
        Pic.Draw (picID, dx, dy, picCopy)
        delay (50)
        picID := Pic.FileNew ("Dark Mario/Mario Walking Opposite Direction/Mario-Walking-Opposite-Direction-1.jpg")
        Pic.Draw (picID, dx, dy, picCopy)
        delay (50)
    elsif ord (key) = 203 then     % left
        % Steps
        steps := steps - 1
        cls
        background
        % Controls
        drawfillbox (x, y, xm, ym, 7)
        x := x - 40
        picID := Pic.FileNew ("Mario/Mario Walking Opposite Direction/Mario-Walking-Opposite-Direction-2.jpg")
        Pic.Draw (picID, x, y, picCopy)
        delay (50)
        picID := Pic.FileNew ("Mario/Mario Walking Opposite Direction/Mario-Walking-Opposite-Direction-3.jpg")
        Pic.Draw (picID, x, y, picCopy)
        delay (50)
        picID := Pic.FileNew ("Mario/Mario Walking Opposite Direction/Mario-Walking-Opposite-Direction-1.jpg")
        Pic.Draw (picID, x, y, picCopy)
        % Dark Mario
        drawfillbox (dx, dy, dxm, dym, 7)
        randint (dx, 1, 600)
        picID := Pic.FileNew ("Dark Mario/Mario Walking Opposite Direction/Mario-Walking-Opposite-Direction-2.jpg")
        Pic.Draw (picID, dx, dy, picCopy)
        delay (50)
        picID := Pic.FileNew ("Dark Mario/Mario Walking Opposite Direction/Mario-Walking-Opposite-Direction-3.jpg")
        Pic.Draw (picID, dx, dy, picCopy)
        delay (50)
        picID := Pic.FileNew ("Dark Mario/Mario Walking Opposite Direction/Mario-Walking-Opposite-Direction-1.jpg")
        Pic.Draw (picID, dx, dy, picCopy)
        delay (50)
    elsif ord (key) = 32 then        % Space To Shoot
        drawfilloval (fireblastx, fireblasty, fireblastshape, fireblastshape, 7)
        fireblastx := fireblastx - 10
        drawfilloval (fireblastx, fireblasty, fireblastshape, fireblastshape, 41)
        % Controls
        drawfillbox (x, y, xm, ym, 7)
        % Apply Gravity For Character Fall
        y -= gravity
        if y < ground then
            y := ground
        end if
        picID := Pic.FileNew ("Mario/Mario Walking Original/Walking-Mario-1.jpg")
        Pic.Draw (picID, x, y, picCopy)
        delay (50)
    elsif ord (key) = 208 then       % Down To Reset Shoot Position
        drawfilloval (fireblastx, fireblasty, fireblastshape, fireblastshape, 7)
        fireblastx := 600
        drawfilloval (fireblastx, fireblasty, fireblastshape, fireblastshape, 41)
    elsif ord (key) = 200 and y = ground then      % Up To Jump
        % Steps
        steps := steps - 1
        cls
        background
        % Controls
        drawfillbox (x, y, xm, ym, 7)
        y := jump
        picID := Pic.FileNew ("Mario/Mario Walking Original/Walking-Mario-2.jpg")
        Pic.Draw (picID, x, y, picCopy)
        delay (50)
        picID := Pic.FileNew ("Mario/Mario Walking Original/Walking-Mario-3.jpg")
        Pic.Draw (picID, x, y, picCopy)
        delay (50)
        picID := Pic.FileNew ("Mario/Mario Walking Original/Walking-Mario-1.jpg")
        Pic.Draw (picID, x, y, picCopy)
        delay (50)
        % Dark Mario
        drawfillbox (dx, dy, dxm, dym, 7)
        randint (dx, 1, 600)
        picID := Pic.FileNew ("Dark Mario/Mario Walking Opposite Direction/Mario-Walking-Opposite-Direction-2.jpg")
        Pic.Draw (picID, dx, dy, picCopy)
        delay (50)
        picID := Pic.FileNew ("Dark Mario/Mario Walking Opposite Direction/Mario-Walking-Opposite-Direction-3.jpg")
        Pic.Draw (picID, dx, dy, picCopy)
        delay (50)
        picID := Pic.FileNew ("Dark Mario/Mario Walking Opposite Direction/Mario-Walking-Opposite-Direction-1.jpg")
        Pic.Draw (picID, dx, dy, picCopy)
    end if

    % Display Fire Blast
    drawfilloval (fireblastx, fireblasty, fireblastshape, fireblastshape, 7)
    fireblastx := fireblastx - 10
    drawfilloval (fireblastx, fireblasty, fireblastshape, fireblastshape, 41)

    % Apply Gravity For Character Fall
    y -= gravity
    if y < ground then
        y := ground
    end if

    % Lives
    if x = dx and y = dy then
        mlives := mlives - 1
        steps := steps - 1
        cls
        background
    elsif fireblastx < dx then
        dmlives := dmlives - 1
        delay (20)
        cls
        background
        fireblastx := 600
    end if
    % Game Over
    if mlives <= 0 or steps <= 0 then
        cls
        GUI.SetBackgroundColour (7)
        picID := Pic.FileNew ("Game-Over.jpg")
        Pic.Draw (picID, 126, 260, picCopy)
        % Buttons
        var game_over : int := GUI.CreateButton (320, 200, 0, "Try Again", Try_Again)
        % Button Press (The Source Of The Stupid Code That Pops Up Right After
        % You Close The Program Because Of The Version Of Windows Being The
        % Latest Which I Hope Turing Fixes In 4.1.2)
        loop
            exit when GUI.ProcessEvent
        end loop
    end if
end loop


And as I thought, you're loading new pictures every single frame. This is a memory leak, as it will continue to ask the Operating System for more memory for your program until you run out. To make it more noticeable for new programmers, Turing only allows a hard limit of just 1000 pictures.

You can free up a loaded picture to load another with Pic.Free.

The better method would be to not reload the picture from file every frame, by loading the pictures outside of the game loop.

Turing:


var marioWalkRightPicId := Pic.Load("...")
var marioWalkLeftPicId := Pic.Load("...")
var marioPicId := marioWalkRightPicId

loop % Game Loop
    % Input / Logic
    if Input.KeyDown(KEY_LEFT_ARROW) then
        marioPicId := marioWalkLeftPicId
    else if Input.KeyDown(KEY_RIGHT_ARROW) then
        % ...
    end if
   
    % Draw
    Pic.Draw(marioPicId, marioPosX, marioPosY, picCopy)
end loop


Merry Chrismas
master-awesome




PostPosted: Wed Dec 24, 2014 7:11 pm   Post subject: Re: My Game Crashes When I Move My Character About 95 Times! How To Fix!

Thank you so much! Now my game is functioning properly and I thank you even more because you actually gave me a proper example instead of making me look for tutorials and gave me a even greater break.

Merry Christmas to you too!!!



Game Test.zip
 Description:

Download
 Filename:  Game Test.zip
 Filesize:  8.96 MB
 Downloaded:  209 Time(s)

Display posts from previous:   
   Index -> Programming, Turing -> Turing Submissions
View previous topic Tell A FriendPrintable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 3 Posts ]
Jump to:   


Style:  
Search: