① 求一個安卓開發小游戲源代碼,臨時交作業用
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,一個類文件,別把它當成一個文件了
② C++做一個小游戲,有源代碼的最好,謝謝
#include <iostream>
#include<fstream>
#include <ctime>
#include <cmath>
#include <stdlib.h>
#include<stdio.h> //時間 //文件
#include <string>
#define random(x)(rand()%x)
using namespace std;
void thunder(int Dif,int Row,int Column,char *USer)
{
int r,c,alls[22][22],backstage[22][22]={0};
srand((int)time(0));
for(r=1;r<=Row;r++) // 生成alls(0~1)1是雷
{
for(c=1;c<=Column;c++)
{
if(random(6)<1) {alls[r][c]=1;} else{alls[r][c]=0;};
}
};
for(r=0;r<=Row+1;r++) //生成 backstage(正確答案)
{
for(int c=0;c<=Column+1;c++)
{
if(alls[r][c]==1)
{
(int)backstage[r][c]='*'; //將1變為 * 代表雷
}
else
{
for(int i=r-1;i<=r+1;i++) //將0變為數字 (代表周圍雷數)
for(int j=c-1;j<=c+1;j++)
{
if(alls[i][j]!=alls[r][c]&&alls[i][j]==1){backstage[r][c]++;};
}
}; //else 結束
}; // for 結束
}; // for 結束
cout<<"======================*********================================"<<endl;
char surface[22][22]; //生成surface(用戶界面)
for(r=0;r<22;r++) //全部為零
for(c=0;c<22;c++)
{
surface[r][c]='0';
}
for(r=1;r<=Row;r++) //中間化 # 形成0包圍#的形式 (通過數 #-->(*||數字) 的個數 贏的時候停止循環)
for(c=1;c<=Column;c++)
{
surface[r][c]='#';
}
for(r=1;r<=Row;r++) //輸出 surface 界面 便於檢查
{
for(c=1;c<=Column;c++) {cout<<" "<<surface[r][c];};
cout<<endl;
};
cout<<"請按格式輸入"<<endl
<<"前兩個數字為坐標,最後一個數字「1」表示此位置為雷,「0」則表示不是。"<<endl
<<"如:1 3 1 表示一行三列是雷;2 4 0 表示二行四列不是雷"<<endl
<<"提示:當數字周圍雷都被掃出時,可再次按要求輸入此位置,可得到周圍數字。"<<endl;
long i=10000000L; //計算時間開始
clock_t start,finish;
double ration;
start=clock();
while(i--); //計算時間開始
int num=Row*Column; //計算#號個數
while(num!=0) //控制 是否點完所有位置
{
int x,y,judge;
cin>>x>>y>>judge;
if(alls[x][y]!=judge)
{
cout<<"you lose!!!"<<endl;
cout<<"The answer is:"<<endl;
for(r=1;r<=Row;r++) //輸了 輸出backstage 顯示正確答案
{
for(int c=1;c<=Column;c++)
{
cout<<" "<<(char)(backstage[r][c]==42?backstage[r][c]:backstage[r][c]+'0'); //輸出backstage
}
cout<<endl;
}
break;
}
else
{
if(alls[x][y]==1) {if(surface[x][y]=='#'){num--;}surface[x][y]='@'; } // 雷 判斷正確 顯示「@」;數「#」
else
{
if(backstage[x][y]!=0) // 數字 判斷正確 顯示數字
{
if(surface[x][y]=='#'){num--; surface[x][y]=backstage[x][y]+'0'; } // 數「#」
else
{
int lei_num=0;
for(int i=x-1;i<=x+1;i++) //數 數字周圍 雷的個數
for(int j=y-1;j<=y+1;j++)
{
if(surface[i][j]=='@')
lei_num++;
}
if(backstage[x][y]==lei_num) // 看數字周圍雷是否全部掃出 提示 顯示數字周圍
{
for(int i=x-1;i<=x+1;i++)
for(int j=y-1;j<=y+1;j++)
if(surface[i][j]=='#') //數「#」
{
surface[i][j]=backstage[i][j]+'0';
num--;
}
}
}
}
else // 數字為零時 顯示零周圍的零
{
if(surface[x][y]=='#'){num--;}; //數「#」
surface[x][y]=backstage[x][y]+'0';
for(int i=x-1;i<=x+1;i++) // 顯示零周圍的數字
for(int j=y-1;j<=y+1;j++)
if(surface[i][j]=='#') // 避免 死循環
{
surface[i][j]=backstage[i][j]+'0';
num--; //數「#」
}
for(int k=0;k<20;k++) //最多20層零 (點出最邊上的零)
{
for (int R=1;R<=Row;R++) //檢查所有零
for(int C=1;C<=Column;C++) //再次顯示零周圍的數字
{
if(surface[R][C]=='0')
{
for(int i=R-1;i<=R+1;i++)
for(int j=C-1;j<=C+1;j++)
{
if(surface[i][j]=='#') // 避免 死循環 數「#」
{
surface[i][j]=backstage[i][j]+'0';
num--;
}
}
}
} //匹配for 內
} //匹配 for 外
}//匹配else
}//匹配else
}//匹配els
cout<<endl;
cout<<"======================*********================================"<<endl;
for(r=1;r<=Row;r++) //輸出界面(已修改)
{
for(c=1;c<=Column;c++) {cout<<" "<<surface[r][c];};
cout<<endl;
};
} //匹配while
finish=clock(); //計算時間結束
ration=(double)(finish-start)/CLOCKS_PER_SEC; //時間變數
if(num==0) //所有
{
cout<<" You win! Congratulations!! "<<endl;
cout<<" Your time is: "<<ration<<endl;
if(Dif==1) //讀取 簡單掃雷 的存儲文件
{
string Name;
string name[6];
double Time,rang;
double times[6];
int i=0;
ifstream inf("掃雷 簡單.txt");
for(i=0;i<5;i++) //文件中信息導入到數組里
{
inf>>Name;inf>>rang>>Time;
name[i]=Name;
times[i]=Time;
}
inf.close();
name[5]=USer; //本輪玩家信息
times[5]=ration;
double t1=0;
string t2;
for(int j=0;j<5;j++) //冒泡排序法
{
for(i=0;i<5-j;i++)
{
if(times[i]>times[i+1])
{
t1=times[i];
times[i]=times[i+1];
times[i+1]=t1;
t2=name[i];
name[i]=name[i+1];
name[i+1]=t2;
}
}
}
ofstream outf("掃雷 簡單.txt");
for(i=0;i<5;i++) //將前五名玩家信息存儲到文件中
{
cout<<name[i]<<" "<<i+1<<" "<<times[i]<<endl;
outf<<name[i]<<" "<<i+1<<" "<<times[i]<<endl;
}
outf.close();
}
if(Dif==2) //讀取 一般掃雷 的存儲文件
{
string Name;
string name[6];
double Time,rang;
double times[6];
int i=0;
ifstream inf("掃雷 一般.txt");
for(i=0;i<5;i++) //文件中信息導入到數組里
{
inf>>Name;inf>>rang>>Time;
name[i]=Name;
times[i]=Time;
}
inf.close();
name[5]=USer; //本輪玩家信息
times[5]=ration;
double t1=0;
string t2;
for(int j=0;j<5;j++) //冒泡排序法
{
for(i=0;i<5-j;i++)
{
if(times[i]>times[i+1])
{
t1=times[i];
times[i]=times[i+1];
times[i+1]=t1;
t2=name[i];
name[i]=name[i+1];
name[i+1]=t2;
}
}
}
ofstream outf("掃雷 一般.txt");
for(i=0;i<5;i++) //將前五名玩家信息存儲到文件中 並輸出
{
cout<<name[i]<<" "<<i+1<<" "<<times[i]<<endl;
outf<<name[i]<<" "<<i+1<<" "<<times[i]<<endl;
}
outf.close();
}
if(Dif==3) //讀取 困難掃雷 的存儲文件
{
string Name;
string name[6];
double Time,rang;
double times[6];
int i=0;
ifstream inf("掃雷 困難.txt");
for(i=0;i<5;i++) //文件中信息導入到數組里
{
inf>>Name;inf>>rang>>Time;
name[i]=Name;
times[i]=Time;
}
inf.close();
name[5]=USer; //本輪玩家信息
times[5]=ration;
double t1=0;
string t2;
for(int j=0;j<5;j++) //冒泡排序法
{
for(i=0;i<5-j;i++)
{
if(times[i]>times[i+1])
{
t1=times[i];
times[i]=times[i+1];
times[i+1]=t1;
t2=name[i];
name[i]=name[i+1];
name[i+1]=t2;
}
}
}
ofstream outf("掃雷 困難.txt");
for(i=0;i<5;i++) //將前五名玩家信息存儲到文件中
{
cout<<name[i]<<" "<<i+1<<" "<<times[i]<<endl;
outf<<name[i]<<" "<<i+1<<" "<<times[i]<<endl;
}
outf.close();
}
}
}
void scale(int dif,char *User) //選擇難度
{
int row,column;
if(dif==1) {row=3;column=3;}
if(dif==2) {row=7;column=7;}
if(dif==3) {row=10;column=10;}
cout<<"The scale is: "<<row<<"*"<<column<<endl;
thunder(dif,row,column,User);
};
int main()
{
int Continue=1;
int difficulty;
char user[10];
cout<<" Welcom to the game! "<<endl
<<" 請輸入用戶名! "<<endl;
cin>>user;
while(Continue==1)
{
cout<<"=======================*******************======================="<<endl
<<" 請選擇難度! "<<endl
<<" 簡單——1 "<<endl
<<" 一般——2 "<<endl
<<" 困難——3 "<<endl;
cin>>difficulty;
scale(difficulty,user);
cout<<"繼續游戲——1 結束游戲——0"<<endl;
cin>>Continue;
}
return 0;
}
掃雷小游戲,自己編的代碼
③ 想要java開發手機游戲的源代碼,什麼游戲都行,貪吃蛇,俄羅斯方塊都OK,
那你這個就是安卓開發
④ 誰能給我一個手機游戲的源代碼啊
這個地址也有,不過直接給你吧,這樣比較好
先給你看看主要的類吧
package Game;
import DreamBubbleMidlet;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Hashtable;
import javax.microedition.lci.Graphics;
import javax.microedition.lci.Image;
import javax.microedition.lci.game.GameCanvas;
import javax.microedition.lci.game.LayerManager;
import javax.microedition.lci.game.Sprite;
public class Game extends GameCanvas implements Runnable {
protected DreamBubbleMidlet dreamBubbleMidlet;
protected Graphics g;
protected Image loadingImage;
protected Image pauseImage;
protected Image cursorImage;
protected Image jackStateImage;
protected Image johnStateImage;
protected Image numberImage;
protected Sprite cursor;
protected Sprite number;
protected LayerManager cursorManager;
protected LayerManager numberManager;
protected Hashtable bombTable;
protected Map map;
protected LayerManager gameLayerManager;
protected Role player;
protected Sprite playerGhost;
protected int screenWidth;
protected int screenHeight;
protected int delay = 50;
protected int[][] bornPlace;
protected int chooseIndex;
protected int stageIndex = 1;
protected int gameClock;
protected int loadPercent;
protected boolean isPause;
protected boolean isEnd;
protected boolean isPlaying;
protected boolean isLoading;
protected Thread mainThread;
public Game(DreamBubbleMidlet dreamBubbleMidlet) {
super(false);
this.setFullScreenMode(true);
this.dreamBubbleMidlet = dreamBubbleMidlet;
this.screenWidth = this.getWidth();
this.screenHeight = this.getHeight();
try {
this.loadingImage = Image.createImage("/Game/Loading.png");
this.pauseImage = Image.createImage("/Game/Pause.png");
this.cursorImage = Image.createImage("/Game/Cursor.png");
this.jackStateImage = Image.createImage("/State/JackState.png");
this.johnStateImage = Image.createImage("/State/JohnState.png");
this.numberImage = Image.createImage("/State/Number.png");
} catch (IOException e) {
e.printStackTrace();
}
this.g = this.getGraphics();
}
public void loadStage(int stage) {
this.isEnd = false;
this.isPause = false;
this.isPlaying = false;
this.gameLayerManager = new LayerManager();
this.cursorManager = new LayerManager();
this.numberManager = new LayerManager();
this.bombTable = new Hashtable();
this.cursor = new Sprite(this.cursorImage, 32, 32);
this.number = new Sprite(this.numberImage, 12, 10);
this.loadPercent = 20;
sleep();
loadMap(stage);
this.loadPercent = 40;
sleep();
loadPlayer();
this.loadPercent = 60;
sleep();
this.gameLayerManager.append(map.getBombLayer());
this.gameLayerManager.append(map.getBuildLayer());
this.gameLayerManager.append(map.getToolLayer());
this.gameLayerManager.append(map.getFloorLayer());
this.gameLayerManager.setViewWindow(0, -5, screenWidth,
Global.MAP_HEIGHT + 5);
this.cursorManager.append(cursor);
this.numberManager.append(number);
this.loadPercent = 80;
sleep();
this.loadPercent = 100;
sleep();
isPlaying = true;
}
public void run() {
while (!isEnd) {
long beginTime = System.currentTimeMillis();
this.drawScreen();
long endTime = System.currentTimeMillis();
if (endTime - beginTime < this.delay) {
try {
Thread.sleep(this.delay - (endTime - beginTime));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public void loadMap(int stage) {
switch (stage) {
case 0:
this.map = new Map(Global.MAP_BLOCK);
this.bornPlace = Global.MAP_BLOCK_BORNPLACE;
break;
case 1:
this.map = new Map(Global.MAP_FACTORY);
this.bornPlace = Global.MAP_FACTORY_BORNPLACE;
break;
case 2:
this.map = new Map(Global.MAP_FOREST);
this.bornPlace = Global.MAP_FOREST_BORNPLACE;
break;
case 3:
this.map = new Map(Global.MAP_PIRATE);
this.bornPlace = Global.MAP_PIRATE_BORNPLACE;
break;
case 4:
this.map = new Map(Global.MAP_FAUBOURG);
this.bornPlace = Global.MAP_FAUBOURG_BORNPLACE;
break;
}
}
public void loadPlayer() {
this.player = SingleGameRole.createSingleGameRole(this, Global.JACK,
this.bornPlace[0][0], this.bornPlace[0][1]);
this.gameLayerManager.append(player);
try {
this.playerGhost = new Sprite(Image.createImage("/Character/Jack.png"),
this.player.width, this.player.height);
this.gameLayerManager.append(playerGhost);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void playerUpdate() {
if(!this.player.isAlive)
this.playerGhost.setVisible(false);
this.playerGhost.setFrame(this.player.getFrame());
this.player.updateRole();
}
public void bombUpdate() {
Enumeration enu = this.bombTable.keys();
while (enu.hasMoreElements()) {
String key = (String) enu.nextElement();
Bomb bomb = (Bomb) (bombTable.get(key));
if (bomb.isvisable) {
bomb.update();
} else {
bombTable.remove(key);
bomb = null;
}
}
}
public void mapUpdate() {
this.map.update();
}
public void drawScreen() {
if (gameClock < 10000)
gameClock++;
else
gameClock = 0;
if (!this.isLoading) {
if (!isPause) {
this.operate();
this.bombUpdate();
this.playerUpdate();
this.mapUpdate();
g.setColor(0x000000);
g.fillRect(0, 0, getWidth(), getHeight());
this.drawState();
gameLayerManager.paint(g, 0, this.screenHeight
- Global.MAP_HEIGHT - 5);
} else {
this.drawPauseFrame();
}
} else {
this.drawLoadingFrame();
}
this.flushGraphics();
}
public void drawFailScreen() {
}
public void drawState() {
if (this.player.type == Global.JACK) {
g.drawImage(jackStateImage, 60, 5, Graphics.TOP | Graphics.LEFT);
}
if (this.player.type == Global.JOHN) {
g.drawImage(johnStateImage, 60, 5, Graphics.TOP | Graphics.LEFT);
}
this.number.setFrame(this.player.bombNums);
this.numberManager.paint(g, 101, 15);
this.number.setFrame(this.player.speed);
this.numberManager.paint(g, 133, 15);
this.number.setFrame(this.player.power);
this.numberManager.paint(g, 165, 15);
}
protected void drawPauseFrame() {
g.setColor(0x000000);
g.fillRect(0, 0, getWidth(), getHeight());
this.drawState();
if (gameClock % 5 == 0)
this.cursor.setFrame((this.cursor.getFrame() + 1) % 4);
this.gameLayerManager.paint(g, 0, this.screenHeight - Global.MAP_HEIGHT
- 5);
this.cursorManager.paint(g, screenWidth / 2 - pauseImage.getWidth() / 2
- 32, screenHeight / 2 - pauseImage.getHeight() / 2
+ this.chooseIndex * 33 + 24);
g.drawImage(pauseImage, screenWidth / 2, screenHeight / 2,
Graphics.HCENTER | Graphics.VCENTER);
}
protected void drawLoadingFrame() {
g.setColor(66, 70, 246);
g.fillRect(0, 0, screenWidth, screenHeight);
g.drawImage(loadingImage, screenWidth / 2, 2 * screenHeight / 5,
Graphics.HCENTER | Graphics.VCENTER);
g.setColor(0, 255, 0);
g.fillRect((screenWidth - 120) / 2, 2 * screenHeight / 3,
(this.loadPercent * 120) / 100, 10);
g.setColor(255, 0, 0);
g.drawRect((screenWidth - 120) / 2, 2 * screenHeight / 3, 120, 10);
}
public void showMe() {
new Loading(this.stageIndex);
if (this.mainThread == null) {
mainThread = new Thread(this);
mainThread.start();
}
this.dreamBubbleMidlet.show(this);
}
public void operate() {
int keyStates = getKeyStates();
this.playerGhost.setPosition(this.player.xCoodinate, this.player.yCoodinate);
if ((keyStates & DOWN_PRESSED) != 0) {
this.player.walk(Global.SOUTH);
} else {
if ((keyStates & UP_PRESSED) != 0) {
this.player.walk(Global.NORTH);
} else {
if ((keyStates & RIGHT_PRESSED) != 0) {
this.player.walk(Global.EAST);
} else {
if ((keyStates & LEFT_PRESSED) != 0) {
this.player.walk(Global.WEST);
}
}
}
}
}
protected void keyPressed(int key) {
if (!this.isPlaying)
return;
if (!this.isPause && key == -7) {// 右鍵
this.chooseIndex = 0;
this.pauseGame();
return;
}
if (key == 35) {// #鍵
this.nextStage();
return;
}
if (key == 42) {// *鍵
this.preStage();
return;
}
if (this.isPause) {
switch (key) {
case -1:
case -3:
if (this.chooseIndex == 0)
this.chooseIndex = 2;
else
this.chooseIndex = (this.chooseIndex - 1) % 3;
break;
case -2:
case -4:
this.chooseIndex = (this.chooseIndex + 1) % 3;
break;
case -5:// 確認鍵
case -6:// 左軟鍵
switch (chooseIndex) {
case 0:
this.continueGame();
break;
case 1:
this.restart();
break;
case 2:
this.endGame();
break;
}
break;
default:
break;
}
} else {
switch (key) {
case 53:
case -5:// 確認鍵
this.player.setBomb(this.player.getRow(), this.player.getCol());
break;
}
}
}
public void restart() {
new Loading(this.stageIndex);
}
public void continueGame() {
this.isPause = false;
this.player.play();
}
public void pauseGame() {
this.isPause = true;
this.player.stop();
}
public void endGame() {
this.isEnd = true;
this.mainThread = null;
System.gc();
try {
Thread.sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
this.dreamBubbleMidlet.menu.showMe();
}
public void nextStage() {
if (this.stageIndex < 4) {
this.stageIndex++;
}
new Loading(this.stageIndex);
}
public void preStage() {
if (this.stageIndex > 0) {
this.stageIndex--;
}
new Loading(this.stageIndex);
}
class Loading implements Runnable {
private Thread innerThread;
private int stageIndex;
public Loading(int stageIndex) {
this.stageIndex = stageIndex;
innerThread = new Thread(this);
innerThread.start();
}
public void run() {
isLoading = true;
loadPercent = 0;
System.gc();
loadStage(stageIndex);
isLoading = false;
}
}
public void sleep() {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
這個是游戲主體類
下面是游戲的人物類
package Game;
import javax.microedition.lci.Image;
import javax.microedition.lci.game.Sprite;
public abstract class Role extends Sprite {
/**
* 人物的基本屬性
*/
protected int type;
protected int xCoodinate;
protected int yCoodinate;
protected int row;
protected int col;
protected int width;
protected int height;
protected int speed;
protected int status;
protected boolean isCanOperate = false;
protected boolean isAlive = true;
/**
* 人物放置炸彈的基本屬性
*/
protected int power;
protected int bombNums;
protected int characterClock = 0;
protected int deadTime = 0;
protected Game game;
protected Role(Image image, int width, int Height, Game game) {
super(image, width, Height);
this.game = game;
}
/**
* 人物拾起道具
* @param tool
*/
public abstract void pickupTool(int tool);
/**
* 碰撞檢測以及坐標的改變,如果對行走條件有特殊需求,既可以在這里寫自己的條件
* @param direction
*/
public abstract void collisionCheck(int direction);
public void updateRole() {
if (this.characterClock < 10000) {
this.characterClock++;
} else {
this.characterClock = 100;
}
int row = this.getRow();
int col = this.getCol();
if (this.isAlive) {
int tool = this.game.map.getToolLayer().getCell(col, row);
if (tool > 0) {
this.pickupTool(tool);
this.game.map.getToolLayer().setCell(col, row, 0);
}
if (this.game.map.hasFeature(row, col, Global.DEADLY)) {
this.isAlive = false;
return;
}
if (this.status == Global.BORN
&& this.characterClock > Global.BORN_TIME) {
this.status = Global.SOUTH;
this.setFrame(Global.SOUTH * 6);
this.isCanOperate = true;
}
if (this.status == Global.BORN) {
if (this.characterClock % 2 == 0)
this.setFrame(Global.BORN * 6 + (this.getFrame() - 1) % 4);
return;
}
} else {
this.isCanOperate = false;
if (this.deadTime <= 20) {
this.deadTime++;
} else {
this.deadTime = 100;
this.setVisible(false);
return;
}
if (this.characterClock % 2 == 0) {
if (this.getFrame() < Global.DEAD * 6) {
this.setFrame(Global.DEAD * 6);
} else {
if (this.getFrame() < 29) {
this.setFrame(this.getFrame() + 1);
} else {
if (this.characterClock % 4 == 0) {
this.setFrame(29);
this.setVisible(true);
} else {
this.setVisible(false);
}
}
}
}
}
}
public void walk(int direction) {
if (!isAlive)
return;
if (!isCanOperate)
return;
if(direction==9) return;
this.collisionCheck(direction);
if (this.characterClock % 2 == 0) {
if (this.status == direction) {
this.setFrame(this.status * 6 + (this.getFrame() + 1) % 6);
} else {
this.status = direction;
this.setFrame(this.status * 6);
}
}
this.setPosition(xCoodinate, yCoodinate);
}
public void stop() {
this.isCanOperate = false;
}
public void play() {
this.isCanOperate = true;
}
public abstract void setBomb(int row, int col);
public void increaseBomb() {
if (this.bombNums < Global.MAX_BOMB_NUMBER)
this.bombNums++;
}
public int getRow() {
return getRow(getBottomY(yCoodinate) - Global.MAP_CELL / 2);
}
public int getCol() {
return getCol(xCoodinate + Global.MAP_CELL / 2);
}
protected int getBottomY(int y) {
return y + this.height - 1;
}
protected int getRightX(int x) {
return x + Global.MAP_CELL - 1;
}
protected int getPreY(int y) {
return getBottomY(y) + 1 - Global.MAP_CELL;
}
protected int getRow(int x) {
return x / Global.MAP_CELL;
}
protected int getCol(int y) {
return y / Global.MAP_CELL;
}
}
我的QQ是609419340
看不明白的可以隨時來問我哦,還可以當時傳給你撒
⑤ 求Flash簡單游戲編程源代碼,簡單的就行,不需要太漂亮的,請發給我。採納後追加分數!
要多簡單的?看看下面的行不?點十下滑鼠,勝利OK不?
var i:uint=0
trace("游戲開始啦!")
stage.addEventListener(MouseEvent.CLICK,go)
function go(e:MouseEvent){
i+=1
trace("你已經點了"+i+"次滑鼠")
if(i>=10){
stage.removeEventListener(MouseEvent.CLICK,go)
trace("游戲結束了!")
}
}
當然你肯定不會滿足這么簡單而且不會失敗的游戲。下面給你個復雜點的源文件 算術題小游戲!
什麼?打不開?哦,請下載FLASHCS5.5版本以上的FLASH軟體。打開了,什麼?還是有點簡單?好的,本想給你上傳個更加復雜的空戰類游戲的,但是只讓我上傳一個附件。
⑥ 求任何網路游戲源碼
魔獸世界的
⑦ 我有一套游戲的源碼 求高人指導開發架設
마슲
⑧ 急求俄羅斯方塊等小游戲的源代碼
俄羅斯方塊——java源代碼提供
import java.awt.*;
import java.awt.event.*;
//俄羅斯方塊類
public class ERS_Block extends Frame{
public static boolean isPlay=false;
public static int level=1,score=0;
public static TextField scoreField,levelField;
public static MyTimer timer;
GameCanvas gameScr;
public static void main(String[] argus){
ERS_Block ers = new ERS_Block("俄羅斯方塊游戲 V1.0 Author:Vincent");
WindowListener win_listener = new WinListener();
ers.addWindowListener(win_listener);
}
//俄羅斯方塊類的構造方法
ERS_Block(String title){
super(title);
setSize(600,480);
setLayout(new GridLayout(1,2));
gameScr = new GameCanvas();
gameScr.addKeyListener(gameScr);
timer = new MyTimer(gameScr);
timer.setDaemon(true);
timer.start();
timer.suspend();
add(gameScr);
Panel rightScr = new Panel();
rightScr.setLayout(new GridLayout(2,1,0,30));
rightScr.setSize(120,500);
add(rightScr);
//右邊信息窗體的布局
MyPanel infoScr = new MyPanel();
infoScr.setLayout(new GridLayout(4,1,0,5));
infoScr.setSize(120,300);
rightScr.add(infoScr);
//定義標簽和初始值
Label scorep = new Label("分數:",Label.LEFT);
Label levelp = new Label("級數:",Label.LEFT);
scoreField = new TextField(8);
levelField = new TextField(8);
scoreField.setEditable(false);
levelField.setEditable(false);
infoScr.add(scorep);
infoScr.add(scoreField);
infoScr.add(levelp);
infoScr.add(levelField);
scorep.setSize(new Dimension(20,60));
scoreField.setSize(new Dimension(20,60));
levelp.setSize(new Dimension(20,60));
levelField.setSize(new Dimension(20,60));
scoreField.setText("0");
levelField.setText("1");
//右邊控制按鈕窗體的布局
MyPanel controlScr = new MyPanel();
controlScr.setLayout(new GridLayout(5,1,0,5));
rightScr.add(controlScr);
//定義按鈕play
Button play_b = new Button("開始游戲");
play_b.setSize(new Dimension(50,200));
play_b.addActionListener(new Command(Command.button_play,gameScr));
//定義按鈕Level UP
Button level_up_b = new Button("提高級數");
level_up_b.setSize(new Dimension(50,200));
level_up_b.addActionListener(new Command(Command.button_levelup,gameScr));
//定義按鈕Level Down
Button level_down_b =new Button("降低級數");
level_down_b.setSize(new Dimension(50,200));
level_down_b.addActionListener(new Command(Command.button_leveldown,gameScr));
//定義按鈕Level Pause
Button pause_b =new Button("游戲暫停");
pause_b.setSize(new Dimension(50,200));
pause_b.addActionListener(new Command(Command.button_pause,gameScr));
//定義按鈕Quit
Button quit_b = new Button("退出遊戲");
quit_b.setSize(new Dimension(50,200));
quit_b.addActionListener(new Command(Command.button_quit,gameScr));
controlScr.add(play_b);
controlScr.add(level_up_b);
controlScr.add(level_down_b);
controlScr.add(pause_b);
controlScr.add(quit_b);
setVisible(true);
gameScr.requestFocus();
}
}
//重寫MyPanel類,使Panel的四周留空間
class MyPanel extends Panel{
public Insets getInsets(){
return new Insets(30,50,30,50);
}
}
//游戲畫布類
class GameCanvas extends Canvas implements KeyListener{
final int unitSize = 30; //小方塊邊長
int rowNum; //正方格的行數
int columnNum; //正方格的列數
int maxAllowRowNum; //允許有多少行未削
int blockInitRow; //新出現塊的起始行坐標
int blockInitCol; //新出現塊的起始列坐標
int [][] scrArr; //屏幕數組
Block b; //對方快的引用
//畫布類的構造方法
GameCanvas(){
rowNum = 15;
columnNum = 10;
maxAllowRowNum = rowNum - 2;
b = new Block(this);
blockInitRow = rowNum - 1;
blockInitCol = columnNum/2 - 2;
scrArr = new int [32][32];
}
//初始化屏幕,並將屏幕數組清零的方法
void initScr(){
for(int i=0;i<rowNum;i++)
for (int j=0; j<columnNum;j++)
scrArr[j]=0;
b.reset();
repaint();
}
//重新刷新畫布方法
public void paint(Graphics g){
for(int i = 0; i < rowNum; i++)
for(int j = 0; j < columnNum; j++)
drawUnit(i,j,scrArr[j]);
}
//畫方塊的方法
public void drawUnit(int row,int col,int type){
scrArr[row][col] = type;
Graphics g = getGraphics();
tch(type){ //表示畫方快的方法
case 0: g.setColor(Color.black);break; //以背景為顏色畫
case 1: g.setColor(Color.blue);break; //畫正在下落的方塊
case 2: g.setColor(Color.magenta);break; //畫已經落下的方法
}
g.fill3DRect(col*unitSize,getSize().height-(row+1)*unitSize,unitSize,unitSize,true);
g.dispose();
}
public Block getBlock(){
return b; //返回block實例的引用
}
//返回屏幕數組中(row,col)位置的屬性值
public int getScrArrXY(int row,int col){
if (row < 0 || row >= rowNum || col < 0 || col >= columnNum)
return(-1);
else
return(scrArr[row][col]);
}
//返回新塊的初始行坐標方法
public int getInitRow(){
return(blockInitRow); //返回新塊的初始行坐標
}
//返回新塊的初始列坐標方法
public int getInitCol(){
return(blockInitCol); //返回新塊的初始列坐標
}
//滿行刪除方法
void deleteFullLine(){
int full_line_num = 0;
int k = 0;
for (int i=0;i<rowNum;i++){
boolean isfull = true;
L1:for(int j=0;j<columnNum;j++)
if(scrArr[j] == 0){
k++;
isfull = false;
break L1;
}
if(isfull) full_line_num++;
if(k!=0 && k-1!=i && !isfull)
for(int j = 0; j < columnNum; j++){
if (scrArr[j] == 0)
drawUnit(k-1,j,0);
else
drawUnit(k-1,j,2);
scrArr[k-1][j] = scrArr[j];
}
}
for(int i = k-1 ;i < rowNum; i++){
for(int j = 0; j < columnNum; j++){
drawUnit(i,j,0);
scrArr[j]=0;
}
}
ERS_Block.score += full_line_num;
ERS_Block.scoreField.setText(""+ERS_Block.score);
}
//判斷游戲是否結束方法
boolean isGameEnd(){
for (int col = 0 ; col <columnNum; col ++){
if(scrArr[maxAllowRowNum][col] !=0)
return true;
}
return false;
}
public void keyTyped(KeyEvent e){
}
public void keyReleased(KeyEvent e){
}
//處理鍵盤輸入的方法
public void keyPressed(KeyEvent e){
if(!ERS_Block.isPlay)
return;
tch(e.getKeyCode()){
case KeyEvent.VK_DOWN:b.fallDown();break;
case KeyEvent.VK_LEFT:b.leftMove();break;
case KeyEvent.VK_RIGHT:b.rightMove();break;
case KeyEvent.VK_SPACE:b.leftTurn();break;
}
}
}
//處理控制類
class Command implements ActionListener{
static final int button_play = 1; //給按鈕分配編號
static final int button_levelup = 2;
static final int button_leveldown = 3;
static final int button_quit = 4;
static final int button_pause = 5;
static boolean pause_resume = true;
int curButton; //當前按鈕
GameCanvas scr;
//控制按鈕類的構造方法
Command(int button,GameCanvas scr){
curButton = button;
this.scr=scr;
}
//按鈕執行方法
public void actionPerformed (ActionEvent e){
tch(curButton){
case button_play:if(!ERS_Block.isPlay){
scr.initScr();
ERS_Block.isPlay = true;
ERS_Block.score = 0;
ERS_Block.scoreField.setText("0");
ERS_Block.timer.resume();
}
scr.requestFocus();
break;
case button_levelup:if(ERS_Block.level < 10){
ERS_Block.level++;
ERS_Block.levelField.setText(""+ERS_Block.level);
ERS_Block.score = 0;
ERS_Block.scoreField.setText(""+ERS_Block.score);
}
scr.requestFocus();
break;
case button_leveldown:if(ERS_Block.level > 1){
ERS_Block.level--;
ERS_Block.levelField.setText(""+ERS_Block.level);
ERS_Block.score = 0;
ERS_Block.scoreField.setText(""+ERS_Block.score);
}
scr.requestFocus();
break;
case button_pause:if(pause_resume){
ERS_Block.timer.suspend();
pause_resume = false;
}else{
ERS_Block.timer.resume();
pause_resume = true;
}
scr.requestFocus();
break;
case button_quit:System.exit(0);
}
}
}
//方塊類
class Block {
static int[][] pattern = {
{0x0f00,0x4444,0x0f00,0x4444},//用十六進至表示,本行表示長條四種狀態
{0x04e0,0x0464,0x00e4,0x04c4},
{0x4620,0x6c00,0x4620,0x6c00},
{0x2640,0xc600,0x2640,0xc600},
{0x6220,0x1700,0x2230,0x0740},
{0x6440,0x0e20,0x44c0,0x8e00},
{0x0660,0x0660,0x0660,0x0660}
};
int blockType; //塊的模式號(0-6)
int turnState; //塊的翻轉狀態(0-3)
int blockState; //快的下落狀態
int row,col; //塊在畫布上的坐標
GameCanvas scr;
//塊類的構造方法
Block(GameCanvas scr){
this.scr = scr;
blockType = (int)(Math.random() * 1000)%7;
turnState = (int)(Math.random() * 1000)%4;
blockState = 1;
row = scr.getInitRow();
col = scr.getInitCol();
}
//重新初始化塊,並顯示新塊
public void reset(){
blockType = (int)(Math.random() * 1000)%7;
turnState = (int)(Math.random() * 1000)%4;
blockState = 1;
row = scr.getInitRow();
col = scr.getInitCol();
dispBlock(1);
}
//實現「塊」翻轉的方法
public void leftTurn(){
if(assertValid(blockType,(turnState + 1)%4,row,col)){
dispBlock(0);
turnState = (turnState + 1)%4;
dispBlock(1);
}
}
//實現「塊」的左移的方法
public void leftMove(){
if(assertValid(blockType,turnState,row,col-1)){
dispBlock(0);
col--;
dispBlock(1);
}
}
//實現塊的右移
public void rightMove(){
if(assertValid(blockType,turnState,row,col+1)){
dispBlock(0);
col++;
dispBlock(1);
}
}
//實現塊落下的操作的方法
public boolean fallDown(){
if(blockState == 2)
return(false);
if(assertValid(blockType,turnState,row-1,col)){
dispBlock(0);
row--;
dispBlock(1);
return(true);
}else{
blockState = 2;
dispBlock(2);
return(false);
}
}
//判斷是否正確的方法
boolean assertValid(int t,int s,int row,int col){
int k = 0x8000;
for(int i = 0; i < 4; i++){
for(int j = 0; j < 4; j++){
if((int)(pattern[t][s]&k) != 0){
int temp = scr.getScrArrXY(row-i,col+j);
if (temp<0||temp==2)
return false;
}
k = k >> 1;
}
}
return true;
}
//同步顯示的方法
public synchronized void dispBlock(int s){
int k = 0x8000;
for (int i = 0; i < 4; i++){
for(int j = 0; j < 4; j++){
if(((int)pattern[blockType][turnState]&k) != 0){
scr.drawUnit(row-i,col+j,s);
}
k=k>>1;
}
}
}
}
//定時線程
class MyTimer extends Thread{
GameCanvas scr;
public MyTimer(GameCanvas scr){
this.scr = scr;
}
public void run(){
while(true){
try{
sleep((10-ERS_Block.level + 1)*100);
}
catch(InterruptedException e){}
if(!scr.getBlock().fallDown()){
scr.deleteFullLine();
if(scr.isGameEnd()){
ERS_Block.isPlay = false;
suspend();
}else
scr.getBlock().reset();
}
}
}
}
class WinListener extends WindowAdapter{
public void windowClosing (WindowEvent l){
System.exit(0);
}
}
⑨ 跪求C++大神,只需要寫一個小游戲源代碼,事成有現金酬謝。
#include <iostream>
using namespace std;
double shengmingli=2000;//定義主角初始生命力
int gongjili=150;//定義主角初始攻擊力
int fangyuli=200;//定義主角初始防禦力
int money=20;//定義主角初始金錢數量
bool guoguan;//定義是否通關判定
void wuqidian();//定義武器店函數
void yaodian();//定義葯店函數
void guaiwu1();//定義小怪物函數
void guaiwu2();//定義大怪物函數
int main()
{
cout<<"歡迎你開始玩打怪物小游戲!\n";
cout<<"小鎮\n";
cout<<"一個1000年的小鎮。周圍有一條河,有一片樹林,很多房子和很多人。\n有一家葯店"<<endl;
cout<<"和一家武器店。\n";
int xiaozhen;//定義選擇項目
cout<<"1.去武器店"<<endl;
cout<<"2.去葯品店"<<endl;
cout<<"3.去打小怪物"<<endl;
cout<<"4.去打大怪物"<<endl;
cout<<"5.退出遊戲"<<endl;
cout<<"6.顯示你的狀態"<<endl;
cin>>xiaozhen;
while(xiaozhen!=5)//輸入5時退出遊戲
{
if(shengmingli<=0)//主角生命力小於等於0時游戲結束
{
cout<<"你死啦!"<<endl;
break;
}
if(guoguan)
{
cout<<"恭喜通關!"<<endl;
break;
}
if(xiaozhen==6)//輸入6可檢測自己的狀態
{
cout<<"你的生命力:"<<shengmingli<<endl;
cout<<"你的攻擊力:"<<gongjili<<endl;
cout<<"你的防禦力:"<<fangyuli<<endl;
cout<<"你擁有的錢:"<<money<<endl;
}
else
switch(xiaozhen)
{
case 1 : wuqidian();break;
case 2 : yaodian();break;
case 3 : guaiwu1();break;
case 4 : guaiwu2();break;
default : cout<<"請不要亂選!"<<endl;break;
}
cin>>xiaozhen;
}
if(xiaozhen==5)
{
cout<<"正在退出遊戲……"<<endl;
}
cin.get();
cin.get();
return 0;
}
void wuqidian()
{
cout<<"歡迎來到武器店!"<<endl;
cout<<"1、買小刀(1M加2攻擊力)"<<endl;
cout<<"2、買短劍(2M加20攻擊力)"<<endl;
cout<<"3、買大砍刀(5M加40攻擊力)"<<endl;
cout<<"4、買雙節棍(7M加60攻擊力)"<<endl;
cout<<"5、買盾牌(2M加30防禦力)"<<endl;
cout<<"6、買鎧甲(5M加60防禦力)"<<endl;
cout<<"7、離開武器店"<<endl;
int wuqidian;
cin>>wuqidian;
while(wuqidian!=7)//輸入7時結束函數
{
switch(wuqidian)
{
case 1 : if(money<10)
cout<<"你的錢不夠"<<endl;//錢不夠時返回Flase
else
cout<<"購買成功!"<<endl;//錢足夠時返回True
gongjili+=2;
money-=1;
break;
case 2 : if(money<80)
cout<<"你的錢不夠"<<endl;
else
cout<<"購買成功!"<<endl;
gongjili+=20;
money-=80;
break;
case 3 : if(money<140)
cout<<"你的錢不夠"<<endl;
else
cout<<"購買成功!"<<endl;
gongjili+=40;
money-=140;
break;
case 4 : if(money<200)
cout<<"你的錢不夠"<<endl;
else
cout<<"購買成功!"<<endl;
gongjili+=60;
money-=200;
break;
case 5 : if(money<60)
cout<<"你的錢不夠"<<endl;
else
cout<<"購買成功!"<<endl;
fangyuli+=30;
money-=60;
break;
fangyuli+=60;
money-=100;
break;
default : cout<<"無"<<endl;
break;
}
cin>>wuqidian;
}
if(wuqidian==7)
{ //返回main()主函數
cout<<"歡迎下次再來!"<<endl;
cout<<"歡迎你開始玩打怪物小游戲!\n";
cout<<"小鎮\n";
cout<<"一個1000年的小鎮。周圍有一條河,有一片樹林,很多房子和很多人。\n有一家葯店"<<endl;
cout<<"和一家武器店。\n";
cout<<"1.去武器店"<<endl;
cout<<"2.去葯品店"<<endl;
cout<<"3.去打小怪物"<<endl;
cout<<"4.去打大怪物"<<endl;
cout<<"5.退出遊戲"<<endl;
cout<<"6.顯示你的狀態"<<endl;
}
}
/*
yaodian()的設置與wuqidian()相同,可參照閱讀.
*/
void yaodian()
{
cout<<"歡迎來到葯品店!"<<endl;
cout<<"1、買1號補血葯(10M加200生命)"<<endl;
cout<<"2、買2號補血葯(50M加1000生命力)"<<endl;
cout<<"3、買3號補血葯(100M加2200生命力)"<<endl;
cout<<"4、離開葯品店"<<endl;
int yaodian;
cin>>yaodian;
while(yaodian!=4)
{
switch(yaodian)
{
case 1 : if(money<10)
cout<<"你的錢不夠"<<endl;
else
cout<<"購買成功!"<<endl;
shengmingli+=200;
money-=10;
break;
case 2 : if(money<50)
cout<<"你的錢不夠"<<endl;
else
cout<<"購買成功!"<<endl;
shengmingli+=1000;
money-=50;
break;
case 3 : if(money<100)
cout<<"你的錢不夠"<<endl;
else
cout<<"購買成功!"<<endl;
shengmingli+=2200;
money-=100;
break;
default : cout<<"無"<<endl;
break;
}
cin>>yaodian;
}
if(yaodian==4)
{
cout<<"歡迎下次再來!"<<endl;
cout<<"歡迎你開始玩打怪物小游戲!\n";
cout<<"小鎮\n";
cout<<"一個1000年的小鎮。周圍有一條河,有一片樹林,很多房子和很多人。\n有一家葯店"<<endl;
cout<<"和一家武器店。\n";
cout<<"1.去武器店"<<endl;
cout<<"2.去葯品店"<<endl;
cout<<"3.去打小怪物"<<endl;
cout<<"4.去打大怪物"<<endl;
cout<<"5.退出遊戲"<<endl;
cout<<"6.顯示你的狀態"<<endl;
}
}
/*這里是兩個戰斗函數,使用指針來處理.避免造成內存崩潰.*/
void guaiwu1()
{
cout<<"開始與小怪物戰斗!!!"<<endl;
double* g_shengmingli=new double;//定義怪物生命
int* g_gongjili=new int;//定義怪物攻擊力
int* g_fangyuli=new int;//定義怪物防禦力
int* g_money=new int;//定義怪物金錢
*g_shengmingli=100;
*g_gongjili=5;
*g_fangyuli=3;
*g_money=5;
double* tongji1=new double;//用來計算主角對怪物的殺傷
double* tongji2=new double;//用來計算怪物對主角的殺傷
*tongji1=0;
*tongji2=0;
int* huihe=new int;//定義回合數
*huihe=1;
cout<<"你開始對小怪物進行攻擊!"<<endl;
int* xuanze=new int;
/*
攻擊計算公式
殺傷=攻擊力*2-防禦力
玩家每回合可以選擇攻擊與逃跑
*/
while((*g_shengmingli)>0 && shengmingli>0 && (*xuanze)!=2)
{
cout<<"現在是"<<"第"<<*huihe<<"回合!"<<endl;
cout<<"請選擇你的動作:\n";
cout<<"1、攻擊\n2、逃跑\n";
cin>>*xuanze;
switch((*xuanze))
{
case 1 : cout<<"你對小怪物發動了攻擊!"<<endl;
*g_shengmingli-=gongjili*2-(*g_fangyuli);
*tongji1=gongjili*2-(*g_fangyuli);
cout<<"你打掉了小怪物"<<*tongji1<<"的生命!"<<endl;
cout<<"小怪物還剩"<<(*g_shengmingli)-(*tongji1)<<"點生命"<<endl;
shengmingli-=(*g_gongjili)*2-fangyuli;
*tongji2=(*g_gongjili)*2-fangyuli;
cout<<"小怪物對你發動了攻擊!"<<endl;
cout<<"小怪物打掉了你"<<*tongji2<<"的生命!"<<endl;
cout<<"你還剩"<<shengmingli-(*tongji2)<<"點生命"<<endl;break;
case 2 : cout<<"你決定逃跑!"<<endl;
cout<<"逃跑成功!"<<endl;continue;
default : cout<<"請不要亂選!"<<endl;
}
(*huihe)++;
}
if((*g_shengmingli)<=0)
{//殺死怪物後的返回
cout<<"小怪物被你殺死了!你真厲害!!!"<<endl;
money+=(*g_money);
cout<<"歡迎你開始玩打怪物小游戲!\n";
cout<<"小鎮\n";
cout<<"一個1000年的小鎮。周圍有一條河,有一片樹林,很多房子和很多人。\n有一家葯店"<<endl;
cout<<"和一家武器店。\n";
cout<<"1.去武器店"<<endl;
cout<<"2.去葯品店"<<endl;
cout<<"3.去打小怪物"<<endl;
cout<<"4.去打大怪物"<<endl;
cout<<"5.退出遊戲"<<endl;
cout<<"6.顯示你的狀態"<<endl;
}
else
if(shengmingli<=0)
{//被怪物殺死後的返回
cout<<"你被小怪物殺死了!游戲結束!!!"<<endl;
}
else
if((*xuanze)==2)
{//逃跑的返回
cout<<"你逃回了小鎮!"<<endl;
cout<<"歡迎你開始玩打怪物小游戲!\n";
cout<<"小鎮\n";
cout<<"一個1000年的小鎮。周圍有一條河,有一片樹林,很多房子和很多人。\n有一家葯店"<<endl;
cout<<"和一家武器店。\n";
cout<<"1.去武器店"<<endl;
cout<<"2.去葯品店"<<endl;
cout<<"3.去打小怪物"<<endl;
cout<<"4.去打大怪物"<<endl;
cout<<"5.退出遊戲"<<endl;
cout<<"6.顯示你的狀態"<<endl;
}
delete g_shengmingli;
delete g_gongjili;
delete g_fangyuli;
delete g_money;
delete tongji1;
delete tongji2;
}
/*
設置均與void函數guaiwu1()相同,可參照上例閱讀.
*/
void guaiwu2()
{
cout<<"開始與大怪物戰斗!!!"<<endl;
double* g_shengmingli=new double;
int* g_gongjili=new int;
int* g_fangyuli=new int;
*g_shengmingli=3600;
*g_gongjili=500;
*g_fangyuli=500;
double* tongji1=new double;
double* tongji2=new double;
*tongji1=0;
*tongji2=0;
int* huihe=new int;
*huihe=1;
cout<<"你開始對大怪物進行攻擊!"<<endl;
int* xuanze=new int;
while((*g_shengmingli)>0 && shengmingli>0 && (*xuanze)!=2)
{
cout<<"現在是"<<"第"<<*huihe<<"回合!"<<endl;
cout<<"請選擇你的動作:\n";
cout<<"1、攻擊\n2、逃跑\n";
cin>>*xuanze;
switch((*xuanze))
{
case 1 : cout<<"你對大怪物發動了攻擊!"<<endl;
*g_shengmingli-=gongjili*2-(*g_fangyuli);
*tongji1=gongjili*2-(*g_fangyuli);
cout<<"你打掉了大怪物"<<*tongji1<<"的生命!"<<endl;
cout<<"大怪物還剩"<<(*g_shengmingli)-(*tongji1)<<"點生命"<<endl;
shengmingli-=(*g_gongjili)*2-fangyuli;
*tongji2=(*g_gongjili)*2-fangyuli;
cout<<"大怪物對你發動了攻擊!"<<endl;
cout<<"大怪物打掉了你"<<*tongji2<<"的生命!"<<endl;
cout<<"你還剩"<<shengmingli-(*tongji2)<<"點生命"<<endl;break;
case 2 : cout<<"你決定逃跑!"<<endl;
cout<<"逃跑成功!"<<endl;continue;
default : cout<<"請不要亂選!"<<endl;
}
(*huihe)++;
}
if((*g_shengmingli)<=0)
{
cout<<"大怪物被你殺死了!你真厲害!!!"<<endl;
guoguan=true;
cout<<"歡迎你開始玩打怪物小游戲!\n";
cout<<"小鎮\n";
cout<<"一個1000年的小鎮。周圍有一條河,有一片樹林,很多房子和很多人。\n有一家葯店"<<endl;
cout<<"和一家武器店。\n";
cout<<"1.去武器店"<<endl;
cout<<"2.去葯品店"<<endl;
cout<<"3.去打小怪物"<<endl;
cout<<"4.去打大怪物"<<endl;
cout<<"5.退出遊戲"<<endl;
cout<<"6.顯示你的狀態"<<endl;
}
else
if(shengmingli<=0)
{
cout<<"你被大怪物殺死了!游戲結束!!!"<<endl;
}
else
if((*xuanze)==2)
{
cout<<"你逃回了小鎮!"<<endl;
cout<<"歡迎你開始玩打怪物小游戲!\n";
cout<<"小鎮\n";
cout<<"一個1000年的小鎮。周圍有一條河,有一片樹林,很多房子和很多人。\n有一家葯店"<<endl;
cout<<"和一家武器店。\n";
cout<<"1.去武器店"<<endl;
cout<<"2.去葯品店"<<endl;
cout<<"3.去打小怪物"<<endl;
cout<<"4.去打大怪物"<<endl;
cout<<"5.退出遊戲"<<endl;
cout<<"6.顯示你的狀態"<<en;
}
delete g_shengmingli;
delete g_gongjili;
delete g_fangyuli;
delete tongji1;
delete tongji2;
}
⑩ 為什麼淘寶賣的網頁游戲源代碼只有10元是騙子
不一定,若代碼極為簡單,或極易設計出來,故而便宜。