Android 可缩放和可滚动棋盘游戏

发布于 2024-11-14 06:38:37 字数 1139 浏览 3 评论 0原文

我正在开发一款 Android 棋盘游戏,并且有一个关于创建可缩放和可滚动棋盘的问题。棋盘包含静态背景、角色(玩家)和使用图块绘制的实际“关卡”。

我的解决方案是拥有一个元素集合(图块、图形、所有游戏元素 - 都有 x、y 坐标和宽度 + 高度)、一个相机和一个渲染器,该渲染器根据cameraX、cameraY、cameraWidth 和cameraHeight 绘制集合。因此,如果用户向右滚动,相机只会适当地设置cameraX - 并且表面是可滚动的。如果用户放大/缩小,渲染器只会适当地缩放每个元素图像。

具有可滚动表面和放大/缩小的渲染器的示例代码

    protected function draw(Canvas c){
      Collection elements = collection.getElements(cameraX,cameraY,cameraWidth,cameraHeight);
      if(elements.size() > 0) {
         for(int i = 0; i < elements.size(); i++) {
              elements.get(i).drawElement(c);
         }
      }
    }
    .
    .
    .
    // element class drawElement function
    protected drawElement function(Canvas c) {
        if(this.image != null) {            
          int w = this.width;
          int h = this.height;
          if(this.zoomFactor < 1) {
             w*=this.zoomFactor;
             h*=this.zoomFactor;
          }
          c.drawBitmap(this.image,this.x,this.y,w,h);
        }
    }
  • 这是最好的解决方案吗?
  • 可以通过其他方式实现吗?
  • 可以使用 ScrollView 实现滚动吗?

我不想使用任何引擎,因为这是一个学校项目。

I'm developing a Android board game and have a question regarding creating a board that is zoomable and scrollable. The board contains a static background, characters (players) and the actual "level" which is drawn using tiles.

My solutions is to have a collection of elements (tiles, figures, all game elements - all have x,y coordinates and width + height), a camera and a renderer that draws the collection according to cameraX,cameraY, cameraWidth and cameraHeight. So if a user would scroll to the right, the camera would just set the cameraX appropriately - and the surface is scrollable. And if a user would zoom in/out the renderer would just scale every element image appropriately.

Example code for the renderer with scrollable surface and zoom in/out

    protected function draw(Canvas c){
      Collection elements = collection.getElements(cameraX,cameraY,cameraWidth,cameraHeight);
      if(elements.size() > 0) {
         for(int i = 0; i < elements.size(); i++) {
              elements.get(i).drawElement(c);
         }
      }
    }
    .
    .
    .
    // element class drawElement function
    protected drawElement function(Canvas c) {
        if(this.image != null) {            
          int w = this.width;
          int h = this.height;
          if(this.zoomFactor < 1) {
             w*=this.zoomFactor;
             h*=this.zoomFactor;
          }
          c.drawBitmap(this.image,this.x,this.y,w,h);
        }
    }
  • Is this the best solutions?
  • Could it be achived somehow else?
  • Could scrolling be achived using a ScrollView?

I dont wanna use any engine, because this is for a school project.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

失与倦" 2024-11-21 06:38:37

实际上你可以稍微简化一下这种情况。如果您确实正在寻找一个简单地因透视而扭曲的平坦纹理平面,那么 Android Camera 类可以帮助您。不要将其与用于拍照的硬件相机混淆。该相机是一个围绕矩阵的辅助类,用于对 2D 对象执行转换。您可以通过谷歌搜索“快速傅里叶变换”来阅读有关这个​​非常复杂的渲染主题的更多信息。基本上,您需要创建一个画布并以完全 2D 的方式进行绘图。然后,在绘制到屏幕之前,您应该使用 Camera 类转换此画布。如果您需要澄清,请告诉我。幕后有很多很酷的数学!

查看 Android API 演示中的示例

http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/animation/Rotate3dAnimation.html

Android graphics.Camera文档

http://developer.android.com/reference/android/graphics/Camera。 html

Actually you can simplify this situation somewhat. If you are indeed seeking a flat texture plane that is simply distorted by perspective, the Android Camera class can help you. Do not confuse this with the hardware camera for taking photos. This camera is a helper class wrapped around a matrix to perform transformations on 2D objects. You can read more about this very complex rendering topic by googling "fast fourier transforms". Basically you will want to create a canvas and do your drawing in a completely 2D way. Then right before you draw to the screen, you should transform this canvas using the Camera class. Let me know if you need some clarification. There is a lot of cool mathematics going on behind the scenes!

Take a look at this sample from the Android API Demos

http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/animation/Rotate3dAnimation.html

Android graphics.Camera documentation

http://developer.android.com/reference/android/graphics/Camera.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文