鱼C论坛

 找回密码
 立即注册
查看: 2072|回复: 7

多个链表里的int变量加减

[复制链接]
发表于 2020-5-29 18:47:34 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. #include <stdio.h>
  2. #include <graphics.h>
  3. #include <windows.h>
  4. #include <mmsystem.h>
  5. #include <time.h>
  6. #include <conio.h>
  7. #include <stdlib.h>
  8. #pragma comment (lib,"winmm.lib")

  9. constexpr auto WINDOWSLENGTH = 640;
  10. constexpr auto WINDOWSHIGHT = 480+20;


  11. IMAGE background, apple1, apple2;
  12. //字母结构体
  13. typedef struct Letter
  14. {
  15.     //数据域
  16.     int x, y;//字母坐标
  17.     char ch;//字母ASCII码
  18.     //指针域;
  19.     struct Letter* pnext = NULL;
  20. }LETTER;
  21. typedef LETTER* Pt_letter;

  22. //初始化函数
  23. char str[10];
  24. void GameInit()
  25. {
  26.     srand((unsigned int)time(NULL));//随机数种子
  27.     //初始化窗口
  28.     initgraph(WINDOWSLENGTH, WINDOWSHIGHT);
  29.     //初始化背景音乐
  30.     mciSendString(L"open ./AppleGame/3.mp3", 0, 0, 0);
  31.     mciSendString(L"play ./AppleGame/3.mp3", 0, 0, 0);
  32.     //加载图片
  33.     loadimage(&background,L"./AppleGame/0.png", WINDOWSLENGTH, WINDOWSHIGHT);
  34.     loadimage(&apple1, L"./AppleGame/1.png",50,50);
  35.     loadimage(&apple2, L"./AppleGame/2.png",50,50);

  36.     settextstyle(25, 20, L"宋体");
  37.     setbkmode(0);
  38. }
  39. //创建链表头结点
  40. Pt_letter Listinit()
  41. {
  42.     Pt_letter p = (Pt_letter)malloc(sizeof(LETTER));
  43.     if (p == NULL)
  44.     {
  45.         return 0;
  46.     }
  47.     p->pnext = NULL;
  48.     return p;
  49. }
  50. //输出图片
  51. void GamePoint(Pt_letter pletter)
  52. {

  53.     BeginBatchDraw();//批量绘制
  54.     //背景图片
  55.     putimage(0, 0, &background);
  56.     //苹果
  57.     putimage(0, 0, &background);
  58.     //字母
  59.     pletter = pletter->pnext;
  60.     while (pletter != NULL)
  61.     {
  62.         
  63.         putimage(pletter->x - 14, pletter->y - 17, &apple2,SRCPAINT);//三光栅,把apple2变透明,这个是win
  64.         putimage(pletter->x-14, pletter->y-17, &apple1,SRCAND);
  65.         outtextxy(pletter->x, pletter->y, pletter->ch);
  66.         
  67.         pletter = pletter->pnext;
  68.     }
  69.    
  70.     //putimage(100, 100, &apple1);//为了测试苹果和字母的关系
  71.     //outtextxy(100+14, 100+17,L"B");
  72.     EndBatchDraw();
  73. }
  74. //添加链表结点
  75. void AddListNode(Pt_letter pletter)
  76. {
  77.     //为了释放方便,用尾插法
  78.     while (pletter->pnext != NULL)
  79.     {
  80.         pletter = pletter->pnext;
  81.     }
  82.     Pt_letter pnode = (Pt_letter)malloc(sizeof(LETTER));
  83.     pnode->x = rand() % 591 + 14;
  84.     pnode->y = 17;
  85.     pnode->ch = rand() % 26 + 'A';
  86.     pnode->pnext = pletter->pnext; //为了让这个结点的pnext指向NULL
  87.         pletter->pnext = pnode; //把头结点和下一个结点相连,从第二次开始,作用就是连接各个结点。

  88. }

  89. void AppleMove(Pt_letter pletter , int speedy)
  90. {
  91.     int speedx = 1;
  92.     Pt_letter pfront = pletter;
  93.     pletter = pletter->pnext;
  94.     while (pletter != NULL)
  95.     {
  96.         pletter->y += speedy;
  97.         if (rand() % 2 == 0)
  98.             pletter->x += speedx;
  99.         else
  100.             pletter->x -= speedx;
  101.         if (pletter->y >= WINDOWSHIGHT)
  102.         {
  103.             pfront->pnext = pletter->pnext;
  104.             free(pletter);
  105.             pletter = pfront;
  106.         }
  107.         pletter = pletter->pnext;
  108.     }
  109. }
  110. //删除结点
  111. void FreePnode(Pt_letter pletter, char key)
  112. {
  113.     int score = 0;
  114.     Pt_letter pfront = pletter;//再pletter的前面的结点
  115.    
  116.     pletter = pletter->pnext;
  117.     while (pletter != NULL )
  118.     {
  119.         if (pletter->ch ==  key || pletter ->ch == key +'A')
  120.         {
  121.             pfront ->pnext = pletter->pnext;
  122.             free(pletter);
  123.             score = 10;
  124.             break;
  125.         }
  126.         else
  127.         {
  128.             pfront = pfront -> pnext;
  129.             pletter = pletter->pnext;
  130.         }
  131.     }
  132.   
  133. }
  134. int main()
  135. {
  136.    
  137.     GameInit();
  138.     Pt_letter pletter = Listinit();
  139.     DWORD t1, t2 ,tt1 ,tt2;
  140.     int speedy = 2;
  141.     char key;
  142.     int score = 0;
  143.     tt1 = GetTickCount();
  144.     t1 = GetTickCount();
  145.    
  146.     while(1)
  147.     {
  148.         
  149.         t2 = GetTickCount();
  150.         if (t2 - t1 >= 1000)
  151.         {
  152.             AddListNode(pletter);
  153.             
  154.             t1 = t2;
  155.         }
  156.         tt2 = GetTickCount();
  157.         if (tt2 - tt1 >= 20)
  158.         {
  159.             AppleMove(pletter, speedy);
  160.             tt1 = tt2;
  161.         }
  162.         GamePoint(pletter);

  163.         //删除链表
  164.         if (_kbhit())
  165.         {
  166.             key = _getch();
  167.             //调节速度 六个档,这里可以随着速度来变换歌曲。。。。。。。。。。。。。暂时还不会
  168.             if (key == '1')            
  169.                 speedy = 1;           
  170.             if (key == '2')
  171.                 speedy = 6;         
  172.             if (key == '3')            
  173.                 speedy = 10;         
  174.              FreePnode(pletter, key);
  175.         }               
  176.     }
  177.    

  178.    
  179.     system("pause");
  180.     return 0;
  181. }
复制代码


97-117也就是void AppleMove(Pt_letter pletter , int speedy)这个函数,不知道是怎么往下走和横着变换的,他不就循环一次就进入下一个链表里了吗,怎么能移动到最下面呢。
56-74也就是void GamePoint(Pt_letter pletter)这个函数,和上面同问,进入循环一次就进入下一次链表,怎么连环输出图片的呢?
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-5-29 18:51:05 | 显示全部楼层

回帖奖励 +3 鱼币

鱼币
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2020-5-29 21:18:30 | 显示全部楼层

不能老这样吧,拿钱不办事
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-5-29 21:35:26 | 显示全部楼层
Juniorboy 发表于 2020-5-29 21:18
不能老这样吧,拿钱不办事


你见我发的欧拉计划的翻译里面,有拿了钱做了题的吗?没错我在为自己白嫖找借口
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-5-29 22:29:50 | 显示全部楼层

回帖奖励 +3 鱼币

永恒的蓝色梦想 发表于 2020-5-29 21:35
你见我发的欧拉计划的翻译里面,有拿了钱做了题的吗?没错我在为自己白嫖找借口

老哥这么多荣誉还缺鱼币?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-5-30 07:57:42 | 显示全部楼层
麻麦皮 发表于 2020-5-29 22:29
老哥这么多荣誉还缺鱼币?

我没什么鱼币,都扔到竞价榜上玩了,因为基本也用不到。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-5-30 17:46:15 | 显示全部楼层

回帖奖励 +3 鱼币

鱼币
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2020-6-1 22:24:39 | 显示全部楼层
别沉啊,别沉啊~别光领鱼币啊
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-7-12 17:53

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表