Problem getting two obstacles being drawn on the same spot regardless of a for loop.
Author |
Message |
AustinRF
|
Posted: Thu Jan 16, 2014 7:31 pm Post subject: Problem getting two obstacles being drawn on the same spot regardless of a for loop. |
|
|
What is it you are trying to achieve?
To make a game for my Grade 11 Computer Science course.
As you can see, there is a loop drawing a line every time another line is 1/6th down the screen. the problem is the first line is drawing where the second one is as well.
What is the problem you are having?
Two obstacles are drawing on one spot regardless of the for loop "limiting" it to two separate ones.
Describe what you have tried to solve this problem
I've gone through my code trying to find my error and it seems I need a new persective on things.
Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)
Turing: |
setscreen ("nocursor")
setscreen ("graphics:640;480")
View.Set ("offscreenonly")
type Line :
record
x : int
y : int
exists : boolean
end record
var blacklines : array 1 .. 6 of Line
const upperBound := upper (blacklines )
const lowerBound := lower (blacklines )
var firstLineY : int := 0
var colours : int := Rand.Int (34, 103)
var fourseventy : int := 470
var leftboxx, rightboxx : real
leftboxx := 0
rightboxx := 640
var randomnumber : real := Rand.Int (round (leftboxx + 20), round (rightboxx - 20))
var x, y, button : int
var font1 : int
font1 := Font.New ("calibri:12")
assert font1 > 0
for i : 1 .. 6
blacklines (i ).x := Rand.Int (round (leftboxx + 20), round (rightboxx - 20))
blacklines (i ).y := 480
blacklines (i ).exists := false
end for
blacklines (1).exists := true
loop
firstLineY := blacklines (1).y
for i : 1 .. 6
if blacklines (i ).y = 0 then
blacklines (i ).x := Rand.Int (round (leftboxx ), round (rightboxx ))
blacklines (i ).y := 480
end if
end for
for i : 1 .. 6
if blacklines (i ).exists = true then
blacklines (i ).y - = 3
end if
end for
for i : 1 .. 6
if firstLineY <= (480 div 6) * i then
blacklines (i ).exists := true
end if
end for
Mouse.Where (x, y, button )
Draw.FillOval (x, y, 10, 10, colours )
Draw.FillBox (0, 0, round (leftboxx ), 480, 7)
Draw.FillBox (640, 0, round (rightboxx ), 480, 7)
for i : 1 .. 6
if blacklines (i ).exists = true then
Draw.FillBox (round (leftboxx ), blacklines (i ).y, round (blacklines (i ).x ), blacklines (i ).y + 10, 7)
Draw.FillBox (round (blacklines (i ).x ) + 75, blacklines (i ).y, round (rightboxx ), blacklines (i ).y + 10, 7)
end if
end for
leftboxx := leftboxx + 0. 5
rightboxx := rightboxx - 0. 5
View.Update
delay (25)
cls
end loop
Font.Free (font1 )
|
Please specify what version of Turing you are using
Latest Version. |
|
|
|
|
|
Sponsor Sponsor
|
|
|
Dreadnought
|
Posted: Thu Jan 16, 2014 9:18 pm Post subject: Re: Problem getting two obstacles being drawn on the same spot regardless of a for loop. |
|
|
Turing: | firstLineY := blacklines (1).y
for i : 1 .. 6
if blacklines (i ).y = 0 then
blacklines (i ).x := Rand.Int (round (leftboxx ), round (rightboxx ))
blacklines (i ).y := 480
end if
end for |
From this we can see that firstLineY is never bigger than 480.
Turing: | for i : 1 .. 6
if firstLineY <= (480 div 6) * i then
blacklines (i).exists := true
end if
end for |
What is the value of (480 div 6) * i when i = 6 ? What does this mean about blacklines(1) and blacklines(6)? |
|
|
|
|
|
AustinRF
|
Posted: Thu Jan 16, 2014 9:31 pm Post subject: Re: Problem getting two obstacles being drawn on the same spot regardless of a for loop. |
|
|
well, they will equal 80, and 480. What I don't get is how to fix it. |
|
|
|
|
|
Zren
|
Posted: Thu Jan 16, 2014 9:51 pm Post subject: RE:Problem getting two obstacles being drawn on the same spot regardless of a for loop. |
|
|
Not terribly related to your problems but:
Turing: |
for i : 1 .. 6
if blacklines (i ).y = 0 then
blacklines (i ).x := Rand.Int (round (leftboxx ), round (rightboxx ))
blacklines (i ).y := 480
end if
end for
|
What happens if blacklines (i).y ends up negative? The "spawning" code won't execute. Try using y <= 0 in case y steps over the value of 0. |
|
|
|
|
|
Dreadnought
|
Posted: Thu Jan 16, 2014 10:22 pm Post subject: Re: Problem getting two obstacles being drawn on the same spot regardless of a for loop. |
|
|
AustinRF wrote:
well, they will equal 80, and 480. What I don't get is how to fix it.
If you print out the values of blacklines(1).y and blacklines(6).y you'll find that they always differ by 3 (except possibly when one is at the top and thee other at the bottom). Why might that be?
What if you try to think about when blacklines(1).exists is true. What about blacklines(6).exists? |
|
|
|
|
|
AustinRF
|
Posted: Fri Jan 17, 2014 2:08 pm Post subject: Re: Problem getting two obstacles being drawn on the same spot regardless of a for loop. |
|
|
Dreadnought @ Thu Jan 16, 2014 10:22 pm wrote: AustinRF wrote:
well, they will equal 80, and 480. What I don't get is how to fix it.
If you print out the values of blacklines(1).y and blacklines(6).y you'll find that they always differ by 3 (except possibly when one is at the top and thee other at the bottom). Why might that be?
What if you try to think about when blacklines(1).exists is true. What about blacklines(6).exists?
Hm.. The problem is they are drawing only 3 pixels apart? Any idea how to fix it? |
|
|
|
|
|
Dreadnought
|
Posted: Fri Jan 17, 2014 10:04 pm Post subject: Re: Problem getting two obstacles being drawn on the same spot regardless of a for loop. |
|
|
Well I would rather you figured it out on your own, but I'll try to point you toward the problem.
So you begin with all your lines at y = 480.
You start lines moving according to this (except for the first line which starts in motion).
Turing: | for i : 1 .. 6
if firstLineY <= (480 div 6) * i then
blacklines (i).exists := true
end if
end for |
Notice that (480 div 6) * 6 is 480. Since your first line starts at 480 what happens to your sixth line?
From here it isn't hard to figure out when each line starts moving. Which line is the last one to start moving? Maybe this could give you a hint of how you could change things. |
|
|
|
|
|
|
|