What is it you are trying to achieve?
Hi, I am a first year programmer (ISC3U), so please bear with me. I've gone a little ahead and started playing with graphics in Turing. What I have done so far is created a program which (based on the Turing walk-through on this site, Thanks! =) ) creates two circles. One circle (the red one), you control with the D-pad on your keyboard. The other ball (the Blue one), moves in the opposite direction you are in, if you are also within an X distance from it. This will eventually be part of my final project (A Zelda RPG...FTW!)..as some sort of enemy maneuvering.
What is the problem you are having?
I would like to know how I can make the blue ball travel linearly in the opposite direction of the red ball based upon the slope between the red and blue ball. Essentially, if I am traveling diagonally right and up at the blue ball, I want the blue ball to travel right and up also, away from the ball I'm controlling...or have the ball move away at the same angle in which I am approaching it
I am also having problems with flickering, I cannot figure out how to get it to work...View.Update and "offscreenonly" do not work for me...
[EDIT] : i have solved the flickering problem. apparently, "View.Update" needs to be pasted AFTER any visual changes are made...which makes so much sense...
Describe what you have tried to solve this problem
<I have tried what I described in the first paragraph. I tried plugging the blue balls x and y coordinates into y = mx + b, by incrementing the x-value of the blue ball by <any value> and then the y value into the linear equation. However, this wont work because no decimal places are allowed with these values...
I have also tried to stop flickering, but it did not work =(
Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)
<Here's my code...updated>
Turing: |
View.Set ("graphics:1000;600,position:center;center,offscreenonly")
var x, y, x2, y2 : int % X and Y are the coordinates of the FIRST Ball (Red) , and X2 and Y2 are the coordinates of the SECOND BALL (Blue)
var y22 : real
var slope, distance_between_centres : real
x := 100
y := 100 %starting points for the two balls
x2 := maxx div 2
y2 := maxy div 2
const slope1 : real := 1. 732 %the slope in quadrant one and three, where angle Theta is 60 degrees to the horizontal
const slope2 : real := 0. 57735 %"" Theta = 30 degrees
const slope3 : real := - 0. 57735 %quadrants 2 and 4...the slope becomes negative
const slope4 : real := - 1. 732 %same thing..negative slope
const r : int := 4 %changes the radius of the balls...yea, yea, go ahead laugh...
const movedist : int := 150 %determines how close you need to get to the blue ball in order to to move it
const speed1 : int := 8 %the speed of your ball
const speed2 : int := 3 %the speed of ball 2 in horizontal or vertical - this vsalue determines how "fast" the blue ball runs away from the red ball
const speed3 : int := 2
%speed of the blue ball (ball2) in diagonals - as does this value...i only wish it could be a decimal value...until then, the diagonal speed is a slight bit faster than the vertical/horizontal value
var chars : array char of boolean
%var line1x : real := (movedist * (cos (30))):0:2 %what i Would* put in place of 130 below on the "drawline"...but the value cannot be a decimal..nor can i seem to round it... :0:2...doesnt seem to work for me
%var liney1 : real := (movedist * (sin (30)))
procedure blueball % to keep the two balls' program apart...easier to read
drawfilloval (x2, y2, r, r, blue) % draws the blue ball
drawoval (x2, y2, movedist, movedist, black)
% These values were derrived from finding the x and y values of a triangle whos hypotenuse was the "movedist" value..at the time these values were written the "movedist" value was 160
drawline (x2 - 130, y2 - 75, x2 + 130, y2 + 75, black) %draws the "section" lines, which provides a visual aid in determining the direction the blue ball will travel
drawline (x2 - 130, y2 + 75, x2 + 130, y2 - 75, black)
drawline (x2 + 75, y2 + 130, x2 - 75, y2 - 130, black)
drawline (x2 + 75, y2 - 130, x2 - 75, y2 + 130, black)
if (x2 - x ) = 0 then % Eliminated the possibility that x and x2 are the same number so that we dont accidentally DIVIDE BY ZERO!...which would crash the program of course...
x := x2 + 1
end if
slope := ((y2 - y ) / (x2 - x )) % finds the slope between the two balls
distance_between_centres := (sqrt ((x2 - x ) ** 2 + (y2 - y ) ** 2)) %finds the distance between the first(red) and second(blue) circles
put "Slope: ", slope : 9, " x: ", x : 3, " y: ", y : 3, " x2: ", x2 : 3, " y2: ", y2 : 3 %outputs the slope, x and y coordinates for the balls
% Diagonal movements
if slope < slope1 and slope > slope2 and distance_between_centres <= movedist and x > x2 and y > y2 then
x2 - = speed3
y2 - = speed3
end if
if slope < slope3 and slope > slope4 and distance_between_centres <= movedist and x > x2 and y < y2 then
x2 - = speed3
y2 + = speed3
end if
if slope < slope1 and slope > slope2 and distance_between_centres <= movedist and x < x2 and y < y2 then
x2 + = speed3
y2 + = speed3
end if
if slope < slope3 and slope > slope4 and distance_between_centres <= movedist and x < x2 and y > y2 then
x2 + = speed3
y2 - = speed3
end if
% X and Y movements
if slope < slope2 and slope > slope3 and distance_between_centres <= movedist and x > x2 then
x2 - = speed2
elsif slope < slope2 and slope > slope3 and distance_between_centres <= movedist and x < x2 then
x2 + = speed2
elsif /*slope > -340 and slope < 200 and slope not= slope1 and slope not= slope2 and slope not= slope3 and slope not= slope4 and*/ y < y2 and distance_between_centres <= movedist then
y2 + = speed2
elsif /*slope > -340 and slope < 200 and slope not= slope1 and slope not= slope2 and slope not= slope3 and slope not= slope4 and*/ y > y2 and distance_between_centres <= movedist then
y2 - = speed2
end if
/*
if slope > -340 and slope < 200 and slope not= slope1 and slope not= slope2 and slope not= slope3 and slope not= slope4 and y > y2 and distance_between_centres <= movedist then
y2 -= speed2
end if
*/
/*if distance_between_centres <= movedist and y < y2 then %THIS WAS ROUGH WORK...didn't work
y2 += speed2
end if
if distance_between_centres <= movedist and y > y2 then
y2 -= speed2
end if*/
%restricts blue ball from exiting boundaries, and allows red ball to push it around more
drawbox (40, 40, maxx - 40, maxy - 40, red) % comment this line if you DO NOT want to see the "restictions" box for the Blue Ball
if y2 < 40 then
y2 := y2 + speed2 % now, if the ball is moving diagonal, it can leave the screen...but that because i CANNOT let the speed be a decimal value...which would otherwise be: sqrt(x2**2 + y2**2)
end if
if y2 > maxy - 40 then
y2 := y2 - speed2
end if
if x2 < 40 then
x2 := x2 + speed2
end if
if x2 > maxx - 40 then
x2 := x2 - speed2
end if
% for some reason, the blue ball likes to slowly creep past the maximum "y" values it was permitted...so i fixed that with this..or this with that? lol
if y2 > maxy - 40 then
y2 := maxy - 40
elsif y2 < 40 then
y2 := 40
end if
end blueball
loop
cls
Draw.Box (20, 20, maxx - 20, maxy - 20, blue)
drawfillbox (21, 21, maxx - 21, maxy - 21, green)
Input.KeyDown (chars )
% moved the red ball according to input
if chars (KEY_UP_ARROW) then
y := y + speed1
end if
if chars (KEY_RIGHT_ARROW) then
x := x + speed1
end if
if chars (KEY_LEFT_ARROW) then
x := x - speed1
end if
if chars (KEY_DOWN_ARROW) then
y := y - speed1
end if
% restricts your ball from travelling off the screen
if y < 20 then
y := y + speed1
end if
if y > maxy - 20 then
y := y - speed1
end if
if x < 20 then
x := x + speed1
end if
if x > maxx - 20 then
x := x - speed1
end if
drawfilloval (x, y, r, r, red) %draws the red ball
blueball % runs the blueball code
drawline (x, y, x2, y2, black) % draws a line between the two balls, so you know which section your ball will fall into
View.Update
delay (20) % wait a bit, so the program does not to superspeed
end loop
|
Please specify what version of Turing you are using
<v4.1.1> |