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

Username:   Password: 
 RegisterRegister   
 I cant figure out how to make it so that you don't teleport if you jump when your under a platform
Index -> Programming, Turing -> Turing Help
Goto page Previous  1, 2
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Dreadnought




PostPosted: Thu Mar 31, 2016 4:12 pm   Post subject: Re: I cant figure out how to make it so that you don't teleport if you jump when your under a platform

Maybe there is some confusion here. I'm not saying that placing the x1, x2, y1, y2 variables for a box into an array will reduce repeated code.

At the moment you are repeating code when you don't need to. For example:
Turing:

if chars (KEY_UP_ARROW) and posy = BOX_y2 and posx > BOX_x1 and posx < BOX_x2 then
    vely := JUMP_SPEED
end if

if chars (KEY_UP_ARROW) and posy = BOX3_y2 and posx > BOX3_x1 and posx < BOX3_x2 then
    vely := JUMP_SPEED
end if

if chars (KEY_UP_ARROW) and posy = BOX8_y2 and posx > BOX8_x1 and posx < BOX8_x2 then
    vely := JUMP_SPEED
end if

if chars (KEY_UP_ARROW) and posy = BOX2_y2 and posx > BOX2_x1 and posx < BOX2_x2 then
    vely := JUMP_SPEED
end if

% Let's ignore the bouncy box since it behaves a little differently

if chars (KEY_UP_ARROW) and posy = BOX1_y2 and posx > BOX1_x1 and posx < BOX1_x2 then
    vely := JUMP_SPEED
end if

if chars (KEY_UP_ARROW) and posy = BOX2_y2 and posx > BOX2_x1 and posx < BOX2_x2 then
    vely := JUMP_SPEED
end if

Notice how you basically have the same 3 lines repeated with the only changes being which box you are checking.
What will happen if you add another 5 or 10 or 20 boxes? Will you just copy the lines and slightly modify each one? Well you can do things that way and I won't stop you.
What will happen if you want to modify the behaviour? Will you modify each copy? Again, you can do this and it will work. However, there is a good way to avoid all this repetitive code.

What exactly is it that changes in each group of three lines? It's the box's "number". We would like to tell Turing that it should do the same thing multiple times but use different variables each time. The first time it can use BOX1_x1, BOX1_y1, BOX1_x2 and BOX1_y2. The second it can use BOX2_x1, BOX2_y1, BOX2_x2 and BOX2_y2. And so on.

In this way, the example with the prime numbers was similar. The repetitive code was
Turing:
put "Prime number 1 is: ", prime1
put "Prime number 2 is: ", prime2
put "Prime number 3 is: ", prime3
put "Prime number 4 is: ", prime4
put "Prime number 5 is: ", prime5

Here we wanted to tell Turing to do the same things multiple times, but use prime1 the first time, prime2 the second time and so on.

So what's the solution? Well, an array is a list of variables with a name for the entire list where we can access the the first, second, third, etc variables in the list with numbers.
In the example I gave, the list was a list of integers called primes and I told it to number the variables 1 to 5 (hence why I used array 1..5 of int). Then I could access the first variable in primes (effectively prime1) by writing primes(1), and similarly for the other variables.
From here, I used a for loop to perform the action (printing "Prime number n is _" once for each variable in the list.

In your case you have many boxes that are already numbered and (as far as I can tell) except for the bouncy box, they are treated the same way by your program. So it might make sense to put the boxes into a list and then use for loops to check collisions and draw boxes for all the boxes in your list. This way, you only write the code once even though it gets used many times.

Now you could make an array for each of x1, y1, x2 and y2. This would still allow to eliminate a lot of repetitive code. However, it might feel a bit unorganized to have a list of x1's, a list of x2's etc. So if you are want to organise your code even more, it would be nice to bundle these together for each box. One way would be to place these into a list of variables with an array (and yes an array of arrays is a like a list of lists of variables). You might not like this because when we replaces names like x_2 or y_2 by numbers like 3 and 4 we are losing the name and making the code harder to understand. If you have the time and want to learn even more new things, you might want to look into record which solves this proble. But understand that there is no problem with using one array for each of the coordinates of the boxes, it's just a bit disorganised.

You probably noticed I ignored the bouncy box. It behaves a little differently from your other boxes, so can can't just have it use the same code as the other boxes. For now I suggest you leave i on its own (unless you want to make other bouncy boxes, in which case you might want to make more arrays.

All that being said, using arrays won't change the functionality of your program. But it will help you write code more efficiently and reduce the likelihood of bugs arising from failing to change all instances of code that is repeated.
Sponsor
Sponsor
Sponsor
sponsor
ThePyroManiac




PostPosted: Fri Apr 01, 2016 8:44 am   Post subject: Re: I cant figure out how to make it so that you don't teleport if you jump when your under a platform

ok will do
ThePyroManiac




PostPosted: Fri Apr 01, 2016 8:49 am   Post subject: RE:I cant figure out how to make it so that you don\'t teleport if you jump when your under a platform

By the way I just wanted to thank you guys for all you're help. Smile Smile Smile
ThePyroManiac




PostPosted: Fri Apr 01, 2016 11:08 am   Post subject: Re: I cant figure out how to make it so that you don't teleport if you jump when your under a platform

I'm going to move onto the collision detection however the Tutorial say

Quote:

In order to use rectangular collision detection, you must have these pieces of information about both of your rectangles :
lowest x value of the rectangle
lowest y value of the rectangle
greatest x value of the rectangle
greatest y value of the rectangle



this is a problem because of two things 1st I only have "posx := 20 posy := 400" and 2nd the
lowest x value of the rectangle
lowest y value of the rectangle
greatest x value of the rectangle
greatest y value of the rectangle
all CHANGE god sake

i mean its probable really simple and im just an idiot but please help
ThePyroManiac




PostPosted: Fri Apr 01, 2016 11:11 am   Post subject: RE:I cant figure out how to make it so that you don\'t teleport if you jump when your under a platform

BTW im not giving up on the array im just going to take a break
Insectoid




PostPosted: Fri Apr 01, 2016 11:49 am   Post subject: RE:I cant figure out how to make it so that you don\'t teleport if you jump when your under a platform

That's what variables are for. Variables change. You don't personally have to know what the values are, just the computer does. Anywhere you need the lowest x values, just use box1_x1. Same for the others. Almost anywhere you need a number, you can use a variable (and 99% of the time you will use a variable).
ThePyroManiac




PostPosted: Fri Apr 01, 2016 12:06 pm   Post subject: RE:I cant figure out how to make it so that you don\'t teleport if you jump when your under a platform

i had 15 posts and 37 Bits but then i donated 5 to Dreadnought and now i only have 3 posts and 7 bits Sad what happened?
ThePyroManiac




PostPosted: Fri Apr 01, 2016 12:07 pm   Post subject: RE:I cant figure out how to make it so that you don\'t teleport if you jump when your under a platform

and the position of the character only has 1 x and y
Sponsor
Sponsor
Sponsor
sponsor
Dreadnought




PostPosted: Fri Apr 01, 2016 3:23 pm   Post subject: Re: I cant figure out how to make it so that you don't teleport if you jump when your under a platform

ThePyroManiac wrote:

and the position of the character only has 1 x and y

Yes, but but given this x and y you can work out what the other values you need are. (Hint: Look at how you draw your character.)
ThePyroManiac




PostPosted: Mon Apr 04, 2016 10:49 am   Post subject: Re: I cant figure out how to make it so that you don't teleport if you jump when your under a platform

ok i think i got it
code:
var BOX3_x1 : int := 50
var BOX3_y1 : int := 175
var BOX3_x2 : int := 150
var BOX3_y2 : int := 200
const GROUND_HEIGHT := 10
const RUN_SPEED := 10
const JUMP_SPEED := 17
const GRAVITY := 1.5
var chars : array char of boolean
var ground : boolean := true
var posx, posy : int
var velx, vely : real

posx := 20
velx := 0
posy := 400
vely := 0

loop









   
 
    Input.KeyDown (chars)
    if chars ('q') then
        exit
    end if
   
    Input.KeyDown (chars)
    if chars ('x') then
        vely := -GRAVITY
    end if
   
    Input.KeyDown (chars)
    if chars ('j') and posy = GROUND_HEIGHT then
        vely := JUMP_SPEED+20
    end if

    if chars (KEY_LEFT_ARROW) then
        velx := -RUN_SPEED
    elsif chars (KEY_RIGHT_ARROW) then
        velx := RUN_SPEED
    else
        velx := 0
    end if
if posy = GROUND_HEIGHT then
        ground := true
    else
        ground := false
    end if
 if chars (KEY_UP_ARROW) and posy = GROUND_HEIGHT then
        vely := JUMP_SPEED
    end if
   

    if chars (KEY_UP_ARROW) and posy = BOX3_y2 and posx > BOX3_x1 and posx < BOX3_x2 then
        vely := JUMP_SPEED
    end if
   
   


   
    vely -= GRAVITY
    posx += round (velx)
    posy += round (vely)

   
    if posy < GROUND_HEIGHT then
        posy := GROUND_HEIGHT
        vely := 0
    end if   


    cls
   
    if posy = GROUND_HEIGHT then
        drawfillbox (posx - 10, posy, posx + 10, posy + 20, 40)
       
   
    else
 
        drawfillbox (posx - 10, posy, posx + 10, posy + 20, 40)
    end if
    drawfillbox (BOX3_x1, BOX3_y1, BOX3_x2, BOX3_y2, 138)
    delay (20)

    if posx + 10 > BOX3_x1 and posx - 10 < BOX3_x2 and posy + 20 > BOX3_y1 and posy < BOX3_y2 then
       posy := BOX3_y2
        vely := 0
       
     end if
end loop

this is just a test but it works so im happy
ThePyroManiac




PostPosted: Mon Apr 04, 2016 11:29 am   Post subject: Re: I cant figure out how to make it so that you don't teleport if you jump when your under a platform

!!I DID IT!!
THANKS FOR ALL THE HELP
code:
%BY ISAAC
%version 2.0
%08/03/2016
%Turing 4.1.1

%Special thanks to Insectoid and Dreadnought
%for all the help on compsci.ca

const GROUND_HEIGHT := 130
const RUN_SPEED := 10
const JUMP_SPEED := 15
const GRAVITY := 1.7

var font1 : int
    font1 := Font.New ("Eras Bold ITC:20:bold")

var font2 : int
    font2 := Font.New ("Eras Bold ITC:10:bold")
   
var ground : boolean := true

var BOX_x1 : int := 500
var BOX_y1 : int := 128
var BOX_x2 : int := 550
var BOX_y2 : int := 175
var BOX1_x1 : int := 493
var BOX1_y1 : int := 175
var BOX1_x2 : int := 556
var BOX1_y2 : int := 185
var BOX2_x1 : int := 350
var BOX2_y1 : int := 275
var BOX2_x2 : int := 450
var BOX2_y2 : int := 300
var BOX3_x1 : int := 100
var BOX3_y1 : int := 250
var BOX3_x2 : int := 200
var BOX3_y2 : int := 275
var BOX4_x1 : int := 5
var BOX4_y1 : int := 300
var BOX4_x2 : int := 85
var BOX4_y2 : int := 20
var BOX5_x1 : int := 245
var BOX5_y1 : int := 250
var BOX5_x2 : int := 85
var BOX5_y2 : int := 20
var BOX6_x1 : int := 500
var BOX6_y1 : int := 350
var BOX6_x2 : int := 85
var BOX6_y2 : int := 20
var a1 : int := 1366
var a2 : int := 130
var a3 : int := 0
var a4 : int := 1000
var b1 : int := 1366
var b2 : int := 130
var b3 : int := 0
var b4 : int := 110
var c1 : int := 1366
var c2 : int := 109
var c3 : int := 0
var c4 : int := 0
var d1 : int := 1366
var d2 : int := 90
var d3 : int := 0
var d4 : int := 80
var e1 : int := 1366
var e2 : int := 60
var e3 : int := 0
var e4 : int := 50
var f1 : int := 500
var f2 : int := 550
var f3 : int := 85
var f4 : int := 20

var BOX7_1, BOX7_2, BOX7_3, BOX7_4 : int
BOX7_1 := 600
BOX7_2 := 225
BOX7_3 := 700
BOX7_4 := 250

var BOX8_1, BOX8_2, BOX8_3, BOX8_4 : int
BOX8_1 := 790
BOX8_2 := 340
BOX8_3 := 890
BOX8_4 := 365


View.Set ("graphics:1260;480,nobuttonbar")

var chars : array char of boolean


var posx, posy : int
var velx, vely : real


posx := 20
velx := 0
posy := 400
vely := 0
loop
Draw.Text ("", 220, 380, font1, red)

    Input.KeyDown (chars)
    if chars ('q') then
        exit
    end if
   
    Input.KeyDown (chars)
    if chars ('x') then
        vely := -GRAVITY
    end if
   
    Input.KeyDown (chars)
    if chars ('j') and posy = GROUND_HEIGHT then
        vely := JUMP_SPEED+20
    end if

Draw.Text ("GIVE ISAAC AN A+", 20, 20, font2, red)
    if chars (KEY_LEFT_ARROW) then
        velx := -RUN_SPEED
    elsif chars (KEY_RIGHT_ARROW) then
        velx := RUN_SPEED
    else
        velx := 0
    end if
if posy = GROUND_HEIGHT then
        ground := true
    else
        ground := false
    end if
 if chars (KEY_UP_ARROW) and posy = GROUND_HEIGHT then
        vely := JUMP_SPEED
    end if
   
    if chars (KEY_UP_ARROW) and posy = BOX_y2 and posx > BOX_x1 and posx < BOX_x2 then
        vely := JUMP_SPEED
    end if

    if chars (KEY_UP_ARROW) and posy = BOX3_y2 and posx > BOX3_x1 and posx < BOX3_x2 then
        vely := JUMP_SPEED
    end if
   
    if chars (KEY_UP_ARROW) and posy = BOX8_4 and posx > BOX8_1 and posx < BOX8_3 then
        vely := JUMP_SPEED
    end if
   
    if chars (KEY_UP_ARROW) and posy = BOX2_y2 and posx > BOX2_x1 and posx < BOX2_x2 then
        vely := JUMP_SPEED
    end if
   
    if posy = BOX7_4 and posx > BOX7_1 and posx < BOX7_3 then
        vely := JUMP_SPEED+5
    end if
   
   
    if chars (KEY_UP_ARROW) and posy = BOX1_y2 and posx > BOX1_x1 and posx < BOX1_x2 then
        vely := JUMP_SPEED
    end if
   
   
    if chars (KEY_UP_ARROW) and posy = BOX2_y2 and posx > BOX2_x1 and posx < BOX2_x2 then
        vely := JUMP_SPEED
    end if


   
    vely -= GRAVITY
    posx += round (velx)
    posy += round (vely)

   
    if posy < GROUND_HEIGHT then
        posy := GROUND_HEIGHT
        vely := 0
    end if
   

   
    if posy not= GROUND_HEIGHT and posy < BOX1_y2 and posx + 10 > BOX1_x1 and posx < BOX1_x2 then
        posy := BOX1_y2
        vely := 0
    end if
   
   
   
    if posx + 10 > BOX2_x1 and posx - 10 < BOX2_x2 and posy + 20 > BOX2_y1 and posy < BOX2_y2 then
       posy := BOX2_y2
        vely := 0
    end if
    if posx + 10 > BOX8_1 and posx - 10 < BOX8_3 and posy + 20 > BOX8_2 and posy < BOX8_4 then
       posy := BOX8_4
        vely := 0
    end if
   
   

      if posx + 10 > BOX7_1 and posx - 10 < BOX7_3 and posy + 20 > BOX7_2 and posy < BOX7_4 then
       posy := BOX7_4
        vely := 0
    end if
   
    if posx + 10 > BOX3_x1 and posx - 10 < BOX3_x2 and posy + 20 > BOX3_y1 and posy < BOX3_y2 then
       posy := BOX3_y2
        vely := 0
    end if
    drawfillbox (a1, a2, a3, a4, 79)
    drawfilloval (f1, f2, f3, f4, 0)
    drawfilloval (BOX6_x1, BOX6_y1, BOX6_x2, BOX6_y2, 0)
    drawfilloval (BOX5_x1, BOX5_y1, BOX5_x2, BOX5_y2, 0)
    drawfilloval (BOX4_x1, BOX4_y1, BOX4_x2, BOX4_y2, 0)
    drawfillbox (b1, b2, b3, b4, 10)
    drawfillbox (c1, c2, c3, c4, 67)
    drawfillbox (d1, d2, d3, d4, 65)
    drawfillbox (e1, e2, e3, e4, 65)
   
    if posy = GROUND_HEIGHT then
        drawfillbox (posx - 10, posy, posx + 10, posy + 20, 40)
       
   
    else
 
        drawfillbox (posx - 10, posy, posx + 10, posy + 20, 40)
    end if



    drawfillbox (BOX_x1, BOX_y1, BOX_x2, BOX_y2, 121)
    drawfillbox (BOX1_x1, BOX1_y1, BOX1_x2, BOX1_y2, 2)
    drawfillbox (BOX3_x1, BOX3_y1, BOX3_x2, BOX3_y2, 138)
    drawfillbox (BOX2_x1, BOX2_y1, BOX2_x2, BOX2_y2, 138)
    drawfillbox (BOX8_1, BOX8_2, BOX8_3, BOX8_4, 138)
    drawfillbox (BOX7_1, BOX7_2, BOX7_3, BOX7_4, 14)
   
    Draw.Text ("SUPER JUMP BROS", 220, 380, font1, red)

    View.Update
    delay (30)
end loop

Now all I need is a goal for the game.
any suggestions
there are no bad ideas
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 2 of 2  [ 26 Posts ]
Goto page Previous  1, 2
Jump to:   


Style:  
Search: