Each ship will have 6 values:
-Pos_x
-Pos_y
-vel_x
-vel_y
-health
-movement_progress

Each will be an int.

There is a max of 150 bullets onscreen at any time, where bulletarray[i]=x and bulletarray[i+1]=y

For the ships, when movement progress is 0, it follows the general
movement of the rest of the ships as specified by int Direction. When movement_progress is not 0, the ship is following a path downwards towards you, and helps keep track of when to make turns and such.


Things I need to figure out:
How enemies will know where to go to when they get done with their decent downwards.
How enemies will be stored.


int x and int y are the X and Y positions of your ship respectivly
scrbuf is the screenbuffer I am using.



More to come Very Happy
Will you be keeping track of which bullet belongs to which ship (I don't think it's necessary). For the bullets, you might also need to keep track of vel_x and vel_y (if bullets have nonstandardized velocity) or just direction (personally, I think vel_x and vel_y is easier, so you can just do x+=vel_x and y+=vel_y on each physics tick). What about keeping track of the damage dealt by each bullet in the bullet array, in case you have multiple types of bullets, which I presume you might?
I think I might just do two arrays, bulletarray and enemybulletarray. As for the velocity of the bullets, they shoot straight forwards, so I just subtract two from the Y of each bullet every time Smile Right now, I don't have multiple types of bullets, so I'm not sure. But it does sound like a good idea Smile

Edit: It just occured to me that if I have a type for the bullets, be it damage or something, that I could have a type(s) for the enemy too Smile
Aww, they all shoot straight forward? So no weapon upgrades that will let you spray a curtain of bullets in every direction?
Oooh, that is a good idea Very Happy I'll work it into my code Very Happy

Edit: Is this what my struct array will look like?

struct mystruct {int,int,int,int,int};
mystruct array[2]={0,0};
Not a very helpful one! Laughing


Code:
struct myStructType{
   int x;
   int y;
   int vel_x;
   int vel_y;
};
#define ENEMY_ARRAY_LEN 50
struct myStructType enemies[ENEMY_ARRAY_LEN];


It's not the funnest thing ever to initialize an array of structs with the {} construct, so try something like this:


Code:
for(int i=0;i<ENEMY_ARRAY_LEN;i++) {
   enemies[x].x = ((x & 0x01) == 0)?10:60;
   enemies[x].y = 16*i;
   enemies[x].vel_x = 0;
   enemies[x].vel_y = 0;
}
Earlier KermM said something about malloc not being able to be global, any way around it?
_player1537 wrote:
Earlier KermM said something about malloc not being able to be global, any way around it?
Let me be more specific. You can't malloc() things in the global scope itself. However, if you declare a variable like struct myType* myVariable = 0; outside of any function, then malloc() it near the beginning of your main(), you can easily use that in other functions. Just be aware that it's a pointer, of course, and to be safe, make sure you check that it's not null anymore.
Not sure I fully understand, could you post a specific example please? Like a code example.
_player1537 wrote:
Not sure I fully understand, could you post a specific example please? Like a code example.


Sure thing, here's something completely arbitrary:


Code:
//includes and all that

struct myStructType {
    int x;
    int y;
    float vel_x;
    float vel_y;
}

struct myStructType* myArray[] = null;
#define myArrayLen 2

int main(int argc, char* argv[]) {
    //initialization
    if (!(myArray = malloc(sizeof(myStructType)*MY_ARRAY_LEN))) {
        //display error message
        exit(-1);
    }
    (*myArray)[1].x = 1;
    doStuff();
}
void doStuff() {
    (*myArray)[0].x = (*myArray)[1].x;
    return;
}
I'll have to double-check that initialization statement (ie, struct myStructType* myArray[] = null;).

Code:
//includes and all that

//Make the struct that is used for the array
struct myStructType {
    int x;
    int y;
    float vel_x;
    float vel_y;
}

//Create the array, and make the [length, data] null
struct myStructType* myArray[] = null;

//Define the length
#define myArrayLen 2

int main(int argc, char* argv[]) {
    //initialization

//If myArray doesn't exist, error out
    if (!(myArray = malloc(sizeof(myStructType)*MY_ARRAY_LEN))) {
        //display error message
        exit(-1);
    }
//No clue for the asterik... but it sets the first element (really the second)'s X value equal to 1
    (*myArray)[1].x = 1;
//Call another function
    doStuff();
}
void doStuff() {
//Make the 0th element of the array's X value equal to the 1st element of the array's X
    (*myArray)[0].x = (*myArray)[1].x;
    return;
}


Is that about right?
Ah, I see you're not familiar with pointers, which is going to make this a little more difficult to explain. Other than your note about the *asterisk (not "asterik"), it's all correct.


Code:
(*myArray)[1].x = 1;
The *myArray means that myArray is actually an integer holding a pointer to the contents of my array. The * looks inside myArray and gets that address. Then, the [1] essentially moves (sizeof(myStructType)) bytes forward in memory, then the .x moves a little bit more (in this case 0 bytes). We'll have to give you a formal introduction to pointers at some point, and how the * and & operators are used.
Ah, ok. Yes, pointers always were confusing to me x.x

Is there anyway to have functions within the Main() function?
_player1537 wrote:
Ah, ok. Yes, pointers always were confusing to me x.x

Is there anyway to have functions within the Main() function?
What do you mean, like sub-functions? No. What would be the point of that? Perhaps if you can explain what you'd want that for I can explain the proper way to do it. Smile
This whole thing with pointers seems like too much work Sad
_player1537 wrote:
This whole thing with pointers seems like too much work Sad
Fine, then there's a different way to do it, but it's basically the same thing. And don't wimp out about pointers just because they're scary! Press onward! Face challenges! I didn't start out knowing all the languages I did; I had to stare at other people's code and be confused first!


Code:
//includes and all that

struct myStructType {
    int x;
    int y;
    float vel_x;
    float vel_y;
}

#define myArrayLen 2

int main(int argc, char* argv[]) {
    //initialization
    struct myStructType* myArray[] = null;
    if (!(myArray = malloc(sizeof(myStructType)*MY_ARRAY_LEN))) {
        //display error message
        exit(-1);
    }
    (*myArray)[1].x = 1;
    doStuff(myArray);
}
void doStuff(struct myArrayType **myArrayPassedIn) {
    (*myArrayPassedIn)[0].x = (*myArrayPassedIn)[1].x;
    return;
}
KermM helped me tonight with getting my code to work, apparently it was segfaulting. He gave me a fix, but when I tried that fix, and then another equally good fix (in terms of the segfault). I'm going to work on this tomorrow when I get time. One thing I think might be happening is maybe AB (an int* pointing to bulletarray) is becoming 0, or the wrong value.

Oh, and I learned about short circuiting. 0 && 1 will check 0 first, which is not true, and stops testing values. 1 && 0 will check 1 first, true, and then check 0. [note: this was for self reference Very Happy]
_Player, we can certainly continue looking at your code today and try to track down what's happening to ab. It's a shame that there's no stderr or stdout to help debug program. Sad Oh, and don't forget that short-circuiting works with both the logical or (||) and logical and (&&) operators.
Can I get some help with this? Here is the relevent code:

Code:
void AddBullet(int x, int y, int vel_x, int vel_y, int* bulletarray);
int main(void)
{
   //...
   int* bulletarray = malloc(4*300*sizeof(int));
   memset(bulletarray,0,4*300*sizeof(int));

   int* ba = bulletarray;

   while (!isKeyPressed(KEY_NSPIRE_ESC))
   {

      if (isKeyPressed(KEY_NSPIRE_CTRL) && 0)
      {
          #if ship == ship1
            addBullet(x+5,y,bullettypes[bullet_type],bullettypes[bullet_type+1],ba);
            AddBullet(x+18,y,bullettypes[bullet_type+2],bullettypes[bullet_type+3],ba);
         #endif
         #if ship == ship2
            AddBullet(x+7,y,bullettypes[bullet_type],bullettypes[bullet_type+1],ba);
            AddBullet(x+16,y,bullettypes[bullet_type+2],bullettypes[bullet_type+3],ba);
         #endif
      }
   }
   return 0;
}


void AddBullet(int x, int y, int vel_x, int vel_y, int* bulletarray)
{
   int i = 0;
   while (i<1100 && bulletarray[i+1]>0)
   {
      i += 4;
   }
   if (i != 1200)
   {
      bulletarray[i] = x;
      bulletarray[i+1] = y;
      bulletarray[i+2] = vel_x;
      bulletarray[i+3] = vel_y;
   }
}


Right now, when it tries to run, it doesn't even start :/ At first I thought it was the short circuiting thing, that didn't work. Then I tried changing the "while (i<1200..." part to "while (i<1100..." that way there was no way to cause a segfault. But it still doesn't work :/ Thanks in advance
Just to check, my previous fix did the trick for a while, right? Or is this still the same thing with no luck?
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 3
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement