1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
|
#include <cstdlib>
#include <stdlib.h>
#include <allegro5/allegro.h>
#include <allegro5/allegro_primitives.h>
const float FPS = 60;
const int SCREEN_W = 640;
const int SCREEN_H = 480;
const int BOUNCER_SIZE = 32;
float startx = 570;
float starty = 200;
int max = 11;
float dx = -10;
struct circle
{
float x;
float y;
float dy;
struct circle *c;
} *top;
void deepcopy(struct circle*& a, struct circle*& b);
void add(struct circle*& a);
void pop(struct circle*& a);
void renew(struct circle*& a);
void push(struct circle*& a, struct circle* b);
void move(struct circle*& a);
int count(struct circle* a);
int count(struct circle* a)
{
int c = 0;
for(struct circle* b = a;b!=0;b=b->c)
{
c++;
}
return c;
}
void deepcopy(struct circle*& a, struct circle*& b)
{
b->x=a->x;
b->y=a->y;
b->dy=a->dy;
}
void add(struct circle*& a)
{
if(a!=0)
{
struct circle* nnew = new circle;
nnew->x=startx;
nnew->y=starty;
nnew->dy= (float)((rand() % (2*max)) - max);
nnew->c = a;
a = nnew;
}
else {
a = new circle;
if(a==0)
{
throw "In add, new operator failed";
}
a->x=startx;
a->y=starty;
a->dy=(float)((rand() % (2*max)) - max);
a->c=0;
}
}
void pop(struct circle*& a)
{
struct circle *c = a;
a=a->c;
free(c);
}
void renew(struct circle*& a)
{
struct circle *nnew;
while(a!=0)
{
if(!((a->x<=0)||(a->x>=SCREEN_W)||(a->y<=0)||(a->y>=SCREEN_H)))
{
struct circle *b = new circle;
deepcopy(a,b);
push(nnew,b);
}
pop(a);
}
a = nnew;
}
void push(struct circle*& a, struct circle* b)
{
b->c=a;
a = b;
}
void move(struct circle*& a)
{
struct circle *b = a;
while(b!=0)
{
b->x+=dx;
b->y+=b->dy;
b=b->c;
}
}
int main(int argc, char **argv)
{
...
while(1)
{
...
if(redraw && al_is_event_queue_empty(event_queue)) {
redraw = false;
al_clear_to_color(al_map_rgb(0,0,0));
//al_draw_bitmap(bouncer, bouncer_x, bouncer_y, 0);
if(count(top)<=max)
{
add(top);
}
if(top==0)
{
throw "top is still 0";
}
// al_draw_bitmap(bouncer,a->x,a->y,0);
struct circle *a = top;
while (a->c!=0)
{
al_draw_bitmap(bouncer,a->x,a->y,0);
if(a->c!=0)
{
a=a->c;
}
}
move(top);
renew(top);
al_flip_display();
}
}
...
return 0;
}
|