android简单小游戏开发源码(急需一个简单的Android程序,需源代码!)

:暂无数据 2026-06-20 23:40:01 :0

android简单小游戏开发源码(急需一个简单的Android程序,需源代码!)

大家好,今天小编来为大家解答以下的问题,关于android简单小游戏开发源码,急需一个简单的Android程序,需源代码!这个很多人还不知道,现在让我们一起来看看吧!

本文目录

急需一个简单的Android程序,需源代码!

 //最简单的helloword
 import android.app.Activity;    
 import android.os.Bundle;    
 import android.widget.TextView;
 
    public class HelloWorld extends Activity {               
       @Override       
       public void onCreate(Bundle savedInstanceState) {            
         super.onCreate(savedInstanceState);            
         TextView tv = new TextView(this);            
         tv.setText(“Hello, World!“);            
         setContentView(tv);       
        }    
     }

求一个安卓开发小游戏源代码,临时交作业用

package com.fiveChess;

import android.app.Activity;
import android.os.Bundle;
import android.view.Display;
import android.view.Menu;
import android.view.MenuItem;
import android.view.Window;
import android.view.WindowManager;

public class MainActivity extends Activity {
GameView gameView = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
Display display = this.getWindowManager().getDefaultDisplay();
gameView = new GameView(this,display.getWidth(),display.getHeight());
setContentView(gameView);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(“重新开始“).setIcon(android.R.drawable.ic_menu_myplaces);
menu.add(“退出“);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getTitle().equals(“重新开始“)){
gameView.canPlay = true;
gameView.chess = new int[gameView.row][gameView.col];
gameView.invalidate();
}else if(item.getTitle().equals(“退出“)){
finish();
}
return super.onOptionsItemSelected(item);
}
}

package com.fiveChess;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.view.MotionEvent;
import android.view.View;

public class GameView extends View {
Context context = null;
int screenWidth,screenHeight;
String message = ““;//提示轮到哪个玩家
int row,col; //划线的行数和列数
int stepLength = 30;//棋盘每格间距
int chess = null;//0代表没有棋子,1代表是黑棋,2代表白旗
boolean isBlack = true;
boolean canPlay = true;
public GameView(Context context,int screenWidth,int screenHeight) {
super(context);
this.context = context;
this.screenWidth = screenWidth;
this.screenHeight = screenHeight;
this.message = “黑棋先行“;
row = (screenHeight-50)/stepLength+1;
col = (screenWidth-10)/stepLength+1;
chess = new int[row][col];

}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Paint paint = new Paint();
paint.setColor(Color.WHITE);
canvas.drawRect(0, 0, screenWidth, screenHeight, paint);//画背景
paint.setColor(Color.BLUE);
paint.setTextSize(25);
canvas.drawText(message, (screenWidth-100)/2, 30, paint);//画最顶层的字
paint.setColor(Color.BLACK);
//画棋盘
for(int i=0;i《row;i++){
canvas.drawLine(10, 50+i*stepLength, 10+(col-1)*stepLength, 50+i*stepLength, paint);
}
for(int i=0;i《col;i++){
canvas.drawLine(10+i*stepLength,50,10+i*stepLength,50+(row-1)*stepLength, paint);
}

for(int r=0;r《row;r++){
for(int c=0;c《col;c++){
if(chess[r][c] == 1){
paint.setColor(Color.BLACK);
paint.setStyle(Style.FILL);
canvas.drawCircle(10+c*stepLength, 50+r*stepLength, 10, paint);
}else if(chess[r][c] == 2){
//画白棋
paint.setColor(Color.WHITE);
paint.setStyle(Style.FILL);
canvas.drawCircle(10+c*stepLength, 50+r*stepLength, 10, paint);

paint.setColor(Color.BLACK);
paint.setStyle(Style.STROKE);
canvas.drawCircle(10+c*stepLength, 50+r*stepLength, 10, paint);
}
}
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if(!canPlay){return false;}
float x = event.getX();
float y = event.getY();
int r = Math.round((y-50)/stepLength);
int c = Math.round((x-10)/stepLength);
if(r《0 || r》row-1 || c《0 || c》col-1){return false;}
if(chess[r][c]!=0){return false;}//若有棋子则不再画棋子了
if(isBlack){
chess[r][c] = 1;
isBlack = false;
message = “轮到白棋“;
}else{
chess[r][c] = 2;
isBlack = true;
message = “轮到黑棋“;
}
invalidate();
if(judge(r, c,0,1)) return false;
if(judge(r, c,1,0)) return false ;
if(judge(r, c,1,1)) return false;
if(judge(r, c,1,-1)) return false;

return super.onTouchEvent(event);
}
private boolean judge(int r, int c,int x,int y) {//r,c表示行和列,x表示在y方向上的偏移,y表示在x方向上的偏移
int count = 1;
int a = r;
int b = c;
while(r》=0 && r《row && c》=0 && c《col && r+x》=0 && r+x《row && c+y》=0 && c+y《col && chess[r][c] == chess[r+x][c+y]){
count++;
if(y》0){
c++;
}else if(y《0){
c--;
}
if(x》0){
r++;
}else if(x《0){
r--;
}
}
while(a》=0 && a《row && b》=0 && b《col && a-x》=0 && a-x《row && b-y》=0 && b-y《col && chess[a][b] == chess[a-x][b-y]){
count++;
if(y》0){
b--;
}else if(y《0){
b++;
}
if(x》0){
a--;
}else if(x《0){
a++;
}
}
if(count》=5){
String str = ““;
if(isBlack){
str = “白棋胜利“;
}else{
str = “黑棋胜利“;
}
new AlertDialog.Builder(context).setTitle(“游戏结束“).setMessage(str).setPositiveButton(“重新开始“, new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
chess = new int[row][col];
invalidate();

}
}).setNegativeButton(“观看棋局“, new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
canPlay = false;

}
}).show();
return true;
}

return false;
}
}
PS:五子棋,无需图片,直接在程序里画出来的。注意我发的是两个文件,一个activity,一个类文件,别把它当成一个文件了

求几个基于android开发的小游戏源码

package com.fiveChess;

import android.app.Activity;
import android.os.Bundle;
import android.view.Display;
import android.view.Menu;
import android.view.MenuItem;
import android.view.Window;
import android.view.WindowManager;

public class MainActivity extends Activity {
GameView gameView = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
Display display = this.getWindowManager().getDefaultDisplay();
gameView = new GameView(this,display.getWidth(),display.getHeight());
setContentView(gameView);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(“重新开始“).setIcon(android.R.drawable.ic_menu_myplaces);
menu.add(“退出“);
return super.onCreateOptionsMenu(menu);
}

关于本次android简单小游戏开发源码和急需一个简单的Android程序,需源代码!的问题分享到这里就结束了,如果解决了您的问题,我们非常高兴。

android简单小游戏开发源码(急需一个简单的Android程序,需源代码!)

本文编辑:admin

更多文章:


协方差计算公式(协方差的计算公式)

协方差计算公式(协方差的计算公式)

这篇文章给大家聊聊关于协方差计算公式,以及协方差的计算公式对应的知识点,希望对各位有所帮助,不要忘了收藏本站哦。

2026年10月11日 08:10

易语言网页api接口怎么调用(易语言,怎么读取网页json的api)

易语言网页api接口怎么调用(易语言,怎么读取网页json的api)

本篇文章给大家谈谈易语言网页api接口怎么调用,以及易语言,怎么读取网页json的api对应的知识点,文章可能有点长,但是希望大家可以阅读完,增长自己的知识,最重要的是希望对各位有所帮助,可以解决了您的问题,不要忘了收藏本站喔。

2026年10月11日 08:00

majority of(the majority of 和 a majority of的区别以及用法例句)

majority of(the majority of 和 a majority of的区别以及用法例句)

这篇文章给大家聊聊关于majority of,以及the majority of 和 a majority of的区别以及用法例句对应的知识点,希望对各位有所帮助,不要忘了收藏本站哦。

2026年10月11日 07:40

汉字机内码查询表(1个汉字的机内码是几位谢谢)

汉字机内码查询表(1个汉字的机内码是几位谢谢)

大家好,关于汉字机内码查询表很多朋友都还不太明白,不过没关系,因为今天小编就来为大家分享关于1个汉字的机内码是几位谢谢的知识点,相信应该可以解决大家的一些困惑和问题,如果碰巧可以解决您的问题,还望关注下本站哦,希望对各位有所帮助!

2026年10月11日 07:20

promote翻译(英语翻译倡导怎么说)

promote翻译(英语翻译倡导怎么说)

其实promote翻译的问题并不复杂,但是又很多的朋友都不太了解英语翻译倡导怎么说,因此呢,今天小编就来为大家分享promote翻译的一些知识,希望可以帮助到大家,下面我们一起来看看这个问题的分析吧!

2026年10月11日 06:30

用手机如何导航?开车用手机导航哪个软件最好

用手机如何导航?开车用手机导航哪个软件最好

今天给各位分享用手机如何导航的知识,其中也会对用手机如何导航进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

2026年10月11日 06:20

多线程技术有什么用(多线程有什么作用)

多线程技术有什么用(多线程有什么作用)

其实多线程技术有什么用的问题并不复杂,但是又很多的朋友都不太了解多线程有什么作用,因此呢,今天小编就来为大家分享多线程技术有什么用的一些知识,希望可以帮助到大家,下面我们一起来看看这个问题的分析吧!

2026年10月11日 05:40

another time(another time和other time的区别)

another time(another time和other time的区别)

大家好,another time相信很多的网友都不是很明白,包括another time和other time的区别也是一样,不过没有关系,接下来就来为大家分享关于another time和another time和other time的区

2026年10月11日 05:00

java开发工具包jdk(JDK是什么意思)

java开发工具包jdk(JDK是什么意思)

其实java开发工具包jdk的问题并不复杂,但是又很多的朋友都不太了解JDK是什么意思,因此呢,今天小编就来为大家分享java开发工具包jdk的一些知识,希望可以帮助到大家,下面我们一起来看看这个问题的分析吧!

2026年10月11日 04:50

mysql 命令(MySQL的基本命令)

mysql 命令(MySQL的基本命令)

大家好,如果您还对mysql 命令不太了解,没有关系,今天就由本站为大家分享mysql 命令的知识,包括MySQL的基本命令的问题都会给大家分析到,还望可以解决大家的问题,下面我们就开始吧!

2026年10月11日 03:00

最近更新

majority of(the majority of 和 a majority of的区别以及用法例句)
2026-10-11 07:40:02 浏览:0
热门文章

标签列表