Different Speeds
SkaxCo

OK, there is a game I am working on. It has some falling enemies, and if you right click some bullets appear. The bullets don't fall that fast. Well, they aren't supposed to. They fall at two different speeds on different machines. One at normal, one at craaaaaaaaazy fast. On both computers, the zombies fall at the same speed. Here is the (relevant) code:

 1 //this is part of game() which is called in main() 2 while((!key[KEY_ESC])&&(halive)){ 3 while(speed_counter > 0){ 4 for(int i = 0; i < BULLET_MAX; i++){ 5 if(bullets.active){ 6 bullets.move_bullet(); 7 } 8 } 9 /*===the end==========================*/ 10 speed_counter--; 11 } 12 /*###=====#=======#=====##############*/ 13 /*##=START=DRAWING=STUFF=#############*/ 14 /*###=====#=======#=====##############*/ 15 //draw stuff... 16 } 17 18 //###################################### 19 //this is in a header 20 class bullet{ 21 public: 22 float x, y; 23 float speedx, speedy; 24 int active; 25 void init_bullet(float dx, float dy, int cur_x, int cur_y){ 26 active = 1; 27 x = cur_x; 28 y = cur_y; 29 speedx = dx; 30 speedy = dy; 31 } 32 void move_bullet(){ 33 speedy += 0.01; 34 if(speedy > 3) 35 speedy = 3; 36 x += speedx; 37 y += speedy; 38 if((x < 0)||(x > 480)||(y < 0)) 39 active = 0; 40 } 41 void kill(){ 42 x = y = -50; 43 active = speedx = speedy = 0; 44 } 45 };

Anyone know what is wrong?

Albin EngstrÃ¶m

i think you need to post more code,
for example, your timing and timing-initialization code.
also post the section moving the zombies.

make sure the zombies isn't moved inside the drawing loop, if you have any... ignore that for now.

also, set up a summary of you game loop.

Onewing

I see no timer logic. Computers run at different speeds, so it's no surprise that incrementing/decrementing variables occurs at different rates across computers.

SkaxCo

Hnggg...my code is long. Lets see...

1int game(){ 2 zombo = load_datafile("ZomOn.dat"); 3 while(!mouse_b){ 4 blit((BITMAP*)zombo[intro].dat, buffy, 0, 0, 0, 0, 480, 640); 5 textprintf_ex(buffy, font, 25, 150, makecol(255,255,255), -1, "Version: 1.7.5.8"); 6 masked_blit((BITMAP*)zombo[crosshair].dat, buffy, 0, 0, mouse_x - 15, mouse_y - 15, 30, 30); 7 blit(buffy, screen, 0, 0, 0, 0, SCREEN_W, SCREEN_H); 8 } 9 /*=======some init stuff==============*/ 10 LOCK_VARIABLE(speed_counter); //Used to set the timer - which regulates the game's speed 11 LOCK_FUNCTION(increment_speed_counter); 12 install_int_ex(increment_speed_counter, BPS_TO_TIMER(100));//Set our BPS 13 player pc; 14 zombie horde[ZOMBIE_MAX]; 15 bullet bullets[BULLET_MAX]; 16 int shrap_delay = 0; 17 int mouse_clicked, last_button, current_button = 0; //For too see if the mouse has JUST been clicked 18 int zalive = 0, halive = 1; 19 int lvl = 2, prog = 0; 20 while((!key[KEY_ESC])&&(halive)){ 21 while(speed_counter > 0){ 22 /*=====raise me some zombies==========*/ 23 if(zalive < lvl){ 24 for(int i = 0; i < ZOMBIE_MAX; i++){ 25 if(!horde<i>.alive){ 26 horde<i>.animate(); 27 zalive++; 28 i = ZOMBIE_MAX; 29 } 30 } 31 } 32 /*=move teh zombies, check 4 land=====*/ 33 for(int i = 0; i < ZOMBIE_MAX; i++){ 34 if(horde<i>.alive){ 35 horde<i>.y += horde<i>.fall_speed; 36 if(horde<i>.frame_delay <= 0){ 37 horde<i>.frame++; 38 horde<i>.frame_delay = 3; 39 } 40 else 41 horde<i>.frame_delay--; 42 if(horde<i>.frame > 8) 43 horde<i>.frame = 1; 44 if(horde<i>.y > 451){ 45 horde<i>.alive = 0; 46 pc.mines--; 47 zalive--; 48 } 49 } 50 if(horde<i>.moving) 51 horde<i>.frame++; 52 } 53 /*====bullet interaction & stuff======*/ 54 shrap_delay--; 55 int zombie_hit = 0; 56 last_button = current_button; 57 current_button = mouse_b; 58 mouse_clicked = (current_button && !last_button); 59 if(mouse_clicked && (mouse_b & 1)){ 60 for(int i = 0; i < ZOMBIE_MAX; i++){ 61 if((yeah this sees if its in the zombie)){ 62 horde<i>.kill(); 63 zalive--; 64 prog++; 65 zombie_hit = 1; 66 if(prog >= (lvl*2)){ 67 lvl++; 68 prog = 0; 69 } 70 pc.score += 1; 71 i = ZOMBIE_MAX; 72 } 73 } 74 if(zombie_hit) 75 pc.combo++; 76 else 77 pc.combo = 0; 78 } 79 else if(mouse_clicked && (mouse_b & 2) && shrap_delay < 0){ 80 shrap_delay = 100; 81 for(int i = 0; i < BULLET_MAX; i++){ 82 if(!bullets<i>.active){ 83 bullets<i>.init_bullet(0.5, -1.0, mouse_x, mouse_y); 84 bullets[i+1].init_bullet(0.2, 0.2, mouse_x, mouse_y); 85 bullets[i+2].init_bullet(-0.2, 0.2, mouse_x, mouse_y); 86 bullets[i+3].init_bullet(-0.5, -1.0, mouse_x, mouse_y); 87 } 88 } 89 } 90 for(int i = 0; i < BULLET_MAX; i++){ 91 if(bullets<i>.active){ 92 bullets<i>.move_bullet(); 93 } 94 /* a hit!!! */ 95 for(int q = 0; q < ZOMBIE_MAX; q++){ 96 if(((bullets<i>.x > horde[q].x + 2)&& 97 (bullets<i>.x < (horde[q].x + 24))) 98 &&((bullets<i>.y > horde[q].y)&& 99 (bullets<i>.y < (horde[q].y + 28)))){ 100 horde[q].kill(); 101 bullets<i>.deactivate(); 102 zalive--; 103 prog++; 104 if(prog >= (lvl*2)){ 105 lvl++; 106 prog = 0; 107 } 108 pc.score += 1; 109 } 110 } 111 } 112 /*====check for death=================*/ 113 if(pc.mines < 0) 114 halive = 0; 115 /*===the end==========================*/ 116 speed_counter--; 117 } 118 /*###=====#=======#=====##############*/ 119 /*##=START=DRAWING=STUFF=#############*/ 120 /*###=====#=======#=====##############*/ 121 /*======clear the background==========*/ 122 blit((BITMAP*)zombo[backdrop].dat, buffy, 0, 0, 0, 0, 480, 480); 123 /*===draw me some zombies=============*/ 124 /*===draw me some bullets=============*/ 125 /*====draw the HUD====================*/ 126 /*======display score=================*/ 127 /*====display the mouse===============*/ 128 /*====================================*/ 129 blit(buffy, screen, 0, 0, 0, 0, SCREEN_W, SCREEN_H); 130 } 131 return 0; 132}

1/*INCLUDES*/ 2#include <allegro.h> 3/*THINGS*/ 4BITMAP *buffy; 5DATAFILE *zombo; 6volatile long speed_counter = 0; 7/*CLASSES*/ 8class bullet{ 9public: 10 float x, y; 11 float speedx, speedy; 12 int active; 13 void init_bullet(float dx, float dy, int cur_x, int cur_y){ 14 active = 1; 15 x = cur_x; 16 y = cur_y; 17 speedx = dx; 18 speedy = dy; 19 } 20 void move_bullet(){ 21 speedy += 0.01; 22 if(speedy > 3) 23 speedy = 3; 24 x += speedx; 25 y += speedy; 26 if((x < 0)||(x > 480)||(y < 0)) 27 active = 0; 28 } 29 void deactivate(){ 30 x = y = -50; 31 active = speedx = speedy = 0; 32 } 33}; 34class player{ 35public: 36 int mines; 37 int score; 38 int combo; 39 void init_player(){ 40 mines = 5; 41 score = 1; 42 combo = 0; 43 } 44}; 45class weapon{ 46public: 47 int damage; 48 float fire_rate; 49 float reload; 50}; 51class zombie{ 52public: 53 float x; 54 float y; 55 float fall_speed; 56 int moving; /*0: moving, 1: exploding*/ 57 int alive; /*1 = alive, 0 = dead*/ 58 int type; 59 int frame; 60 int frame_delay; 61 void animate(){ 62 x = (rand() % (SCREEN_W - 30)) + 1; 63 y = -30; 64 fall_speed = rand() % 10; 65 fall_speed /= 15; 66 fall_speed += 0.5; 67 alive = 1; 68 moving = 0; 69 type = rand() % 4; 70 frame = (rand() % 8) + 1; 71 frame_delay = 3; 72 } 73 void kill(){ 74 x = y = 500; 75 alive = 0; 76 moving = 1; 77 } 78}; 79/*CONSTANTS*/ 80#define PI 3.14159265 81const int FIELD_W = 480; 82const int FIELD_H = 480; 83const int BULLET_MAX = 400; 84const int ZOMBIE_MAX = 100; 85/*COLORS*/ 86/*FUNC PROTO'S*/ 87void increment_speed_counter(); 88/*FUNCS*/ 89void increment_speed_counter(){ // A function to increment the speed counter 90 speed_counter++; // This will just increment the speed counter by one. 91}END_OF_FUNCTION(increment_speed_counter); // Make sure you tell it that it's the end

I tried to abbreviate...the drawing I just plain deleted. The titles should make it easier to wade through. Another (unmentioned) problem is that bullets don't go away when they hit an enemy. Yet another problem is when I quit, it complains abou some call-stack stuff, but that is irrelevant and I won't include what I think is the problem code (oddly, it is only sometimes). And yes there is timer logic.

HardTranceFan

You're not using the timer variable correctly. Try replacing the code around the "while((!key[KEY_ESC])&&(halive)){" with the following (<<<<<<< denotes the changed or added lines):

```  int lvl = 2, prog = 0, s_counter;  //  <<<<<<<

while((!key[KEY_ESC])&&(halive)){
if (speed_counter == 0)    //  <<<<<<<
rest(1);    //  <<<<<<<
s_counter = speed_counter;  //  <<<<<<<
speed_counter = 0;    //  <<<<<<<
while(s_counter > 0){    //  <<<<<<<
```

And get rid of the line that decrements speed_counter.

The rest(1) is to give the OS some processing time.
[/edit]

SkaxCo

I have no idea...at ALL what you are saying. Where would that go in my code? It is not clear at all. So all of that is before my logic? Because this code...

 1 int s_counter = 1; 2 while((!key[KEY_ESC])&&(halive)){ 3 if (speed_counter == 0) // <<<<<<< 4 rest(1); // <<<<<<< 5 s_counter = speed_counter; // <<<<<<< 6 speed_counter = 0; // <<<<<<< 7 while(s_counter > 0){ // <<<<<<< 8 //logic 9 /*===the end==========================*/ 10 //speed_counter--; 11 } 12 /*###=====#=======#=====##############*/ 13 /*##=START=DRAWING=STUFF=#############*/ 14 /*###=====#=======#=====##############*/ 15 //drawing 16 /*====================================*/ 17 blit(buffy, screen, 0, 0, 0, 0, SCREEN_W, SCREEN_H); 18 } 19 return h_death(pc.score); 20 }

...just makes the program not run. It freezes.

Dustin Dettmer

I used to find these threads so interesting... I can't seem to motivate myself to help out anymore.

I really should quit programming for a job, its corrupting me

HardTranceFan

Sorry, slightly incorrect instructions - put "s_counter--;" instead of the commented line "speed_counter--;".

SkaxCo

Well, it works. The bullets should be falling at MOST twice as fast as the zombies. Can some people check it out? If so, se how fast the bullets fall. Instructions: Left click to auto shoot, right click to shrapnel blast. When the bar on the far right is dark red, you can shrapnel blast. Score is score, combo is combo, mines are mines. I would tell the story, but I can't now.

HardTranceFan

Bullets? I can't see any. But the zombies drop down at various rates. And the shrapnel fans out and drops at a reasonable rate.

SkaxCo

Yeah, I meant the shrapnel. It just uses a class called "bullet" so thats what I think of it as. Thank you for testing it.

Goalie Ca

offtopic:

Quote:

I used to find these threads so interesting... I can't seem to motivate myself to help out anymore.

My problem is that i don't even read threads.. but i glance.. hope i can help, and end up posting something completely irrevelevant.

Albin EngstrÃ¶m

Whats up with this depressing mood??... anyway, it work fine.