douchun1859 2013-10-10 02:22
浏览 48
已采纳

如何使这个PHP / MySQL游戏地图更有效率?

I am currently working on a grid-based game map prototype. Each "tile" is 24px by 24px and the script draws out 31 x-based tiles and 21 y-based tiles for a total of 651 tiles on the screen.

The problem I'm having is that the way I have my script going, it's making a database call for every single tile that's drawn at every page refresh, meaning it's making 651 database calls to check what the tile is and the coordinates of the tile. To me this seems obscenely inefficient.

The x and y coordinates are stored in a session variable. When the user clicks one of the directional buttons below the map, it offsets the appropriate coordinate accordingly and redraws the map.

If I were to create a game based on the logic I'm using, and playing the number game let's hypothetically say I've got 100 players playing the game at any one time and they're all browsing through the map, I would have 65,100 database calls simultaneously. That can't be good for system resources.

I am not a noob when it comes to PHP/MySQL programming as I have created and launched a few different browser-based games, however, this is my first venture into generating a map and navigating around it.

The tile map can be viewed here:

http://fordserver.com/gamemap/

The actual code for this can be found here:

http://fordserver.com/gamemap/index.txt

Can anyone recommend a good way to get this running more smoothly? You'll notice that when using one of the directional buttons that the game can take quite awhile to generate the actual map, especially if you are just visiting it for the first time.

I've tried to think of different ways to use arrays and everything else, but really, the game needs to check the database for each individual tile as I plan to incorporate functions so that it shows where a specific user currently is, or a building, etc.

I appreciate any help I can get with this.

Ricky

  • 写回答

1条回答 默认 最新

  • duanrong6802 2013-10-10 02:36
    关注

    I once implemented a simple game that also used a cartesian coordinate system and had rows of data in the database for each point. I got around it simply by placing a single call along these lines:

    $xCord=34;
    $xCord=16;
    // Example data
    
    
    select
        ID,
        tileImage,
        xcord,
        ycord,
        otherStuff
    from
        mainLocationTable
    where
        abs(xCord-$xCord)<15
        and abs(yCord-$yCord)<15
    

    This will give you 15 tiles either side of the player location itself. If course you can customize this to suit your needs, but the principle is the same. If you store the x,y cartesian coordinates in the database, it's easy as pie to pull out all the data you need in a single query and go from there.

    As you also store the data of the player location in a session, you can simply have the code itself do further checking for details when it comes to the tile that the player is on - or use a second call to get much more detailed information for a single location.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 uniapp的uni-datetime-picker组件在ios端不适配
  • ¥15 前端 uniapp App端在离线状态如何使用modbus 连接手机蓝牙进行读写操控机器?
  • ¥15 SQL语句根据字段自动生成行
  • ¥500 “掌声响起来”软件(不确定性人工智能)
  • ¥500 我要找大模型一体机产品手册和应用案例
  • ¥60 用r语言进行数据分析
  • ¥20 关于游戏c++语言代码问题
  • ¥15 如何制作永久二维码,最好是微信也可以扫开的。(相关搜索:管理系统)
  • ¥15 delphi indy cookie 有效期
  • ¥15 labelme打不开怎么办