I want to create a simple sushi game where plates of sushi are moved on a platform in front of you (only in the horizontal direction; no vertical movement), and you have to collect certain plates of sushi for points. I'm new to game development.
This is what I currently have in Android Studio (took out irrelevant parts of the code). I can't figure out how to get the sushi images to scroll across the screen. I'm also very lost as to how I'm supposed to "tick" the game. I added a timer, but can't wrap my head around how to use it. Should I have a while loop somewhere that keeps updating the game as long as a boolean variable gameOver is false?
Currently, all I get when I run the app is the background image.
Any help would be appreciated. Thanks!
public class MainActivity extends ActionBarActivity {
private static int highScore;
private Canvas canvas = new Canvas();
// View view = (View) findViewById(R.id.imageView);
ImageView imageView;
Timer timer;
List<Sushi> sushis = new ArrayList<Sushi>();
List<Integer> sushiImages = new ArrayList<Integer>();
private static int points = 0;
private boolean isGameOver = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = new ImageView(this);
loadSushiImages();
makeSushi();
addTimer();
}
public void loadSushiImages() {
sushiImages.add(R.drawable.ebi);
sushiImages.add(R.drawable.anago);
}
public int getScreenWidth() {
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
return dm.widthPixels;
}
public void update() {
moveSushis();
imageView.invalidate();
drawSushis();
}
public void moveSushis() {
for (int i = 0; i < sushis.size(); i++) {
Sushi s = sushis.get(i);
s.move();
if (s.getX() > getScreenWidth()) {
sushis.remove(s);
makeSushi();
}
}
}
public void drawSushis() {
for (int i = 0; i < sushis.size(); i++) {
Drawable icon = getResources().getDrawable(sushis.get(i).getPicNumber());
icon.setBounds(
0 - icon.getIntrinsicWidth() / 2, 0 - icon.getIntrinsicHeight(),
icon.getIntrinsicWidth() / 2, 0);
icon.draw(canvas);
}
}
public void makeSushi() {
Random r = new Random();
int sushiNumber = sushiImages.get(r.nextInt(sushiImages.size() - 1));
Sushi s = new Sushi(sushiNumber);
sushis.add(s);
Drawable icon = getResources().getDrawable(sushiNumber);
icon.setBounds(
0 - icon.getIntrinsicWidth() / 2, 0 - icon.getIntrinsicHeight(),
icon.getIntrinsicWidth() / 2, 0);
icon.draw(canvas);
/* Bitmap b = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b); */
}
private void addTimer() {
timer = new Timer(true);
// update();
}
}
Aucun commentaire:
Enregistrer un commentaire