Snake Apple Snake Tile View Layou t private

  • Slides: 22
Download presentation

遊戲架構 Snake(蛇) Apple(食物) Snake Tile View (面板) Layou t

遊戲架構 Snake(蛇) Apple(食物) Snake Tile View (面板) Layou t

建立新的遊戲 private void init. New. Game() { m. Snake. Trail. clear(); m. Apple. List.

建立新的遊戲 private void init. New. Game() { m. Snake. Trail. clear(); m. Apple. List. clear();

繪製牆壁 private void update. Walls() { for ( int x = 0 ; x

繪製牆壁 private void update. Walls() { for ( int x = 0 ; x < m. XTile. Count; x++) { set. Tile(GREEN_STAR, x, 0 ); set. Tile(GREEN_STAR, x, m. YTile. Count 1 ); } for ( int y = 1 ; y < m. YTile. Count - 1 ; y++) { set. Tile(GREEN_STAR, 0 , y); set. Tile(GREEN_STAR, m. XTile. Count - 1 , y);

蛇-初始位置 m. Snake. Trail. add( new Coordinate( 7 , 7 )); m. Snake. Trail.

蛇-初始位置 m. Snake. Trail. add( new Coordinate( 7 , 7 )); m. Snake. Trail. add( new Coordinate( 6 , 7 )); m. Snake. Trail. add( new Coordinate( 5 , 7 )); m. Snake. Trail. add( new Coordinate( 4 , 7 )); m. Snake. Trail. add( new Coordinate( 3 , 7 )); m. Snake. Trail. add( new Coordinate( 2 , 7 )); m. Next. Direction = NORTH;

蛇-移動 分成上(北)下(南)左(西)右(東)四種 KEYCODE_DPAD_UP KEYCODE_DPAD_DOWN KEYCODE_DPAD_LEFT KEYCODE_DPAD_RIGHT

蛇-移動 分成上(北)下(南)左(西)右(東)四種 KEYCODE_DPAD_UP KEYCODE_DPAD_DOWN KEYCODE_DPAD_LEFT KEYCODE_DPAD_RIGHT

蛇-判斷方向 if (key. Code == Key. Event. KEYCODE_DPAD_DOWN) { if (m. Direction != NORTH)

蛇-判斷方向 if (key. Code == Key. Event. KEYCODE_DPAD_DOWN) { if (m. Direction != NORTH) { m. Next. Direction = SOUTH; } return (true); } 假如鍵盤按↓, 蛇頭不面向北, 則面向南

if (key. Code == Key. Event. KEYCODE_DPAD_LEFT) { if (m. Direction != EAST) {

if (key. Code == Key. Event. KEYCODE_DPAD_LEFT) { if (m. Direction != EAST) { m. Next. Direction = WEST; } return (true); } 假如鍵盤按←, 蛇頭不面向東, 則面向西

if (key. Code == Key. Event. KEYCODE_DPAD_RIGHT) { if (m. Direction != WEST) {

if (key. Code == Key. Event. KEYCODE_DPAD_RIGHT) { if (m. Direction != WEST) { m. Next. Direction = EAST; } return (true); } 假如鍵盤按→, 蛇頭不面向西, 則面向東

蛇-吃到自己 int snakelength = m. Snake. Trail. size(); for (int snakeindex = 0; snakeindex

蛇-吃到自己 int snakelength = m. Snake. Trail. size(); for (int snakeindex = 0; snakeindex < snakelength; snakeindex++) { Coordinate c = m. Snake. Trail. get(snakeindex); if (c. equals(new. Head)) { set. Mode(LOSE); return; } }

蛇-碰到牆壁 if ((new. Head. x < 1) || (new. Head. y < 1) ||

蛇-碰到牆壁 if ((new. Head. x < 1) || (new. Head. y < 1) || (new. Head. x > m. XTile. Count - 2) || (new. Head. y > m. YTile. Count - 2)) { set. Mode(LOSE); return; }

蛇-吃到重繪 int index = 0 ; for (Coordinate c : m. Snake. Trail) {

蛇-吃到重繪 int index = 0 ; for (Coordinate c : m. Snake. Trail) { if (index == 0 ) { set. Tile(YELLOW_STAR, cx, cy); } else { set. Tile(RED_STAR, cx, cy); } index++; } }

蘋果-繪製 private void update. Apples() { for (Coordinate c : m. Apple. List) {

蘋果-繪製 private void update. Apples() { for (Coordinate c : m. Apple. List) { set. Tile(YELLOW_STAR, cx, cy); } }

蘋果-顯示 private void update. Apples() { for (Coordinate c : m. Apple. List) {

蘋果-顯示 private void update. Apples() { for (Coordinate c : m. Apple. List) { set. Tile(YELLOW_STAR, c. x, c. y); } }

蘋果-被吃掉 int applecount = m. Apple. List. size(); for ( int appleindex = 0

蘋果-被吃掉 int applecount = m. Apple. List. size(); for ( int appleindex = 0 ; appleindex < applecount; appleindex++) { Coordinate c = m. Apple. List. get(appleindex); if (c. equals(new. Head)) { m. Apple. List. remove(c); add. Random. Apple(); m. Score++; m. Move. Delay *= 0. 9 ; grow. Snake = true ; } }

參考資料 SNAKE程式碼: http: //blog. csdn. net/biaoqi/article/details/6618313 http: //blog. csdn. net/spy 19881201/article/details/5826433 http: //developer. android.

參考資料 SNAKE程式碼: http: //blog. csdn. net/biaoqi/article/details/6618313 http: //blog. csdn. net/spy 19881201/article/details/5826433 http: //developer. android. com/resources/samples/Snake/index. html

 END

END