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

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




PostPosted: Mon Oct 12, 2015 5:03 pm   Post subject: 2D platformer

What is it you are trying to achieve?
A friend and I are writing a simple (ha!) 2D platformer.

What is the problem you are having?
So far, we've written the code for scrolling the background, but when we try to add sprites, it just goes catastrophic. Screen starts flashing. Alone, the scrolling code is fine, as well as the basic code for our sprites. There are no conflicting variables.

Describe what you have tried to solve this problem
We tested both pieces of code individually. They worked fine.

Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)

Turing:


%Platformer Game
var jump : int := 20
var grav : int := 1
var startTime : int
var lastFrameTime : int := 0
var frameTime : int := 0
var frameRate : int
var lastFrameRate : int := 0
var key : array char of boolean
var cx, cy : int
var vel : real
var backx, backy : int := 0
var backWidth, backHeight : int
var back : int := Pic.FileNew ("background.bmp")
var backscroll : int := 0
backWidth := Pic.Width (back)
backHeight := Pic.Height (back)
cx := 150
cy := 400
vel := 0
/*var mariopicture : int := Pic.FileNew ("mariopicture.bmp")
 var mariosprite : int := Sprite.New (mariopicture)*/



procedure DrawScreen
    Pic.Draw (back, backx, backy, picCopy)
    drawfillbox (cx - 10, cy, cx + 30, cy + 40, red)
    drawfillbox (0, 0, maxx, 10, black)
end DrawScreen

View.Set ("offscreenonly,graphics:1200,900")

loop
drawfillbox (0,0,10,10,gray)   
Input.KeyDown (key)

    if key (KEY_LEFT_ARROW) then
        cx -= 15
    elsif key (KEY_RIGHT_ARROW) then
        cx += 15
    end if

    if key (KEY_UP_ARROW) and cy = 10 then
        vel := jump
    end if

    vel -= grav
    cy += round (vel)

    if cy < 10 then
        cy := 10
        vel := 0
    end if

    if cx < 150 then
        if backx = 0 then
            backx := 0
            if backx = 0 then
                if cx < 30 then
                    cx := 30
                end if
            end if
        else
            cx := 150
            if backx = 0 then
                backx := 0
            else
                backx += 15
                if backx > 0 then
                backx := 0
                end if
            end if
        end if
    end if
    if cx > 1050 then
        if backx <= -6610 then
            backx := -6610
            if cx > 1150 then
                cx := 1150
            end if
        else
            cx := 1050
            if backx <= -6760 then
                backx := -6760
            else
                backx -= 15
            end if
        end if
    end if
    drawfillbox (0, 0, maxx, 10, black)
    delay (25)
    DrawScreen
    View.Update
    cls
end loop




Please specify what version of Turing you are using
4.1.1.

EDIT: Forgot to mention, this version doesn't include the full code for the sprites.

All that consisted of was-

Instead of:

Turing:


drawfillbox (cx - 10, cy, cx + 30, cy + 40, red)
drawfillbox (0, 0, maxx, 10, black)

% It was:

Sprite.SetPosition (mariosprite, cx - 10, cy, true)
Sprite.Show (mariosprite)

Sponsor
Sponsor
Sponsor
sponsor
Insectoid




PostPosted: Mon Oct 12, 2015 7:16 pm   Post subject: RE:2D platformer

You shouldn't need to include Sprite.Show more than once in your code except in special circumstances. Also, you have a lot of code that does nothing.

code:
if backx = 0 then
            backx := 0 %What's going on here?
            if backx = 0 then %Will this ever return false?
                if cx < 30 then
                    cx := 30
                end if
            end if
        else
            cx := 150
            if backx = 0 then %This doesn't do anything
                backx := 0
            else
                backx += 15
                if backx > 0 then
                backx := 0
                end if
            end if
        end if


A good chunk of that code makes no sense at all. It might work, but it's really, really bad. There shouldn't be if statements that never fail and there shouldn't be lines that do literally nothing.
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: