[C] c语言游戏 雷霆战机游戏源代码 →→→→→进入此内容的聊天室

来自 Gamboge Pudu, 2024-03-07, 写在 C, 查看 16 次.
URL http://www.code666.cn/view/140cc736
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<math.h>
  4. #include<graphics.h>
  5. #include<conio.h>
  6. #include<time.h>
  7. #include<mmsystem.h>
  8. #pragma comment(lib,"winmm.lib")
  9.  
  10.  
  11.  
  12.  
  13. typedef struct Node
  14. {
  15.         int x;
  16.         int y;
  17.         struct Node *pnext;
  18. }NODE;
  19.  
  20. #define WINDOW_WIDTH 1024
  21. #define WINDOW_HEIGHT 680
  22. #define WIDTH 480
  23.  
  24. //我机图片尺寸
  25. #define pw 86
  26. #define ph 82
  27.  
  28. //敌机图片尺寸
  29. #define aw 70
  30. #define ah 70
  31.  
  32. #define boss1w 192
  33. #define boss1h 290
  34.  
  35.  
  36. //敌机重出现的y坐标
  37. #define APStart -ah-20
  38.  
  39.  
  40.  
  41.  
  42.  
  43. NODE *p_bullet = NULL;
  44. //MyPlane
  45. NODE *p_MP = NULL;
  46. //AttackPlane
  47. NODE* p_AP = NULL;
  48. //子弹时间差
  49. NODE* p_AP2 = NULL;
  50. DWORD b1, b2, b3, b4, b5, b6;
  51.  
  52. IMAGE i_MP,i_MPS,i_AP,i_APS;
  53. IMAGE i_backeve, i_backxing, i_backduicheng, i_backguan,i_backcontrol,i_backgo;
  54. IMAGE i_boss1_1, i_boss1_1S, i_boss1_2, i_boss1_2S;
  55.  
  56. //backxing的左右移动
  57. int left = (WINDOW_WIDTH / 2 - WIDTH / 2);
  58. //分数
  59. int score = 0;
  60. //击毁敌机的数量
  61. int kill = 0;
  62. //boss是否出现
  63. int boss1show = 0;
  64. //boss1贴图开关
  65. int boss1image = 0;
  66. int boss1hp = 20;
  67.  
  68. int line1_x = WINDOW_WIDTH / 2 - 20;
  69. int line1_y = boss1h;
  70. int line2_x = WINDOW_WIDTH / 2 + 20;
  71. int line2_y = boss1h;
  72.  
  73. //Beam只播放一次
  74. int test = 1;
  75. int MP_HP = 1;
  76.  
  77.  
  78.  
  79. void CreateList()
  80. {
  81.         p_MP = (NODE*)malloc(sizeof(NODE));
  82.         p_MP->x = WINDOW_WIDTH / 2 - pw / 2;
  83.         p_MP->y = WINDOW_HEIGHT-100;
  84.         p_MP->pnext = NULL;
  85.  
  86.  
  87.         p_bullet = (NODE*)malloc(sizeof(NODE));
  88.         p_bullet->pnext = NULL;
  89.         b1 = GetTickCount();
  90.  
  91.  
  92.         p_AP = (NODE*)malloc(sizeof(NODE));
  93.         srand((unsigned)time(NULL));
  94.         p_AP->x = rand() % (WIDTH - aw) + (WINDOW_WIDTH / 2 - WIDTH / 2);
  95.         p_AP->y = APStart;
  96.         p_AP->pnext = NULL;
  97.         b3 = GetTickCount();
  98.  
  99.         p_AP2 = (NODE*)malloc(sizeof(NODE));
  100.         p_AP2->x = rand() % (WIDTH - aw) + (WINDOW_WIDTH / 2 - WIDTH / 2);
  101.         p_AP2->y = -350;
  102.         p_AP2->pnext = NULL;
  103.         b5 = GetTickCount();
  104. }
  105.  
  106. void GameBackInit()
  107. {
  108.         loadimage(&i_MP, L"mp.jpg");
  109.         loadimage(&i_MPS, L"mpbit.jpg");
  110.         loadimage(&i_backeve, L"backeve.jpg");
  111.         loadimage(&i_backxing, L"backtaikong.jpg");
  112.         loadimage(&i_AP, L"AP.jpg", aw, ah);
  113.         loadimage(&i_APS, L"APS.jpg", aw, ah);
  114.         loadimage(&i_backduicheng, L"backduicheng.jpg");
  115.         loadimage(&i_backguan, L"backguan.jpg", WIDTH, WINDOW_HEIGHT);
  116.         loadimage(&i_backcontrol, L"backcontrol.jpg",WINDOW_WIDTH,WINDOW_HEIGHT);
  117.         loadimage(&i_boss1_1, L"boss1_1.jpg");
  118.         loadimage(&i_boss1_1S, L"boss1_1S.jpg");
  119.         loadimage(&i_boss1_2, L"boss1_2.jpg");
  120.         loadimage(&i_boss1_2S, L"boss1_1S.jpg");
  121.         loadimage(&i_backgo, L"Gameover.jpg", WINDOW_WIDTH, WINDOW_HEIGHT);
  122.  
  123.  
  124.         putimage(0, 30, &i_backeve);
  125.         Sleep(1000);
  126.         PlaySound(L"baozou.wav", NULL, SND_FILENAME | SND_ASYNC);
  127.  
  128.         putimage(0, 0, &i_backcontrol);
  129.         outtextxy(600, 540, L"【PRESS】 按任意键进入游戏");
  130.         system("pause");
  131.  
  132.         mciSendString(L"open bgmusic.mp3 alias bg", NULL, 0, NULL);
  133.         mciSendString(L"play bg repeat", NULL, 0, NULL);
  134.  
  135.         putimage((WINDOW_WIDTH / 2 - WIDTH / 2), 0, WIDTH, WINDOW_HEIGHT, &i_backguan, 0, 0, SRCCOPY);
  136.         putimage(0, 0, (WINDOW_WIDTH / 2 - WIDTH / 2), WINDOW_HEIGHT, &i_backduicheng, 0, 100, SRCCOPY);
  137.         putimage((WINDOW_WIDTH / 2 + WIDTH / 2), 0, (WINDOW_WIDTH / 2 - WIDTH / 2), WINDOW_HEIGHT, &i_backduicheng, 1200 - (WINDOW_WIDTH / 2 - WIDTH / 2), 100, SRCCOPY);
  138.         //字体出现的高度
  139.         int text_h = WINDOW_HEIGHT/2;
  140.         Sleep(300);
  141.         BeginBatchDraw();
  142.         for (int i = 0; i < text_h; i++)
  143.         {
  144.                 clearrectangle((WINDOW_WIDTH / 2 - WIDTH / 2), 0, (WINDOW_WIDTH / 2 + WIDTH / 2), WINDOW_HEIGHT);
  145.                 putimage((WINDOW_WIDTH / 2 - WIDTH / 2), 0-i, WIDTH, text_h , &i_backguan, 0, 0, SRCCOPY);
  146.                 putimage((WINDOW_WIDTH / 2 - WIDTH / 2), text_h + i, WIDTH, WINDOW_HEIGHT - (text_h + i), &i_backguan, 0, text_h, SRCCOPY);
  147.                 putimage((WINDOW_WIDTH / 2 - WIDTH / 2), text_h - i, WIDTH, 2*i, &i_backxing, left, text_h-i, SRCCOPY);
  148.                 FlushBatchDraw();
  149.                 Sleep(5);
  150.         }
  151.         EndBatchDraw();
  152.  
  153.        
  154.         Sleep(100);
  155.  
  156.  
  157. }
  158.  
  159.  
  160. void Boss1show()
  161. {
  162.        
  163.         p_AP->y = WINDOW_HEIGHT + 100;
  164.         p_AP2->y = WINDOW_HEIGHT + 100;
  165.  
  166.         if (boss1hp >14)
  167.         {
  168.                 putimage(WINDOW_WIDTH / 2 - boss1w / 2, -boss1h + boss1image, &i_boss1_1S, NOTSRCERASE);
  169.                 putimage(WINDOW_WIDTH / 2 - boss1w / 2, -boss1h + boss1image, &i_boss1_1, SRCINVERT);
  170.         }
  171.         else if(boss1hp >= 9 && boss1hp <=14)
  172.         {
  173.                
  174.                 if (boss1hp % 2 == 0)
  175.                 {
  176.                         setlinecolor(0x996666);
  177.                         setlinestyle(PS_DOT, 3);
  178.                         line(line1_x, line1_y, line1_x, WINDOW_HEIGHT);
  179.                         line(line2_x, line2_y, line2_x, WINDOW_HEIGHT);
  180.  
  181.                         putimage(WINDOW_WIDTH / 2 - boss1w / 2, -boss1h + boss1image, &i_boss1_2S, NOTSRCERASE);
  182.                         putimage(WINDOW_WIDTH / 2 - boss1w / 2, -boss1h + boss1image, &i_boss1_2, SRCINVERT);
  183.                        
  184.                 }
  185.                 else
  186.                 {
  187.                         setlinecolor(0xCC6666);
  188.                         setlinestyle(PS_DOT, 3);
  189.                         line(line1_x, line1_y, line1_x, WINDOW_HEIGHT);
  190.                         line(line2_x, line2_y, line2_x, WINDOW_HEIGHT);
  191.  
  192.                        
  193.                         putimage(WINDOW_WIDTH / 2 - boss1w / 2, -boss1h + boss1image, &i_boss1_1S, NOTSRCERASE);
  194.                         putimage(WINDOW_WIDTH / 2 - boss1w / 2, -boss1h + boss1image, &i_boss1_1, SRCINVERT);
  195.                 }
  196.                
  197.                
  198.         }
  199.         else
  200.         {
  201.                
  202.                 if (test == 1)
  203.                 {
  204.                         PlaySound(L"Beam.wav", NULL, SND_FILENAME | SND_ASYNC);
  205.                         test++;
  206.                 }
  207.                 setlinecolor(0xFF6666);
  208.                 setlinestyle(PS_DASH, 5);
  209.                 line(line1_x, line1_y, line1_x, WINDOW_HEIGHT);
  210.                 line(line2_x, line2_y, line2_x, WINDOW_HEIGHT);
  211.                 line(WINDOW_WIDTH / 2 - boss1w / 2, boss1h -90, 482, boss1h + 50);
  212.                 line(WINDOW_WIDTH / 2 + boss1w / 2, boss1h - 90, 542, boss1h + 50);
  213.                 putimage(WINDOW_WIDTH / 2 - boss1w / 2, -boss1h + boss1image, &i_boss1_2S, NOTSRCERASE);
  214.                 putimage(WINDOW_WIDTH / 2 - boss1w / 2, -boss1h + boss1image, &i_boss1_2, SRCINVERT);
  215.                 if ((boss1hp!=8)&&(p_MP->x - line1_x) > -pw && (p_MP->x - line2_x)<0&& (p_MP->y - line1_y)>-ph)                 MP_HP = 0;
  216.         }
  217.  
  218.        
  219.         if(boss1image<=boss1h ) boss1image+=2;
  220.  
  221. }
  222.  
  223. void AddNode(int flag)
  224. {
  225.         //后插法,更新第二个位置
  226.         if (flag == 0)
  227.         {
  228.                 NODE* p_new = (NODE*)malloc(sizeof(NODE));
  229.  
  230.                 p_new->x = p_MP->x + 35;
  231.                 p_new->y = p_MP->y - 45;
  232.  
  233.                 p_new->pnext = p_bullet->pnext;
  234.                 p_bullet->pnext = p_new;
  235.         }
  236. }
  237.  
  238.  
  239.  
  240. int main()
  241. {
  242.         //create a window
  243.         initgraph(WINDOW_WIDTH, WINDOW_HEIGHT);
  244.  
  245.         setfillcolor(0xFF9999);
  246.  
  247.         GameBackInit();
  248.  
  249.         char key;
  250.         CreateList();
  251.         //批量绘图
  252.        
  253.         BeginBatchDraw();
  254.         while (1)
  255.         {      
  256.                
  257.                 //清画板,要不然就成重叠的残影了
  258.                 cleardevice();
  259.  
  260.  
  261.  
  262.                 putimage((WINDOW_WIDTH / 2 - WIDTH / 2), 0, WIDTH, WINDOW_HEIGHT, &i_backxing, left, 0, SRCCOPY);
  263.                 putimage(0, 0, (WINDOW_WIDTH / 2 - WIDTH / 2), WINDOW_HEIGHT, &i_backduicheng, 0, 100, SRCCOPY);
  264.                 putimage((WINDOW_WIDTH / 2 + WIDTH / 2), 0, (WINDOW_WIDTH / 2 - WIDTH / 2), WINDOW_HEIGHT, &i_backduicheng, 1200 - (WINDOW_WIDTH / 2 - WIDTH / 2), 100, SRCCOPY);
  265.  
  266.  
  267.                 putimage(p_MP->x, p_MP->y, &i_MPS, NOTSRCERASE);
  268.                 putimage(p_MP->x, p_MP->y, &i_MP, SRCINVERT);
  269.                 putimage(p_AP->x, p_AP->y, &i_APS, NOTSRCERASE);
  270.                 putimage(p_AP->x, p_AP->y, &i_AP, SRCINVERT);
  271.                 putimage(p_AP2->x, p_AP2->y, &i_APS, NOTSRCERASE);
  272.                 putimage(p_AP2->x, p_AP2->y, &i_AP, SRCINVERT);
  273.                
  274.                
  275.  
  276.                 //MP单位时间发射子弹的数量
  277.                 b2 = GetTickCount();
  278.                 //不能等于,有偏差
  279.                 if (b2 - b1 >= 600)
  280.                 {
  281.                         AddNode(0);
  282.                         b1 = b2;
  283.                 }
  284.  
  285.  
  286.  
  287.  
  288.                 //我方战机子弹递增
  289.                 NODE* P = p_bullet->pnext;
  290.  
  291.  
  292.                 while (P != NULL)
  293.                 {
  294.                         if (boss1show == 0)
  295.                         {
  296.  
  297.                                 //确定敌机重生位置不是在原位置
  298.                                 int mid;
  299.                                 //10是子弹宽度,但半个子弹打中也算,要不然太难了,就右边是-3,左边是-7
  300.                                 if ((P->y - p_AP->y) < ah && (P->y - p_AP->y) >= 0 && (P->x - p_AP->x) < aw -3 && (P->x - p_AP->x) >= -7)
  301.                                 {
  302.                                         P->y = APStart -100;
  303.                                         P = P->pnext;
  304.                                         p_AP->y = APStart;
  305.                                         kill++;
  306.  
  307.                                         PlaySound(L"Bomb.wav", NULL, SND_FILENAME | SND_ASYNC);
  308.                                         while (1)
  309.                                         {
  310.                                                 mid = rand() % (WIDTH - aw) + (WINDOW_WIDTH / 2 - WIDTH / 2);
  311.                                                 if (abs(mid - p_AP->x) > aw)
  312.                                                 {
  313.                                                         p_AP->x = mid;
  314.                                                         break;
  315.                                                 }
  316.                                         }
  317.                                        
  318.  
  319.                                 }
  320.                                 else if((P->y - p_AP2->y) < ah && (P->y - p_AP2->y) >= 0 && (P->x - p_AP2->x) < aw - 3 && (P->x - p_AP2->x) >= -7)
  321.                                 {
  322.                                         P->y = APStart -100;
  323.                                         P = P->pnext;
  324.                                         p_AP2->y = APStart;
  325.                                         kill++;
  326.  
  327.                                         while (1)
  328.                                         {
  329.                                                 mid = rand() % (WIDTH - aw) + (WINDOW_WIDTH / 2 - WIDTH / 2);
  330.                                                 if (abs(mid - p_AP2->x) > aw)
  331.                                                 {
  332.                                                         p_AP2->x = mid;
  333.                                                         break;
  334.                                                 }
  335.                                         }
  336.                                         PlaySound(L"Bomb.wav", NULL, SND_FILENAME | SND_ASYNC);
  337.                                 }
  338.                                 else
  339.                                 {
  340.                                         fillroundrect(P->x, P->y, P->x + 10, P->y + 35, 10, 30);
  341.                                         P->y -= 5;
  342.                                         P = P->pnext;
  343.                                 }
  344.                         }
  345.                         else if (boss1show == 1)
  346.                         {
  347.                                 if (boss1image > boss1h)
  348.                                 {
  349.                                         if ((P->y) < boss1h && (P->y) >= 0 && (P->x - (WINDOW_WIDTH / 2 - boss1w / 2)) < boss1w - 3 && (P->x - (WINDOW_WIDTH / 2 - boss1w / 2)) >= -7)
  350.                                         {
  351.                                                 P->y = APStart -100;
  352.                                                 P = P->pnext;
  353.                                                 boss1hp--;
  354.                                                 if (boss1hp>9||boss1hp<7)       PlaySound(L"Bomb.wav", NULL, SND_FILENAME | SND_ASYNC);
  355.                                         }
  356.                                         else
  357.                                         {
  358.                                                 fillroundrect(P->x, P->y, P->x + 10, P->y - 35, 10, 30);
  359.                                                 P->y -= 10;
  360.                                                 P = P->pnext;
  361.                                         }
  362.  
  363.                                         TCHAR s_boss1hp[100];
  364.                                         _stprintf(s_boss1hp, _T("【Boss】HP:%d"), boss1hp);
  365.                                         outtextxy((WINDOW_WIDTH / 2 + WIDTH / 2) + 45, 200, s_boss1hp);
  366.  
  367.                                         if (boss1hp <= 0)
  368.                                         {
  369.                                                 boss1show = 0;
  370.                                                 kill += 50;
  371.                                         }
  372.                                 }
  373.                                 else
  374.                                 {
  375.                                         fillroundrect(P->x, P->y, P->x + 10, P->y + 35, 10, 30);
  376.                                         P->y -= 5;
  377.                                         P = P->pnext;
  378.                                 }
  379.                         }
  380.                 }
  381.  
  382.  
  383.  
  384.  
  385.  
  386.                 //AP飞行的速度
  387.                 b4 = GetTickCount();
  388.                 //不能等于,有偏差
  389.                 if (b4- b3 >= 50)
  390.                 {
  391.                         if (p_AP->y < WINDOW_HEIGHT)
  392.                         {
  393.                                 p_AP->y += 3;
  394.                         }
  395.                         else
  396.                         {
  397.                                 p_AP->y = 0;
  398.                                 p_AP->x = rand() % (WIDTH - aw) + (WINDOW_WIDTH / 2 - WIDTH / 2);
  399.                         }
  400.                         b3 = b4;
  401.                 }
  402.  
  403.  
  404.  
  405.  
  406.                 //AP2飞行的速度
  407.                 b6 = GetTickCount();
  408.                 //不能等于,有偏差
  409.                 if (b6 - b5 >= 50)
  410.                 {
  411.                         if (p_AP2->y < WINDOW_HEIGHT)
  412.                         {
  413.                                 p_AP2->y += 3;
  414.                         }
  415.                         else
  416.                         {
  417.                                 p_AP2->y = 0;
  418.                                 p_AP2->x = rand() % (WIDTH - aw) + (WINDOW_WIDTH / 2 - WIDTH / 2);
  419.                         }
  420.                         b5 = b6;
  421.                 }
  422.                
  423.                
  424.  
  425.  
  426.  
  427.                 if (kill==10&& boss1hp > 0) boss1show = 1;
  428.  
  429.  
  430.  
  431.  
  432.                 if (boss1show==1)
  433.                 {
  434.                         Boss1show();
  435.                 }
  436.  
  437.  
  438.  
  439.  
  440.                 if ((p_MP->x - p_AP->x) > -pw && (p_MP->x - p_AP->x)<pw && (p_MP->y - p_AP->y)>-ph && (p_MP->y - p_AP->y)<ph )                  MP_HP = 0;
  441.                 else if ((p_MP->x - p_AP2->x) > -pw && (p_MP->x - p_AP2->x)<pw && (p_MP->y - p_AP2->y)>-ph && (p_MP->y - p_AP2->y)<ph)  MP_HP = 0;
  442.                 else if (boss1show==1&&boss1image>boss1h&&(p_MP->x-(WINDOW_WIDTH / 2 - boss1w / 2)) >-pw && (p_MP->x-(WINDOW_WIDTH / 2 + boss1w / 2))<pw && p_MP->y<boss1h)             MP_HP = 0;
  443.                
  444.                 if (MP_HP == 0)
  445.                 {
  446.                         mciSendString(L"close bg", NULL, 0, NULL);
  447.                         mciSendString(L"open bggo.mp3 alias bg", NULL, 0, NULL);
  448.                         mciSendString(L"play bg repeat", NULL, 0, NULL);
  449.                         putimage(0, 0, &i_backgo, SRCCOPY);
  450.                         outtextxy(430, 540, L"3秒后自动退出");
  451.                         EndBatchDraw();
  452.                         Sleep(3000);
  453.                         closegraph();
  454.                         return 0;
  455.                 }
  456.  
  457.                
  458.                 TCHAR s_score[100];
  459.                 _stprintf(s_score, _T("你的分数:%d"), kill);
  460.                 outtextxy((WINDOW_WIDTH / 2 + WIDTH / 2) + 50, WINDOW_HEIGHT/2, s_score);
  461.                
  462.                
  463.                
  464.  
  465.  
  466.                 FlushBatchDraw();
  467.  
  468.                
  469.                 //子弹飞行速度以及按键延迟等
  470.                 Sleep(15);
  471.  
  472.  
  473.  
  474.                 if (kbhit())
  475.                 {
  476.                         key = getch();
  477.                         switch (key)
  478.                         {
  479.                         case 72://上
  480.                                 p_MP->y -= 5;
  481.                                 break;
  482.                         case 80://下
  483.                                 p_MP->y += 5;
  484.                                 break;
  485.                         case 75://左
  486.                                 p_MP->x -= 5;
  487.                                 left -= 5;
  488.                                 break;
  489.                         case 77://右
  490.                                 p_MP->x += 5;
  491.                                 left += 5;
  492.                                 break;
  493.                         }
  494.                 }
  495.  
  496.  
  497.  
  498.                 if (p_MP->x<(WINDOW_WIDTH / 2 - WIDTH / 2))
  499.                         p_MP->x = (WINDOW_WIDTH / 2 - WIDTH / 2);
  500.                 if (p_MP->x>(WINDOW_WIDTH / 2 + WIDTH / 2 - pw))
  501.                         p_MP->x = (WINDOW_WIDTH / 2 + WIDTH / 2 - pw);
  502.                 if (p_MP->y<0 )
  503.                         p_MP->y = 0;
  504.                 if (p_MP->y>WINDOW_HEIGHT - ph)
  505.                         p_MP->y = WINDOW_HEIGHT - ph;
  506.  
  507.  
  508.                 if (left < 0)
  509.                         left = 0;
  510.                 if (left>1280 - WIDTH)
  511.                         left = 1280 - WIDTH;
  512.  
  513.         }
  514.        
  515.         EndBatchDraw();
  516.         closegraph();
  517.         return 0;
  518. }

回复 "c语言游戏 雷霆战机游戏源代码"

这儿你可以回复上面这条便签

captcha