In this blog or article we will see some animation program like Write a C++ program to control a ball using arrow keys. Apply the concept of polymorphism.So our learning objective is To learn scanline polygon fill algorithm.
So let's move to our theory part
Theory:
In theory we will first see what is animation small brief
What is Animation ?
Animation is the process of designing, drawing, making layouts and preparation of photographic sequences which are integrated in the multimedia and gaming products. Animation involves the exploitation and management of still images to generate the illusion of movement. How to move an element to left, right, up and down using arrow keys?
So now let's talk about coding
Code:
To detect which arrow key is pressed, you can use ncurses.h header file. Arrow key's code is
defined as: KEY_UP, KEY_DOWN, KEY_LEFT, KEY_RIGHT.
#include<ncurses.h>
int main()
{
int ch;
/* Curses Initialisations */
initscr();
raw();
keypad(stdscr, TRUE);
noecho();
printw("Welcome - Press # to Exit\n");
while((ch = getch()) != '#')
{
switch(ch)
{
case KEY_UP: printw("\nUp Arrow");
break;
case KEY_DOWN: printw("\nDown Arrow");
break;
case KEY_LEFT: printw("\nLeft Arrow");
break;
case KEY_RIGHT: printw("\nRight Arrow");
break;
default:
{
printw("\nThe pressed key is ");
attron(A_BOLD);
printw("%c", ch);
attroff(A_BOLD);
}
}
}
printw("\n\nBye Now!\n");
refresh();
getch();
endwin();
return 0;
}
How to draw a sine wave using c++?
#include <math.h>
#include <graphics.h>
#include <iostream>
int main() {
int gd = DETECT, gm;
int angle = 0;
double x, y;
initgraph(&gd, &gm, NULL);
line(0, getmaxy() / 2, getmaxx(), getmaxy() / 2);
/* generate a sine wave */
for(x = 0; x < getmaxx(); x+=3) {
/* calculate y value given x */
y = 50*sin(angle*3.141/180);
y = getmaxy()/2 - y;
/* color a pixel at the given position */
putpixel(x, y, 15);
delay(100);
/* increment angle */
angle+=5;
}
/* deallocate memory allocated for graphics screen */
closegraph();
return 0;
}
Conclusion:
We implement Sussefully code the all part.
About project:
This the first project we have done there are more projects is ready I will post soon!!
0 Comments