/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package com.raceontarantasses; import android.app.Activity; import android.content.Context; import android.content.Intent; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Paint.Align; import android.media.MediaPlayer; import android.util.AttributeSet; import android.view.Display; import android.view.SurfaceHolder; import android.view.SurfaceView; import android.view.View; import android.view.View.OnClickListener; import samodelkin.androidsimplestgamelibrary.*; /** * * @author Сергей */ public class MyGameSurface extends SurfaceView implements SurfaceHolder.Callback, Runnable, OnClickListener { private Activity activity; private Tarantas player; private EnemyTarantas enemy; private LayerSprite decoration1, decoration2, decoration3, decoration4; //декорации для уровней private LayerTiles map; private int[] indexes = { 1, 1, 1, 1, 1, 1 }; private LayerSprite finish; private LayerChief lc; private Display d; private int level; private boolean stopping; private MediaPlayer mp; public MyGameSurface(Context context) { super(context); } public MyGameSurface(Context context, AttributeSet attrs) { super(context); activity=(Activity) context; d = activity.getWindowManager().getDefaultDisplay(); player = new Tarantas(new int[]{R.drawable.s1, R.drawable.s2, R.drawable.s3, R.drawable.s4, R.drawable.s5}, getResources()); player.setFrameSequence(new int[]{0, 0, 1, 1, 2, 2, 3, 3, 4, 4}); player.setPosition(d.getWidth() / 3, d.getHeight() - player.getHeight() - 50); enemy = new EnemyTarantas(new int[]{R.drawable.s21, R.drawable.s22, R.drawable.s23, R.drawable.s24, R.drawable.s25}, getResources()); enemy.setPosition(d.getWidth() / 2, d.getHeight() - enemy.getHeight() - 90); enemy.setFrameSequence(new int[]{0, 0, 1, 1, 2, 2, 3, 3, 4, 4}); map = new LayerTiles(getResources(), new int[]{R.drawable.bg}, indexes, 6, 1); map.setPosition(0, d.getHeight() - map.getTileHeight()); finish = new LayerSprite(R.drawable.finish, getResources()); finish.setPosition(3000, d.getHeight() - 260); decoration1 = new LayerSprite(R.drawable.decor1, getResources()); decoration1.setPosition(0, d.getHeight() - decoration1.getHeight()); decoration2 = new LayerSprite(R.drawable.decor2, getResources()); decoration2.setPosition(0, d.getHeight() - decoration2.getHeight()); decoration3 = new LayerSprite(R.drawable.decor3, getResources()); decoration3.setPosition(0, d.getHeight() - decoration3.getHeight()); decoration4 = new LayerSprite(R.drawable.decor4, getResources()); decoration4.setPosition(0, d.getHeight() - decoration4.getHeight()); lc = new LayerChief(); lc.append(map); lc.append(finish); lc.append(enemy); lc.append(player); lc.append(decoration1); level = 1; stopping = false; mp=MediaPlayer.create(context, R.raw.game); mp.setLooping(true); mp.start(); getHolder().addCallback(this); this.setOnClickListener(this); } public void run() { while (!stopping) { Canvas c = getHolder().lockCanvas(); c.drawColor(Color.BLACK); draw(c); getHolder().unlockCanvasAndPost(c); update(); try { Thread.sleep(50); } catch (Exception e) { } } } public void draw(Canvas c) { lc.paint(c); } private void drawMessage(Canvas c, String s) { Paint p = new Paint(); p.setColor(Color.BLACK); p.setTextSize(20); p.setTextAlign(Align.CENTER); c.drawText(s, d.getWidth() / 2, d.getHeight() / 2, p); } public void update() { //обновление игрока if (player.getSpeedX() > 0) { player.update(); player.downSpeed(0.2); } //обновление соперника enemy.update(); //задаем положение окна просмотра относительно игрока lc.setView(player.getX() - (d.getWidth() / 3), 0); //обработка проигрыша if (enemy.getX() > finish.getX() + 200 && player.getX() < enemy.getX()) { Canvas c = getHolder().lockCanvas(); drawMessage(c, "Вы проиграли."); getHolder().unlockCanvasAndPost(c); try { Thread.sleep(3000); } catch (Exception e) { } stopping = true; mp.stop(); } //обработка выигрыша if (player.getX() > finish.getX() + 200 && enemy.getX() < player.getX()) { Canvas c = getHolder().lockCanvas(); drawMessage(c, "Уровень "+level+" пройден!"); getHolder().unlockCanvasAndPost(c); try { Thread.sleep(3000); } catch (Exception e) { } level++; nextLevel(level); } //обработка прохождения игры if (level==5){ Intent i=new Intent(activity,TitreActivity.class); activity.startActivity(i); stopping=true; try{ mp.stop(); }catch(Exception e){} } } private void nextLevel(int level) { player.setPosition(d.getWidth() / 3, d.getHeight() - player.getHeight() - 50); player.resetSpeed(); enemy.setPosition(d.getWidth() / 2, d.getHeight() - enemy.getHeight() - 90); enemy.resetSpeed(); switch (level) { case 2: lc.remove(decoration1); lc.append(decoration2); break; case 3: lc.remove(decoration2); lc.append(decoration3); break; case 4: lc.remove(decoration3); lc.append(decoration4); break; } } public void onClick(View v) { player.upSpeed(3); } public void surfaceCreated(SurfaceHolder holder) { new Thread(this).start(); } public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { } public void surfaceDestroyed(SurfaceHolder holder) { } }