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

Username:   Password: 
 RegisterRegister   
 Line direction
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Flikerator




PostPosted: Fri Feb 18, 2005 7:23 pm   Post subject: Line direction

I want the line to try and go towards the mouse, like it is now. But only draw about 20 pixels (Give or take) towards it and then stop. Does anyone know how to do that?

code:
var mx,my,mz:int
var x,y:int:=200
var ch : array char of boolean
colourback(black)

loop
       Input.KeyDown (ch)
        if ch ('w') or ch ('8') then
            if y < maxy - 20 then
                y += 5
            end if
        elsif ch ('s') or ch ('2') then
            if y > 20 then
                y -= 5
            end if
        elsif ch ('d') or ch ('6') then
            if x < maxx - 20 then
                x += 5
            end if
        elsif ch ('a') or ch ('4') then
            if x > 20 then
                x -= 5
            end if
        elsif ch ('7') then
            if y < maxy - 20 and x > 20 then
                y += 2
                x -= 2
            end if
        elsif ch ('9') then
            if y < maxy - 20 and x < maxx - 20 then
                y += 2
                x += 2
            end if
        elsif ch ('3') then
            if y > 20 and x < maxx - 20 then
                y -= 2
                x += 2
            end if
        elsif ch ('1') then
            if y > 20 and x > 20 then
                x -= 2
                y -= 2
            end if
        end if
Mouse.Where (mx,my,mz)
Draw.Line (x,y,mx,my,yellow)
delay (25)
View.Update
cls
end loop
Sponsor
Sponsor
Sponsor
sponsor
Neo




PostPosted: Fri Feb 18, 2005 8:03 pm   Post subject: (No subject)

Do you want a really easy way out? Draw a black circle where the mouse pointer is after youve drawn the line. Laughing
Flikerator




PostPosted: Fri Feb 18, 2005 9:10 pm   Post subject: (No subject)

No because I will have many different backgrounds Razz
(Not just colours, like actually pictured backgrounds.
Flikerator




PostPosted: Sat Feb 19, 2005 9:17 am   Post subject: (No subject)

Can someone help me out here?
Martin




PostPosted: Sat Feb 19, 2005 10:02 am   Post subject: (No subject)

Sure. It's easy with a bit of trig.

So let's say that you want your line to be of length L and the start and end points are (x1, y1) and (x2, y2) respectively.

Now, you can calculate the length of line (x1, y1, x2, y2) with pythagarus easily enough. Let's call that M.

Now you can draw a line (x1, y1, x2, y2) easily, but you want a line that is a stretch of that line. That is, you want a line that is scaled by a factor of 'L/M'

So your line will be: (x1, y1, x1 + (x2 - x1) * (L/M), y1 + (y2 - y1) * (L/M))

Hope it helps.
Drakain Zeil




PostPosted: Sat Feb 19, 2005 10:02 am   Post subject: (No subject)

Math.Distance ?
Flikerator




PostPosted: Sat Feb 19, 2005 11:47 am   Post subject: (No subject)

code:

var x2, y2, mz : int
var x1, y1 : int := 200
var M, L : real := 5
var m, l : int := 5
var ch : array char of boolean
colourback (black)
View.Set ("offscreenonly")
loop
    Input.KeyDown (ch)
    if ch ('w') or ch ('8') then
        if y1 < maxy - 20 then
            y1 += 5
        end if
    elsif ch ('s') or ch ('2') then
        if y1 > 20 then
            y1 -= 5
        end if
    elsif ch ('d') or ch ('6') then
        if x1 < maxx - 20 then
            x1 += 5
        end if
    elsif ch ('a') or ch ('4') then
        if x1 > 20 then
            x1 -= 5
        end if
    elsif ch ('7') then
        if y1 < maxy - 20 and x1 > 20 then
            y1 += 2
            x1 -= 2
        end if
    elsif ch ('9') then
        if y1 < maxy - 20 and x1 < maxx - 20 then
            y1 += 2
            x1 += 2
        end if
    elsif ch ('3') then
        if y1 > 20 and x1 < maxx - 20 then
            y1 -= 2
            x1 += 2
        end if
    elsif ch ('1') then
        if y1 > 20 and x1 > 20 then
            x1 -= 2
            y1 -= 2
        end if
    end if
    Mouse.Where (x2, y2, mz)
    L := Math.Distance (x1, y1, x2, y2)
    M := Math.Distance (x1, y1, x2, y2)
    colour (white)
    locate (1, 1)
    put round (L)
    put round (M)
    Draw.Line (x1, y1, x1 + (x2 - x1) * (L div M), y1 + (y2 - y1) * (L div M), brightgreen)

    Draw.FillOval (x1, y1, 10, 10, yellow)
    delay (25)
    View.Update
    cls
end loop


Alright when I read that M/L thing this is what I came up with. It doesn't work correctly because both my "M" and "L" are the same. Which one do I change and how?
Martin




PostPosted: Sat Feb 19, 2005 1:42 pm   Post subject: (No subject)

Don't use div.

Divide, multiply and then round the result down.

20 * (2 div 4) = 0, but round (20 * (2/4)) = 10
Sponsor
Sponsor
Sponsor
sponsor
Flikerator




PostPosted: Sat Feb 19, 2005 2:32 pm   Post subject: (No subject)

What absolutely must I change. Because I changed the div to "/" and tried a phew other things but im still just stabbing in the dark at what to do. Thanks for all the help though Razz
Flikerator




PostPosted: Sat Feb 19, 2005 5:03 pm   Post subject: (No subject)

Cervantes helped me out. We gots it to work yay ^^ Looks awsome too Razz

I also added a sprint feature with energy : D So I can add that into my zombie game.
Flikerator




PostPosted: Fri Feb 25, 2005 6:58 pm   Post subject: (No subject)

Ive been working on this for half an hour or so. It is probably a really easy answer but I can't figure it out. I need it to face the opposite direction of the mouse but only go a certain length.

I already have it going towards it but I need it to go away. If you need me to post more of the code just say so.

code:

        Draw.Line (x1, y1, x1 + round ((x2 - x1) * (L / M)), y1 + round ((y2 - y1) * (L / M)), 22)
M := Math.Distance (x1,y1,x2,y2)
Cervantes




PostPosted: Fri Feb 25, 2005 7:19 pm   Post subject: (No subject)

30 minutes? That sucks. Confused
It's the same code, just subtract the round ((x2 - x1) stuff instead of add it. Same with y.
code:

    Draw.Line (x1, y1, x1 - round ((x2 - x1) * (L / M)), y1 - round ((y2 - y1) * (L / M)), 22)
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  [ 12 Posts ]
Jump to:   


Style:  
Search: