Selasa, 25 Maret 2014

Ebook Chapter 5 Problem Solving and Program Design in C

  • 5.11 Figure 5.23 Program to draw a moving ball
#include <cstdlib>
#include <iostream>
#include <graphics.h>
#define TRUE 1

using namespace std;

int main(void)
{
    const int PAUSE=20;
    const int DELTA=5;
    const int RADIUS=30;
    const int COLOR=RED;
   
    int width,height,x,y,stepX,stepY;
    width = getmaxwidth();
    height = getmaxheight();
    initwindow(width, height,"Pong - close window to quit",0,0,TRUE);
    x = RADIUS;
    y = RADIUS;
    stepX = DELTA;
    stepY = DELTA;
   
    while (TRUE){
          clearviewport();
          setfillstyle(SOLID_FILL, COLOR);
          fillellipse(x, y, RADIUS, RADIUS);
         
          swapbuffers();
          delay(PAUSE);
         
          if(x<= RADIUS)
                 stepX = DELTA;
          else if (x>= width - RADIUS)
               stepX = -DELTA;
              
          if(y <= RADIUS)
               stepY = DELTA;
          else if (y >= height - RADIUS)
               stepY + -DELTA;
              
          x = x + stepX;
          y = y + stepY;
          }
   
    closegraph();
    system("PAUSE");
    return EXIT_SUCCESS;
}

0 komentar:

Posting Komentar

 

Anita © 2010

Blogger Templates by Splashy Templates