Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » sliding puck - air hockey game

This thread is locked; no one can reply to it. rss feed Print
sliding puck - air hockey game
zavirax
Member #8,634
May 2007

How do i get a puck to slide across the screen?
How do i do the rebounds off of walls
how do i get it to go in the goal because there isnt a hole in the white bounding box?

here is my source please show what you mena if making a suggestion thanks a lot!

#SelectExpand
1/* RULES: 2Get the puck into the other players goal, 3the winner is the person with the most goals 4You Cant Leave your half of the field 5 6 */ 7 8#include <allegro.h> 9#include <stdio.h> 10 11int p1_x = 100; /* sets the position of the O sign of playerone(x-axis) */ 12int p1_y = 260; /*(y-axis)*/ 13 14int width_p1 = 6; /* how wide the p1's and p2's cursor is*/ 15int height_p1 = 6; /* how hugh the p1's and p2's cursor is*/ 16int width_p = 10; 17int height_p = 10; 18 19int p1_bb_L = p1_x; /*bb is bounding boox*/ 20int p1_bb_R =(p1_bb_L+width_p1); /*the right side is just a width away from the left so if we add width we have the right side*/ 21int p1_bb_T = p1_y; 22int p1_bb_B =(p1_y+height_p1); /* the same but for bottom and top*/ 23 24int p2_x = 530; /* playertwo*/ 25int p2_y = 255; 26 27int p2_bb_L = p2_x; 28int p2_bb_T = p2_y; 29int p2_bb_R = p2_x + width_p1; 30int p2_bb_B = p2_y + height_p1; 31 32int outside_bb_left = 60;/* playing field boundong box*/ 33int outside_bb_up = 120; 34int outside_bb_down = 400; 35int outside_bb_right = 575; 36int middle_line = 322; 37 38int puck_x = 140; 39int puck_y = 250; 40 41int puck_left=puck_x; /* puck bounding box*/ 42int puck_up=puck_y; 43int puck_right=puck_x+width_p; 44int puck_down=puck_y+height_p; 45 46 47void puck(int puck_left, int puck_up, int puck_right, int puck_down, BITMAP *buffer){ 48 49 rectfill( buffer, puck_left , puck_up, puck_right, puck_down, makecol(0, 255, 0));/* puck*/ 50 51} 52 53 54 55void playerone(int p1_x, int p1_y, BITMAP *buffer){ 56 57 textout_ex( buffer, font, "O", p1_x, p1_y, makecol( 255, 0, 0), makecol( 0, 0, 0) ); 58 /* makes a '+' sign and sets the colour as white and to start where x and y are set*/ 59 textout_ex( buffer, font, "O", p1_x, p1_y, makecol( 255, 0, 0), makecol( 0, 0, 0) ); 60 /* create player one's cursor and make him red*/ 61} 62void playertwo(int p2_x, int p2_y, BITMAP *buffer){ 63 64 65 66 textout_ex(buffer, font, "O", p2_x,p2_y , makecol(0, 0, 255), makecol(0, 0, 0)); 67 /* create player two's cursor and make him blue*/ 68 textout_ex(buffer, font, "O", p2_x, p2_y, makecol(0, 0, 255), makecol(0, 0, 0)); 69} 70 71 72int main() 73 74{ 75 76 allegro_init(); 77 install_keyboard(); 78 set_gfx_mode( GFX_AUTODETECT, 640, 480, 0, 0); 79 80 /* makes a '+' sign and sets the colour as white and to go where x and y are set*/ 81 82 83 while ( !key[KEY_ESC] ){ 84 85 clear_keybuf(); 86 87 88 BITMAP *buffer = NULL;/* Declare a BITMAP called buffer.*/ 89 buffer = create_bitmap(640,480); /*Create an empty bitmap the size of the screen*/ 90 91 textout_centre_ex(buffer, font,"Air Hockey" , 320, 100, makecol(255, 0, 255), -1); 92 93 rect( buffer, 60, 120, 580, 400, makecol(255, 255, 255));/*playing field*/ 94 rect( buffer, 40, 230, 60, 290, makecol( 0, 255, 0)); /*player one's net to defend*/ 95 rect( buffer, 580, 230, 600, 290, makecol(0, 255, 0)); /*player two's net to defend*/ 96 line( buffer, 320, 120, 320, 400, makecol( 255, 255, 255));/*half way line*/ 97 circle(buffer, 320, 260, 75, makecol(255, 255, 255));/*center circle*/ 98 /* makes the playing surface*/ 99 /* notice that all this is being drawn to the buffer to stop flickering*/ 100 101 102 playerone(p1_x,p1_y,buffer); 103 if (key[KEY_W]) p1_y-- && p1_bb_T-- && p1_bb_B--; /*up when you press up*/ 104 if (key[KEY_S]) p1_y++ && p1_bb_T++ && p1_bb_B++; /*down when you press down*/ 105 if (key[KEY_D]) p1_x++ && p1_bb_R++ && p1_bb_L++; /*right wehn you press right*/ 106 if (key[KEY_A]) p1_x-- && p1_bb_R-- && p1_bb_L--; /*left when you press ledft*/ 107 108 /* stops playerone leaving the playing field*/ 109 110 111 if (p1_bb_L==outside_bb_left){ 112 p1_bb_L++ && p1_bb_R++ && p1_x++; 113 } 114 if (p1_bb_T==outside_bb_up){ 115 p1_bb_T++ && p1_bb_B++ && p1_y++; 116 } 117 if (p1_bb_B==outside_bb_down){ 118 p1_bb_T-- && p1_bb_B-- && p1_y--; 119 } 120 if (p1_bb_R==middle_line){ 121 p1_bb_L-- && p1_bb_R-- && p1_x--; 122 } 123 124 125 126 127 playertwo(p2_x,p2_y,buffer); 128 if (key[KEY_UP]) p2_y-- && p2_bb_B-- && p2_bb_T--; /*up when you press up*/ 129 if (key[KEY_DOWN]) p2_y++ && p2_bb_B++ && p2_bb_T++; /*down when you press down*/ 130 if (key[KEY_RIGHT]) p2_x++ && p2_bb_L++ && p2_bb_R++ ; /*right wehn you press right*/ 131 if (key[KEY_LEFT]) p2_x-- && p2_bb_L-- && p2_bb_R--; /*left when you press ledft*/ 132 133 /* stops playertwo leaving the playing field*/ 134 135 if (p2_bb_L==outside_bb_right){ 136 p2_bb_L-- && p2_bb_R-- && p2_x--; 137 } 138 if (p2_bb_T==outside_bb_up){ 139 p2_bb_T++ && p2_bb_B++ && p2_y++; 140 } 141 if (p2_bb_B==outside_bb_down){ 142 p2_bb_T-- && p2_bb_B-- && p2_y--; 143 } 144 if (p2_bb_R==middle_line){ 145 p2_bb_L++ && p2_bb_R++ && p2_x++; 146 } 147 148 149 150 151 puck(puck_left,puck_up,puck_right,puck_down,buffer); 152 /* attempts to make the puck move when hit*/ 153 154 155 /* this makes sure the puck doesn't leave the playing field*/ 156 if (puck_right==outside_bb_right){ 157 puck_left--&&puck_right--; 158 } 159 160 if (puck_left==outside_bb_left){ 161 puck_left++&&puck_right++; 162 } 163 164 if (puck_up==outside_bb_up){ 165 puck_up++&&puck_down++; 166 } 167 168 if (puck_down==outside_bb_down){ 169 puck_up--&&puck_down--; 170 } 171 172 /*COLLISION <--- RIGHT HERE*/ 173 /*playerone and the puck*/ 174 if ( 175 ( p1_bb_L <= puck_left && p1_bb_R == puck_left) && 176 ( p1_bb_T >= puck_up && p1_bb_B <=puck_down) 177 ) 178 { 179 puck_left++ && puck_right++; 180 } 181 182 if ( 183 (p1_bb_R >= puck_right && p1_bb_L == puck_right) && 184 (p1_bb_T >= puck_up && p1_bb_B <= puck_down) 185 ) 186 { 187 puck_left-- && puck_right--; 188 } 189 190 if ( 191 (p1_bb_T <= puck_up && p1_bb_B == puck_up) && 192 (p1_bb_L >= puck_left && p1_bb_R <= puck_right) 193 ) 194 { 195 puck_up++ && puck_down++; 196 } 197 198 if ( 199 (p1_bb_T >= puck_up && p1_bb_T == puck_down) && 200 (p1_bb_L >= puck_left && p1_bb_R <= puck_right) 201 ) 202 { 203 puck_up-- && puck_down--; 204 } 205 /*playertwo and the puck*/ 206 207 if ( 208 ( p2_bb_L <= puck_left && p2_bb_R == puck_left) && 209 ( p2_bb_T >= puck_up && p2_bb_B <=puck_down) 210 ) 211 { 212 puck_left++ && puck_right++; 213 } 214 215 if ( 216 (p2_bb_R >= puck_right && p2_bb_L == puck_right) && 217 (p2_bb_T >= puck_up && p2_bb_B <= puck_down) 218 ) 219 { 220 puck_left-- && puck_right--; 221 } 222 223 if ( 224 (p2_bb_T <= puck_up && p2_bb_B == puck_up) && 225 (p2_bb_L >= puck_left && p2_bb_R <= puck_right) 226 ) 227 { 228 puck_up++ && puck_down++; 229 } 230 231 if ( 232 (p2_bb_T >= puck_up && p2_bb_T == puck_down) && 233 (p2_bb_L >= puck_left && p2_bb_R <= puck_right) 234 ) 235 { 236 puck_up-- && puck_down--; 237 } 238 239 /* SHOULD move the puck left and right and up and down when pushed*/ 240 /*END OF COLLISION*/ 241 242 blit(buffer, screen, 0,0,0,0,640,480);/*Draw the buffer to the screen*/ 243 clear(buffer);//Clear the buffer 244 245 rest(15); 246 247 } 248 249 return 0; 250 251} 252END_OF_MAIN()

LennyLen
Member #5,313
December 2004
avatar

Quote:

How do i get a puck to slide across the screen?

Create x-velocity and y-velocity values, then add these to the puck's position each iteration of the loop.

Quote:

How do i do the rebounds off of walls

If the puck hits the left or right wall, reverse the x-velocity. if it hits the top or bottom wall, reverse the y-velocity.

edit: You're creating the buffer BITMAP every time your loop runs. Just create it once, before the loop.

Tobias Dammers
Member #2,604
August 2002
avatar

Quote:

{
puck_up++ && puck_down++;
}

And what the fairy-tale is that supposed to do? You do realize that && is a logical-and operator? The above statement does the following:
1. Increase puck_up by 1.
2. Check if puck_up is zero; if so, exit, else, continue.
3. Increase puck_down by 1.
4. Check if puck_down is zero; if so, evaluate to false, else to true.
5. Throw away the result of the comparison made in 4.
So effectively, if puck_up is -1, it increases only puck_up, otherwise it increases both puck_up and puck_down.

Then, learn what structs are and how to use them. For example, both player paddles essentially do the same thing, so they can both be of the same struct type, like this (assuming C++):

struct PADDLE {
  int x;
  int y;
  int w;
  int h;
};

PADDLE player[2] = {
  { 100, 100, 10, 10 }, // player 1
  { 100, 500, 10, 10 }  // player 2
};

Please learn some C/C++. Pick up a good (e-)book. There are even very good free ones, such as this one.

---
Me make music: Triofobie
---
"We need Tobias and his awesome trombone, too." - Johan Halmén

Kibiz0r
Member #6,203
September 2005
avatar

Quote:

1. Increase puck_up by 1.
2. Check if puck_up is zero; if so, exit, else, continue.
3. Increase puck_down by 1.
4. Check if puck_down is zero; if so, evaluate to false, else to true.
5. Throw away the result of the comparison made in 4.

No.

1. Check if puck_up is zero
2. Increase puck_up by 1
3. Exit if puck_up was zero when 1 was executed, even if it isn't zero now
4. Check if puck_down is zero
5. Increase puck_down by 1
6. Throw away result from 4.

EXTRANEOUS NITPICK ENDED

Albin Engström
Member #8,110
December 2006
avatar

zaviax said:

How do i get a puck to slide across the screen?
How do i do the rebounds off of walls
how do i get it to go in the goal because there isnt a hole in the white bounding box?

here is my source please show what you mena if making a suggestion thanks a lot!

Some tips from someone who's also learning from these great masters:

1. Don't forget to meditate some on the things the people of this forum are >giving< you.

2. NEVER post your code and ask people to write big things for you, posting code that doesn't work is sometimes a must, but letting other people do your work (and don't even see tip 1) wont get you anywhere unless you don't care about 'cheating' as long as your friends think you did it. What you asked for is pretty much the whole game..

3. there is no tip number 3.

oh: a tip to myself: read what people say before posting "great" tips... ^^

Have a nice day. Albin out.

LennyLen
Member #5,313
December 2004
avatar

zavirax:

Quote:

void playerone(int p1_x, int p1_y, BITMAP *buffer){
  
  textout_ex( buffer, font, "O", p1_x, p1_y, makecol( 255, 0, 0), makecol( 0, 0, 0) );
  /* makes a '+' sign and sets the colour as white and to start where x and y are set*/
  textout_ex( buffer, font, "O", p1_x, p1_y, makecol( 255, 0, 0), makecol( 0, 0, 0) );
  /* create player one's cursor and make him red*/
}

Why do you have two identical textout_ex() calls following each other? The same applies for the playertwo() function.

Quote:

    if (
      ( p1_bb_L <= puck_left && p1_bb_R == puck_left) &&
      ( p1_bb_T >= puck_up && p1_bb_B <=puck_down)
    )

If, at later stages of development, you decide to move either the players or the puck at more than 1 pixel per loop, the type of collision detection you do above will no longer be effective. If player 1 is moving right towards the puck at say 3 pixels per loop, it could go from being 1 pixel to the left of the leftmost side puck to 2 pixels to the right of the leftmost side. The collision check would fail and the player would pass through the puck.

The following collision check works no matter how fast objects are traveling:

if (p1_bb_L <= puck_right && p1_bb_T <= puck_down && p1_bb_R >= puck_left && p1_bb_B >= puck_up)
This won't tell you what side the collision occured from however. But you can determine the result of the collision based on the velocities of the objects colliding.

Quote:

puck_left++ && puck_right++;

Even if this did what you thought it did, it's better to change puck_x and puck_y rather than the position of the bounding box.

Quote:

int puck_left=puck_x;  /* puck bounding box*/
int puck_up=puck_y;
int puck_right=puck_x+width_p;
int puck_down=puck_y+height_p;

I would #define those values instead of making them variables. Now, you only need to keep track of the puck_x and puck_y values, and the bounding box values will be updated automatically.

Those who know these things:

Quote:

    textout_centre_ex(buffer, font,"Air Hockey" , 320, 100, makecol(255, 0, 255), -1);

    rect( buffer, 60, 120, 580, 400, makecol(255, 255, 255));/*playing field*/
    rect( buffer, 40, 230, 60, 290, makecol( 0, 255, 0));    /*player one's net to defend*/
    rect( buffer, 580, 230, 600, 290, makecol(0, 255, 0));   /*player two's net to defend*/
    line( buffer, 320, 120, 320, 400, makecol( 255, 255, 255));/*half way line*/
    circle(buffer, 320, 260, 75, makecol(255, 255, 255));/*center circle*/

Would it make an improvement to speed if, instead of doing these drawing routines every redraw, they'd been prerendered to a bitmap which would then be blitted to the buffer before other object were drawn?

Three Harris
Member #6,226
September 2005

LennyLen said:

edit: You're creating the buffer BITMAP every time your loop runs. Just create it once, before the loop.

I told him of that on his last identical post.

Quote:

Would it make an improvement to speed if, instead of doing these drawing routines every redraw, they'd been prerendered to a bitmap which would then be blitted to the buffer before other object were drawn?

dam nice point.

zavirax
Member #8,634
May 2007

oculd you give an example of how i would do the velocity please and how to reverse it...i changed the buffer positioning and now it works properly thanks i wondered why it still flickered.

gnolam
Member #2,030
March 2002
avatar

float x, y;
float xv, yv;

// move
x += xv;
y += yv;

I'll leave reversing a direction as an exercise to the reader. :P

--
Move to the Democratic People's Republic of Vivendi Universal (formerly known as Sweden) - officially democracy- and privacy-free since 2008-06-18!

Aaron Whipple
Member #4,001
November 2003
avatar

I really don't want to be rude, but it is apparent this this game is far too advanced for you. Try making some simple programs for now, like moving objects across the screen. This is far too many people wasting their time to help you create something that you wont even understand if you apply everything everyone is telling you. Again, I'm not trying to be mean. I'm just trying to steer you in a direction which is more likely to improve your understanding on programming. If you work on the basics, I'm certain you can do a project like this in no time.

Paul whoknows
Member #5,081
September 2004
avatar

Learn how to use vectors, that's the key!:)

Quote:

Try making some simple programs for now,

Please, not another tetris and/or pong clone!

____

"The unlimited potential has been replaced by the concrete reality of what I programmed today." - Jordan Mechner.

Johan Halmén
Member #1,550
September 2001

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Years of thorough research have revealed that the red "x" that closes a window, really isn't red, but white on red background.

Years of thorough research have revealed that what people find beautiful about the Mandelbrot set is not the set itself, but all the rest.

Go to: