can some please help me i got a problem with a this program( I'm not very experience with allegro)
every time i shoot a bullet(while bulleet==true) and collide with a black wall the screen goes flickering ,tryed many things but have failed??? here's the code just get any maze with black walls and name it maze1.bmp
#include <allegro.h>
#include <iostream.h>
BITMAP *maze1,*bullet,*laser;
BITMAP *test;
int x=7;
int y=0;
int color,color2,color3;
int face;
int bx=7;
int by=0;
bool bulleet;
void prosses()
{
if(bulleet==true)
{
clear(bullet);
if(face==1)bx+=2; color2=getpixel(maze1,bx+1,by);
if(face==2)bx-=2; color2=getpixel(maze1,bx-1,by);
if(face==3)by-=2; color2=getpixel(maze1,bx,by-1);
if(face==4)by+=2; color2=getpixel(maze1,bx,by+1);
if(color2==0){bx=x; by=y; clear(bullet); bulleet=false; goto end;}
blit(test, bullet, 0, 0, 0, 0, test->w, test->h);
circlefill(bullet,bx,by,2,makecol( 255, 255, 0 ));
blit(bullet, screen, 0, 0, 0, 0, test->w, test->h);
}
end:;
}
int drawmaze1()
{
maze1 = load_bitmap( "maze1.bmp", NULL);
circlefill(test,x,y,8,makecol( 255, 255, 0 ));
draw_sprite( screen, maze1, 0, 0);
}
END_OF_FUNCTION(drawmaze1);
void setup()
{
drawmaze1();
}
END_OF_FUNCTION(setup);
void input()
{
if(key[KEY_RIGHT])
{
if(bulleet==true)
{
prosses();
}
clear(test);
color=getpixel(maze1,x+8,y);
if(color!=0)
{
if(bulleet==true){x++;}
else
{
x++;
bx++;
}
next:
blit(maze1, test, 0, 0, 0, 0, test->w, test->h);
rest(10);
circlefill(test,x,y,7,makecol( 255, 255, 0 ));
if(bulleet==false)face=1;
blit(test, screen, 0, 0, 0, 0, test->w, test->h);
}
if(bulleet==true)
{
prosses();
}
}
if(key[KEY_LEFT])
{
if(bulleet==true)
{
prosses();
}
clear(test);
color=getpixel(maze1,x-8,y);
if(color==0)goto end;
if(bulleet==true){x--;}
else
{
x--;
bx--;
}
next2:
blit(maze1, test, 0, 0, 0, 0, maze1->w, maze1->h);
rest(10);
circlefill(test,x,y,7,makecol( 255, 255, 0 ));
if(bulleet==false)face=2;
blit(test, screen, 0, 0, 0, 0, test->w, test->h);
if(bulleet==true)
{
prosses();
}
}
if(key[KEY_UP])
{
if(bulleet==true)
{
prosses();
}
clear(test);
color=getpixel(maze1,x,y-8);
if(color!=0)
{
if(bulleet==true){y--;}
else{y--;
by--;
}
next3:
blit(maze1, test, 0, 0, 0, 0, maze1->w, maze1->h);
rest(10);
circlefill(test,x,y,7,makecol( 255, 255, 0 ));
if(bulleet==false)face=3;
blit(test, screen, 0, 0, 0, 0, test->w, test->h);
}
if(bulleet==true)
{
prosses();
}
}
if(key[KEY_DOWN])
{
if(bulleet==true)
{
prosses();
}
clear(test);
color=getpixel(maze1,x,y+8);
if (color==0)goto end;
if(bulleet==true){y++;}
else{
y++;
by++;
}
next4:
blit(maze1, test, 0, 0, 0, 0, maze1->w, maze1->h);
rest(10);
circlefill(test,x,y,7,makecol( 255, 255, 0 ));
if(bulleet==false)face=4;
blit(test, bullet, 0, 0, 0, 0, test->w, test->h);
blit(bullet, screen, 0, 0, 0, 0, test->w, test->h);
if(bulleet==true)
{
prosses();
}
}
if(key[KEY_A])
{
if(face==1) color3=getpixel(maze1,bx+8,bx);
if(face==2) color3=getpixel(maze1,bx-8,bx);
if(face==3) color3=getpixel(maze1,bx,by-8);
if(face==4) color3=getpixel(maze1,bx,by+8);
if (color3!=0)bulleet=true;
}
end:;
}
END_OF_FUNCTION(input);
int main(){
allegro_init();
install_mouse();
install_keyboard();
install_timer();
set_color_depth(16);
set_gfx_mode( GFX_AUTODETECT, 800, 600, 0, 0);
test=create_bitmap(800,600);
bullet=create_bitmap(800,600);
setup();
while ( !key[KEY_ESC])
{
blit(maze1, test, 0, 0, 0, 0, maze1->w, maze1->h);
circlefill(test,x,y,7,makecol( 255, 255, 0 ));
blit(test, screen, 0, 0, 0, 0, test->w, test->h);
input();
prosses();
}
}
END_OF_MAIN();
Ther reason why you are not getting any replies is probably because you items are too long for any one to read it please try and shorten your thread
OK .. You're a beginner that's without any doubt..
some tips..
- make your code readable !!! and put some comment in it.
Only a few people are willing to go through a code the way you posted it. It's verry hard to read
like this
1 | #include <allegro.h> |
2 | #include <iostream.h> |
3 | |
4 | BITMAP *maze1,*bullet,*laser; |
5 | BITMAP *test; |
6 | int x=7; |
7 | int y=0; |
8 | int color,color2,color3; |
9 | int face; |
10 | int bx=7; |
11 | int by=0; |
12 | |
13 | bool bulleet; // Don't do this use blBullet instead |
14 | //that way you'll always know it's a boolean |
15 | |
16 | |
17 | // UGLY !!! |
18 | /* |
19 | void prosses() |
20 | { |
21 | if(bulleet == true) // Don't check if (bulleet == true) just check if (bullet) |
22 | { |
23 | clear(bullet); |
24 | if(face==1)bx+=2; color2=getpixel(maze1,bx+1,by); |
25 | if(face==2)bx-=2; color2=getpixel(maze1,bx-1,by); |
26 | if(face==3)by-=2; color2=getpixel(maze1,bx,by-1); |
27 | if(face==4)by+=2; color2=getpixel(maze1,bx,by+1); |
28 | if(color2==0){bx=x; by=y; clear(bullet); bulleet=false; goto end;} |
29 | blit(test, bullet, 0, 0, 0, 0, test->w, test->h); |
30 | circlefill(bullet,bx,by,2,makecol( 255, 255, 0 )); |
31 | blit(bullet, screen, 0, 0, 0, 0, test->w, test->h); |
32 | } |
33 | end:; |
34 | } |
35 | */ |
36 | |
37 | // Do this instead |
38 | void prosses() |
39 | { |
40 | if(bulleet) |
41 | { |
42 | clear(bullet); |
43 | switch(face) |
44 | { |
45 | case 1: |
46 | bx+=2; |
47 | color2=getpixel(maze1,bx+1,by); |
48 | break; |
49 | |
50 | case 2: |
51 | bx-=2; |
52 | color2=getpixel(maze1,bx-1,by); |
53 | break; |
54 | |
55 | case 3: |
56 | by-=2; |
57 | color2=getpixel(maze1,bx,by-1); |
58 | break; |
59 | |
60 | case 4: |
61 | by+=2; |
62 | color2=getpixel(maze1,bx,by+1); |
63 | break; |
64 | } |
65 | |
66 | if(color2==0) |
67 | { |
68 | bx=x; |
69 | by=y; |
70 | clear(bullet); |
71 | bulleet=false; |
72 | } |
73 | else |
74 | { |
75 | blit(test, bullet, 0, 0, 0, 0, test->w, test->h); |
76 | circlefill(bullet,bx,by,2,makecol( 255, 255, 0 )); |
77 | blit(bullet, screen, 0, 0, 0, 0, test->w, test->h); |
78 | } |
79 | } |
80 | } |
81 | |
82 | |
83 | |
84 | |
85 | int drawmaze1() |
86 | { |
87 | maze1 = load_bitmap( "maze1.bmp", NULL); |
88 | circlefill(test,x,y,8,makecol( 255, 255, 0 )); |
89 | draw_sprite( screen, maze1, 0, 0); |
90 | } |
91 | //END_OF_FUNCTION(drawmaze1); not needed |
92 | |
93 | |
94 | |
95 | |
96 | |
97 | void setup() |
98 | { |
99 | drawmaze1(); |
100 | } |
101 | //END_OF_FUNCTION(setup); not needed |
102 | |
103 | |
104 | |
105 | |
106 | void input() |
107 | { |
108 | if(key[KEY_RIGHT]) |
109 | { |
110 | if(bulleet==true) |
111 | { |
112 | prosses(); |
113 | } |
114 | clear(test); |
115 | color=getpixel(maze1,x+8,y); |
116 | if(color!=0) |
117 | { |
118 | if(bulleet==true) |
119 | { |
120 | x++; |
121 | } |
122 | else |
123 | { |
124 | x++; |
125 | bx++; |
126 | } |
127 | next: |
128 | blit(maze1, test, 0, 0, 0, 0, test->w, test->h); |
129 | rest(10); // <--- DON'T EVER !!! |
130 | |
131 | circlefill(test,x,y,7,makecol( 255, 255, 0 )); |
132 | if(!bulleet) |
133 | { |
134 | face=1; |
135 | } |
136 | blit(test, screen, 0, 0, 0, 0, test->w, test->h); |
137 | } |
138 | |
139 | if(bulleet) |
140 | { |
141 | prosses(); |
142 | } |
143 | } |
144 | /* |
145 | |
146 | I tried to clean up till here .. you do the rest ;) |
147 | I suggest you'll follow some good C++ tutorials BEFORE trying to make a game |
148 | there's A LOT wrong with your coding |
149 | |
150 | */ |
You have more than just a flicker problem
- Buy a GOOD book about C++
- look here http://www.cplusplus.com/doc/tutorial/
AFTER writing some normal programs in a good style .. try to make a SIMPLE game
and ask me or anyone else here if you get stuck
Thanks for the tips..
I known my program is in bad structure because this is my first program to do with allegro so i just want to test some functions.
k thanks for the tips
Here are some more... sentences start with a capital letter, and end in a period (full stop). The personal pronoun, I, is always capitalized, and there's no such word as "mi."
// Don't do this use blBullet instead
//that way you'll always know it's a boolean
Hungarian notation is the worst thing ever. I'd advise AGAINST it because then if you need to change the type later, you will have to go through and update all your references to it. I'd advice a more OOP approach if you're doing C++ anyway.
that's just search and replace it's one second work ( almost )