Aiming on Tanks Game
Author |
Message |
theschaef
|
Posted: Sat Apr 22, 2006 1:50 pm Post subject: Aiming on Tanks Game |
|
|
Hey, I'm trying to make a "tanks" game and cannot get the aiming to work properly. Take a look at the code and see what you think i can do.. it doesnt shoot correctly forward and doesnt shoot at all when i make it go backwards.
code: |
View.Set("graphics:max;max, offscreenonly")
var x,y,xl1,yl1, xl2, yl2:int
var projx, projy, shoot:int
var angle:real
var vel, velx, vely:real
var chars:array char of boolean
angle:=0.0
x:= 210
y:=200
xl1:=210
yl1:=110
xl2:=210
yl2:=135
vel:=3
velx:=0
vely:=3
projx:=210
projy:=110
shoot:=0
loop
Draw.FillOval(210,100,18,10,1)
Draw.FillBox(0,0,maxx,100,2)
Draw.FillOval(x,y,5,5,4)
Input.KeyDown(chars)
if chars(KEY_RIGHT_ARROW)and y>100 or chars(KEY_RIGHT_ARROW) and x<200 and y=100 then
x+=1
xl2+=1
elsif chars(KEY_LEFT_ARROW) and y>100 or chars(KEY_LEFT_ARROW) and x>200 and y=100 then
x-=1
xl2-=1
elsif chars(KEY_ENTER)then
shoot:=1
end if
if shoot=1 then
Draw.FillOval(projx, projy, 5, 5 ,2)
projx+=round(velx)
projy+=round(vely)
end if
y:= round(sqrt(-(x-195)**2+14400)) + 100
if xl2-xl1 not = 0 then
angle:=arctand((yl2-yl1) / (xl2-xl1))
else
angle:=90.0
end if
velx:=vel * cosd(angle)
vely:=vel * sind(angle)
put round(velx)
put round(vely)
View.Update
cls
end loop
|
|
|
|
|
|
|
Sponsor Sponsor
|
|
|
Cervantes
|
Posted: Sat Apr 22, 2006 2:38 pm Post subject: (No subject) |
|
|
Welcome to CompSci.ca.
Format your code properly! It makes helping you a lot easier. It also makes it easier for you, the programmer. Commenting is good too. |
|
|
|
|
|
theschaef
|
Posted: Sat Apr 22, 2006 3:44 pm Post subject: (No subject) |
|
|
The main problem comes in when i have to use "round" for the x and y velocities of the projectile (ie. "projy += round (vely)"). I know that there is a way to do this because ive seen programs where it works, i just dont know how!
code: |
View.Set ("graphics:max;max, offscreenonly")
var x, y, xl1, yl1, xl2, yl2 : int
var projx, projy, shoot : int
var angle : real
var vel, velx, vely : real
var chars : array char of boolean
angle := 0.0
x := 210
y := 200
xl1 := 210
yl1 := 110
xl2 := 210
yl2 := 135
vel := 3
velx := 0
vely := 3
projx := 210
projy := 110
shoot := 0
loop
%draws the tank, ground and aiming dot
Draw.FillOval (210, 100, 18, 10, 1)
Draw.FillBox (0, 0, maxx, 100, 2)
Draw.FillOval (x, y, 5, 5, 4)
% moves the aiming dot left or right
Input.KeyDown (chars)
if chars (KEY_RIGHT_ARROW) and y > 100 or chars (KEY_RIGHT_ARROW) and x < 200 and y = 100 then
x += 1
xl2 += 1
elsif chars (KEY_LEFT_ARROW) and y > 100 or chars (KEY_LEFT_ARROW) and x > 200 and y = 100 then
x -= 1
xl2 -= 1
elsif chars (KEY_ENTER) then
shoot := 1 % starts the projectile moving
end if
if shoot = 1 then
Draw.FillOval (projx, projy, 5, 5, 2)
projx += round (velx)
projy += round (vely)
end if
y := round (sqrt (- (x - 195) ** 2 + 14400)) + 100 %makes the aimer move in a circular motion
%finds the angle that the aimer is at relative to the ground
if xl2 - xl1 not= 0 then
angle := arctand ((yl2 - yl1) / (xl2 - xl1))
else
angle := 90.0
end if
%sets the x and y components of the velocity
velx := vel * cosd (angle)
vely := vel * sind (angle)
View.Update
cls
end loop
|
|
|
|
|
|
|
HellblazerX
|
Posted: Sat Apr 22, 2006 8:18 pm Post subject: (No subject) |
|
|
First of all, when the user presses the arrow keys, why have the x values changed when you can just change the angle?
So, to draw your aimer with the angle:
code: | Input.KeyDown (chars)
if chars (KEY_LEFT_ARROW) and angle < 180 then
angle += 1
elsif chars (KEY_RIGHT_ARROW) and angle > 0 then
angle -= 1
elsif chars (KEY_ENTER) then
shoot := 1 % starts the projectile moving
end if
x := round (50 * cosd (angle)) + 210
y := round (50 * sind (angle)) + 100
Draw.FillOval (210, 100, 18, 10, 1)
Draw.FillBox (0, 0, maxx, 100, 2)
Draw.FillOval (x, y, 5, 5, 4)
|
secondly, create a second "angle" variable, this time for the projectile, because after shooting, you can still control the projectile. |
|
|
|
|
|
|
|