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
|
#include "primlib.h"
#include <stdlib.h>
//
#define PillarsNumber 5
#define PillarsHeight 200
#define PillarsWidth 15
#define DisksNumber 5
#define DisksHeight (PillarsHeight/DisksNumber)
#define BaseHeight 20
//
typedef struct Disk Disk;
struct Disk
{
int xUpLeft;
int yUpLeft;
int xDownRight;
int yDownRight;
};
Disk *position[DisksNumber];
int main(int argc, char* argv[])
{
if(initGraph())
{
exit(3);
}
//
int DiskPerPillar[DisksNumber];
int DisksWidth = (PillarsWidth+(5*screenWidth()/(3*PillarsNumber+1)))/3;
//
void DisplayPillars()
{
for(int i=0; i < PillarsNumber; i++)
{
filledRect((i+1)*screenWidth()/(PillarsNumber+1)-(PillarsWidth/4), screenHeight()-PillarsHeight-BaseHeight, (i+1)*screenWidth()/(PillarsNumber+1)+(PillarsWidth/4), screenHeight()-BaseHeight, RED);
}
}
void DisplayDisks()
{
for(int n=0; n < DisksNumber; n++)
{
filledRect((screenWidth()/(PillarsNumber+1)-(DisksWidth-PillarsWidth)/2)+n*(DisksWidth-PillarsWidth)/(2*DisksNumber),
screenHeight()-BaseHeight-(n+1)*DisksHeight,
(screenWidth()/(PillarsNumber+1)+PillarsWidth+(DisksWidth-PillarsWidth)/2)-n*(DisksWidth-PillarsWidth)/(2*DisksNumber),
screenHeight()-BaseHeight-n*DisksHeight, WHITE);
//create strutc
position[n]->xUpLeft = (screenWidth()/(PillarsNumber+1)-(DisksWidth-PillarsWidth)/2)+n*(DisksWidth-PillarsWidth)/(2*DisksNumber);
position[n]->yUpLeft = screenHeight()-BaseHeight-(n+1)*DisksHeight;
position[n]->xDownRight = (screenWidth()/(PillarsNumber+1)+PillarsWidth+(DisksWidth-PillarsWidth)/2)-n*(DisksWidth-PillarsWidth)/(2*DisksNumber);
position[n]->yDownRight = screenHeight()-BaseHeight-n*DisksHeight;
}
}
//animation
void animation(int KeyPressOne, int KeyPressTwo)
{
position[KeyPressOne]->xUpLeft = (screenWidth()/(PillarsNumber+1)-(DisksWidth-PillarsWidth)/2)+(KeyPressTwo-1)*(DisksWidth-PillarsWidth)/(2*DisksNumber);
position[KeyPressOne]->yUpLeft = screenHeight()-BaseHeight-(DiskPerPillar[KeyPressTwo]+1)*DisksHeight;
position[KeyPressOne]->xDownRight = (screenWidth()/(PillarsNumber+1)+PillarsWidth+(DisksWidth-PillarsWidth)/2)-(KeyPressTwo-1)*(DisksWidth-PillarsWidth)/(2*DisksNumber);
position[KeyPressOne]->yDownRight = screenHeight()-BaseHeight-(DiskPerPillar[KeyPressTwo])*DisksHeight;
DiskPerPillar[KeyPressTwo-1]++;
}
void movement()
{
int KeyPressOne = getkey();
int KeyPressTwo = getkey();
if (KeyPressOne !=0 && KeyPressTwo !=0)
{
animation(KeyPressOne,KeyPressTwo);
for(int b=0;b<DisksNumber;b++)
{
filledRect(position[b]->xUpLeft,position[b]->yUpLeft,position[b]->xDownRight,position[b]->yDownRight,GREEN);
updateScreen();
SDL_Delay(500);
}
}
}
for(int c=0;c<DisksNumber;c++)
{
if (c==0)
{
DiskPerPillar[c]= DisksNumber;
}
else
{
DiskPerPillar[c] = 0;
}
}
//Affichage
filledRect(0, 0, screenWidth() - 1, screenHeight() - 1, BLACK);
filledRect(0, screenHeight()-BaseHeight, screenWidth(), screenHeight(), YELLOW);
DisplayPillars();
DisplayDisks();
updateScreen();
movement();
SDL_Delay(5000);
return 0;
} |
Partager