Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8941381
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T11:17:09+00:00 2026-06-15T11:17:09+00:00

I’m developing a simple game like BSD robots . This game contains a rather

  • 0

I’m developing a simple game like BSD robots. This game contains a rather large board (over 40 cells) and it doesn’t look nice even at 10 inch tablet. My decision was to scroll (move) surface view and not to scale each figure which size is like finger spot.
I’ve read tons articles about surface view but I cannot understand how to move it?

My activity code:

package ru.onyanov.robots;
public class MainActivity extends Activity {

public BoardView board;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
    requestWindowFeature(Window.FEATURE_NO_TITLE);

    board = new BoardView(this);
    setContentView(board);
}
}

Class Board:

package ru.onyanov.robots;
public class BoardView extends SurfaceView {
private GameThread mThread;
private boolean running = false;
public final int sizeX = 50;
public final int sizeY = 35;
public final int cellSize = 64;

private int robotCount = 4;

public ArrayList<Cell> cells = new ArrayList<Cell>();
private ArrayList<Robot> robots = new ArrayList<Robot>();
private Hero hero;
public Bitmap imageCell;
public Bitmap imageRobot;
public Bitmap imageHero;

public BoardView(Context context) {
    super(context);     

    makeGraphic();
    constructCells();
    constructRobots();      
    hero = new Hero(this, 3, 2, imageHero);

    mThread = new GameThread(this);

    getHolder().addCallback(new SurfaceHolder.Callback() {
        public void surfaceDestroyed(SurfaceHolder holder) {
            boolean retry = true;
            mThread.setRunning(false);
            while (retry) {
                try {
                    mThread.join();
                    retry = false;
                } catch (InterruptedException e) {
                }
            }
        }

        public void surfaceCreated(SurfaceHolder holder) {
            mThread.setRunning(true);
            mThread.start();
        }

        public void surfaceChanged(SurfaceHolder holder, int format,
                int width, int height) {


        }
    });
}

private void makeGraphic() {
    Bitmap cellImageSource = BitmapFactory.decodeResource(getResources(),
            R.drawable.cell);
    imageCell = Bitmap.createScaledBitmap(cellImageSource, cellSize,
            cellSize, false);
    imageRobot = BitmapFactory.decodeResource(getResources(),
            R.drawable.robot);
    imageHero = BitmapFactory.decodeResource(getResources(),
            R.drawable.hero);

}

private void constructRobots() {
    Robot robot;
    Random rand = new Random();
    for (int r = 0; r < robotCount; r++) {
        int x = rand.nextInt(sizeX);
        int y = rand.nextInt(sizeY);
        robot = new Robot(this, x, y, imageRobot);
        robots.add(robot);
    }
    return;
}

private void constructCells() {
    Cell cell;
    for (int y = 0; y < sizeY; y++) {
        for (int x = 0; x < sizeX; x++) {
            cell = new Cell(this, x, y, imageCell);
            cells.add(cell);
        }
    }
    return;
}

protected void onDraw(Canvas canvas) {
    canvas.drawColor(Color.WHITE);
    Cell cell;
    for (int c = 0; c < cells.size(); c++) {
        cell = cells.get(c);
        cell.onDraw(canvas);
    }
    Robot robot;
    for (int r = 0; r < robots.size(); r++) {
        robot = robots.get(r);
        robot.onDraw(canvas);
    }
    hero.onDraw(canvas);
}

public boolean onTouchEvent(MotionEvent e) {
    int shotX = (int) e.getX();
    int shotY = (int) e.getY();

    if (e.getAction() == MotionEvent.ACTION_MOVE){
        //TODO move board
        showToast("move Board");
    } else if (e.getAction() == MotionEvent.ACTION_UP) {
        showToast("touchPoint: " + shotX + ", "+shotY);
        hero.moveByDirection(shotX, shotY);
        this.scrollTo(shotX, shotY);
    }
    return true;
}

public void showToast(String mes) {
    Toast toast = Toast.makeText(getContext(), mes, Toast.LENGTH_LONG);
    toast.show();
}

public class GameThread extends Thread {
    private BoardView view;

    public GameThread(BoardView view) {
        this.view = view;

    }

    public void setRunning(boolean run) {
        running = run;
    }

    public void run() {
        while (running) {
            Canvas canvas = null;
            try {
                canvas = view.getHolder().lockCanvas();
                synchronized (view.getHolder()) {
                    onDraw(canvas);                     
                }
                canvas.scale(300, 300);
            } catch (Exception e) {
            } finally {
                if (canvas != null) {
                    view.getHolder().unlockCanvasAndPost(canvas);
                }
            }
        }
    }
}
}

Some magic digits here are temporary. Now I just want to move BoardView;

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T11:17:10+00:00Added an answer on June 15, 2026 at 11:17 am

    The issue was to add x and y fields to SurfaceView, change them at onTouchEvent and draw all the elements in canvas with x- and y-offset. It’s strange that I didn’t recieved any answers…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
For some reason, after submitting a string like this Jack’s Spindle from a text
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace
I have this code to decode numeric html entities to the UTF8 equivalent character.
I am doing a simple coin flipping experiment for class that involves flipping a
I am trying to render a haml file in a javascript response like so:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.