3 Replies - 756 Views - Last Post: 03 June 2010 - 11:30 PM Rate Topic: -----

#1 Guest_enigma322*


Reputation:

Help with Wyrm Game. Collision issues.

Posted 02 June 2010 - 10:44 PM

So I am using SDL to run the graphics... I am stuck at trying to check to see if the wyrm head has eaten one of the following nodes. But whenever he eats an apple, it exits out. and there are no errors, but it says that the d->x, d->y, and d->next are undefined..a little help please?


#include "SDL.h"
#include "SDLHelpers.h"
#include "Array.h"
#include "CSIS10B.h"
#include <stdlib.h>
#include <time.h>
#include "Seg.h"
#include "Apple.h"
#include "DList.h"


// ============================================================================
//  Global Variables
// ============================================================================


SDL_Surface* g_wyrmbmp = 0;
SDL_Surface* applebmp;
int count = 0;



void draw(Seg* hd)
{  
	Seg* itr;      // use an iterator to draw all nodes in list
    for(itr=hd; itr!=0; itr=itr->next )
    {
        SDLBlit(g_wyrmbmp, g_window, itr->x,itr->y);
    }
}

void pause(int msec)  // pause for msec milliseconds
{
	unsigned long ticks=SDL_GetTicks();  // gets current time in millisecs
	while(SDL_GetTicks()<ticks+msec);    // keep checking until ticks passes stop time
}


void crawl(Seg*& hd, int& dir, int& grow)
{   Seg* p;
    switch(dir)  // first check if you're hitting edge of screen
    {            //   and turn right if you are
		case 0:  if (hd->y<=24) dir=1; break;
         case 1:  if (hd->x>=WIDTH-24) dir=2; break;
         case 2:  if (hd->y>=HEIGHT-24) dir=3; break;
         case 3:  if (hd->x<=24) dir=0; break;      
    }
    switch(dir)  // make a new head with x,y based on dir of travel
    {
        case 0: 
            hd=new Seg(hd->x, hd->y-24, hd);
  	 	 	break;
        case 1: 
            hd=new Seg(hd->x+24, hd->y, hd);
            break;
        case 2: 
            hd=new Seg(hd->x, hd->y+24, hd);
            break;
        case 3: 
            hd=new Seg(hd->x-24, hd->y, hd);
            break;
    }
    p=hd;         // use itr p to get to second to last node
   if (grow == 1)
	   grow = 0;
   else
   {
   while(p->next->next!=NULL) 
    p=p->next;
  	delete p->next;           // and remove last node
  	p->next=NULL;
   }
}
--------------------------NEED HELP HERE-------------------------------------
bool headEtail(Seg* c)
{
	Seg* d;
	d=c;
		while (c != 0)
		{
		
		
			c=c->next;
		if (abs(c->x-d->x)<20 && abs(c->y-d->y)<20);
		return true;
		}
		
		return 0;
}

int main( int argc, char* argv[] )
{
    srand( time(0) );
//  Initialize wyrm

	Seg *wyrm=new Seg(40,276,NULL);  // wyrm points to head of list
    /*wyrm->next=new Seg(40,300,NULL); // other links you can add in
    wyrm->next->next=new Seg(40,324,NULL);
    wyrm->next->next->next=new Seg(40,348,NULL);
    wyrm->next->next->next->next=new Seg(40,372,NULL);
    wyrm->next->next->next->next->next=new Seg(40,396,NULL);*/
 

	Apple aApp;

	List appList(100);             // create list of bubbles
		for (int k=0; k<100; k++)     // populate list with random bubbles
	{
		Apple newApple;
		appList.insert(newApple, k);
	}


    // declare coordinates.
    int x, y, dir=0, grow=0;


    // declare event holder
    SDL_Event event;

    // initialize the video system.
    SDL_Initialize("Wyrms!");
    SDL_Init(SDL_INIT_TIMER);  // start timer 

    // load the bitmap:

	g_wyrmbmp = SDL_LoadBMP( "seg.bmp" );
	applebmp = SDL_LoadBMP("apple.bmp");

    while( 1 )
    {
        //look for an event
        if( SDL_PollEvent( &event ) )
        {
            //an event was found
            if( event.type == SDL_QUIT ) 
                break;
            if( event.type == SDL_MOUSEBUTTONDOWN ) 
            {
                // get the mouse state.
                SDL_GetMouseState( &x, &y );

            }
            if( event.type == SDL_KEYDOWN )
            {
                // a key was pressed.
                if( event.key.keysym.sym == SDLK_ESCAPE )
                {
                    // if ESC was pressed, quit the program.
                    SDL_Event quit;
                    quit.type = SDL_QUIT;
                    SDL_PushEvent( &quit );
                }
 
   				if( event.key.keysym.sym == SDLK_UP &&(dir==1||dir==3))
                    dir=0;

                if( event.key.keysym.sym == SDLK_DOWN &&(dir==1||dir==3))
                   dir=2;

                if( event.key.keysym.sym == SDLK_LEFT &&(dir==0||dir==2))
                    dir=3;

                if( event.key.keysym.sym == SDLK_RIGHT&&(dir==0||dir==2) )
                     dir=1;
            }
        }   // end event loop.

 
        // clear the screen to white.
		ClearScreen(WHITE);

		draw(wyrm);
		if (headEtail(wyrm))
		{
				grow = 1;
		}

		if (count<1)
			{
				aApp=appList.retrieve(0);
				SDLBlit(applebmp,g_window, aApp.x,aApp.y);
			}

		if (aApp.EatenBy(wyrm))
			{
				grow = 1;
				count++;
				aApp = appList.retrieve(count);
			}

			SDLBlit(applebmp,g_window, aApp.x,aApp.y);

		crawl(wyrm,dir,grow);

        // update the entire window.
        SDL_UpdateRect( g_window, 0, 0, 0, 0 );
		pause(100);   // wait time in milliseconds
    
	}
    // done
    return 0;
}


Admin Edit: Please use code tags when posting your code. Code tags are used like so => :code:

Thanks,
PsychoCoder :)

Is This A Good Question/Topic? 0

Replies To: Help with Wyrm Game. Collision issues.

#2 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1642
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Help with Wyrm Game. Collision issues.

Posted 02 June 2010 - 11:02 PM

Moved to Game Programming :)
Was This Post Helpful? 0
  • +
  • -

#3 Guest_enigma322*


Reputation:

Re: Help with Wyrm Game. Collision issues.

Posted 03 June 2010 - 03:15 AM

Thanks for the tips on putting on code, and moved to the right section. I was a bit in a hurry when I posted this.
Was This Post Helpful? 0

#4 Guest_enigma322*


Reputation:

Re: Help with Wyrm Game. Collision issues.

Posted 03 June 2010 - 11:30 PM

UPDATE: so i think i'm getting errors because my hd->next doesn't have an x value to it. I am having trouble coming up with a way to assign it one...any help?
Was This Post Helpful? 0

Page 1 of 1