C语言游戏与图形编程
反弹小球
#define _CRT_SECURE_NO_DEPRECATE
#define _CRT_NONSTDC_NO_DEPRECATE
#include
#include
#include
#include
int high, width;
int ball_x, ball_y;
int ball_vx, ball_vy;
int position_x, position_y;
int ridus;
int left, right;
int ball_number;
void gotoxy(int x, int y)
{
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
COORD pos;
pos.X = x;
pos.Y = y;
SetConsoleCursorPosition(handle,pos);
}
void startup()
{
high = 15;
width = 20;
ball_x = 0;
ball_y = width / 2;
ball_vx = 1;
ball_vy = 1;
ridus = 5;
position_x = high;
position_y = width / 2;
left = position_y - ridus;
right = position_y + ridus;
ball_number = 0;
}
void show()
{
int i, j;
for (i = 0; i <= high + 1; i++)
{
for (j = 0; j <= width; j++)
{
if ((i == ball_x) && (j == ball_y))
printf("0");
else if (j == width)
printf("|");
else if (i == high + 1)
printf("-");
else if ((i == high) && (j >= left) && (j <= right))
printf("*");
else
printf(" ");
}
printf("\n");
}
printf("反弹小球数:%d\n", ball_number);
}
void updataWithoutInput()
{
if (ball_x == high - 1)
{
if ((ball_y >= left) && (ball_y <= right))
{
ball_number++;
printf("\a");
}
else
{
printf("游戏失败\n");
system("pause");
exit(0);
}
}
ball_x = ball_x + ball_vx;
ball_y = ball_y + ball_vy;
if ((ball_x == 0) || (ball_x == high - 1))
ball_vx = -ball_vx;
if ((ball_y == 0) || (ball_y == width- 1))
ball_vy = -ball_vy;
Sleep(500);
}
void updateWithInput()
{
char input;
if (kbhit())
{
input = getch();
if (input == 'a')
{
position_y--;
left = position_y - ridus;
right = position_y + ridus;
}
if (input == 'd')
{
position_y++;
left = position_y - ridus;
right = position_y + ridus;
}
}
}
int main()
{
startup();
while (1)
{
show();
updataWithoutInput();
updateWithInput();
}
return 0;
}
飞机游戏
#include
#include
#include
#include
#include
#define EnemyNum 5
#define High 25
#define Width 50
int score;
int position_x, position_y;
int enemy_x[EnemyNum], enemy_y[EnemyNum];
int canvas[High][Width] = {0};
int BulletWidth;
int EnemyMoveSpeed;
void gotoxy(int x, int y)
{
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
COORD pos;
pos.X = x;
pos.Y = y;
SetConsoleCursorPosition(handle, pos);
}
void startup()
{
int i, j;
position_x = High / 2;
position_y = Width / 2;
canvas[position_x][position_y] = 1;
int k;
for (k = 0; k < EnemyNum; k++)
{
enemy_x[k] = rand() % 2;
enemy_y[k] = rand() % Width;
canvas[enemy_x[k]][enemy_y[k]] = 3;
}
score = 0;
BulletWidth = 0;
EnemyMoveSpeed = 10;
}
void show()
{
gotoxy(0, 0);
int i, j;
for (i = 0; i <= High; i++)
{
for (j = 0; j <= Width; j++)
{
if (canvas[i][j] == 0)
printf(" ");
else if (canvas[i][j] == 1)
printf("*");
else if (canvas[i][j] == 2)
printf("|");
else if (canvas[i][j] == 3)
printf("@");
}
printf("\n");
}
printf("得分:%3d\n", score);
Sleep(20);
}
void updateWithoutInput()
{
int i, j,k;
for (i = 0; i < High; i++)
{
for (j = 0; j < Width; j++)
{
if (canvas[i][j] == 2)
{
for (k = 0; k < EnemyNum; k++)
{
if ((i == enemy_x[k]) && (j == enemy_y[k]))
{
score++;
if (score % 5 == 0 && EnemyMoveSpeed > 3)
EnemyMoveSpeed--;
if (score % 5 == 0)
BulletWidth++;
canvas[enemy_x[k]][enemy_y[k]] = 0;
enemy_x[k] = rand() % 2;
enemy_y[k]=rand()%Width;
canvas[enemy_x[k]][enemy_y[k]] = 3;
canvas[i][j] = 0;
}
}
canvas[i][j] = 0;
if (i > 0)
canvas[i - 1][j] = 2;
}
}
}
static int speed = 0;
if (speed <20)
speed++;
for (k = 0; k < EnemyNum; k++)
{
if ((position_x == enemy_x[k]) && (position_y == enemy_y[k]))
{
printf("失败!\n");
Sleep(3000);
system("PAUSE");
exit(0);
}
if (enemy_x[k] > High)
{
canvas[enemy_x[k]][enemy_y[k]] = 0;
enemy_x[k] = rand() % 2;
enemy_y[k] = rand() % Width;
canvas[enemy_x[k]][enemy_y[k]] = 3;
score--;
}
if (speed == EnemyMoveSpeed)
{
for (k = 0; k < EnemyNum; k++)
{
canvas[enemy_x[k]][enemy_y[k]] = 0;
enemy_x[k]++;
speed = 0;
canvas[enemy_x[k]][enemy_y[k]] = 3;
}
}
}
}
void updateWithInput()
{
char input;
if (_kbhit())
{
input = _getch();
if (input == 'a')
{
canvas[position_x][position_y] = 0;
position_y--;
canvas[position_x][position_y] = 1;
}
else if (input == 'd')
{
canvas[position_x][position_y] = 0;
position_y++;
canvas[position_x][position_y] = 1;
}
else if (input == 'w')
{
canvas[position_x][position_y] = 0;
position_x--;
canvas[position_x][position_y] = 1;
}
else if (input == 's')
{
canvas[position_x][position_y] = 0;
position_x++;
canvas[position_x][position_y] = 1;
}
else if (input == ' ')
{
int left = position_y - BulletWidth;
int right = position_y + BulletWidth;
if (left < 0)
left = 0;
if(right>Width-1)
right=Width-1;
int k;
for(k=left;k<=right;k++)
canvas[position_x - 1][k] = 2;
}
}
}
int main()
{
startup();
while (1)
{
show();
updateWithoutInput();
updateWithInput();
}
return 0;
}
飞翔小鸟flapbird
#include
#include
#include
#include
int high, width;
int bird_x, bird_y;
int bar1_y, bar1_xDown, bar1_xTop;
int score;
void gotoxy(int x, int y)
{
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
COORD pos;
pos.X = x;
pos.Y = y;
SetConsoleCursorPosition(handle,pos);
}
void startup()
{
high = 15;
width = 20;
bird_x = 0;
bird_y = width / 3;
bar1_y = width / 2;
bar1_xDown = high / 3;
bar1_xTop = high / 2;
score = 0;
}
void show()
{
gotoxy(0, 0);
int i, j;
for (i = 0; i < high; i++)
{
for (j = 0; j < width; j++)
{
if (i == bird_x && j == bird_y)
printf("@");
else if ((j == bar1_y) && ((i <= bar1_xDown) || (i > bar1_xTop)))
{
printf("*");
}
else
printf(" ");
}
printf("\n");
}
}
void updataWithoutInput()
{
bird_x++;
bar1_y--;
if (bird_y == bar1_y)
{
if ((bird_x >= bar1_xDown) && (bird_x <= bar1_xTop))
score++;
else
{
}
}
if (bar1_y <= 0)
{
bar1_y = width;
int temp = rand() % int(high * 0.8);
bar1_xDown = temp - high / 10;
bar1_xTop = temp + high / 10;
}
Sleep(150);
}
void updataWithInput()
{
char input;
if (_kbhit())
{
input = _getch();
if (input == ' ')
bird_x = bird_x - 2;
}
}
int main()
{
startup();
while (1)
{
show();
updataWithoutInput();
updataWithInput();
}
return 0;
}
多小球反弹(解决重叠问题)
#include
#include
#include
#include
#define High 480
#define Width 640
#define BallNum 15
int main()
{
float ball_x[BallNum], ball_y[BallNum];
float ball_vx[BallNum], ball_vy[BallNum];
float radius;
int i, j;
radius = 20;
for (i = 0; i < BallNum; i++)
{
ball_x[i] = rand() % int(Width - 4 * radius) + 2 * radius;
ball_y[i] = rand() % int(High - 4 * radius) + 2 * radius;
ball_vx[i] = (rand() % 2) * 2 - 1;
ball_vy[i] = (rand() % 2) * 2 - 1;
}
for (i = 0; i < BallNum; i++)
{
for (j = 0; j < BallNum; j++)
{
if (i != j)
{
float dist2;
dist2 = sqrt((ball_x[i] - ball_x[j]) * (ball_x[i] - ball_x[j]) + (ball_y[i] - ball_y[j]) * (ball_y[i] - ball_y[j]));
if (dist2 < 2 * radius )
{
ball_x[j] = rand() % int(Width - 4 * radius) + 2 * radius;
ball_y[j] = rand() % int(High - 4 * radius) + 2 * radius;
ball_vx[j] = (rand() % 2) * 2 - 1;
ball_vy[j] = (rand() % 2) * 2 - 1;
}
}
}
}
initgraph(High, Width);
BeginBatchDraw();
while (1)
{
setcolor(BLACK);
setfillcolor(BLACK);
for (i = 0; i < BallNum; i++)
{
fillcircle(ball_x[i], ball_y[i], radius);
}
for (i = 0; i < BallNum; i++)
{
ball_x[i] = ball_x[i] + ball_vx[i];
ball_y[i] = ball_y[i] + ball_vy[i];
if (ball_x[i] < radius)
ball_x[i] = radius;
if (ball_y[i] < radius)
ball_y[i] = radius;
if (ball_x[i] > Width - radius)
ball_x[i] = Width - radius;
if (ball_y[i] > High - radius)
ball_y[i] = High - radius;
}
for (i = 0; i < BallNum; i++)
{
if ((ball_x[i] <= radius) || (ball_x[i] >= Width - radius))
ball_vx[i] = -ball_vx[i];
if ((ball_y[i] <= radius) || (ball_y[i]) >= High - radius)
ball_vy[i] = -ball_vy[i];
}
float minDistances2[BallNum][2];
for (i = 0; i < BallNum; i++)
{
minDistances2[i][0] = 9999999;
minDistances2[i][1] = -1;
}
for (i = 0; i < BallNum; i++)
{
for (j = 0; j < BallNum; j++)
{
if (i != j)
{
float dist2;
dist2 = (ball_x[i] - ball_x[j]) * (ball_x[i] - ball_x[j]) + (ball_y[i] - ball_y[j]) * (ball_y[i] - ball_y[j]);
if (dist2 < minDistances2[i][0])
{
minDistances2[i][0] = dist2;
minDistances2[i][1] = j;
}
}
}
}
for (i = 0; i < BallNum; i++)
{
if (minDistances2[i][0] <= 4 * radius * radius)
{
j = minDistances2[i][1];
int temp;
temp = ball_vx[i];ball_vx[i] = ball_vx[j]; ball_vx[j] = temp;
temp = ball_vy[i]; ball_vy[i] = ball_vy[j]; ball_vy[j] = temp;
minDistances2[j][0] = 999999999;
minDistances2[j][1] = -1;
}
}
setcolor(YELLOW);
setfillcolor(GREEN);
for (i = 0; i < BallNum; i++)
fillcircle(ball_x[i], ball_y[i], radius);
FlushBatchDraw();
Sleep(3);
}
EndBatchDraw();
closegraph();
return 0;
}
钟表
#include
#include
#include
#include
#include
#define High 480
#define Width 640
#define PI 3.14159
int main()
{
initgraph(Width, High);
int center_x, center_y;
center_x = Width / 2;
center_y = High / 2;
int secondLength= Width / 5;
int minuteLength = Width / 7;
int hourLength = Width /9;
float angle = 0;
int secondEnd_x, secondEnd_y;
int minuteEnd_x,minuteEnd_y;
int hourEnd_x, hourEnd_y;
SYSTEMTIME ti;
long double secondAngle = 0;
long double minuteAngle = 0;
long double hourAngle = 0;
while (1)
{
setlinestyle(PS_SOLID, 1);
setlinecolor(WHITE);
circle(center_x, center_y, Width / 4);
int x, y, i;
for (i = 0; i < 60; i++)
{
x = center_x + int(Width / 4.3 * sin(PI * 2 * i / 60));
y = center_y + int(Width / 4.3 * cos(PI * 2 * i / 60));
if (i % 15 == 0)
bar(x - 5, y - 5, x + 5, y + 5);
else if (i % 5 == 0)
circle(x, y, 3);
else
putpixel(x, y, WHITE);
}
outtextxy(center_x - 25, center_y + Width / 6, _T("我的时钟"));
GetLocalTime(&ti);
secondAngle = ti.wSecond * 2 * PI / 60;
minuteAngle = ti.wMinute * 2 * PI / 60;
hourAngle = ti.wHour * 2 * PI / 12;
minuteEnd_x = center_x + minuteLength * sin(minuteAngle);
minuteEnd_y = center_y - minuteLength * cos(minuteAngle);
secondEnd_x = center_x + secondLength * sin(secondAngle);
secondEnd_y = center_y - secondLength * cos(secondAngle);
hourEnd_x = center_x + hourLength * sin(hourAngle);
hourEnd_y = center_y - hourLength * cos(hourAngle);
setlinestyle(PS_SOLID, 2);
setcolor(WHITE);
line(center_x, center_y, secondEnd_x, secondEnd_y);
setlinestyle(PS_SOLID, 4);
setcolor(BLUE);
line(center_x, center_y, minuteEnd_x, minuteEnd_y);
setlinestyle(PS_SOLID, 6);
setcolor(RED);
line(center_x, center_y, hourEnd_x, hourEnd_y);
Sleep(10);
setcolor(BLACK);
setlinestyle(PS_SOLID, 2);
line(center_x, center_y, secondEnd_x, secondEnd_y);
setlinestyle(PS_SOLID, 4);
line(center_x, center_y, minuteEnd_x, minuteEnd_y);
setlinestyle(PS_SOLID, 6);
line(center_x, center_y, hourEnd_x, hourEnd_y);
}
_getch();
closegraph();
return 0;
}
飞翔小鸟(图片与声音)(bug较多,不完整)
#include
#include
#include
#pragma comment(lib,"Winmm.lib")
#define High 600
#define Width 350
IMAGE img_bk, img_bd1, img_bd2,img_bar_up1, img_bar_up2, img_bar_down1, img_bar_down2;
int bird_x;
int bird_y;
int image_x[] = { 0 };
int image_y[] = { 0 };
void startup()
{
initgraph( Width, High);
loadimage(&img_bk, _T("E:\\image\\background.jpg"));
loadimage(&img_bd1, _T("E:\\image\\bird1.jpg"));
loadimage(&img_bd2, _T("E:\\image\\bird2.jpg"));
loadimage(&img_bar_up1, _T("E:\\image\\bar_up1.gif"));
loadimage(&img_bar_up2, _T("E:\\image\\bar_up2.gif"));
loadimage(&img_bar_down1, _T("E:\\image\\bar_down1.gif"));
loadimage(&img_bar_down2, _T("E:\\image\\bar_down2.gif"));
bird_x = 50;
bird_y = 200;
BeginBatchDraw();
image_x[0] = rand() % 301 + rand() % 75 + 30;
image_y[0] = rand() % 315 + rand() % 95;
for (int i = 1; i < 3; i++)
{
image_x[i] = image_x[i - 1] + rand() % 77+150;
image_y[i] = image_y[i - 1] + rand() % 77;
}
mciSendString(_T("open E:\\image\\background.mp3 alias bkmusic"), NULL, 0, NULL);
mciSendString(_T("play bkmusic repeat"), NULL, 0, NULL);
}
void show()
{
putimage(0, 0, &img_bk);
int x = 50;
for (int i = 0; i < 2; i++)
{
putimage(image_x[i], -300, &img_bar_up1, NOTSRCERASE);
putimage(image_x[i], -300, &img_bar_up2, SRCINVERT);
putimage(image_x[i], 400 , &img_bar_up1, NOTSRCERASE);
putimage(image_x[i], 400 , &img_bar_up2, SRCINVERT);
}
putimage(bird_x, bird_y, &img_bd1, NOTSRCERASE);
putimage(bird_x, bird_y, &img_bd2, SRCINVERT);
FlushBatchDraw();
Sleep(50);
}
void updateWithoutInput()
{
if (bird_y < 580)
bird_y = bird_y + 3;
for (int i = 0; i < 3; i++)
{
if (image_x[i] > 0)
image_x[i] -= 3;
if (image_x[i] <= 0)
{
image_x[i]= image_x[i+1]+250;
Sleep(100);
}
}
}
void updateWithInput()
{
char input;
if (_kbhit())
{
input = _getch();
if (input == ' ' && bird_y > 20)
{
bird_y = bird_y - 60;
mciSendString(_T("close jpmusic"), NULL, 0, NULL);
mciSendString(_T("open E:\\image\\jump.mp3 alias jpmusic"), NULL, 0, NULL);
mciSendString(_T("play jpmusic"), NULL, 0, NULL);
}
}
}
void gameover()
{
EndBatchDraw();
_getch();
closegraph();
}
int main()
{
startup();
while (1)
{
show();
updateWithoutInput();
updateWithInput();
}
gameover();
return 0;
}
飞机大战
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include
#pragma comment(lib,"Winmm.lib")
#define High 864
#define Width 591
IMAGE img_bk;
int position_x, position_y;
IMAGE img_planeNormal1, img_planeNormal2;
int bullet_x, bullet_y;
IMAGE img_bullet1, img_bullet2;
int bulletxy[Width][High] = { 0 };
float enemy_x, enemy_y;
IMAGE img_enemyPlane1, img_enemyPlane2;
IMAGE img_planeExplode1, img_planeExplode2;
int isExplode;
int score;
void startup()
{
initgraph(Width, High);
mciSendString(_T("open E:\\image\\plane\\game_music.mp3 alias bkmusic"), NULL, 0, NULL);
mciSendString(_T("play bkmusic repeat"), NULL, 0, NULL);
loadimage(&img_bk, _T("E:\\image\\plane\\background.jpg"));
loadimage(&img_planeNormal1, _T("E:\\image\\plane\\planeNormal_1.jpg"));
loadimage(&img_planeNormal2, _T("E:\\image\\plane\\planeNormal_2.jpg"));
loadimage(&img_bullet1, _T("E:\\image\\plane\\bullet1.jpg"));
loadimage(&img_bullet2, _T("E:\\image\\plane\\bullet2.jpg"));
loadimage(&img_planeNormal2, _T("E:\\image\\plane\\planeNormal_2.jpg"));
loadimage(&img_planeNormal2, _T("E:\\image\\plane\\planeNormal_2.jpg"));
loadimage(&img_enemyPlane1, _T("E:\\image\\plane\\enemyPlane1.jpg"));
loadimage(&img_enemyPlane2, _T("E:\\image\\plane\\enemyPlane2.jpg"));
loadimage(&img_planeExplode1, _T("E:\\image\\plane\\planeExplode_1.jpg"));
loadimage(&img_planeExplode2, _T("E:\\image\\plane\\planeExplode_2.jpg"));
position_x = Width * 0.5;
position_y = High * 0.7;
bullet_x = position_x;
bullet_y = -85;
enemy_x = Width * 0.5;
enemy_y = 10;
BeginBatchDraw();
}
void show()
{
putimage(0, 0, &img_bk);
if (isExplode == 0)
{
putimage(position_x - 50, position_y - 60, &img_planeNormal1, NOTSRCERASE);
putimage(position_x - 50, position_y - 60, &img_planeNormal2, SRCINVERT);
putimage(bullet_x - 7, bullet_y, &img_bullet1, NOTSRCERASE);
putimage(bullet_x - 7, bullet_y, &img_bullet2, SRCINVERT);
putimage(enemy_x, enemy_y, &img_enemyPlane1, NOTSRCERASE);
putimage(enemy_x, enemy_y, &img_enemyPlane2, SRCINVERT);
}
else
{
putimage(position_x-50, position_y-60, &img_planeExplode1, NOTSRCERASE);
putimage(position_x-50, position_y-60, &img_planeExplode2, SRCINVERT);
}
outtextxy(Width * 0.48, High * 0.95,_T("得分:"));
char s[5];
sprintf(s, "%d", score);
outtextxy(Width * 0.55, High * 0.95, (int)s);
FlushBatchDraw();
Sleep(2);
}
void updateWithoutInput()
{
if (isExplode == 0)
{
if (bullet_y > -25)
bullet_y = bullet_y - 2;
if (enemy_y < High - 25)
enemy_y = enemy_y + 0.5;
else
enemy_y = 10;
if (abs(bullet_x - enemy_x) + abs(bullet_y - enemy_y) < 80)
{
enemy_x = rand() % Width;
enemy_y = -40;
bullet_y = -85;
mciSendString(_T("close gemusic"), NULL, 0, NULL);
mciSendString(_T("open E:\\image\\plane\\gotEnemy.mp3 alias gemusic"), NULL, 0, NULL);
mciSendString(_T("play gemusic"), NULL, 0, NULL);
score++;
if (score > 0 && score % 5 == 0 && score % 2 != 0)
{
mciSendString(_T("close 5music"), NULL, 0, NULL);
mciSendString(_T("open E:\\image\\plane\\5.mp3 alias 5music"), NULL, 0, NULL);
mciSendString(_T("play 5music"), NULL, 0, NULL);
}
if (score % 10 == 0)
{
mciSendString(_T("close 10music"), NULL, 0, NULL);
mciSendString(_T("open E:\\image\\plane\\10.mp3 alias 10music"), NULL, 0, NULL);
mciSendString(_T("play 10music"), NULL, 0, NULL);
}
}
if (abs(position_x - enemy_x) + abs(position_y - enemy_y) < 150)
{
isExplode = 1;
mciSendString(_T("close exmusic"), NULL, 0, NULL);
mciSendString(_T("open E:\\image\\plane\\explode.mp3 alias exmusic"), NULL, 0, NULL);
mciSendString(_T("play exmusic"), NULL, 0, NULL);
}
}
}
void updateWithInput()
{
if (isExplode == 0)
{
MOUSEMSG m;
while (MouseHit())
{
m = GetMouseMsg();
if (m.uMsg == WM_MOUSEMOVE)
{
position_x = m.x;
position_y = m.y;
}
else if (m.uMsg == WM_LBUTTONDOWN)
{
bullet_x = position_x;
bullet_y = position_y - 85;
mciSendString(_T("close fgmusic"), NULL, 0, NULL);
mciSendString(_T("open E:\\image\\plane\\f_gun.mp3 alias fgmusic"), NULL, 0, NULL);
mciSendString(_T("play fgmusic"), NULL, 0, NULL);
}
}
}
}
void gameover()
{
EndBatchDraw();
_getch();
closegraph();
}
int main()
{
startup();
while (1)
{
show();
updateWithoutInput();
updateWithInput();
}
gameover();
return 0;
}