The simple application of data structure in game

Source: Internet
Author: User

In the game's writing, inevitably appear many application data structure place, some simple game, just by a few data structure combination, so that the data structure in the game programming plays a very important role.

This paper mainly describes the application of data structure in the game, including the list, sequence table, stack, queue, binary tree and the introduction of the graph. Before reading this article, readers should have a good understanding of the data structure and familiar with the various functions of C + + language. OK, now let's start with the list!

1, linked list

In this section, we will use a similar thunder and lightning aircraft shooting game to explain the application of the linked list in the game. In aircraft games, the linked list is mainly used in the bomb module. First of all, the plane's bullets are to appear frequently, eliminate, the number is also unpredictable. The main advantage of the linked list is that it is easy to insert, delete operation. We then introduce the data structure of the linked list. First, the following source code is analyzed, in which we define the coordinate structure and the bullet list.

struct CPOINT
  {
    int x;  // X轴坐标
    int y;  // Y轴坐标
  };
  struct BULLET
  {
    struct BULLE* next;  // 指向下一个子弹
    CPOINT bulletpos;  // 子弹的坐标
    int m_ispeed;  // 子弹的速度
  };


  接下来的代码清单是飞机类中关于子弹的定义:

  class CMYPLANE
  {
  public:
    void AddBullet(struct BULLET*);  // 加入子弹的函数,每隔一定时间加弹
    void RefreshBullet();  // 刷新子弹
  privated:
    struct BULLET *st_llMyBullet;  // 声明飞机的子弹链表
  };

in void Addbullet (struct bullet*), what we do is just insert a node into the list, and every once in a while, the effect of successive rounds is produced.

This is the main source code for the bullet function:

void AddBullet(struct BULLET*)
  {
    struct BULLET *st_llNew,*st_llTemp;  // 定义临时链表
    st_llNew=_StrucHead;  // 链表头(已初始化)
    st_llNew->(BULLET st_llMyBullet *)malloc(sizeof(st_llMyBullet));  // 分配内存
    st_llTemp= =_NewBullet;  // 临时存值
    st_llNew->next=st_llTemp->next; st_llTemp->next=st_llNew;
  }


  函数Void RefreshBullet()中,我们只要将链表历遍一次就行,将子弹的各种数据更新,其中主要的源代码如下:

  while(st_llMyBullet->next!=NULL)
  {
    // 查找
    st_llMyBullet->bulletpos.x-=m_ispeed;  // 更新子弹数据
    ………
    st_llMyBullet=st_llMyBullet->next;  // 查找运算
  }

Through the above analysis, in the game, the linked list is mainly used in the large-scale deletion, add the application. However, it also has the corresponding disadvantage, that is, the query is sequential lookup, more time-consuming, and storage density is small, the demand for space is larger.

If through some control of game data, limit large-scale add, that is to determine the upper limit of memory demand, can apply sequential table to replace the list, in some cases, the sequential table can make up the loss of the time performance of the linked list. Of course, the application of linked lists, sequential tables or mainly rely on the specific situation at that time. So, now, into our next section, the most widely used data structure in the game-sequential table.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.