Hi, Can anyone please read through the source code of my program here?:
| 1 | #include <allegro.h> |
| 2 | #include <stdio.h> |
| 3 | |
| 4 | int x = 100; /* sets the position of the + sign(x-axis) */ |
| 5 | int y = 260; /*(y-axis)*/ |
| 6 | |
| 7 | int a = 320; /* for the puck*/ |
| 8 | int b = 100; |
| 9 | |
| 10 | int c = 520; /* playertwo*/ |
| 11 | int d = 260; |
| 12 | |
| 13 | void playerone(int x, int y, BITMAP *buffer){ |
| 14 | |
| 15 | textout_ex( buffer, font, "+", x, y, makecol( 255, 0, 0), makecol( 0, 0, 0) ); |
| 16 | /* makes a '+' sign and sets the colour as white and to start where x and y are set*/ |
| 17 | textout_ex( buffer, font, "+", x, y, makecol( 255, 0, 0), makecol( 0, 0, 0) ); |
| 18 | /* create player one's cursor and make him red*/ |
| 19 | } |
| 20 | void playertwo(int c, int d, BITMAP *buffer){ |
| 21 | |
| 22 | textout_ex(buffer, font, "+",c ,d , makecol(0, 0, 255), makecol(0, 0, 0)); |
| 23 | /* create player two's cursor and make him blue*/ |
| 24 | textout_ex(buffer, font, "+", c, d, makecol(0, 0, 255), makecol(0, 0, 0)); |
| 25 | } |
| 26 | int main() |
| 27 | |
| 28 | { |
| 29 | |
| 30 | allegro_init(); |
| 31 | install_keyboard(); |
| 32 | set_gfx_mode( GFX_AUTODETECT, 640, 480, 0, 0); |
| 33 | |
| 34 | while ( !key[KEY_ESC] ){ |
| 35 | |
| 36 | clear_keybuf(); |
| 37 | |
| 38 | //acquire_screen(); |
| 39 | |
| 40 | BITMAP *buffer = NULL;/* Declare a BITMAP called buffer.*/ |
| 41 | buffer = create_bitmap(640,480); /*Create an empty bitmap the size of the screen*/ |
| 42 | |
| 43 | playerone(x,y,buffer); |
| 44 | if (key[KEY_W]) --y; /*up when you press up*/ |
| 45 | if (key[KEY_S]) ++y; /*down when you press down*/ |
| 46 | if (key[KEY_D]) ++x; /*right when you press right*/ |
| 47 | if (key[KEY_A]) --x; /*left when you press Left*/ |
| 48 | playertwo(c,d,buffer); |
| 49 | if (key[KEY_UP]) --d; /*up when you press up*/ |
| 50 | if (key[KEY_DOWN]) ++d; /*down when you press down*/ |
| 51 | if (key[KEY_RIGHT]) ++c; /*right when you press right*/ |
| 52 | if (key[KEY_LEFT]) --c; /*left when you press left*/ |
| 53 | |
| 54 | |
| 55 | |
| 56 | /* makes a '+' sign and sets the colour as white and to go where x and y are set*/ |
| 57 | textout_centre_ex(buffer, font,"Air Hockey" , a, b, makecol(255, 0, 255), -1); |
| 58 | //release_screen(); |
| 59 | |
| 60 | |
| 61 | circlefill( buffer, 140, 260, 5, makecol(255, 255, 255));/* puck*/ |
| 62 | rect( buffer, 60, 120, 580, 400, makecol(255, 255, 255));/*playing field*/ |
| 63 | rect( buffer, 40, 230, 60, 290, makecol( 0, 255, 0)); /*player one's net to defend*/ |
| 64 | rect( buffer, 580, 230, 600, 290, makecol(0, 255, 0)); /*player two's net to defend*/ |
| 65 | line( buffer, 320, 120, 320, 400, makecol( 255, 255, 255));/*half way line*/ |
| 66 | circle(buffer, 320, 260, 50, makecol(255, 255, 255));/*center circle*/ |
| 67 | /* makes the playing surface*/ |
| 68 | /* notice that all this is being drawn to the buffer to stop flickering*/ |
| 69 | |
| 70 | blit(buffer, screen, 0,0,0,0,640,480);/*Draw the buffer to the screen*/ |
| 71 | |
| 72 | rest(12); |
| 73 | |
| 74 | } |
| 75 | |
| 76 | return 0; |
| 77 | |
| 78 | } |
| 79 | END_OF_MAIN(); |
Sorry that It's a mess I'm very new to it. I'm trying to crate air hockey, but I'm stuck at getting things to collide. Can i have suggestions please. NOTICE: My Playerone and Playertwo are not images they are text essentially. '+'
Thanks guys.;D
Generally, when pasting source code its nice to encapsulate them in [code] tags. Like so:
<code> void foobar(int); int var = 5; foobar(5 * 2); </code>
I said I was a noob. Thanks but how does this help my code????
Because then people will actually read your code and possibly even try to help.
Because then people will actually read your code and possibly even try to help.
He's right, I really don't want to read it myself. Fix it up and I might take a look. Yeah, I know, it's not really nice, but I just can't stand to look at that conglomeration above...
There's no point at the moment of doing any collision detection - you're writing the line "Air Hockey" where the puck should be, and drawing the puck at a fixed position.
Once you get round to the collision detection, have a search for bounding boxes.
You should draw the puck at the end, after the rest of the playing field has been drawn. Make sure you are drawing stuff in the right order (z-ordering).
ok ill try to fix it up, Here's it now i know it's not perfect:
| 1 | #include <allegro.h> |
| 2 | #include <stdio.h> |
| 3 | |
| 4 | int x = 100; /* sets the position of the + sign(x-axis) */ |
| 5 | int y = 260; /*(y-axis)*/ |
| 6 | |
| 7 | int a = 140; /* for the puck*/ |
| 8 | int b = 250; |
| 9 | int c = 150; |
| 10 | int d = 260; |
| 11 | |
| 12 | int e = 520; /* playertwo*/ |
| 13 | int f = 290; |
| 14 | |
| 15 | int outside_bb_left = 60;/* playing field boundong box*/ |
| 16 | int outside_bb_up = 120; |
| 17 | int outside_bb_down = 395; |
| 18 | int outside_bb_right = 575; |
| 19 | int middle_line = 316; |
| 20 | |
| 21 | int puck_left=a;/* puck bounding box*/ |
| 22 | int puck_up=b; |
| 23 | int puck_right=c; |
| 24 | int puck_down=d; |
| 25 | |
| 26 | |
| 27 | |
| 28 | |
| 29 | void puck(int a, int b, int c, int d, BITMAP *buffer){ |
| 30 | |
| 31 | rectfill( buffer, a , b,c ,d , makecol(0, 255, 0));/* puck*/ |
| 32 | |
| 33 | } |
| 34 | |
| 35 | |
| 36 | |
| 37 | void playerone(int x, int y, BITMAP *buffer){ |
| 38 | |
| 39 | textout_ex( buffer, font, "+", x, y, makecol( 255, 0, 0), makecol( 0, 0, 0) ); |
| 40 | /* makes a '+' sign and sets the colour as white and to start where x and y are set*/ |
| 41 | textout_ex( buffer, font, "+", x, y, makecol( 255, 0, 0), makecol( 0, 0, 0) ); |
| 42 | /* create player one's cursor and make him red*/ |
| 43 | } |
| 44 | void playertwo(int e, int f, BITMAP *buffer){ |
| 45 | |
| 46 | textout_ex(buffer, font, "+",e ,f , makecol(0, 0, 255), makecol(0, 0, 0)); |
| 47 | /* create player two's cursor and make him blue*/ |
| 48 | textout_ex(buffer, font, "+", e, f, makecol(0, 0, 255), makecol(0, 0, 0)); |
| 49 | } |
| 50 | |
| 51 | |
| 52 | int main() |
| 53 | |
| 54 | { |
| 55 | |
| 56 | allegro_init(); |
| 57 | install_keyboard(); |
| 58 | set_gfx_mode( GFX_AUTODETECT, 640, 480, 0, 0); |
| 59 | |
| 60 | /* makes a '+' sign and sets the colour as white and to go where x and y are set*/ |
| 61 | |
| 62 | |
| 63 | while ( !key[KEY_ESC] ){ |
| 64 | |
| 65 | clear_keybuf(); |
| 66 | |
| 67 | |
| 68 | BITMAP *buffer = NULL;/* Declare a BITMAP called buffer.*/ |
| 69 | buffer = create_bitmap(640,480); /*Create an empty bitmap the size of the screen*/ |
| 70 | |
| 71 | textout_centre_ex(buffer, font,"Air Hockey" , 320, 100, makecol(255, 0, 255), -1); |
| 72 | //release_screen(); |
| 73 | |
| 74 | rect( buffer, 60, 120, 580, 400, makecol(255, 255, 255));/*playing field*/ |
| 75 | rect( buffer, 40, 230, 60, 290, makecol( 0, 255, 0)); /*player one's net to defend*/ |
| 76 | rect( buffer, 580, 230, 600, 290, makecol(0, 255, 0)); /*player two's net to defend*/ |
| 77 | line( buffer, 320, 120, 320, 400, makecol( 255, 255, 255));/*half way line*/ |
| 78 | circle(buffer, 320, 260, 75, makecol(255, 255, 255));/*center circle*/ |
| 79 | /* makes the playing surface*/ |
| 80 | /* notice that all this is being drawn to the buffer to stop flickering*/ |
| 81 | |
| 82 | |
| 83 | |
| 84 | playerone(x,y,buffer); |
| 85 | if (key[KEY_W]) --y; /*up when you press up*/ |
| 86 | if (key[KEY_S]) ++y; /*down when you press down*/ |
| 87 | if (key[KEY_D]) ++x; /*right wehn you press right*/ |
| 88 | if (key[KEY_A]) --x; /*left when you press ledft*/ |
| 89 | |
| 90 | /* stops playerone leaving the playing field*/ |
| 91 | |
| 92 | |
| 93 | if (x==outside_bb_left){ |
| 94 | x++; |
| 95 | } |
| 96 | |
| 97 | if (y==outside_bb_up){ |
| 98 | y++; |
| 99 | } |
| 100 | if (y==outside_bb_down){ |
| 101 | y--; |
| 102 | } |
| 103 | if (x==middle_line){ |
| 104 | x--; |
| 105 | } |
| 106 | |
| 107 | |
| 108 | |
| 109 | |
| 110 | |
| 111 | |
| 112 | playertwo(e,f,buffer); |
| 113 | if (key[KEY_UP]) --f; /*up when you press up*/ |
| 114 | if (key[KEY_DOWN]) ++f; /*down when you press down*/ |
| 115 | if (key[KEY_RIGHT]) ++e; /*right wehn you press right*/ |
| 116 | if (key[KEY_LEFT]) --e; /*left when you press ledft*/ |
| 117 | |
| 118 | /* stops playertwo leaving the playing field*/ |
| 119 | |
| 120 | if(e==middle_line){ |
| 121 | e++; |
| 122 | } |
| 123 | |
| 124 | if(f==outside_bb_up){ |
| 125 | f++; |
| 126 | } |
| 127 | |
| 128 | if(f==outside_bb_down){ |
| 129 | f--; |
| 130 | } |
| 131 | |
| 132 | if(e==outside_bb_right){ |
| 133 | e--; |
| 134 | } |
| 135 | |
| 136 | |
| 137 | |
| 138 | |
| 139 | |
| 140 | puck(a,b,c,d,buffer); |
| 141 | /* attempts tp make the puck move when hit*/ |
| 142 | |
| 143 | |
| 144 | /* this makes sure the puck doesn't leave the playing field*/ |
| 145 | if (a==outside_bb_right){ |
| 146 | a--; |
| 147 | } |
| 148 | |
| 149 | if (b==outside_bb_left){ |
| 150 | a++; |
| 151 | } |
| 152 | |
| 153 | if (c==outside_bb_up){ |
| 154 | b++; |
| 155 | } |
| 156 | |
| 157 | if (d==outside_bb_down){ |
| 158 | b--; |
| 159 | } |
| 160 | |
| 161 | |
| 162 | if (x==a&&x<c&&y<=b&&y>=d){ |
| 163 | a++&&c++; |
| 164 | } |
| 165 | if (x<=c&&x>a&&y<=d&&y>b){ |
| 166 | a--&&c--; |
| 167 | } |
| 168 | |
| 169 | |
| 170 | |
| 171 | blit(buffer, screen, 0,0,0,0,640,480);/*Draw the buffer to the screen*/ |
| 172 | |
| 173 | rest(12); |
| 174 | |
| 175 | } |
| 176 | |
| 177 | return 0; |
| 178 | |
| 179 | } |
| 180 | END_OF_MAIN(); |
i changed the puck to a square just for easiness. My problem now is getting it to move right( Haven't done the up and down...it should move right and left(it only goes left)) thanks guys
Edit your post before someone sees it
. they've told you already: USE CODE TAGS.
Here's how you do it(again):
crap, i can't make an example but many has already..
it will look like this:
| 1 | |
| 2 | #include <allegro.h> |
| 3 | #include <stdio.h> |
| 4 | |
| 5 | int x = 100; /* sets the position of the + sign(x-axis) */ |
| 6 | int y = 260; /*(y-axis)*/ |
| 7 | |
| 8 | int a = 140; /* for the puck*/ |
| 9 | int b = 250; |
| 10 | int c = 150; |
| 11 | int d = 260; |
| 12 | |
| 13 | int e = 520; /* playertwo*/ |
| 14 | int f = 290; |
| 15 | |
| 16 | int outside_bb_left = 60;/* playing field boundong box*/ |
| 17 | int outside_bb_up = 120; |
| 18 | int outside_bb_down = 395; |
| 19 | int outside_bb_right = 575; |
| 20 | int middle_line = 316; |
| 21 | |
| 22 | int puck_left=a;/* puck bounding box*/ |
| 23 | int puck_up=b; |
| 24 | int puck_right=c; |
| 25 | int puck_down=d; |
| 26 | |
| 27 | |
| 28 | |
| 29 | |
| 30 | void puck(int a, int b, int c, int d, BITMAP *buffer){ |
| 31 | |
| 32 | rectfill( buffer, a , b,c ,d , makecol(0, 255, 0));/* puck*/ |
| 33 | |
| 34 | } |
| 35 | |
| 36 | |
| 37 | |
| 38 | void playerone(int x, int y, BITMAP *buffer){ |
| 39 | |
| 40 | textout_ex( buffer, font, "+", x, y, makecol( 255, 0, 0), makecol( 0, 0, 0) ); |
| 41 | /* makes a '+' sign and sets the colour as white and to start where x and y are set*/ |
| 42 | textout_ex( buffer, font, "+", x, y, makecol( 255, 0, 0), makecol( 0, 0, 0) ); |
| 43 | /* create player one's cursor and make him red*/ |
| 44 | } |
| 45 | void playertwo(int e, int f, BITMAP *buffer){ |
| 46 | |
| 47 | textout_ex(buffer, font, "+",e ,f , makecol(0, 0, 255), makecol(0, 0, 0)); |
| 48 | /* create player two's cursor and make him blue*/ |
| 49 | textout_ex(buffer, font, "+", e, f, makecol(0, 0, 255), makecol(0, 0, 0)); |
| 50 | } |
| 51 | |
| 52 | |
| 53 | int main() |
| 54 | |
| 55 | { |
| 56 | |
| 57 | allegro_init(); |
| 58 | install_keyboard(); |
| 59 | set_gfx_mode( GFX_AUTODETECT, 640, 480, 0, 0); |
| 60 | |
| 61 | /* makes a '+' sign and sets the colour as white and to go where x and y are set*/ |
| 62 | |
| 63 | |
| 64 | while ( !key[KEY_ESC] ){ |
| 65 | |
| 66 | clear_keybuf(); |
| 67 | |
| 68 | |
| 69 | BITMAP *buffer = NULL;/* Declare a BITMAP called buffer.*/ |
| 70 | buffer = create_bitmap(640,480); /*Create an empty bitmap the size of the screen*/ |
| 71 | |
| 72 | textout_centre_ex(buffer, font,"Air Hockey" , 320, 100, makecol(255, 0, 255), -1); |
| 73 | //release_screen(); |
| 74 | |
| 75 | rect( buffer, 60, 120, 580, 400, makecol(255, 255, 255));/*playing field*/ |
| 76 | rect( buffer, 40, 230, 60, 290, makecol( 0, 255, 0)); /*player one's net to defend*/ |
| 77 | rect( buffer, 580, 230, 600, 290, makecol(0, 255, 0)); /*player two's net to defend*/ |
| 78 | line( buffer, 320, 120, 320, 400, makecol( 255, 255, 255));/*half way line*/ |
| 79 | circle(buffer, 320, 260, 75, makecol(255, 255, 255));/*center circle*/ |
| 80 | /* makes the playing surface*/ |
| 81 | /* notice that all this is being drawn to the buffer to stop flickering*/ |
| 82 | |
| 83 | |
| 84 | |
| 85 | playerone(x,y,buffer); |
| 86 | if (key[KEY_W]) --y; /*up when you press up*/ |
| 87 | if (key[KEY_S]) ++y; /*down when you press down*/ |
| 88 | if (key[KEY_D]) ++x; /*right wehn you press right*/ |
| 89 | if (key[KEY_A]) --x; /*left when you press ledft*/ |
| 90 | |
| 91 | /* stops playerone leaving the playing field*/ |
| 92 | |
| 93 | |
| 94 | if (x==outside_bb_left){ |
| 95 | x++; |
| 96 | } |
| 97 | |
| 98 | if (y==outside_bb_up){ |
| 99 | y++; |
| 100 | } |
| 101 | if (y==outside_bb_down){ |
| 102 | y--; |
| 103 | } |
| 104 | if (x==middle_line){ |
| 105 | x--; |
| 106 | } |
| 107 | |
| 108 | |
| 109 | |
| 110 | |
| 111 | |
| 112 | |
| 113 | playertwo(e,f,buffer); |
| 114 | if (key[KEY_UP]) --f; /*up when you press up*/ |
| 115 | if (key[KEY_DOWN]) ++f; /*down when you press down*/ |
| 116 | if (key[KEY_RIGHT]) ++e; /*right wehn you press right*/ |
| 117 | if (key[KEY_LEFT]) --e; /*left when you press ledft*/ |
| 118 | |
| 119 | /* stops playertwo leaving the playing field*/ |
| 120 | |
| 121 | if(e==middle_line){ |
| 122 | e++; |
| 123 | } |
| 124 | |
| 125 | if(f==outside_bb_up){ |
| 126 | f++; |
| 127 | } |
| 128 | |
| 129 | if(f==outside_bb_down){ |
| 130 | f--; |
| 131 | } |
| 132 | |
| 133 | if(e==outside_bb_right){ |
| 134 | e--; |
| 135 | } |
| 136 | |
| 137 | |
| 138 | |
| 139 | |
| 140 | |
| 141 | puck(a,b,c,d,buffer); |
| 142 | /* attempts tp make the puck move when hit*/ |
| 143 | |
| 144 | |
| 145 | /* this makes sure the puck doesn't leave the playing field*/ |
| 146 | if (a==outside_bb_right){ |
| 147 | a--; |
| 148 | } |
| 149 | |
| 150 | if (b==outside_bb_left){ |
| 151 | a++; |
| 152 | } |
| 153 | |
| 154 | if (c==outside_bb_up){ |
| 155 | b++; |
| 156 | } |
| 157 | |
| 158 | if (d==outside_bb_down){ |
| 159 | b--; |
| 160 | } |
| 161 | |
| 162 | |
| 163 | if (x==a&&x<c&&y<=b&&y>=d){ |
| 164 | a++&&c++; |
| 165 | } |
| 166 | if (x<=c&&x>a&&y<=d&&y>b){ |
| 167 | a--&&c--; |
| 168 | } |
| 169 | |
| 170 | |
| 171 | |
| 172 | blit(buffer, screen, 0,0,0,0,640,480);/*Draw the buffer to the screen*/ |
| 173 | |
| 174 | rest(12); |
| 175 | |
| 176 | } |
| 177 | |
| 178 | return 0; |
| 179 | |
| 180 | } |
| 181 | END_OF_MAIN(); |
Be happy
Use code tags, indent your code. Use braces for each and every code block, even if it's a single line.
Create a struct to hold your player data.
Create an array of this struct so you can easily add more players (and even more important: you don't have to duplicate your code).
Get rid of the ";" after END_OF_MAIN
Oh, and use speaking variable names.
I'll say it too - there are posting protocols. Ignoring this advice is breaking one of them. Please update your post and use the [code]...[/code] tags.
I'll help this time, but no more if you don't use tags.
1. Use meaningful variable names. It takes ages to work out and remember what a, b, c... x, y are all for. Use names like xPuck, yPuck, xPlayer1, yPlayer1, xPlayer2 etc. It makes the whole thing easier to understand. [edit]Or, as spellcaster pointed out, use structs with meaningful names[/edit]
2. The line (x==a&&x<c&&y<=b&&y>=d) is wrong, and is why the puck won't go right. There is never a situation when the player position will be above and below the puck at the same time, and x==a is not the test condition you want to perform.
How do i do the code tags again?
Is it ... at the start and end respectively?
Also, I'll change the variables.
ALSO, I dont know how to do 'structs', sorry I'm such a noob.
thanks for putting up with me
umm Let's try this: (if you can't read it tell me what to improve again...thanks for the help again)
| 1 | |
| 2 | #include <allegro.h> |
| 3 | #include <stdio.h> |
| 4 | |
| 5 | int p1_x = 100; /* sets the position of the + sign(x-axis) */ |
| 6 | int p1_y = 260; /*(y-axis)*/ |
| 7 | |
| 8 | |
| 9 | int p2_x = 520; /* playertwo*/ |
| 10 | int p2_y = 290; |
| 11 | |
| 12 | int outside_bb_left = 60;/* playing field boundong box*/ |
| 13 | int outside_bb_up = 120; |
| 14 | int outside_bb_down = 395; |
| 15 | int outside_bb_right = 575; |
| 16 | int middle_line = 316; |
| 17 | |
| 18 | int puck_left=140;/* puck bounding box*/ |
| 19 | int puck_up=250; |
| 20 | int puck_right=150; |
| 21 | int puck_down=260; |
| 22 | |
| 23 | |
| 24 | void puck(int puck_left, int puck_up, int puck_right, int puck_down, BITMAP *buffer){ |
| 25 | |
| 26 | rectfill( buffer, puck_left , puck_up, puck_right, puck_down, makecol(0, 255, 0));/* puck*/ |
| 27 | |
| 28 | } |
| 29 | |
| 30 | |
| 31 | |
| 32 | void playerone(int p1_x, int p1_y, BITMAP *buffer){ |
| 33 | |
| 34 | textout_ex( buffer, font, "+", p1_x, p1_y, makecol( 255, 0, 0), makecol( 0, 0, 0) ); |
| 35 | /* makes a '+' sign and sets the colour as white and to start where x and y are set*/ |
| 36 | textout_ex( buffer, font, "+", p1_x, p1_y, makecol( 255, 0, 0), makecol( 0, 0, 0) ); |
| 37 | /* create player one's cursor and make him red*/ |
| 38 | } |
| 39 | void playertwo(int p2_x, int p2_y, BITMAP *buffer){ |
| 40 | |
| 41 | textout_ex(buffer, font, "+", p2_x,p2_y , makecol(0, 0, 255), makecol(0, 0, 0)); |
| 42 | /* create player two's cursor and make him blue*/ |
| 43 | textout_ex(buffer, font, "+", p2_x, p2_y, makecol(0, 0, 255), makecol(0, 0, 0)); |
| 44 | } |
| 45 | |
| 46 | |
| 47 | int main() |
| 48 | |
| 49 | { |
| 50 | |
| 51 | allegro_init(); |
| 52 | install_keyboard(); |
| 53 | set_gfx_mode( GFX_AUTODETECT, 640, 480, 0, 0); |
| 54 | |
| 55 | /* makes a '+' sign and sets the colour as white and to go where x and y are set*/ |
| 56 | |
| 57 | |
| 58 | while ( !key[KEY_ESC] ){ |
| 59 | |
| 60 | clear_keybuf(); |
| 61 | |
| 62 | |
| 63 | BITMAP *buffer = NULL;/* Declare a BITMAP called buffer.*/ |
| 64 | buffer = create_bitmap(640,480); /*Create an empty bitmap the size of the screen*/ |
| 65 | |
| 66 | textout_centre_ex(buffer, font,"Air Hockey" , 320, 100, makecol(255, 0, 255), -1); |
| 67 | |
| 68 | rect( buffer, 60, 120, 580, 400, makecol(255, 255, 255));/*playing field*/ |
| 69 | rect( buffer, 40, 230, 60, 290, makecol( 0, 255, 0)); /*player one's net to defend*/ |
| 70 | rect( buffer, 580, 230, 600, 290, makecol(0, 255, 0)); /*player two's net to defend*/ |
| 71 | line( buffer, 320, 120, 320, 400, makecol( 255, 255, 255));/*half way line*/ |
| 72 | circle(buffer, 320, 260, 75, makecol(255, 255, 255));/*center circle*/ |
| 73 | /* makes the playing surface*/ |
| 74 | /* notice that all this is being drawn to the buffer to stop flickering*/ |
| 75 | |
| 76 | |
| 77 | playerone(p1_x,p1_y,buffer); |
| 78 | if (key[KEY_W]) --p1_y; /*up when you press up*/ |
| 79 | if (key[KEY_S]) ++p1_y; /*down when you press down*/ |
| 80 | if (key[KEY_D]) ++p1_x; /*right wehn you press right*/ |
| 81 | if (key[KEY_A]) --p1_x; /*left when you press ledft*/ |
| 82 | |
| 83 | /* stops playerone leaving the playing field*/ |
| 84 | |
| 85 | |
| 86 | if (p1_x==outside_bb_left){ |
| 87 | p1_x++; |
| 88 | } |
| 89 | if (p1_y==outside_bb_up){ |
| 90 | p1_y++; |
| 91 | } |
| 92 | if (p1_y==outside_bb_down){ |
| 93 | p1_y--; |
| 94 | } |
| 95 | if (p1_x==middle_line){ |
| 96 | p1_x--; |
| 97 | } |
| 98 | |
| 99 | |
| 100 | |
| 101 | |
| 102 | |
| 103 | |
| 104 | playertwo(p2_x,p2_y,buffer); |
| 105 | if (key[KEY_UP]) p2_y--; /*up when you press up*/ |
| 106 | if (key[KEY_DOWN]) p2_y++; /*down when you press down*/ |
| 107 | if (key[KEY_RIGHT]) p2_x++; /*right wehn you press right*/ |
| 108 | if (key[KEY_LEFT]) p2_x--; /*left when you press ledft*/ |
| 109 | |
| 110 | /* stops playertwo leaving the playing field*/ |
| 111 | |
| 112 | if(p2_x==middle_line){ |
| 113 | p2_x++; |
| 114 | } |
| 115 | |
| 116 | if(p2_y==outside_bb_up){ |
| 117 | p2_y++; |
| 118 | } |
| 119 | |
| 120 | if(p2_y==outside_bb_down){ |
| 121 | p2_y--; |
| 122 | } |
| 123 | |
| 124 | if(p2_x==outside_bb_right){ |
| 125 | p2_x--; |
| 126 | } |
| 127 | |
| 128 | |
| 129 | |
| 130 | |
| 131 | |
| 132 | puck(puck_left,puck_up,puck_right,puck_down,buffer); |
| 133 | /* attempts to make the puck move when hit*/ |
| 134 | |
| 135 | |
| 136 | /* this makes sure the puck doesn't leave the playing field*/ |
| 137 | if (puck_right==outside_bb_right){ |
| 138 | puck_left--&&puck_right--; |
| 139 | } |
| 140 | |
| 141 | if (puck_left==outside_bb_left){ |
| 142 | puck_left++&&puck_right++; |
| 143 | } |
| 144 | |
| 145 | if (puck_up==outside_bb_up){ |
| 146 | puck_up++&&puck_down++; |
| 147 | } |
| 148 | |
| 149 | if (puck_down==outside_bb_down){ |
| 150 | puck_up--&&puck_down--; |
| 151 | } |
| 152 | |
| 153 | |
| 154 | if (p1_x==puck_right&&p1_y<=puck_up&&p1_y>=puck_down){ |
| 155 | puck_left++&&puck_right++; |
| 156 | } |
| 157 | if (p1_x==puck_left&&p1_y<=puck_up&&p1_y>=puck_down){ |
| 158 | puck_left--&&puck_right--; |
| 159 | } |
| 160 | |
| 161 | |
| 162 | |
| 163 | blit(buffer, screen, 0,0,0,0,640,480);/*Draw the buffer to the screen*/ |
| 164 | |
| 165 | rest(15); |
| 166 | |
| 167 | } |
| 168 | |
| 169 | return 0; |
| 170 | |
| 171 | } |
| 172 | END_OF_MAIN() |
A much nicer view
.
btw, what's the problem?
ran your game but i don't know what should work and not..
Well what It Is, Is that I cant get the puck to move the way i want so i don't know what i should do..can't find any help anywhere except here. By the way im on a deadline, this is a school course...I'm dumb!.:)
...I'm dumb!:)
A happy one.::)
Well what It Is, Is that I cant get the puck to move the way i want so i don't know what i should do..can't find any help anywhere except here. By the way im on a deadline, this is a school course...I'm dumb!:)
Here is an allegro pong tutorial, which you can adapt to 4 way movement and collision detection.
Sorry but, Im not sure what i should do with it...is there nothing you would suggest for my code?
I suggest you don't create the buffer in the main loop and not destroy it. That is a massive memory leak.
but I'm stuck at getting things to collide
Just use this:
inline bool collide(int x1, int y1, int x2, int y2, int xx1, int yy1, int xx2, int yy2) { if (x2 <= xx1) return false; else if (x1 >= xx2) return false; else if (y2 <= yy1) return false; else if (y1 >= yy2) return false; else return true; }
Sorry but, I'm not sure what i should do with it...
Sorry, my bad - the page I linked to and my suggestions didn't match (there is a link to the pong tutorial down that page if you looked). Here is the correct link to the pong tutorial.
Yeah I already found It on there, don't worry about It.
btw how would i use that boolean expression thing, could you show in my code please. thanks
Here's my code so far i can push around...any ideas how to make it slide so i only have to hit it not push ... also how would i get it rebound of the sides ... also how can i make a hole in the outside of the field so that it can go in the goal... and also ... however i think i know how, how can make a scoreboard?
lots of questions hehe sorry to be such a stupid person.