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

Username:   Password: 
 RegisterRegister   
 Drawing an oval at a permanent position
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
ntba1




PostPosted: Sun May 21, 2006 11:05 pm   Post subject: Drawing an oval at a permanent position

First sorry for the confusing title I didn't know what to name this thread Very Happy . Anywho I am in need of some assistence from you fine folks here at compsci. For a program I'm making I need to move an oval on the screen using arrow keys and that I have done that no problem but what I can't figure out how to do is make it so that when I press a button, let's say the space bar well then I need it to draw a same sized oval at that location permanently. The way I moved my oval is during each key press it draws a red circle moves 20 pixels and then draws a black one where the first one was IE:

code:

if In2 = chr(200) and y < maxy - 100 then
            drawfilloval (x, y, 10, 10, 0)
            y := y + 20
            drawfilloval (x, y, 10, 10, 4)
            locate (1, 10)
            put "X: ", x, " Y: ", y
        end if

        if In2 = chr(203) and x > 21 then
            drawfilloval (x, y, 10, 10, 0)
            x := x - 20
            drawfilloval (x, y, 10, 10, 4)
            locate (1, 10)
            put "X: ", x, " Y: ", y
        end if

        if In2 = chr(208) and y > 21 then
            drawfilloval (x, y, 10, 10, 0)
            y := y - 20
            drawfilloval (x, y, 10, 10, 4)
            locate (1, 10)
            put "X: ", x, " Y: ", y
        end if

        if In2 = chr(205) and x < maxx - 20 then
            drawfilloval (x, y, 10, 10, 0)
            x := x + 20
            drawfilloval (x, y, 10, 10, 4)
            locate (1, 10)
            put "X: ", x, " Y: ", y
        end if


That's just a part of the bigger program but I put that part on to show that I actully coded something instead of just asking for someone to write it for me. Back to the problem at hand, as you can see I can't really draw that oval I wanted casue as soon as I move the oval the new one I want to draw permanently at that position will be drawn over black. Any ideas?
Sponsor
Sponsor
Sponsor
sponsor
NikG




PostPosted: Sun May 21, 2006 11:27 pm   Post subject: (No subject)

Hmm.... how about a boolean variable that is set to true when you press space. Then all you need is a check in each of those input checks to see if the boolean is true.i.e.
code:
   if In2 = chr(203) and x > 21 then
       if SpacePressed = false then %or ~SpacePressed
           drawfilloval (x, y, 10, 10, 0)
       else
           SpacePressed := false
       end if
       x := x - 20
       drawfilloval (x, y, 10, 10, 4)
       locate (1, 10)
       put "X: ", x, " Y: ", y
   end if

There are better solutions, but you'd probably have to change everything around then...
ntba1




PostPosted: Mon May 22, 2006 9:06 am   Post subject: (No subject)

Thanks for the reply, but I have already tried something along those lines and that doesn't work because the code I posted above is for the capture of keys(ie. If I press up arrow oval moves up) therfore I cannot put another key capture inside that one on account of not being able to press two buttons at the same time(ie. I cannot press the space bar at the same time as pushing the up arrow).
MysticVegeta




PostPosted: Mon May 22, 2006 10:26 am   Post subject: (No subject)

ok, well first look for Input.KeyDown, use that instead of getch (char).
second, you see how you draw ovals in each of the if structures, tahts completely useless because what you could do is
Pseudo:
if (upKey) then
    y += c
elsif (downKey) then
    y -= c
elsif (rightKey) then
    x += c
elsif (leftKey) then
    x -= c
end if
cls
drawfilloval (x, y, j, k, color)


j, k, and c are some constants.
Isnt that more simplified? Notice also how that transparent (0 colored) circle is being replaced with cls, to stop it to flicker there is always View.Update.

Third: After getting it to work with Input.KeyDown, post your "entire" code here. By "entire" I mean enough to make the program compile, i.e. vars are declared etc.
ntba1




PostPosted: Mon May 22, 2006 10:50 am   Post subject: (No subject)

Ok Dokie I've got some explaining to do. Firstly I probably should mention everytime I post something I'm using classic Turing 6.5 and there's no way around this as out teacher insists on this version and although we have Turing 4.0.4c installed he will not let us use it so.. Input,View.Update and all those other goodies will NOT work. Second the code you posted will not work on account of other things on the screen that I can't redraw so I can't cls the problem I need solved requires me to keep the drawn ovals permanently on the screen and this is probably my fault becuase I did not tell you guys what I'm making and did not post full code. This is our final Turing assignment and we are supposed to make a game, for inspiration I went on here and found someone made a "Warmer/Colder" game and I really liked the idea so I used his idea for my own version of this game(Take it as a compliment not as me stealing) Laughing Here is the code:

code:

setscreen ("graphics:v16")
setscreen ("noecho")
setscreen ("nocursor")
randomize
var rX, rY, x, y, C, SizeX, SizeY : int
var In1, In2 : string (1)
var xD, yD, Distance, Distance1 : real
var guess : int := 30
var status:string:= "Normal"
x := 320
y := 240

randint (rX, 1, 31)
randint (rY, 1, 19)
rY := rY * 20
rX := rX * 20

%Intro

locate (1, 27)
put "Welcome to BombSquad"
locate (29, 20)
put "Press any key to continue..."
locate (3, 5)

put "Objective: "

color (2)
locate (4, 5)
put "You are part of an elite bomb squad team who works for the LAPD in the"
locate (5, 4)
put "year 2029. Your objective is to find the hidden bomb using new heat"
locate (6, 4)
put "sensing technology provided for you. Good Luck!"
color (15)

color (7)
locate (8, 5)
put "Instructions: "
color (5)
locate (9, 5)
put "Using the arrow keys, move your character across the screen in search"
locate (10, 4)
put "of the bomb. Press the space bar to activate the heat sensor. You have"
locate (11, 4)
put "30 chances before the bombs internal sensor picks up your heat"
locate (12, 4)
put "signature and detonates. Press q to quit, again good luck"
color (7)
locate (15, 5)
put "Heat Sensor Display (HSD): "
locate (17, 16)
put "Cold - Very Very Far"
locate (19, 16)
put "Chilly - Very Far"
locate (21, 16)
put "Fair -  Far"
locate (17, 42)
put "Warm - Close"
locate (19, 42)
put "Hot - Very Close"
locate (21, 42)
put "Normal - No Activity"



drawfilloval (100, 220, 10, 10, 1)
drawfilloval (100, 190, 10, 10, 11)
drawfilloval (100, 160, 10, 10, 14)
drawfilloval (300, 220, 10, 10, 12)
drawfilloval (300, 190, 10, 10, 4)
drawfilloval (300, 160, 10, 10, 15)




color (15)



getch (In1)
cls




drawbox (0, 0, maxx, 400, 15)
%----drawfilloval (rX, rY, 10, 10, 5)
drawfilloval (x, y, 10, 10, 15)

locate (1, 2)
put "Location X: ", x, " Y: ", y
locate (1, 27)
put "Guesses Left: ", guess
locate (1,45)
put "Status: ",status
%-----put rX, " ", rY
loop
    getch (In2)

    Distance := 0
    xD := rX - x
    yD := rY - y
    Distance := (xD * xD + yD * yD)
    Distance1 := round (sqrt (Distance))
    locate (1, 2)
    put "Location X: ", x, " Y: ", y
    locate (1, 27)
    put "Guesses Left: ", guess
    locate(1,45)
    put "Status: ",status
    % ----- ,"D: ",Distance1

    if x = rX and y = rY and In2 = chr (32) then
        put "Good!"
        drawfillstar (rX - 20, rY - 20, rX + 20, rY + 20, 14)
        play ("ABC")
        exit

    elsif guess = 0 then
        put "Boom!"
        exit


    else


        if In2 = chr (200) and y < maxy - 100 then
            drawfilloval (x, y, 10, 10, 0)
            y := y + 20
            drawfilloval (x, y, 10, 10, 15)
        end if

        if In2 = chr (203) and x > 21 then
            drawfilloval (x, y, 10, 10, 0)
            x := x - 20
            drawfilloval (x, y, 10, 10, 15)
        end if

        if In2 = chr (208) and y > 21 then
            drawfilloval (x, y, 10, 10, 0)
            y := y - 20
            drawfilloval (x, y, 10, 10, 15)
        end if

        if In2 = chr (205) and x < maxx - 20 then
            drawfilloval (x, y, 10, 10, 0)
            x := x + 20
            drawfilloval (x, y, 10, 10, 15)
        end if

        if In2 = "q" then
            exit
        end if

        if In2 = chr (32) and Distance1 <= 100 then
            drawfilloval (x, y, 20, 20, 4)
            guess := guess - 1
            status := "Very Close"
        elsif In2 = chr (32) and Distance1 >= 100 and Distance1 <= 200 then
            drawfilloval (x, y, 20, 20, 12)
            guess := guess - 1
            status := "Close"
        elsif In2 = chr (32) and Distance1 >= 200 and Distance1 <= 300 then
            drawfilloval (x, y, 20, 20, 14)
            guess := guess - 1
            status := "Far"
        elsif In2 = chr (32) and Distance1 >= 300 and Distance1 <= 400 then
            drawfilloval (x, y, 20, 20, 11)
            guess := guess - 1
            status := "Very Far"
        elsif In2 = chr (32) and Distance1 >= 400 then
            drawfilloval (x, y, 20, 20, 1)
            guess := guess - 1
            status := "Very Very Far"
        end if

    end if

end loop
locate (2, 1)



As you can see it leaves weird "C" shaped cirles that I would like to be the same size as the main circle. This might need some work to run in Turing 4, sorry for that. I hope someone can help Cool
HellblazerX




PostPosted: Mon May 22, 2006 8:16 pm   Post subject: (No subject)

ntba1 wrote:
classic Turing 6.5
Shocked

Ok, I get what you're asking. In that case, you'll need an array (or a set of arrays, for I doubt they had records back then) to keep track of those "permanent" circles of yours. Now, I don't think they had flexible arrays back then, so you'll have to make two sets of arrays (for the x and y values of those circles) with a set limit of elements. Since you're limited to a 32 X 16 grid, you could make two sets of arrays each with 512 elements and every time the user presses the space bar, you record the current x and y values into an element of the array, and during every loop, you could use a for loop to draw all the circles. Ex:

code:
var circlesX, circlesY : array 1 .. 512 of int
var count : int := 1
%the rest of your code
if In2 = chr (32) then
    circlesX (count) := x
    circlesY (count) := y
    count += 1
end if
for i : 1 .. count
    drawfilloval (circlesX (count), circlesY (count), red)
end for


Now, to prevent the user from drawing mulitple circles in one spot, you could use a for loop to check through the array to see if such a spot is occupied, or use 2D arrays and check with that. Also, you'll need another array to keep track of the color.
ntba1




PostPosted: Mon May 22, 2006 8:27 pm   Post subject: (No subject)

Hey that's a new one, looks like a tough one and just for the sake of it yes they did have flexible arrays and records. It says so in this book my teacher let me borrow becasue I'm pretty much the only one in my class who wants to learn this. BTW the book was published in 1989. I'll go ahead and give this a try. And one more thing the grid isn't 32x16 it's something like 600x390 (I can't remember now) but what I did was I took let's say 600 and divided that by 20 and I put the randint function to get a number between 1 and 30 that way whatever that number is I multiplied it by 20 that way I didn't get a number larger than 600 but at the same time kept the random points even (IE. 105,120,130) that way when I'm moving my "character" by 20 when the random numbers are 121,107 which would never be on top of each other. Who said math was a bad thing lol. There's also a distance between two points formula in there somewhere.
ntba1




PostPosted: Mon May 22, 2006 8:31 pm   Post subject: (No subject)

Also forgot to mention that the circles aren't of any importance except to remind the player of where they have been. So the circles just have to be there they don't have to have any specific co-ordinates so the array thing might just be taking it a little to far for something not too important. No offense I really apprecite the help though.
Sponsor
Sponsor
Sponsor
sponsor
ntba1




PostPosted: Mon May 22, 2006 9:15 pm   Post subject: (No subject)

Helblazer I retract my previous statement, I tried this and got soooo close to having my solution. BUT I get an error at the part where I draw the oval from the array co-ords saying the variable has no value. Crying or Very sad Although I did manage to get it to work partly when: I set the first Array position with 320,240, pressed space and moved up one and BAM the circle stayed at that position but of course I tried it again it still drew it at that positon cause I was telling it to but I was SOO close. Here is the updated code:
code:

setscreen ("graphics:v16")
setscreen ("noecho")
setscreen ("nocursor")
randomize
var rX, rY, x, y, C, SizeX, SizeY : int
var In1, In2 : string (1)
var xD, yD, Distance, Distance1 : real
var guess : int := 30
var status : string := "Normal"
var CirclesX, CirclesY : array 1 .. 31 of int
var count:int := 1


x := 320
y := 240

randint (rX, 1, 31)
randint (rY, 1, 19)
rY := rY * 20
rX := rX * 20

%Intro

locate (1, 27)
put "Welcome to BombSquad"
locate (29, 20)
put "Press any key to continue..."
locate (3, 5)

put "Objective: "

color (2)
locate (4, 5)
put "You are part of an elite bomb squad team who works for the LAPD in the"
locate (5, 4)
put "year 2029. Your objective is to find the hidden bomb using new heat"
locate (6, 4)
put "sensing technology provided for you. Good Luck!"
color (15)

color (7)
locate (8, 5)
put "Instructions: "
color (5)
locate (9, 5)
put "Using the arrow keys, move your character across the screen in search"
locate (10, 4)
put "of the bomb. Press the space bar to activate the heat sensor. You have"
locate (11, 4)
put "30 chances before the bombs internal sensor picks up your heat"
locate (12, 4)
put "signature and detonates. Press q to quit, again good luck!"
color (7)
locate (15, 5)
put "Heat Sensor Display (HSD): "
locate (17, 16)
put "Cold - Very Very Far"
locate (19, 16)
put "Chilly - Very Far"
locate (21, 16)
put "Fair -  Far"
locate (17, 42)
put "Warm - Close"
locate (19, 42)
put "Hot - Very Close"
locate (21, 42)
put "Normal - No Activity"



drawfilloval (100, 220, 10, 10, 1)
drawfilloval (100, 190, 10, 10, 11)
drawfilloval (100, 160, 10, 10, 14)
drawfilloval (300, 220, 10, 10, 12)
drawfilloval (300, 190, 10, 10, 4)
drawfilloval (300, 160, 10, 10, 15)




color (15)



getch (In1)
cls




drawbox (0, 0, maxx, 400, 15)
%----drawfilloval (rX, rY, 10, 10, 5)
drawfilloval (x, y, 10, 10, 15)

locate (1, 2)
put "Location X: ", x, " Y: ", y
locate (1, 27)
put "Guesses Left: ", guess
locate (1, 45)
put "Status: ", status
%-----put rX, " ", rY
loop
    getch (In2)

    Distance := 0
    xD := rX - x
    yD := rY - y
    Distance := (xD * xD + yD * yD)
    Distance1 := round (sqrt (Distance))
    locate (1, 2)
    put "Location X: ", x, " Y: ", y
    locate (1, 27)
    put "Guesses Left: ", guess
    locate (1, 45)
    put "Status: ", status
    % ----- ,"D: ",Distance1

    if x = rX and y = rY and In2 = chr (32) then
        locate (2, 23)
        put "Good Job!, Press R to replay or Enter to exit."
        drawfillstar (rX - 20, rY - 20, rX + 20, rY + 20, 14)
        play ("ABC")
        exit

    elsif guess = 0 then
        var sX : int := 300
        var sY : int := 375
        var sX1 : int := 340
        var sY1 : int := 375
        loop
            drawfillstar (sX, sY, sX1, sY1, 14)
            for i : 1 .. 2000
            end for
            sX := sX - 5
            sY := sY - 5
            sX1 := sX1 + 5
            sY1 := sY1 + 5
            drawfillstar (sX, sY, sX1, sY1, 14)

            drawfillstar (sX, sY, sX1, sY1, 4)
            for i : 1 .. 1000
            end for
            sX := sX - 2
            sY := sY - 2
            sX1 := sX1 + 2
            sY1 := sY1 + 2
            drawfillstar (sX, sY, sX1, sY1, 4)

            exit when sX < - 300
        end loop
        cls
        put "You Died, press R to play again and Enter to quit."

        exit
    else


        if In2 = chr (200) and y < maxy - 100 then
            drawfilloval (x, y, 10, 10, 0)
            y := y + 20
            drawfilloval (x, y, 10, 10, 15)
        end if

        if In2 = chr (203) and x > 21 then
            drawfilloval (x, y, 10, 10, 0)
            x := x - 20
            drawfilloval (x, y, 10, 10, 15)
        end if

        if In2 = chr (208) and y > 21 then
            drawfilloval (x, y, 10, 10, 0)
            y := y - 20
            drawfilloval (x, y, 10, 10, 15)
        end if

        if In2 = chr (205) and x < maxx - 20 then
            drawfilloval (x, y, 10, 10, 0)
            x := x + 20
            drawfilloval (x, y, 10, 10, 15)
        end if

        if In2 = "q" then
            exit
        end if

        if In2 = chr (32) then
        CirclesX(count) := x
        CirclesY(count) := y
        count := count + 1
        for j:1..count
        drawfilloval(CirclesX(count), CirclesY(count),10,10,14)
        end for
end if
end if
end loop
locate (2, 1)
color (0)
NikG




PostPosted: Mon May 22, 2006 11:43 pm   Post subject: (No subject)

code:
   for j:1..count
       drawfilloval(CirclesX(count), CirclesY(count),10,10,14)
   end for

2 things to change in that code... change the count in for j:1..count to count -1 and change the count in the drawfilloval to j
HellblazerX




PostPosted: Tue May 23, 2006 10:14 am   Post subject: (No subject)

Also, that part of code needs to be outside the if structure, because those circles should be drawn no matter what.
code:
    if count > 0 then
        for j : 1 .. count - 1
            drawfilloval (CirclesX (j), CirclesY (j), 10, 10, 14)
        end for
    end if

Also, the code for drawing the circle controlled by the user needs to be outside the if structures as well, because you only draw the circle if you press something.
Clayton




PostPosted: Tue May 23, 2006 4:39 pm   Post subject: (No subject)

ok, you can do this using flexible arrays (6.5 omg....), whenever the user presses the space bar, just save the coordinates to the max element of your flexible array, set a new upper limit to the array, then move, if you need help using flexible arrays check the Turing Walkthrough for the tut on it Very Happy
ntba1




PostPosted: Thu May 25, 2006 8:19 pm   Post subject: (No subject)

Thank you all I got it to work. Quite a good game if you ask me. Works without a hitch. That was the last hurdle I had. Wink
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  [ 13 Posts ]
Jump to:   


Style:  
Search: