#include <stdlib.h>
#include <SDL/SDL.h>
SDL_Event event;
SDL_TimerID timer;
SDL_Surface *display;
SDL_Surface *image;
SDL_Rect drect;
SDL_Rect srect;
int quit = 0;
int xbox = 400;
int ybox = 300;
int rbox = 0;
int ubox = 0;
int speed = 1;
int level = 1;
int radbox = 120;
int xxbox = 50;
int yybox = 50;
int rrbox = 1;
int uubox = 1;
double punkte = 0;
int zufall;
Uint32 haupt_funk (Uint32 intervall, void *parameter)
{
if (xxbox <= 35) rrbox = 1;
if (xxbox >= 765) rrbox = -1;
if (yybox <= 35) uubox = 1;
if (yybox >= 565) uubox = -1;
xxbox = xxbox + rrbox;
yybox = yybox + uubox;
zufall = 1+(int) (25.0*rand()/(RAND_MAX+1.0));
if (zufall >= 20 && zufall <= 24) rbox = -1;
if (zufall >= 5 && zufall <= 9) rbox = 1;
if (zufall >= 10 && zufall <= 14) ubox = -1;
if (zufall >= 15 && zufall <= 19) ubox = 1;
if (xbox <= radbox) rbox = 1;
if (xbox >= 800 - radbox) rbox = -1;
if (ybox <= radbox) ubox = 1;
if (ybox >= 600 - radbox) ubox = -1;
xbox = xbox + (rbox * speed);
ybox = ybox + (ubox * speed);
punkte = punkte + 0.01;
if (punkte >= 150) {
radbox = 100;
speed = 2;
punkte = punkte + 0.02;
level = 2;
} else if (punkte >= 400) {
radbox = 90;
speed = 3;
punkte = punkte + 0.03;
level = 3;
} else if (punkte >= 700) {
radbox = 80;
speed = 3;
punkte = punkte + 0.04;
level = 4;
} else if (punkte >= 1300) {
radbox = 70;
speed = 4;
punkte = punkte + 0.05;
level = 5;
} else {
radbox = 60;
speed = 5;
punkte = punkte + 0.06;
level = 6;
}
srect.x = 0;
srect.y = 0;
srect.w = 10;
srect.h = 10;
drect.x = 0;
drect.y = 0;
drect.w = 800;
drect.h = 600;
SDL_FillRect(display, &drect, SDL_MapRGB(display->format, 0, 0, 0));
drect.x = xxbox - 35;
drect.y = yybox - 35;
drect.w = 70;
drect.h = 70;
SDL_FillRect(display, &drect, SDL_MapRGB(display->format, 0, 50, 127));
drect.w = 10;
drect.h = 10;
for( drect.x = xbox - radbox; drect.x < xbox + radbox; drect.x = drect.x + 10) {
drect.y = ybox - radbox;
SDL_BlitSurface(image, &srect, display, &drect);
SDL_UpdateRects(display,1,&drect);
drect.y = ybox + radbox - 10;
SDL_BlitSurface(image, &srect, display, &drect);
}
for( drect.y = ybox - radbox; drect.y < ybox + radbox; drect.y = drect.y + 10) {
drect.x = xbox - radbox;
SDL_BlitSurface(image, &srect, display, &drect);
SDL_UpdateRects(display,1,&drect);
drect.x = xbox + radbox - 10;
SDL_BlitSurface(image, &srect, display, &drect);
}
drect.x = 0;
drect.y = 0;
drect.w = 800;
drect.h = 600;
SDL_UpdateRects(display,1,&drect);
return intervall;
}
int main()
{
if ( SDL_Init(SDL_INIT_VIDEO|SDL_INIT_TIMER) < 0 )
{
fprintf(stderr, "SDL konnte nicht initialisiert werden: %s\n",
SDL_GetError());
exit(1);
}
display = SDL_SetVideoMode( 800, 600, 16, SDL_FULLSCREEN );
if ( display == NULL )
{
fprintf(stderr, "Konnte kein Fenster 800x600px oeffnen: %s\n",
SDL_GetError());
exit(1);
}
image = SDL_LoadBMP("sperr.bmp");
if (image == NULL)
{
fprintf(stderr, "Das Bild konnte nicht geladen werden: %s\n",
SDL_GetError());
exit(-1);
}
srect.x = 0;
srect.y = 0;
srect.w = 10;
srect.h = 10;
drect.x = 0;
drect.y = 0;
drect.w = 800;
drect.h = 600;
SDL_FillRect(display, &drect, SDL_MapRGB(display->format, 0, 0, 0));
drect.x = xxbox - 35;
drect.y = yybox - 35;
drect.w = 70;
drect.h = 70;
SDL_FillRect(display, &drect, SDL_MapRGB(display->format, 0, 50, 127));
drect.w = 10;
drect.h = 10;
for( drect.x = xbox - radbox; drect.x < xbox + radbox; drect.x = drect.x + 10) {
drect.y = ybox - radbox;
SDL_BlitSurface(image, &srect, display, &drect);
SDL_UpdateRects(display,1,&drect);
drect.y = ybox + radbox - 10;
SDL_BlitSurface(image, &srect, display, &drect);
}
for( drect.y = ybox - radbox; drect.y < ybox + radbox; drect.y = drect.y + 10) {
drect.x = xbox - radbox;
SDL_BlitSurface(image, &srect, display, &drect);
SDL_UpdateRects(display,1,&drect);
drect.x = xbox + radbox - 10;
SDL_BlitSurface(image, &srect, display, &drect);
}
drect.x = 0;
drect.y = 0;
drect.w = 800;
drect.h = 600;
SDL_UpdateRects(display,1,&drect);
while( quit == 0 )
{
while( SDL_PollEvent( &event ) )
{
switch( event.type )
{
case SDL_MOUSEBUTTONDOWN:
if (event.button.x >= xbox - radbox && event.button.x <= xbox + radbox && event.button.y >= ybox - radbox && event.button.y <= ybox + radbox) {
timer = SDL_AddTimer (10, haupt_funk, NULL);
quit = 1;
}
break;
}
}
}
quit = 0;
while( quit == 0 )
{
while( SDL_PollEvent( &event ) )
{
switch( event.type )
{
case SDL_MOUSEMOTION:
if (event.button.x <= xbox - radbox || event.button.x >= xbox + radbox || event.button.y <= ybox - radbox || event.button.y >= ybox + radbox) quit = 1;
break;
case SDL_MOUSEBUTTONDOWN:
if (event.button.x >= xxbox - 35 && event.button.x <= xxbox + 35 && event.button.y >= yybox - 35 && event.button.y <= yybox + 35) punkte = punkte + (5*level);
}
}
}
fprintf(stderr, "%f punkte\n", punkte);
SDL_FreeSurface(image);
atexit(SDL_Quit);
return 0;
}