Archived

This topic is now archived and is closed to further replies.

project help please!

This topic is 5309 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi everyone, I''m in need of some help for a Uni project. I have to create a version of Boulderdash using openGL. So far I have done the levels, which you can interactively select and load in, but I''m having problems with the movement of my character. I can get him to move around the screen, but I''m having problems with him digging the dirt, etc. i.e, I need to be able to stop him moving through boulders and walls, and for the dirt to disappear when he passes over it. I wondered if anyone could look at my source code and tell me why my move function isn''t doing this - logically I think it should..... I haven''t written out the whole function, but what is there should do what I want - but it doesn''t. Any advice much appreciated (sorry this is long winded!!!) :0) /*********************************************************************************************** LISA GONZALEZ BOULDERDASH COMPUTER PROGRAMMING 2 PROJECT a1490273 *********** MAY 2003 Some source code was obtained from http:\\programming.swordfighter.co.uk. Other websites looked at/used for research: www.nehe.gamedev.net, www.openGL.org A HUGE thanks to Eike Anderson for much needed help! ************************************************************************************************/ #include <GL/glut.h> #include <stdio.h> #include <stdlib.h> #include "image.h" #define windowWidth 600 /* Parameters for window size */ #define windowHeight 650 /* Alter these to suit need */ #define topx 225 #define topy 50 int pressedq = 0; /* global variables to store key pressed information */ int presseda = 0; int pressedz = 0; int pressedx = 0; /*********************************************************************************************** I have created a structure to easily keep track of the objects that are in the game. The variables used are ''movex'' and ''movey'', and they will store the current positions of the objects. This structure can be used for all game objects, such as the player (Rockford), the boulders, the diamonds, enemies and special items. I have created a new structure underneath called player, which will hold the player (Rockford) information. This way it is easy to keep track of object positions, and access the variables easily. ***********************************************************************************************/ struct object { int movex; int movey; }; struct object player; int move(int, int); /* ''move'' function prototype */ image boulder; /* Initialise boulder texture */ image diamond; /* Initialise diamond texture */ image dirt; /* Initialise dirt texture */ image exit_open; /* Initialise exit open texture */ image wall; /* Initialise wall texture */ image rockford; /* Initialise rockford texture */ GLuint boulder_texture, diamond_texture, dirt_texture, exit_open_texture, wall_texture, rockford_texture; /*********************************************************************************************** These are dynamic arrays that will read in the characters that make up the cave levels. These are stored as text files (currently in my ''boulderdash'' directory). Using a dynamic array is better performance wise than a static one, as it saves memory. The ''cave'' text files are opened using fopen, then the width and height of the cave level is read in using fscanf. The for loops then fill the array with the character string using dynamic memory allocation. Finally, the text file is closed. ***********************************************************************************************/ char **cave_array; /* Pointers for ''cave_array'' */ FILE *cave; int height = 0, width = 0; /* Variables to store height and width */ /* of ''cave_array'' */ void init(void) { int i, j; int level = 0; char filename[100]; glClearColor(0.3, 0.2, 0.1, 0.0); /* Clear window to black */ glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(0,20,0,22.5,-1,1); /* Ortho projection */ printf(" BOULDERDASH PLAYING INSTRUCTIONS \n"); printf("**********************************\n\n"); printf("OBJECT OF THE GAME:\n\n"); printf("To move your character (rockford) around the cave collecting all of the\n"); printf("diamonds and avoiding the falling boulders. Dig through the dirt to collect\n"); printf("the diamonds. Boulders can fall through dirt that has been digged - watch out!\n"); printf("The exit will open once all diamonds on the level have been collected.\n\n"); printf("CONTROLS:\n\n"); printf("z = move Rockford left\n"); printf("x = move Rockford right\n"); printf("q = move Rockford up\n"); printf("a = move Rockford down\n\n"); printf("Please select which level you wish to play (1 - 3): "); scanf("%d", &level); sprintf(filename, "C:\\Documents and Settings\\LisaG\\Desktop\\boulderdash\\cave%d.txt", level); cave = fopen(filename, "r"); if (cave != NULL) { fscanf(cave, "%d%d", &width, &height); /* Open ''cave?'' text file, */ /* and scan characters into */ cave_array = (char**) malloc (height*sizeof(char*)); /* dynamic array ''cave_array'' */ if (cave_array != NULL) { for (i=0;i = (char*) calloc (width, sizeof(char)); } } for (i=height-1;i>=0;i--) /* For loops to fill ''cave_array'' with */ for (j=0;j

Share this post


Link to post
Share on other sites
Hi Lisa,

It kind of hard to figure out what exactly the problem is just by looking at code that I can''t compile and step through, but after skimming it I have a couple thoughts that might be related to your problems.

First, you talk about needing dirt to disappear. It looks like your level is stored in cave_array, which is drawn in your draw_level function. But I don''t see anywhere else in the code where the cave_array is manipulated, ie. dirt removed, so that the dirt isn''t drawn the next time draw_level is called.

About moving through boulders and walls, it looks like you''re only calling the move() function if x is pressed, but not for q, a, or z. So only x is getting checked for collision with boulders, the others are just moving the player regardless.

I recommend running your app in a window (or running on a second machine, through remote debugging if possible) so your game and your debugger are not overlapping, and step through your code. It will really help you identify problem spots in the code.

Share this post


Link to post
Share on other sites
i'm a bit perplexed with the way you allocate your cave_array

if (cave != NULL)
{
fscanf(cave, "%d%d", &width, &height); /* Open 'cave?' text file, */
/* and scan characters into */
cave_array = (char**) malloc (height*sizeof(char*)); /* dynamic array 'cave_array' */
if (cave_array != NULL)
{
for (i=0;i {
cave_array = (char*) calloc (width, sizeof(char));
}
}

shouldn't the last bit be

*cave_array[ i ] = (char*) calloc (width, sizeof(char));

as all you are doing it replacing the first allocation with the second allocation ??

when you do you switch/case then you have remember that cave_array is a pointer to a pointer.. just using cave_array[ i ][ j ] won't work.

might be best if you just allocate a fixed sized array (for the maximum map size) and load it into that.

http://members.iinet.net.au/~cleathley/

[edited by - jumpman on May 29, 2003 12:03:55 AM]

Share this post


Link to post
Share on other sites