Posted: 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
endif
if chars (KEY_UP_ARROW)and posy = BOX3_y2 and posx > BOX3_x1 and posx < BOX3_x2 then
vely := JUMP_SPEED
endif
if chars (KEY_UP_ARROW)and posy = BOX8_y2 and posx > BOX8_x1 and posx < BOX8_x2 then
vely := JUMP_SPEED
endif
if chars (KEY_UP_ARROW)and posy = BOX2_y2 and posx > BOX2_x1 and posx < BOX2_x2 then
vely := JUMP_SPEED
endif
% 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
endif
if chars (KEY_UP_ARROW)and posy = BOX2_y2 and posx > BOX2_x1 and posx < BOX2_x2 then
vely := JUMP_SPEED
endif
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
ThePyroManiac
Posted: 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
Posted: 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.
ThePyroManiac
Posted: 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
Posted: 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
Posted: 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
Posted: 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 what happened?
ThePyroManiac
Posted: 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
Dreadnought
Posted: 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
Posted: 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
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
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
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