Posted: Fri Sep 05, 2014 10:58 am Post subject: More than just 1 appear, and not all dispear after maxy
What is it you are trying to achieve?
im trying to shoot bullets (more than 1 on screen), and i want each seperate bullet to disapear when they reach maxy
What is the problem you are having?
a) only one bullet will show up
b) if i do figure out how to add moar bullets, i expect they will all dispear when only 1 reaches maxy, any way to prevent?
Describe what you have tried to solve this problem
boolean and int values to see if anything would change
Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)
unfortunately, i cant attach files so here is my code...
Turing:
%sey.mo.raz << neccesary to open file DO NOT DELETE!
%ARRAYS, CONSTANTS, BOOLEAN AND INT VAR'S var b_KeyUp :boolean:=false var b_notMoving: boolean:=false
var i_bulletsOnScreen: int var i_spacebar: int var i_cd :int var i_cd1: int var x, y :int var x2, y2 :int
const c_NumberOfStars :=400 var starX :array1.. c_NumberOfStars ofint var starY :array1.. c_NumberOfStars ofint var chars :arraycharofboolean
%GIVING VALUES (I PUT IT HERE INSTEAD OF BESIDE THE INTS FOR CLARITY)
i_spacebar :=1
i_bulletsOnScreen :=0
i_cd :=6
i_cd1:=6
x :=maxx-850
y :=100
y2 := y
x2 := x
% P L A Y E R
% THIS IS OUTSIDE LOOP OTHERWISE STARS WILL NOT SLOW DOWN ON SCREEN for star :1.. c_NumberOfStars
starX (star):= Rand.Int (0, maxx)
starY (star):= Rand.Int (0, maxy) endfor
% E N T I T I E S % E N T I T I E S % E N T I T I E S
%entities cannot be made before cls otherways they will not appear
%BULLET CREATE if i_spacebar =1then Pic.Draw(bullet, x2+35, y2, picCopy)
i_bulletsOnScreen +=1
y2 +=10 if y2 > maxythen
i_spacebar :=2
y2 := y
x2 := x
endif endif
%SHIP CREATE/ SHIP TURN LEFT OR RIGHT CREATE ASWELL... if i_cd > 0and i_cd1 > 0then
b_notMoving :=true Pic.Draw(ShipC,x,y,picCopy)
elsif i_cd < 1then Pic.Draw(ShipR3, x, y, picCopy)
b_notMoving :=false
elsif i_cd < 5then Pic.Draw(ShipR2, x, y, picCopy)
b_notMoving :=false
elsif i_cd1 < 1then Pic.Draw(ShipL3, x, y, picCopy)
b_notMoving :=false
elsif i_cd1 < 5then Pic.Draw(ShipL2, x, y, picCopy)
b_notMoving :=false
endif
%RANDOMELY GENERATE STARS for star :1.. c_NumberOfStars
starY (star) -= Rand.Int (1, 5) if starY (star) < 0then
starY (star):=maxy
starY (star):= Rand.Int (100, maxx) endif Draw.FillOval(starX (star), starY (star) +starY (star),1, 1, white) endfor
Posted: Fri Sep 05, 2014 3:58 pm Post subject: RE:More than just 1 appear, and not all dispear after maxy
Quote:
const NoOfFlakes := 400
Ha. Someone found the compsci snowflake particle engine "contest" thread.
Since Turing has doesn't have any simple tools to add/remove items from an array, use a fixed sized array, along with another variable to represent the current index. The mod operator is used to wrap around to 0..
Eg:
Turing:
var bullets :array1.. 100of Bullet
var currentBulletIndex :=lower(bullets)% 1
initialize () loop % Input if onKeyPress (SHOOT_BULLET_KEY)then
currentBulletIndex :=(currentBulletIndex + 1) if currentBulletIndex > upper(bullets)then
currentBulletIndex :=lower(bullets) endif var bullet := bullets (currentBulletIndex)
initBullet (bullet)
bullet.pos.x :=234 endif
Also, records are awesome. They allow you to bundle variables together as a new data type, which allows you to create an array of "a thing with lots of variables" much easier.
Incomplete Example
Turing:
type Vector2D : record
x :real
y :real endrecord
type Bullet : record
pos : Vector2D % Position
vel : Vector2D % Velocity
visible :boolean endrecord
proc initBullet (var b : Bullet)
b.pos.x :=0 % ... end initBullet
proc updateBullet (var b : Bullet)
b.pos.x += b.vel.x
b.pos.y += b.vel.y
end updateBullet
proc drawBullet (b : Bullet) if b.visible then % ... endif end drawParticle
var bullets :array1.. 100of Bullet
var currentBulletIndex :=lower(bullets)% 1
initialize () loop % Input if onKeyPress (SHOOT_BULLET_KEY)then
currentBulletIndex :=(currentBulletIndex + 1) if currentBulletIndex > upper(bullets)then
currentBulletIndex :=lower(bullets) endif var bullet := bullets (currentBulletIndex)
initBullet (bullet)
bullet.pos.x :=234% Align bullet with the player's ship endif
% Update for i :lower(bullets).. upper(bullets)
updateBullet (bullets (i)) endfor
Posted: Fri Sep 05, 2014 4:57 pm Post subject: Re: More than just 1 appear, and not all dispear after maxy
Wow, that seems really cool only, i dont undetstand it xD ik wat an array is, but i dont understand any of the code u wrote xD. im still new to turing and i hope u could explain dis 2 me more thourouly
Zren
Posted: Fri Sep 05, 2014 5:18 pm Post subject: RE:More than just 1 appear, and not all dispear after maxy
Thought as much.
Here's a simplier example.
Press space to "Fire Bullets"
Turing:
var SHOOT_BULLET_KEY :=' '
var chars :arraycharofboolean var bulletY :array1.. 10ofint var bulletVisible :array1.. 10ofboolean var currentBulletIndex :=lower(bulletY)% lower() grabs the lowest index = 1
proc initialize () for i :lower(bulletY).. upper(bulletY)
bulletY (i):=0
bulletVisible (i):=false endfor end initialize
View.Set("offscreenonly")
initialize () loop % Input Input.KeyDown(chars) if chars (SHOOT_BULLET_KEY)then
currentBulletIndex :=(currentBulletIndex + 1) if currentBulletIndex > upper(bulletY)then
currentBulletIndex :=lower(bulletY)
% Reset to "ship" endif endif
% Update
% Draw Frame cls put"currentBulletIndex: ", currentBulletIndex
for i :lower(bulletY).. upper(bulletY) put"bullets(", i :3, "): y=", bulletY (i)," visible=", bulletVisible (i)," current=", i = currentBulletIndex
endfor View.Update()
The key is to make the array larger than the maximum number of bullets you'd ever need on the screen.
1. Make the bulletY(i) value increment every frame.
2. Make the bulletY(i) value increment every frame only if bulletVisible(i) is true.
3. Set bulletVisible(currentBulletIndex) to true when pressing the shoot key.
4. Set bulletVisible(i) to false when bulletY(i) is "off the screen".
amz_best
Posted: Fri Sep 05, 2014 6:09 pm Post subject: RE:More than just 1 appear, and not all dispear after maxy
wow that is amazing. i can see wat u did and it makes so much moar sense! i got how it works and i typed it out already! except now i dunno how to add the photos when you press space or how 2 make them go up in their sepetrately xD
Zren
Posted: Fri Sep 05, 2014 6:16 pm Post subject: RE:More than just 1 appear, and not all dispear after maxy
Quote:
them go up in their sepetrately
What's your code?
More steps:
5. Load Bullet Picture. Only use one picture for all bullets, as we can redraw it multiple times.
6. Add a bulletX array.
7. For each bullet the arrays, draw the bullet picture at ( bulletX(i), bulletY(i) ).
8. Only draw a bullet if bulletVisible(i) is true.
amz_best
Posted: Fri Sep 05, 2014 6:27 pm Post subject: Re: More than just 1 appear, and not all dispear after maxy
Everytime i run it, it says argument is wrong type, is it becuz im using real numbers?
Turing:
%sey.mo.raz << neccesary to open file DO NOT DELETE!
%ARRAYS, CONSTANTS, BOOLEAN AND INT VAR'S var b_KeyUp :boolean:=false var b_notMoving: boolean:=false
var i_cd :int var i_cd1: int
var x, y, y2 :int
var bulletY :array1.. 10ofint var bulletX :array1.. 10ofint var bulletVisible :array1.. 10ofboolean var currentBulletIndex :=lower(bulletY)
const c_NumberOfStars :=400 var starX :array1.. c_NumberOfStars ofint var starY :array1.. c_NumberOfStars ofint var chars :arraycharofboolean
%GIVING VALUES (I PUT IT HERE INSTEAD OF BESIDE THE INTS FOR CLARITY)
i_cd :=6
i_cd1:=6
x :=maxx-850
y :=100
y2 := y
type Vector2D : record
x :real
y :real endrecord
type Bullet : record
pos : Vector2D % Position
vel : Vector2D % Velocity
visible :boolean endrecord
proc drawBullet (b : Bullet) if b.visible then Pic.Draw(bullet,x,y,picCopy) endif end drawBullet
% P L A Y E R
% THIS IS OUTSIDE LOOP OTHERWISE STARS WILL NOT SLOW DOWN ON SCREEN for star :1.. c_NumberOfStars
starX (star):= Rand.Int (0, maxx)
starY (star):= Rand.Int (0, maxy) endfor
% THIS IS TO HAVE BULLETS APPEAR AND DISAPEAR SEPERATELY proc initialize () for i :lower(bulletY).. upper(bulletY)
bulletY (i):=0
bulletVisible (i):=false endfor end initialize
% E N T I T I E S % E N T I T I E S % E N T I T I E S
for i :lower(bulletY).. upper(bulletY)
drawBullet (bulletY(i), bulletX(i)) endfor
%RANDOMELY GENERATE STARS %entities cannot be made before cls otherways they will not appear for star :1.. c_NumberOfStars
starY (star) -= Rand.Int (1, 5) if starY (star) < 0then
starY (star):=maxy
starY (star):= Rand.Int (100, maxx) endif Draw.FillOval(starX (star), starY (star) +starY (star),1, 1, white) endfor
%SHIP CREATE/ SHIP TURN LEFT OR RIGHT CREATE ASWELL... if i_cd > 0and i_cd1 > 0then
b_notMoving :=true Pic.Draw(ShipC,x,y,picCopy)
elsif i_cd < 1then Pic.Draw(ShipR3, x, y, picCopy)
b_notMoving :=false
elsif i_cd < 5then Pic.Draw(ShipR2, x, y, picCopy)
b_notMoving :=false
elsif i_cd1 < 1then Pic.Draw(ShipL3, x, y, picCopy)
b_notMoving :=false
elsif i_cd1 < 5then Pic.Draw(ShipL2, x, y, picCopy)
b_notMoving :=false
Posted: Fri Sep 05, 2014 6:34 pm Post subject: RE:More than just 1 appear, and not all dispear after maxy
It's because of this code from my first example.
code:
type Vector2D :
record
x : real
y : real
end record
type Bullet :
record
pos : Vector2D % Position
vel : Vector2D % Velocity
visible : boolean
end record
proc drawBullet (b : Bullet)
if b.visible then
Pic.Draw (bullet,x,y,picCopy)
end if
end drawBullet
Because you're calling
code:
drawBullet (bulletY(i), bulletX(i) )
When the function is define as
code:
proc drawBullet (b : Bullet)
Don't use any of the code from my first example as it's incompatible with the code from the second one, as I removed a number of techniques to simplify it.
Since I don't think you understand procedures/functions very well, don't use them.
Instead of:
code:
% E N T I T I E S
% E N T I T I E S
% E N T I T I E S
for i : lower (bulletY) .. upper (bulletY)
drawBullet (bulletY(i), bulletX(i) )
end for
Just do
code:
% E N T I T I E S
% E N T I T I E S
% E N T I T I E S
for i : lower (bulletY) .. upper (bulletY)
if bulletVisible(i) then
% ...
end if
end for
Sponsor Sponsor
Zren
Posted: Fri Sep 05, 2014 6:37 pm Post subject: RE:More than just 1 appear, and not all dispear after maxy
By the way. Press the indent button (often) in Turings menu bar.
amz_best
Posted: Fri Sep 05, 2014 7:01 pm Post subject: RE:More than just 1 appear, and not all dispear after maxy
alrite, on the line u gave me,
"if bulletVisible(i) then"
it says variable has no value.
im sorry if u r fustrated in me...
Zren
Posted: Fri Sep 05, 2014 7:06 pm Post subject: RE:More than just 1 appear, and not all dispear after maxy
Yeeeeeep. You really don' understand functions/procedures. xD You'll probably learn about them in the next few weeks.
Basically the code wasn't being run.
code:
% THIS IS TO HAVE BULLETS APPEAR AND DISAPEAR SEPERATELY
proc initialize ()
for i : lower (bulletY) .. upper (bulletY)
bulletY (i) := 0
bulletVisible (i) := false
end for
end initialize
As you never called the procedure like so:
code:
initialize()
Just move the code out of the procedure. Eg:
code:
proc initialize ()
for i : lower (bulletY) .. upper (bulletY)
bulletY (i) := 0
bulletVisible (i) := false
end for
end initialize
View.Set ("offscreenonly")
initialize ()
loop
% Input
Input.KeyDown (chars)
if chars (SHOOT_BULLET_KEY) then
currentBulletIndex := (currentBulletIndex + 1)
if currentBulletIndex > upper (bulletY) then
currentBulletIndex := lower (bulletY)
to
code:
View.Set ("offscreenonly")
for i : lower (bulletY) .. upper (bulletY)
bulletY (i) := 0
bulletVisible (i) := false
end for
loop
% Input
Input.KeyDown (chars)
if chars (SHOOT_BULLET_KEY) then
currentBulletIndex := (currentBulletIndex + 1)
if currentBulletIndex > upper (bulletY) then
currentBulletIndex := lower (bulletY)
Sorry for teasing you with so many advanced concepts when your still on your first week.
amz_best
Posted: Fri Sep 05, 2014 7:12 pm Post subject: RE:More than just 1 appear, and not all dispear after maxy
As soon as i saw initialize(), i went back into my code and saw that its not initialized before the loop, so i simply added initialize() and now its working fine, but bullets dont appear when i press space.
Zren
Posted: Fri Sep 05, 2014 7:20 pm Post subject: RE:More than just 1 appear, and not all dispear after maxy
Where exactly are you drawing the bullets? I wrote
code:
% ...
as I assume you'd fill in the blanks.
Might as well post your code again, this time make sure to click this button first.
amz_best
Posted: Fri Sep 05, 2014 7:22 pm Post subject: Re: More than just 1 appear, and not all dispear after maxy
Turing:
%sey.mo.raz << neccesary to open file DO NOT DELETE!
type Bullet : record
pos : Vector2D % Position
vel : Vector2D % Velocity
visible :boolean endrecord
proc drawBullet (b : Bullet) if b.visible then Pic.Draw(bullet, x, y, picCopy) endif end drawBullet
% P L A Y E R
% THIS IS OUTSIDE LOOP OTHERWISE STARS WILL NOT SLOW DOWN ON SCREEN for star :1.. c_NumberOfStars
starX (star):= Rand.Int (0, maxx)
starY (star):= Rand.Int (0, maxy) endfor
% THIS IS TO HAVE BULLETS APPEAR AND DISAPEAR SEPERATELY proc initialize () for i :lower(bulletY).. upper(bulletY)
bulletY (i):=0
bulletVisible (i):=false endfor end initialize
initialize () loop Input.KeyDown(chars)
%MOVE SHIP UP if chars (KEY_UP_ARROW)then
y := y + 10 endif
%MOVE SHIP RIGHT if chars (KEY_RIGHT_ARROW)then
x := x + 20
b_KeyUp :=true
i_cd -=1 Pic.Draw(ShipR2, x, y, picCopy) endif
%MOVE SHIP LEFT if chars (KEY_LEFT_ARROW)then if i_cd =6then
x := x - 20
i_cd1 -=1
b_KeyUp :=true else
b_KeyUp :=false endif endif
%MOVE SHIP DOWN if chars (KEY_DOWN_ARROW)then
y := y - 10 endif
%SHOOT BULLET if chars (' ')then
currentBulletIndex :=(currentBulletIndex + 1) if currentBulletIndex > upper(bulletY)then
currentBulletIndex :=lower(bulletY) endif endif
%MAKE SURE SHIP DOESNT LEAVE THE SCREEN if x > maxx - 85then
x :=maxx - 85 endif if x < -10then
x := -10 endif
if y > maxy - 100then
y :=maxy - 100 endif if y < -10then
y := -10 endif
%MAKE SURE CHANGING PICTURE OF SHIP REMAINS SMOOTH AND NOT GLITCHED... if b_KeyUp =falsethen
i_cd :=6
i_cd1 :=6 endif
% E N T I T I E S % E N T I T I E S % E N T I T I E S
for i :lower(bulletY).. upper(bulletY) if bulletVisible (i)then Pic.Draw(bullet, x + 35, y2, picCopy) endif endfor
%RANDOMELY GENERATE STARS %entities cannot be made before cls otherways they will not appear for star :1.. c_NumberOfStars
starY (star) -= Rand.Int (1, 5) if starY (star) < 0then
starY (star):=maxy
starY (star):= Rand.Int (100, maxx) endif Draw.FillOval(starX (star), starY (star) + starY (star),1, 1, white) endfor
%SHIP CREATE/ SHIP TURN LEFT OR RIGHT CREATE ASWELL... if i_cd > 0and i_cd1 > 0then
b_notMoving :=true Pic.Draw(ShipC, x, y, picCopy)
elsif i_cd < 1then Pic.Draw(ShipR3, x, y, picCopy)
b_notMoving :=false
elsif i_cd < 5then Pic.Draw(ShipR2, x, y, picCopy)
b_notMoving :=false
elsif i_cd1 < 1then Pic.Draw(ShipL3, x, y, picCopy)
b_notMoving :=false
elsif i_cd1 < 5then Pic.Draw(ShipL2, x, y, picCopy)
b_notMoving :=false