Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Game completely screwed up.

Credits go to HardTranceFan and Onewing for helping out!
This thread is locked; no one can reply to it. rss feed Print
Game completely screwed up.
gamelord12
Member #8,586
May 2007

Everything was working pretty much how I wanted when only one player's character was drawn to the screen. Now, I run into all sorts of other errors. The best way for you guys to help me would probably be to compile and run my program for yourselves. I'll attach a zip of the files you need.

#SelectExpand
1// Sword Play 2 3// Known issues: 4 5// two players are stuck together 6// player 2, as far as I can tell, is not starting at the location I specified (x-coordinate 440) 7// game crashes for reasons unknown 8// occasionally game will skip the last frame of any animation, even when only one player is there and works properly...is that always going to happen or can that be fixed? 9// many more sprite sheets still needed 10 11#include <allegro.h> 12 13#define BLACK makecol(0, 0, 0) 14#define GREEN makecol(0, 255, 0) 15#define WHITE makecol(255, 255, 255) 16 17volatile int frame1 = 0, frame2 = 0, p1 = 40, p2 = 440, player, stamina1 = 100, stamina2 = 100, ticks = 0; 18const int frameRate = 8, staticy = 200; 19bool first1 = true, otherFirst1 = false, first2 = true, otherFirst2 = false; // restarts the frame count every time a button is pressed 20 21// obtains sprites from a sprite sheet 22BITMAP *grabframe(BITMAP *source, int width, int height, int startx, int starty, int columns, int frame) 23{ 24 BITMAP *temp = create_bitmap(width, height); 25 26 int x = startx + (frame % columns) * width; 27 int y = starty + (frame / columns) * height; 28 29 blit(source, temp, x, y, 0, 0, width, height); 30 31 return temp; 32} 33BITMAP *buffer; // used to smooth out the animation 34BITMAP *buffer2; // used to smooth out animation even more 35BITMAP *pic; // temporary variable for putting sprites into an array 36BITMAP *SwordPlayTitle; // title screen picture 37 38// sprite arrays end in s (for sprite) to differentiate from functions 39BITMAP *fightStances[4]; 40BITMAP *runForwards[9]; 41BITMAP *runBackwards[5]; 42 43/*BITMAP *attackHeads[10]; 44BITMAP *attackHands[10]; 45BITMAP *attackFoots[10]; 46BITMAP *defendHeads[1]; 47BITMAP *defendHands[1]; 48BITMAP *defendFoots[1]; 49 50BITMAP *powerStruggles[1]; 51BITMAP *pushs[4];*/ 52 53void time(void); // used to count hundredths of a second 54void frame1Function(int maxframes); // controls animation of current action for player 1 55void frame2Function(int maxframes); // controls animation of current action for player 2 56 57void runForward(int player); // 9 frames 58void runBackward(int player); // 5 frames 59 60/*void attackHead(int player); 61void attackHand(int player); 62void attackFoot(int player); 63void defendHead(int player); 64void defendHand(int player); 65void defendFoot(int player); 66 67void push(int player); // 5 frames: one player stumbles backward, other stays in fightStance 68void powerStruggle(void); // both players go through same two frames, then loser is pushed*/ 69 70void stamina(void); // displays players' stamina bars 71void increaseStamina(void); // slightly regenerates stamina every second 72/*void decreaseStamina(void); // decreases player's stamina when an attack is blocked*/ 73 74int main(void) 75{ 76 allegro_init(); // uses Allegro 77 install_timer(); // initializes timing functionality 78 install_keyboard(); // allows use of keyboard 79 set_color_depth(32); // sets color depth for pallette of sprites 80 set_gfx_mode(GFX_AUTODETECT_WINDOWED, 640, 480, 0, 0); // makes the screen a 640x480 window 81 SwordPlayTitle = load_bitmap("SwordPlayTitle.bmp", NULL); // loads title screen image 82 83 blit(SwordPlayTitle, screen, 0, 0, 0, 0, SwordPlayTitle->w, SwordPlayTitle->h); // puts the title screen picture on the screen 84 textprintf_centre_ex(screen, font, SCREEN_W / 2, 460, makecol(255, 255, 255), -1, "Press Any Key to Continue..."); 85 destroy_bitmap(SwordPlayTitle); // frees up memory by destroying a now unneeded title screen 86 readkey(); // waits for input to proceed 87 88 rest(50); // brief pause between input and loading the actual game 89 90 // loads frames for the fighting stance animation 91 pic = load_bitmap("FightStanceSpriteSheet.bmp", NULL); 92 for(int x = 0; x <= 3; x++) 93 { 94 fightStances[x] = grabframe(pic, 240, 240, 0, 0, 4, x); 95 } 96 97 // loads frames for the running forward animation 98 pic = load_bitmap("RunForwardSheet.bmp", NULL); 99 for(int x = 0; x <= 8; x++) 100 { 101 runForwards[x] = grabframe(pic, 240, 240, 0, 0, 4, x); 102 } 103 104 // loads frames for the running backward animation 105 pic = load_bitmap("RunBackwardSheet.bmp", NULL); 106 for(int x = 0; x <= 4; x++) 107 { 108 runBackwards[x] = grabframe(pic, 240, 240, 0, 0, 4, x); 109 } 110 111 destroy_bitmap(pic); // frees up memory by destroying a now unneeded temporary variable 112 buffer = create_bitmap(SCREEN_W, SCREEN_H); // makes the buffer 113 buffer2 = create_bitmap(SCREEN_W, SCREEN_H); // makes the buffer's buffer 114 install_int_ex(time, BPS_TO_TIMER(100)); // calls the timer function 100 times every second 115 install_int_ex(increaseStamina, BPS_TO_TIMER(1)); // increases the stamina bars for both players by 1 every second 116 117 // main game loop 118 while(!key[KEY_ESC]) 119 { 120 clear_to_color(buffer, WHITE); 121 /*// attack commands for player 1 122 if(key[KEY_W] && key[KEY_SPACE]) 123 attackHead(1); 124 else if(key[KEY_SPACE]) 125 attackHand(1); 126 else if(key[KEY_S] && key[KEY_SPACE]) 127 attackFoot(1); 128 129 // defend commands for player 1 130 else if(key[KEY_W] && key[KEY_LSHIFT]) 131 defendHead(1); 132 else if(key[KEY_LSHIFT]) 133 defendHand(1); 134 else if(key[KEY_S] && key[KEY_LSHIFT]) 135 defendFoot(1);*/ 136 137 // movement commands for player 1 138 if(key[KEY_D]) 139 { 140 if(first1 == true) 141 { 142 frame1 = 0; 143 first1 = false; 144 otherFirst1 = true; 145 } 146 runForward(1); 147 draw_sprite(buffer, runForwards[frame1], p1, staticy); 148 } 149 else if(key[KEY_A]) 150 { 151 if(first1 == true) 152 { 153 frame1 = 0; 154 first1 = false; 155 otherFirst1 = true; 156 } 157 runBackward(1); 158 draw_sprite(buffer, runBackwards[frame1], p1, staticy); 159 } 160 else 161 { 162 first1 = true; 163 if(otherFirst1 == true) 164 { 165 otherFirst1 = false; 166 frame1 = 0; 167 } 168 frame1Function(3); 169 draw_sprite(buffer, fightStances[frame1], p1, staticy); 170 } 171 172 /*// attack commands for player 2 173 if(key[KEY_W] && key[KEY_SPACE]) 174 attackHead(1); 175 else if(key[KEY_SPACE]) 176 attackHand(1); 177 else if(key[KEY_S] && key[KEY_SPACE]) 178 attackFoot(1); 179 180 // defend commands for player 2 181 else if(key[KEY_W] && key[KEY_LSHIFT]) 182 defendHead(1); 183 else if(key[KEY_LSHIFT]) 184 defendHand(1); 185 else if(key[KEY_S] && key[KEY_LSHIFT]) 186 defendFoot(1);*/ 187 188 // movement commands for player 2 189 if(key[KEY_LEFT]) 190 { 191 if(first2 == true) 192 { 193 frame2 = 0; 194 first2 = false; 195 otherFirst2 = true; 196 } 197 runForward(2); 198 draw_sprite_h_flip(buffer, runForwards[frame1], p1, staticy); 199 } 200 else if(key[KEY_RIGHT]) 201 { 202 if(first2 == true) 203 { 204 frame2 = 0; 205 first2 = false; 206 otherFirst2 = true; 207 } 208 runBackward(2); 209 draw_sprite_h_flip(buffer, runBackwards[frame1], p1, staticy); 210 } 211 else 212 { 213 first2 = true; 214 if(otherFirst2 == true) 215 { 216 otherFirst2 = false; 217 frame2 = 0; 218 } 219 frame2Function(3); 220 draw_sprite_h_flip(buffer, fightStances[frame1], p1, staticy); 221 } 222 223 stamina(); // displays the stamina bars on the screen 224 blit(buffer, buffer2, 0, 0, 0, 0, SCREEN_W, SCREEN_H); // puts everything that just happened in the last split-second on the second buffer 225 blit(buffer2, screen, 0, 0, 0, 0, SCREEN_W, SCREEN_H); // puts that buffer on the screen 226 clear_bitmap(buffer); // erases the buffer so that it can be changed again 227 } 228 remove_int(increaseStamina); // frees up the memory used to call this function over and over again 229 230 return 0; // makes this code compatible with non-MS compilers 231}END_OF_MAIN() 232 233void time() 234{ 235 ticks++; 236 237 if(ticks >= 100) 238 ticks = 0; 239} 240 241void frame1Function(int maxframes) 242{ 243 if(frame1 >= maxframes) 244 frame1 = 0; 245 246 float comparisonVariable = 100 / frameRate; 247 248 if(ticks >= comparisonVariable) 249 { 250 ticks = 0; 251 252 if(frame1 >= maxframes) 253 frame1 = 0; 254 else 255 frame1++; 256 } 257} 258 259void frame2Function(int maxframes) 260{ 261 if(frame2 >= maxframes) 262 frame2 = 0; 263 264 float comparisonVariable = 100 / frameRate; 265 266 if(ticks >= comparisonVariable) 267 { 268 ticks = 0; 269 270 if(frame2 >= maxframes) 271 frame2 = 0; 272 else 273 frame2++; 274 } 275} 276 277void runForward(int player) 278{ 279 if(player == 1) 280 { 281 frame1Function(8); 282 283 if(p1 <= (p2 - 240)) 284 p1++; 285 } 286 287 if(player == 2) 288 { 289 frame2Function(8); 290 291 if(p2 >= p1) 292 p2--; 293 } 294} 295 296void runBackward(int player) 297{ 298 if(player == 1) 299 { 300 frame1Function(4); 301 302 if(p1 > 0) 303 p1--; 304 } 305 306 if(player == 2) 307 { 308 frame1Function(4); 309 310 if(p2 < SCREEN_W) 311 p2++; 312 } 313} 314 315void stamina() 316{ 317 textprintf_ex(buffer, font, 5, 15, BLACK, -1, "Player 1"); 318 textprintf_right_ex(buffer, font, SCREEN_W - 5, 15, BLACK, -1, "Player 2"); 319 rectfill(buffer, 5, 5, 5 + stamina1, 10, GREEN); 320 rectfill(buffer, SCREEN_W - 105, 5, SCREEN_W - 105 + stamina2, 10, GREEN); 321} 322 323void increaseStamina() 324{ 325 stamina1++; 326 if(stamina1 > 100) 327 stamina1 = 100; 328 stamina2++; 329 if(stamina2 > 100) 330 stamina2 = 100; 331}

Goalie Ca
Member #2,579
July 2002
avatar

1) Use a more descriptive title
2) Give us some background. Help us help you. DOn't ask for us to just "fix it".

Debugging has to be systematic to work. Trace through your program at the first sign of a problem (or better yet. right before something happens). step through execution line by line while watching variables.

Test individual functions for correctness if you can.

-------------
Bah weep granah weep nini bong!

HardTranceFan
Member #7,317
June 2006
avatar

How come you still haven't fixed your animation problem? I explained in quite some detail, complete with examples, how to do that in my last post to your thread (the response to your second post on that thread).

[edited to provide further help]

In response to your known issues in the comments at the start of the program:

Quote:

// two players are stuck together
// player 2, as far as I can tell, is not starting at the location I specified (x-coordinate 440)

Yep, that's because you're plonking them both at the came location, (p1, staticy). You're also using frame1 animation to display player 2 (albeit flipped).

Quote:

// game crashes for reasons unknown

Run it in debug mode and see where it stops?

Quote:

// occasionally game will skip the last frame of any animation, even when only one player is there and works properly...is that always going to happen or can that be fixed?

See my comment at the top of this post for that one.

Two of these 3 issues are quite basic errors (particularly the positioning one), and it indicates you have made very little effort, if any, to work out for your self why it's not working. That just plain lazy. I'm not going to help again if you're not going to give debugging a good shot before asking for help.

--
"Shame your mind don't shine like your possessions do" - Faithless (I want more part 1)

gamelord12
Member #8,586
May 2007

I don't know how to read a debug screen.

Onewing
Member #6,152
August 2005
avatar

What IDE are you using? (Dev-C++, Code::Blocks, Visual Studio, etc.)?

------------
Solo-Games.org | My Tech Blog: The Digital Helm

gamelord12
Member #8,586
May 2007

I'm using Visual Studio 2005.

Onewing
Member #6,152
August 2005
avatar

Quote:

I don't know how to read a debug screen.

Quote:

I'm using Visual Studio 2005.

Okay, you should have a debugger built into Visual Studio 2005. You can go to your help menu and do a search for "debugger" or even "debug", which will give you a start on how it works.

Note that you can set "breakpoints" and when running in debug mode, the program will pause at this breakpoint. Then you can manually step line-by-line through your code and see exactly where your code bombs.

Knowing how to use a debugger is not optional, it is a part of programming. No better time to learn. ;)

------------
Solo-Games.org | My Tech Blog: The Digital Helm

gamelord12
Member #8,586
May 2007

Alright, got it working. Most of my errors were just due to copy+pasting and forgetting to change the things I was supposed to. Thanks for the tip on the debugging. I asked my brother for help in debugging, who just finished his sophomore year in college (computer science major) and oddly enough, he doesn't know how to read a debug screen either. Usually the help files in VS just remind you of syntax without actually explaining anything, but I'll give it a go.

Goalie Ca
Member #2,579
July 2002
avatar

Quote:

who just finished his sophomore year in college (computer science major) and oddly enough, he doesn't know how to read a debug screen either.

Not strange at all. As far as i know they never "teach" anyone how to use a debugger and the students just end up using "printf" debugging.

-------------
Bah weep granah weep nini bong!

Jonatan Hedborg
Member #4,886
July 2004
avatar

Meh. I was taught how to use GDB at one of my uni courses.

Julian Guarin
Member #5,787
April 2005
avatar

Debugging is not only a matter of using a GNU/GDB interface or the M$ or the borland's one. Its also a matter of code.

There are plenty of coding techniques in doing debugging. glibc, or the MFC or the C standard support functions and macros in order to make debugging. In our particular case, allegro also provides some basic routines for debugging your program. check the "debugging" session in the manual.

And by the way I never was taught GDB in the university but thats shame because a lot of things about the processor and the system could be learned by the time. :-/

however : fprintf(stdout,"this s the greatest debugger ever %d\n",var);

against what are you, hard to know it is,
but which side are you, yet harder.

"You've been neglecting my chickens again, Gwydion. Feed them, and quickly!."

Go to: