Computer Science Canada

Making an Etch-A-Sketch?

Author:  vdemons [ Sun May 15, 2011 2:44 pm ]
Post subject:  Making an Etch-A-Sketch?

I am trying to make an Etch-A-Sketch program for school and I got it to make lines with the arrow keys however, I want to make it so that it makes lines only when you click a button then stop making line when you click it again. Right now it always makes lines.

Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)
<View.Set ("graphics")

var x, y, radius : int
x := 200
y := 100
radius:=15

Draw.FillOval (x, y, radius, radius, red)

var chars : array char of boolean
View.Set ("offscreenonly")
loop
Input.KeyDown (chars)
if chars (KEY_UP_ARROW) then
y+=2
Draw.FillOval (x, y, radius, radius, red)
elsif chars (KEY_RIGHT_ARROW) then
x+=2
Draw.FillOval (x, y, radius, radius, red)
elsif chars (KEY_LEFT_ARROW) then
x-=2
Draw.FillOval (x, y, radius, radius, red)
elsif chars (KEY_DOWN_ARROW) then
y-=2
Draw.FillOval (x, y, radius, radius, red)
end if
View.Update
Time.Delay (5)
end loop>

Author:  mirhagk [ Sun May 15, 2011 2:52 pm ]
Post subject:  RE:Making an Etch-A-Sketch?

So have you checked out any of the mouse commands, i'd suggest going into the Turing help file, and doing a search for mouse.

Author:  Tony [ Sun May 15, 2011 2:56 pm ]
Post subject:  RE:Making an Etch-A-Sketch?

did you mean to ask a question in there somewhere? If so, please fill out the provided template next time.
code:

if <button-is-pressed> then
   % draw the line
else
   % don't draw the line
end if

Author:  vdemons [ Sun May 15, 2011 2:58 pm ]
Post subject:  RE:Making an Etch-A-Sketch?

What I really mean, is how do I make my circle move without making a line?

Author:  Tony [ Sun May 15, 2011 3:11 pm ]
Post subject:  Re: RE:Making an Etch-A-Sketch?

vdemons @ Sun May 15, 2011 2:58 pm wrote:
What I really mean, is...

Nice, now we are getting somewhere.

As you move the circle, you'd need to clear the screen from the circle's previous location. I imagine that right now you do that by drawing a piece of a "line" on top.

The two basic approaches are either:
- re-draw the entire screen as it was before.
- draw some minimal changes on top.

Author:  vdemons [ Sun May 15, 2011 3:16 pm ]
Post subject:  RE:Making an Etch-A-Sketch?

...and how would I do that, the only way I know of doing that is to use "cls" but it erases the entire screen!

Author:  Tony [ Sun May 15, 2011 3:20 pm ]
Post subject:  RE:Making an Etch-A-Sketch?

That's one way. Then you would need to re-draw the entire screen, as it was before (less the moving non-drawing circle). Meaning that you'd have to remember what was on the screen, somehow.

Author:  vdemons [ Sun May 15, 2011 3:21 pm ]
Post subject:  RE:Making an Etch-A-Sketch?

Is there a command to do that?

Author:  vdemons [ Sun May 15, 2011 3:26 pm ]
Post subject:  RE:Making an Etch-A-Sketch?

What about "View.Update" would it do?

Author:  Tony [ Sun May 15, 2011 3:30 pm ]
Post subject:  RE:Making an Etch-A-Sketch?

- array of variables
- whatdotcolor
- Pic.New
in no particular order, could all be useful for at least 3 different approaches

Author:  Raknarg [ Sun May 15, 2011 3:40 pm ]
Post subject:  RE:Making an Etch-A-Sketch?

If I were you, I'd use a flexible array to save the dots positions.

Author:  Tony [ Sun May 15, 2011 3:45 pm ]
Post subject:  RE:Making an Etch-A-Sketch?

@Raknarg -- if the screen size doesn't change, a static sized array offers a number of advantages.

Author:  Raknarg [ Sun May 15, 2011 3:48 pm ]
Post subject:  RE:Making an Etch-A-Sketch?

Are you sure? I thought he could use the flexible array so that it would only save the amount of positions needed... whats the advantage?

Author:  Tony [ Sun May 15, 2011 3:52 pm ]
Post subject:  RE:Making an Etch-A-Sketch?

Mostly in performance:

- no need to resize the array every time a new pixel is drawn
- constant time lookup for an arbitrary position

The downside is naturally the space requirement, but that is only the case if there are very few dots in a very large (mostly blank) screen.

Author:  Raknarg [ Sun May 15, 2011 3:56 pm ]
Post subject:  RE:Making an Etch-A-Sketch?

hmm... Well, the space requirement is the only real reason I would've suggested it, having a large for loop when you dont need it seems to slow down things a lot... or at least in bigger games.

Author:  vdemons [ Sun May 15, 2011 4:01 pm ]
Post subject:  RE:Making an Etch-A-Sketch?

Sorry I tried I just don`t know how to use them, I tried and failed! Sad

Author:  Raknarg [ Sun May 15, 2011 4:05 pm ]
Post subject:  RE:Making an Etch-A-Sketch?

Show what you have so far.

Author:  vdemons [ Sun May 15, 2011 4:12 pm ]
Post subject:  Re: Making an Etch-A-Sketch?

This is it, (I had more but I deleted it when it didn't work) btw, if you add cls after view.update it moves without lines, but I don't know how to merge the two since cls clears the entire screen!



View.Set ("graphics")
var x, y, radius : int
x := 200
y := 100
radius := 5



var chars : array char of boolean
View.Set ("offscreenonly")
loop
Input.KeyDown (chars)
if chars (KEY_UP_ARROW) then
y += 1
elsif chars (KEY_RIGHT_ARROW) then
x += 1
elsif chars (KEY_LEFT_ARROW) then
x -= 1
elsif chars (KEY_DOWN_ARROW) then
y -= 1
end if
if y + 12 >= maxy then
y -= 1
end if
if y - 12 <= 0 then
y += 1
end if
if x + 12 >= maxx then
x -= 1
end if
if x - 12 <= 0 then
x += 1
end if
Draw.FillOval (x, y, radius, radius, red)
Time.Delay (5)
View.Update
end loop

Author:  Raknarg [ Sun May 15, 2011 4:22 pm ]
Post subject:  RE:Making an Etch-A-Sketch?

If I were you, I would (as I said before) use an array to keep track of the dot positions. You don't have to use a flexible array in reality, but I think you should have at least an array.

Author:  Tony [ Sun May 15, 2011 4:22 pm ]
Post subject:  Re: Making an Etch-A-Sketch?

vdemons @ Sun May 15, 2011 4:12 pm wrote:
I had more but I deleted it when it didn't work

That was the interesting part. The rest is mostly what you've had before.

Author:  vdemons [ Sun May 15, 2011 4:52 pm ]
Post subject:  RE:Making an Etch-A-Sketch?

Well I'm trying my best.

Author:  vdemons [ Sun May 15, 2011 4:54 pm ]
Post subject:  RE:Making an Etch-A-Sketch?

I used the picID but that didn't work so I deleted it, but @Raknarg how would I use an array in this situation i haven't seen and array used like you described before.

Author:  Raknarg [ Sun May 15, 2011 5:05 pm ]
Post subject:  RE:Making an Etch-A-Sketch?

if (a key is pressed) then
move x or y
oldx (number) or oldy (number) = x or y
end if

for i : 1 .. upper (array)
Draw.FillOval (oldx (i), oldy (i), 5, red)
end for

It's a pretty bad representation but that's what I had in mind if you understand it.

Author:  klutzedufus [ Sun May 15, 2011 9:01 pm ]
Post subject:  RE:Making an Etch-A-Sketch?

I think that you should use multiple if statements instead of elsifs for the keys pressed down, so that you can move diagonally as well.

Author:  HRI [ Mon May 16, 2011 9:57 am ]
Post subject:  Re: Making an Etch-A-Sketch?

drawfillbox (x-radius,y-radius,x+radius,y+radius) will clear a rectangle the size of the circle from the screen.

Also, surprised no one mentioned this:
code:
[syntax="turing"]post code here[/syntax]


A bit of cleanup:
Turing:

View.Set ("graphics,offscreenonly") %setscreen ("graphics,offscreenonly")
var x, y : int
x := 200
y := 100

const RADIUS : int := 5 % constants should be uppercase, radius does not change



var chars : array char of boolean

loop
    Input.KeyDown (chars)
   
    if chars (KEY_UP_ARROW) then
        y := min (maxy - 12, y + 1) % easy way of making sure it doesn't go too far up
    end if % as said, allows you to move more than one direction at once

    if chars (KEY_RIGHT_ARROW) then
        x := min (maxx - 12, x + 1)
    end if

    if chars (KEY_LEFT_ARROW) then
        x := max (12, x - 1)
    end if

    if chars (KEY_DOWN_ARROW) then
        y := max (12, y - 1)
    end if

    % replaced check with walls above
 
    Draw.FillOval (x, y, RADIUS, RADIUS, red) % drawfilloval (x, y, RADIUS, RADIUS, red) %brightred could be nicer
    View.UpdateArea (0, 0, maxx, maxy) % faster than View.Update, updates from the lower left corner to upper right corner
    Time.Delay (5) %delay (5)
    drawfillbox (x - RADIUS, y - RADIUS, x + RADIUS, y + RADIUS, white) % clears circle
end loop


I'm not totally sure what exactly the goal is, but I hope this helps somewhat.


: