Reputation: 303
I'm trying to add some items from a csv file into my canvas view. I'm making a game where you "dig" a hole and get some loot. Like a minesweeper type of game. But I'm not sure how to add the items at random on the canvas. All my items are in an arraylist, but not sure how to add them on the game. Then update my score whenever loot has been found.
My code:
// List of items from CSV file.
private ArrayList<ItemObject> mLoot;
private String itemSize;
@Override
protected void onFinishInflate() {
super.onFinishInflate();
// Let the view know that we want to draw.
setWillNotDraw(false);
getHolder().addCallback(this);
Resources res = getResources();
// background image
mBackground = BitmapFactory.decodeResource(res, R.drawable.field);
mBlackPaint = new Paint();
// Score text
mScoreTextPaint = new Paint();
mScoreTextPaint.setColor(Color.WHITE);
mScoreTextPaint.setTextSize(60.0f);
mScoreTextPaint.setTextAlign(Paint.Align.CENTER);
// Show hole image
mHole = BitmapFactory.decodeResource(res, R.drawable.hole);
mPoints = new ArrayList<>();
// Get CSV items
InputStream inputStream = getResources().openRawResource(R.raw.items);
GetCSVFile csvFile = new GetCSVFile(inputStream);
mLoot = csvFile.read();
itemSize = String.valueOf(mLoot.size());
for(ItemObject itemData:mLoot ) {
//TODO: add a random x,y position for loot?
// itemData.getLoot() - "gold"
// itemData.getValue() - "50"
}
My onDraw method:
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
// Clear the canvas by drawing a single color.
canvas.drawColor(Color.BLACK);
// Draw image to cover all dimensions of the screen.
canvas.drawBitmap(mBackground, null, mDimensions, mBlackPaint);
// Draw a circle at each touch.
for(Point p: mPoints){
// show hole's image when a player touches the screen
canvas.drawBitmap(mHole,p.x,p.y,null);
}
// Draw text in middle of screen.
canvas.drawText("Hidden Treasure: " + itemSize,
mDimensions.width() / 2.0f,
mDimensions.height() / 15.0f,
mScoreTextPaint);
} // end onDraw
Upvotes: 0
Views: 78
Reputation: 4848
If you are using the entire screen then get the screen width and height:
DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
int maxHeight = displayMetrics.heightPixels;
int maxWidth = displayMetrics.widthPixels;
Otherwise determine the maxX and maxY values you need.
Create a little method to get a random position;
private int randomPosition(int min, int max)
{
int range = (max - min) + 1;
return (int)(Math.random() * range) + min;
}
In your "ItemObject" you should add the setters and getters for the object coordinates:
String item = "";
int x = 0;
int y = 0;
public void setItem(String item) { this.item = item; }
public void setX(int x) { this.x = x; }
public void setY(int y) { this.y = y; }
public String getItem() { return this.item; }
public int getX() { return this.x; }
public int getY() { return this.y; }
After you get the content of your csv File as a string you can call a split on the delimiter:
String myDelimiter = ","; // Or what ever your delimiter is
String[] lines = csvData.Split(myDelimiter);
Now loop through the lines and get he random positions;
for(String s : lines ) {
// if 0 is the smallest x coordinates
int x = random(0, maxWidth);
int y = random(0, maxHeight);
ItemObject item = new ItemObject();
item.setItem(s)
item.setX(x);
item.setY(y);
mLoot.add(item);
}
In your drawing routine just use step through "mLoot" and draw the objects.
Upvotes: 1