Skip to content

Instantly share code, notes, and snippets.

@simplexityx
Created November 13, 2016 11:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save simplexityx/daf8094610f56254f87c928c80237c8f to your computer and use it in GitHub Desktop.
Save simplexityx/daf8094610f56254f87c928c80237c8f to your computer and use it in GitHub Desktop.
//initializing lists and setting balls into list
SDL_Event event;
int quitting = 0;
int i;
int listsize;
list_iterator_t *iter;
list_t *list=list_create();
iter = list_createiterator(list);
listsize=list_size(list);
object_t *ball;
for(i = 0; i < 5; i++){
ball = CreateObject(screen,sphere_model,SPHERE_NUMTRIANGLES);
printf("SpeedX: %f\tSpeedy: %f\tTX: %f\t TY: %f\n", ball->speedx, ball->speedy,ball->tx, ball->ty);
list_addlast(list,ball);
}
//initiliazing animation
while(!quitting)
{
ClearScreen(screen);
/* Event-loop: goes through the event queue
* responding to the events we care about.
*/
while (SDL_PollEvent(&event))
{
switch (event.type)
{
case SDL_QUIT: // Quit events include Alt-F4, Ctrl-C, closing the window
quitting = 1;
break;
case SDL_KEYDOWN:
if (event.key.keysym.sym == SDLK_ESCAPE) // Quit with escape
quitting = 1;
}
}
list_resetiterator(iter);
//animation loop
while((ball=list_next(iter))){
DrawObject(ball);
ball->tx += ball->speedx;
ball->ty += ball->speedy;
ball->rotation+=ball->speedx;
if(ball->tx + ball->radius >= screen->w || ball->tx - ball->radius <= 0)
ball->speedx *= -1;
if(ball->ty + ball->radius >= screen->h || ball->ty - ball->radius <= 0){
ball->speedy *= -1;
if(ball->speedx > 0.3 || ball->speedx < -0.3) {
if(ball->speedx > 0)
ball->speedx -= 0.5;
else if(ball->speedx < 0)
ball->speedx += 0.5;
}
else
ball->speedx = 0.0;
if((ball->speedy < 0.5 && ball->speedy > -0.5) || (ball->speedy > -0.5 && ball->speedy < -0.5))
ball->speedy = 0;
}
if(ball->speedy > 0.0)
ball->speedy += 0.5;
else if(ball->speedy < 0.0)
ball->speedy += 0.7;
if(ball->speedx > 0)
ball->speedx -= 0.015;
else if(ball->speedx < 0)
ball->speedx += 0.015;
ball->ttl=0;
if(ball->speedy==0){
SDL_Delay(500);
ball->ttl=ball->speedy;
}
if(ball->ttl==ball->speedy){
list_remove(list,ball);
DestroyObject(ball);
}
printf("SpeedX: %f\tSpeedY: %f\t\n", ball->speedx,ball->speedy);
}
SDL_UpdateRect(screen, 0, 0, 0, 0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment