Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Professional Discussion / Checking if any variable in an array equals a value

Author
Message
Thyde
14
Years of Service
User Offline
Joined: 25th Sep 2011
Location: Bristol, United Kingdom
Posted: 25th Sep 2011 23:51
Hey guys, I've come back to Dark Basic after doing it for my A Level 2 or 3 years ago.
I've been dabbling in C++ for the last 6 months, but decided to start learning again by making a space invaders game.

I've made a datatype/structure or whatever it is in DB that holds the values xpos, ypos, id and alive as integers.

I've used this (probably extremely bad) code to set the X and Y positions
(and I admit to fixing little bits as I'm posting this)


So any c&c on that would be appreciated

but I've come to checking if the ships go off screen in any way, and if they do then reverse their direction.

I realise that the for loops and if statements merged aren't a good idea, but this is what I pieced together



I just get a command out of place at line 80 (next K) error.

I understand that what I've put is wrong, but is there a way to check if any values in my array (which holds 48 variables) is, for example, greater than 600?

Thanks in advance, and I realise I may sound like a moron haha

Awesome sig for awesome guy
IanM
Retired Moderator
23
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 26th Sep 2011 00:45
There's not a lot in your code to go on, but this is my best guess as to what you mean:


Thyde
14
Years of Service
User Offline
Joined: 25th Sep 2011
Location: Bristol, United Kingdom
Posted: 26th Sep 2011 02:23
Thanks for a reply, I guess I must've been a bit vague
I was just looking for a way to check all values in an array at once, like


but I'm using this now, although it's less efficient than I would've liked



Thanks for the help though I used what you put and modified it slightly, I just think my syntax was wrong

Awesome sig for awesome guy
IanM
Retired Moderator
23
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 26th Sep 2011 15:03
Quote: "I was just looking for a way to check all values in an array at once"

Ok, then the answer is no - you have to loop through all items and compare separately.

Your code is incorrect though.

The first enemy > 600 will move everything down. Then the next enemy > 600 will do the same, etc. You need to terminate the loop as soon as you've moved the enemy items down - do this by putting the EXIT command immediately before both the 'Next P' and 'Next J' commands.

Thyde
14
Years of Service
User Offline
Joined: 25th Sep 2011
Location: Bristol, United Kingdom
Posted: 26th Sep 2011 16:26
Hmm yeah I thought that as well, but 'move' is changed before everything's moved down in the nested for loop, which I guess negates it as the step after when we get to the move down again the ships will be at 599 or wherever. I'll put it in just in case though

Sorry to go OT, but I thought while I'm here I'll ask for more help


I was playing around with sprite collisions (and yeah, I actually have no idea about DB nowadays) but I added on to my for loop for drawing sprites



This code draws sprites 3-50, giving them the x and y values from the enemies array 1-48, or Z-2.
I tried only drawing if sprite exists, but it doesn't delete anything it just stops updating the position, so I tried doing delete sprite Z, but I keep getting "Sprite does not exist at line 85" (if sprite collision(2, Z)) if I destroy the bottom right (theoretically sprite 50) ship

I realise I still probably make sense, I'm not entirely sure how to word everything but I'm really grateful for the help haha

Awesome sig for awesome guy
IanM
Retired Moderator
23
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 26th Sep 2011 20:21
I've just really read what you coded - this won't work anywhere near how you expect:


It won't actually execute the code in the loop at all:

for K = 1 to 48 and move=1
- evaluate move=1, which is true and results in a 1, becoming:
for K = 1 to 48 and 1
- evaluate 48 and 1, which is 0 (it's a binary AND), becoming:
for K = 1 to 0

You can't combine checks in a for loop like that - I suspect you've come from a C or C++ background where you can combine multiple checks within a for statement.

This is how the loop should go:


If your 'alive' field simply mirrors whether the sprite exists, then get rid of it.



By moving the collision detection inside the SPRITE EXIST check, it gets rid of that pesky 'sprite does not exist' error.

Thyde
14
Years of Service
User Offline
Joined: 25th Sep 2011
Location: Bristol, United Kingdom
Posted: 26th Sep 2011 20:53
Thanks so much, that makes a lot more sense.
Yeah I have been using C++ for the last 5-6 months, I've been reading guides for DB, but haven't found anything useful on the syntax difference before.

Awesome sig for awesome guy
Pincho Paxton
23
Years of Service
User Offline
Joined: 8th Dec 2002
Location:
Posted: 26th Sep 2011 22:51
Maybe you would be better off with Dark GDK which allows you to use C++?

Thyde
14
Years of Service
User Offline
Joined: 25th Sep 2011
Location: Bristol, United Kingdom
Posted: 27th Sep 2011 00:05
Quote: "Maybe you would be better off with Dark GDK which allows you to use C++?"


I actually have it, I think, just not installed with my MSVS. I might get on to that soon.
Well, I bought the big bundle package with DBP, DBC, FPSC, Voices, Matter, Shader, etc

Awesome sig for awesome guy

Login to post a reply

Server time is: 2026-07-11 01:15:18
Your offset time is: 2026-07-11 01:15:18