Page 1 of 1

Space If Evaders

PostPosted: Wed Jan 18, 2012 6:53 pm
by foleyjo
Hi all

Just a small little project I set myself. I have tried to make a space invaders game without using the word If in the code. (The game title doesn't count)

I have cheated on 1 or 2 steps and used a switch case statement but this was when I couldn't find an alternative.

So an example would be an actor that has 3 hit points and when they run out it's destroyed. I did not know an alternative to

Code: Select all
if(hp <=0)
  destroyactor;


other than
Code: Select all
switch(hp)
{
case 0: destroyactor; break;
}



****UPDATE - Changed the switch case statements to use a for loop like in SuperSonics example. Also fixed some tiny bugs that I found. BOY I HATE THOSE BUGS :x

Re: Space If Evaders

PostPosted: Wed Jan 18, 2012 7:56 pm
by SuperSonic
Try making an int in global code like this:
Code: Select all
int i;

Then on your actor's draw code, put this:
Code: Select all
for(i = hp; i < 1; i++)
{
    destroyactor;
}

:D

Re: Space If Evaders

PostPosted: Wed Jan 18, 2012 8:39 pm
by Jagmaster
That's a good solution SS. :o

Re: Space If Evaders

PostPosted: Wed Jan 18, 2012 8:56 pm
by SuperSonic
Thanks^^

Re: Space If Evaders

PostPosted: Wed Jan 18, 2012 9:31 pm
by foleyjo
I like it :D

I will make the changes when I get the chance

Re: Space If Evaders

PostPosted: Wed Jan 18, 2012 11:35 pm
by Game A Gogo
You should realized that just because you're not using the word's "if" in your script does not mean you're doing anything better.

it's not the if that takes a lot of computing but the condition check and there's usually no way to avoid this!

Using a for loop or a switch case is actually a worst practice than using if, as they all do a condition check, but the for and switch try to do more than it should!

Re: Space If Evaders

PostPosted: Thu Jan 19, 2012 12:00 am
by Fojam
i thought switch was more efficient than if?

Re: Space If Evaders

PostPosted: Thu Jan 19, 2012 2:45 am
by Game A Gogo
Not if you're just checking for one state

Re: Space If Evaders

PostPosted: Thu Jan 19, 2012 8:00 am
by foleyjo
I would have thought the for loop SS exampled would be just as efficient because the for loop would be ignored until the condition was true.

Don't worry GAG I didn't think this was the best solution for a space invaders game :lol: . I know exactly where to put the if's to make everything better.
The reason I did this was due to something I read in one of the If Haters threads. I can't remember who said it but they said while at college their teacher gave them a project to rewrite some code without using if's .
I thought that would be a nice challenge to test what I have learned so far.

Re: Space If Evaders

PostPosted: Thu Jan 19, 2012 4:57 pm
by SuperSonic
foleyjo wrote:I like it :D

I will make the changes when I get the chance

Awsome. Glad I could help :P

Re: Space If Evaders

PostPosted: Thu Jan 19, 2012 7:37 pm
by tintran
nice remake!