package whu.iss.sric.view;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import whu.iss.sric.android.R;
import whu.iss.sric.android.SoundPlay;
import android.content.Context;
import android.graphics.Point;
import android.media.MediaPlayer;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.MotionEvent;
public class GameView extends BoardView {
private static final int REFRESH_VIEW = 1;
public static final int WIN = 1;
public static final int LOSE = 2;
public static final int PAUSE = 3;
public static final int PLAY = 4;
public static final int QUIT = 5;
private int Help = 3;
private int Refresh = 3;
/**
* 第一关为100秒钟的时间
*/
private int totalTime = 100;
private int leftTime;
public static SoundPlay soundPlay;
public MediaPlayer player;
private RefreshTime refreshTime;
private RefreshHandler refreshHandler = new RefreshHandler();
/**
* 用来停止计时器的线程
*/
private boolean isStop;
private OnTimerListener timerListener = null;
private OnStateListener stateListener = null;
private OnToolsChangeListener toolsChangedListener = null;
private List<Point> path = new ArrayList<Point>();
public GameView(Context context, AttributeSet atts) {
super(context, atts);
player = MediaPlayer.create(context, R.raw.back2new);
player.setLooping(true);//设置循环播放
}
public static final int ID_SOUND_CHOOSE = 0;
public static final int ID_SOUND_DISAPEAR = 1;
public static final int ID_SOUND_WIN = 4;
public static final int ID_SOUND_LOSE = 5;
public static final int ID_SOUND_REFRESH = 6;
public static final int ID_SOUND_TIP = 7;
public static final int ID_SOUND_ERROR = 8;
public void startPlay(){
Help = 3;
Refresh = 3;
isStop = false;
toolsChangedListener.onRefreshChanged(Refresh);
toolsChangedListener.onTipChanged(Help);
leftTime = totalTime;
initMap();
player.start();
refreshTime = new RefreshTime();
refreshTime.start();
GameView.this.invalidate();
}
public void startNextPlay(){
//下一关为上一关减去10秒的时间
totalTime-=10;
startPlay();
}
public static void initSound(Context context){
soundPlay = new SoundPlay();
soundPlay.initSounds(context);
soundPlay.loadSfx(context, R.raw.choose, ID_SOUND_CHOOSE);
soundPlay.loadSfx(context, R.raw.disappear1, ID_SOUND_DISAPEAR);
soundPlay.loadSfx(context, R.raw.win, ID_SOUND_WIN);
soundPlay.loadSfx(context, R.raw.lose, ID_SOUND_LOSE);
soundPlay.loadSfx(context, R.raw.item1, ID_SOUND_REFRESH);
soundPlay.loadSfx(context, R.raw.item2, ID_SOUND_TIP);
soundPlay.loadSfx(context, R.raw.alarm, ID_SOUND_ERROR);
}
public void setOnTimerListener(OnTimerListener timerListener){
this.timerListener = timerListener;
}
public void setOnStateListener(OnStateListener stateListener){
this.stateListener = stateListener;
}
public void setOnToolsChangedListener(OnToolsChangeListener toolsChangedListener){
this.toolsChangedListener = toolsChangedListener;
}
public void stopTimer(){
isStop = true;
}
class RefreshHandler extends Handler {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if (msg.what == REFRESH_VIEW) {
GameView.this.invalidate();
if (win()) {
setMode(WIN);
soundPlay.play(ID_SOUND_WIN, 0);
isStop = true;
} else if (die()) {
change();
}
}
}
public void sleep(int delayTime) {
this.removeMessages(0);
Message message = new Message();
message.what = REFRESH_VIEW;
sendMessageDelayed(message, delayTime);
}
}
class RefreshTime extends Thread {
public void run() {
while (leftTime >= 0 && !isStop) {
timerListener.onTimer(leftTime);
leftTime--;
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
if(!isStop){
setMode(LOSE);
soundPlay.play(ID_SOUND_LOSE, 0);
}
}
}
public int getTotalTime(){
return totalTime;
}
public int getTipNum(){
return Help;
}
public int getRefreshNum(){
return Refresh;
}
@Override
public boolean onTouchEvent(MotionEvent event) {
int x = (int) event.getX();
int y = (int) event.getY();
Point p = screenToindex(x, y);
if (map[p.x][p.y] > 0) {
if (selected.size() == 1) {
if (link(selected.get(0), p)) {
selected.add(p);
drawLine(path.toArray(new Point[] {}));
soundPlay.play(ID_SOUND_DISAPEAR, 0);
refreshHandler.sleep(500);
} else {
selected.clear();
selected.add(p);
soundPlay.play(ID_SOUND_CHOOSE, 0);
GameView.this.invalidate();
}
} else {
selected.add(p);
soundPlay.play(ID_SOUND_CHOOSE, 0);
GameView.this.invalidate();
}
}
return super.onTouchEvent(event);
}
public void initMap() {
int x = 1;
int y = 0;
for (int i = 1; i < xCount - 1; i++) {
for (int j = 1; j < yCount - 1; j++) {
map[i][j] = x;
if (y == 1) {
x++;
y = 0;
if (x == iconCounts) {
x = 1;
}
} else {
y = 1;
}
}
}
change();
}
private void change() {
Random random = new Random();
int tmpV, tmpX, tmpY;
for (int x = 1; x < xCount - 1; x++) {
for (int y = 1; y < yCount - 1; y++) {
tmpX = 1 + random.nextInt(xCount - 2);
tmpY = 1 + random.nextInt(yCount - 2);
tmpV = map[x][y];
map[x][y] = map[tmpX][tmpY];
map[tmpX][tmpY] = tmpV;
}
}
if (die()) {
change();
}
GameView.this.invalidate();
}
public void setMode(int stateMode) {
this.stateListener.OnStateChanged(stateMode);
}
private boolean die() {
for (int y = 1; y < yCount - 1; y++) {
for (int x = 1; x < xCount - 1; x++) {
if (map[x][y] != 0) {
for (int j = y; j < yCount - 1; j++) {
if (j == y) {
for (int i = x + 1; i < xCount - 1; i++) {
if (map[i][j] == map[x][y]
&& link(new Point(x, y),
new Point(i, j))) {
return false;
}
}
} else {
for (int i = 1; i < xCount - 1; i++) {
if (map[i][j] == map[x][y]
&& link(new Point(x, y),
new Point(i, j))) {
return false;
}
}
}
}
}
}
}
return true;
}
List<Point> p1E = new ArrayList<Point>();
List<Point> p2E = new ArrayList<Point>();
private boolean link(Point p1, Point p2) {
if (p1.equals(p2)) {
return false;
}
path.clear();
if (map[p1.x][p1.y] == map[p2.x][p2.y]) {
if (linkD(p1, p2)) {
path.add(p1);
path.add(p2);
return true;
}
Point p = new Point(p1.x, p2.y);
if (map[p.x][p.y] == 0) {
if (linkD(p1, p) && linkD(p, p2)) {
path.add(p1);
path.add(p);
path.add(p2);
return true;
}
}
p = new Point(p2.x, p1.y);
if (map[p.x][p.y] == 0) {
if (linkD(p1, p) && linkD(p, p2)) {
path.add(p1);
path.add(p);
path.add(p2);
return true;
}
}
expandX(p1, p1E);
expandX(p2, p2E);
for (Point pt1 : p1E) {
for (Point pt2 : p2E) {
if (pt1.x == pt2.x) {
if (linkD(pt1, pt2)) {
path.add(p1);
path.add(pt1);
path.add(pt2);
path.add(p2);
return true;
}
}
}
}
expandY(p1, p1E);
expandY(p2, p2E);
for (Point pt1 : p1E) {
for (Point pt2 : p2E) {
if (pt1.y == pt2.y) {
if (linkD(pt1, pt2)) {
path.add(p1);
path.add(pt1);
path.add(pt2);
path.add(p2);
return true;
}
}
}
}
return false;
}
return false;
}
private boolean linkD(Point p1, Point p2) {
if (p1.x == p2.x) {
没有合适的资源?快使用搜索试试~ 我知道了~
Android游戏源码带详细注释的水果消消看源码.zip
共129个文件
png:64个
class:23个
xml:12个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 111 浏览量
2022-07-08
09:23:51
上传
评论
收藏 3.95MB ZIP 举报
温馨提示
Android游戏源码带详细注释的水果消消看源码.zip
资源推荐
资源详情
资源评论
收起资源包目录
Android游戏源码带详细注释的水果消消看源码.zip (129个子文件)
resources.ap_ 1.21MB
lianliankan.Android.apk 1.23MB
jarlist.cache 120B
proguard.cfg 1KB
GameView.class 10KB
WelActivity.class 5KB
BoardView.class 5KB
MyDialog.class 3KB
SoundPlay.class 2KB
WelActivity$1.class 2KB
GameView$RefreshHandler.class 1KB
R$drawable.class 1KB
GameView$RefreshTime.class 1KB
MyDialog$1.class 988B
R$id.class 869B
MyDialog$2.class 834B
R.class 689B
R$raw.class 627B
R$string.class 604B
R$anim.class 528B
R$layout.class 438B
R$style.class 399B
BuildConfig.class 349B
R$attr.class 346B
OnToolsChangeListener.class 198B
OnStateListener.class 161B
OnTimerListener.class 154B
.classpath 475B
classes.dex 29KB
annotations-5bbb0a59cf663c535a64f12025aaf441.jar 943B
GameView.java 10KB
WelActivity.java 5KB
BoardView.java 5KB
R.java 4KB
MyDialog.java 2KB
SoundPlay.java 2KB
BuildConfig.java 162B
OnToolsChangeListener.java 158B
OnStateListener.java 115B
OnTimerListener.java 107B
mainbg.jpg 197KB
back2new.ogg 474KB
bg.ogg 242KB
win.ogg 16KB
lose.ogg 15KB
item1.ogg 10KB
item2.ogg 9KB
alarm.ogg 9KB
disappear1.ogg 7KB
choose.ogg 6KB
buttons_bg1.png 29KB
buttons_bg1.png 27KB
title.png 24KB
title.png 21KB
icon.png 18KB
icon.png 18KB
refresh.png 15KB
fruit_10.png 12KB
refresh.png 12KB
fruit_01.png 12KB
light_bulb.png 12KB
icon.png 11KB
buttons_bg20.png 11KB
fruit_14.png 11KB
fruit_08.png 10KB
fruit_11.png 10KB
icon.png 10KB
fruit_15.png 10KB
fruit_07.png 10KB
fruit_04.png 10KB
fruit_05.png 10KB
fruit_19.png 10KB
fruit_13.png 10KB
fruit_03.png 9KB
fruit_10.png 9KB
fruit_01.png 9KB
fruit_17.png 9KB
fruit_09.png 8KB
fruit_18.png 8KB
light_bulb.png 8KB
fruit_14.png 8KB
buttons_bg20.png 7KB
fruit_02.png 7KB
fruit_11.png 7KB
fruit_15.png 7KB
fruit_06.png 7KB
fruit_05.png 7KB
fruit_04.png 7KB
fruit_19.png 7KB
fruit_08.png 7KB
fruit_13.png 6KB
fruit_07.png 6KB
fruit_12.png 6KB
buttons_play.png 6KB
icon.png 6KB
fruit_17.png 6KB
fruit_03.png 6KB
fruit_18.png 5KB
icon.png 5KB
buttons_replay.png 5KB
共 129 条
- 1
- 2
资源评论
reg183
- 粉丝: 1817
- 资源: 1万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- MySQL是一种广泛使用的开源关系型数据库管理系统,它提供了丰富的SQL语句用于数据库的创建、查询、更新和管理 以下是一些常见的
- MySQL是一种广泛使用的开源关系型数据库管理系统,它提供了丰富的SQL语句用于数据库的创建、查询、更新和管理 以下是一些常见
- MySQL是一种广泛使用的开源关系型数据库管理系统,它提供了丰富的SQL语句用于数据库的创建、查询、更新和管理 以下是一些常见的
- 基于Javascript的结婚请帖设计源码 - Invitation
- mysql语句大全及用法
- mysql语句大全及用法
- mysql语句大全及用法
- MySQL是一种广泛使用的开源关系型数据库管理系统
- MySQL是一种广泛使用的开源关系型数据库管理系统
- MySQL是一种广泛使用的开源关系型数据库管理系统
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功