消除類游戲源碼
A. c語言實現用小球消除磚塊,滑鼠控制擋板,求源代碼
#include<graphics.h>
#include<conio.h>
#define HIGH 480
#define WIDTH 640
#define N 14//磚塊的數目
int ball_x,ball_y;
int ball_vx,ball_vy;
int radius;
int bar_x,bar_y;
int bar_high,bar_width;
int bar_left,bar_right,bar_top,bar_bottom;
int isbrick[N];
int brick_high,brick_width;
void startup()//數據初始化
{
initgraph(640,480);
ball_x=WIDTH/2;
ball_y=HIGH/2;
ball_vx=4;
ball_vy=4;
radius=20;
bar_high=HIGH/14;
bar_width=WIDTH/4;
bar_x=WIDTH/2;
bar_y=HIGH-bar_high/2;
bar_left=bar_x-bar_width/2;
bar_right=bar_x+bar_width/2;
bar_top=bar_y-bar_high/2;
bar_bottom=bar_y+bar_high/2;
brick_width=WIDTH/N;
brick_high=HIGH/N;
int i;
for(i=0;i<N;i++)
isbrick[i]=1;
BeginBatchDraw();
}
void clear()//清除畫面
{
setcolor(BLACK);
setfillcolor(BLACK);
fillcircle(ball_x,ball_y,radius);
bar(bar_left,bar_top,bar_right,bar_bottom);
int i,brick_left,brick_right,brick_top,brick_bottom;
for(i=0;i<N;i++)
{
brick_left=i*brick_width;
brick_right=brick_left+brick_width;
brick_top=0;
brick_bottom=brick_high;
if(!isbrick[i])
fillrectangle(brick_left,brick_top,brick_right,brick_bottom);
}
}
void show()//顯示畫面
{
setcolor(RED);
setfillcolor(WHITE);
fillcircle(ball_x,ball_y,radius);
bar(bar_left,bar_top,bar_right,bar_bottom);
int i,brick_left,brick_right,brick_top,brick_bottom;
for(i=0;i<N;i++)
{
brick_left=i*brick_width;
brick_right=brick_left+brick_width;
brick_top=0;
brick_bottom=brick_high;
if(isbrick[i])
{setcolor(WHITE);
setfillcolor(BROWN);
fillrectangle(brick_left,brick_top,brick_right,brick_bottom);
}
}
FlushBatchDraw();
Sleep(10);
}
void output()//與用戶輸入無關的更新
{
if(((ball_y+radius>=bar_top)&&(ball_y+radius<bar_bottom-bar_high/3))
||((ball_y-radius<=bar_bottom)&&(ball_y-radius>bar_top-bar_high/3)))
if((ball_x>=bar_left)&&(ball_x<=bar_right))//擋板與小球碰撞
ball_vy=-ball_vy;
ball_x=ball_x+ball_vx;//小球更新坐標
ball_y=ball_y+ball_vy;
if((ball_x<=radius)||(ball_x>=WIDTH-radius))//小球碰到邊框返回
ball_vx=-ball_vx;
if((ball_y<=radius)||(ball_y>=HIGH-radius))
ball_vy=-ball_vy;
int i,brick_left,brick_right,brick_top,brick_bottom;//判斷小球是否和磚塊碰撞
for(i=0;i<N;i++)
{
if(isbrick[i])//磚塊存在
{
brick_left=i*brick_width;
brick_right=brick_left+brick_width;
brick_bottom=brick_high;
if((ball_y==brick_bottom+radius)&&(ball_x>=brick_left)&&(ball_x<=brick_right))
{
isbrick[i]=0;
ball_vy=-ball_vy;
}
}
}
}
void Input()//與用戶輸入有關的更新
{
{
MOUSEMSG m;//定義滑鼠信息
if(MouseHit())//檢測當前是否有滑鼠信息
{
m=GetMouseMsg();//獲取一條滑鼠信息
if(m.uMsg==WM_MOUSEMOVE)
{
//擋板的值得滑鼠的位置
bar_x=m.x;
bar_y=m.y;
bar_left=bar_x-bar_width/2;
bar_right=bar_x+bar_width/2;
bar_top=bar_y-bar_high/2;
bar_bottom=bar_y+bar_high/2;
}
else if(m.uMsg==WM_LBUTTONDOWN)//按下滑鼠左鍵,初始化小球的位置為擋板上面中心
{
ball_x=bar_x;
ball_y=bar_top-radius-3;
}
}
}
}
void gameover()//游戲結束
{
EndBatchDraw();
closegraph();
}
main()
{
startup();
while(1)
{
clear();
output();
Input();
show();
}
gameover();
return 0;
}
B. 誰能給我一個手機游戲的源代碼啊
這個地址也有,不過直接給你吧,這樣比較好
先給你看看主要的類吧
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
看不明白的可以隨時來問我哦,還可以當時傳給你撒
C. 有沒有Android版的 2048(俄羅斯方塊版本、消消樂版本) 游戲的源代碼
你到CSDN上邊查一下,一大把,都是源碼。
D. 求小游戲源代碼
貓版超級瑪麗游戲しょぼんのアクションBGMcastle.mp3
.......................................
geon.mp3
.......................................field.mp3
.......................................puyo.mp3
.......................................star4.mp3
....................................desktop.ini
....................................loadg.cpp
....................................main.cpp
....................................main.h
....................................RESrock.png
.......................................rock2.png
.......................................haikei.png
.......................................item.png
.......................................omake.png
.......................................omake2.png
.......................................player.png
.......................................syobon3.png
....................................... eki.png
....................................SE4-clear.mp3
......................................allclear.mp3
......................................rockbreak.mp3
......................................rockcoin.mp3
......................................rockkinoko.mp3
......................................coin.mp3
......................................death.mp3
......................................dokan.mp3
......................................gameover.mp3
......................................goal.mp3
......................................hintblock.mp3
......................................humi.mp3
......................................jump.mp3
......................................jumpblock.mp3
......................................kirra.mp3
......................................koura.mp3
......................................powerup.mp3
......................................pswitch.mp3
...................................... ekifire.mp3
.....................................YSapploc.msi
.......................................AppLoc.tmp
....................................しょぼんのアクション.exe
....................................リードミー.txt
....................................點我開始玩.cmd
....................................BGM
....................................RES
....................................SE
....................................SYS
................しょぼんのアクション
http://www.pudn.com/downloads385/sourcecode/game/detail1652652.html
http://www.pudn.com/downloads234/sourcecode/game/detail1099372.html
http://syobon.codeplex.com/releases/62191/download/214890
變態版http://download.csdn.net/download/lc19890326/861250
VF
- 表單(scx/sct)可以用類瀏覽器的ViewClassCode功能得到包括所有屬性和代碼的prg格式文本
報表(frx/frt)無法導出成prg格式
菜單(mnx/mnt)編譯時已自動生成了prg格式的文件mpr
注意「文件類型」一定要選擇「窗體」類型,否則「查看代碼」將處於隱藏狀態,無法看到原代碼,切記!!
1:如果只是想獲得網站的樣式,使用瀏覽器的「保存」功能就可以實現!關鍵是下載到CSS文件就可以了!
2:找到對方使用的整站程序
E. 求java小游戲源代碼
[最佳答案] 連連看java源代碼 import javax.swing.*; import java.awt.*; import java.awt.event.*; pu... int x0=0,y0=0,x=0,y=0,fristMsg=0,secondMsg=0,validateLV; //游戲按鈕的位...
F. 求java rpg小游戲源代碼 最好是文字rpg 不需要很復雜 只是交作業用
連連看的小源碼
package Lianliankan;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class lianliankan implements ActionListener
{
JFrame mainFrame; //主面板
Container thisContainer;
JPanel centerPanel,southPanel,northPanel; //子面板
JButton diamondsButton[][] = new JButton[6][5];//游戲按鈕數組
JButton exitButton,resetButton,newlyButton; //退出,重列,重新開始按鈕
JLabel fractionLable=new JLabel("0"); //分數標簽
JButton firstButton,secondButton; //分別記錄兩次被選中的按鈕
int grid[][] = new int[8][7];//儲存游戲按鈕位置
static boolean pressInformation=false; //判斷是否有按鈕被選中
int x0=0,y0=0,x=0,y=0,fristMsg=0,secondMsg=0,validateLV; //游戲按鈕的位置坐標
int i,j,k,n;//消除方法控制
public void init(){
mainFrame=new JFrame("JKJ連連看");
thisContainer = mainFrame.getContentPane();
thisContainer.setLayout(new BorderLayout());
centerPanel=new JPanel();
southPanel=new JPanel();
northPanel=new JPanel();
thisContainer.add(centerPanel,"Center");
thisContainer.add(southPanel,"South");
thisContainer.add(northPanel,"North");
centerPanel.setLayout(new GridLayout(6,5));
for(int cols = 0;cols < 6;cols++){
for(int rows = 0;rows < 5;rows++ ){
diamondsButton[cols][rows]=new JButton(String.valueOf(grid[cols+1][rows+1]));
diamondsButton[cols][rows].addActionListener(this);
centerPanel.add(diamondsButton[cols][rows]);
}
}
exitButton=new JButton("退出");
exitButton.addActionListener(this);
resetButton=new JButton("重列");
resetButton.addActionListener(this);
newlyButton=new JButton("再來一局");
newlyButton.addActionListener(this);
southPanel.add(exitButton);
southPanel.add(resetButton);
southPanel.add(newlyButton);
fractionLable.setText(String.valueOf(Integer.parseInt(fractionLable.getText())));
northPanel.add(fractionLable);
mainFrame.setBounds(280,100,500,450);
mainFrame.setVisible(true);
}
public void randomBuild() {
int randoms,cols,rows;
for(int twins=1;twins<=15;twins++) {
randoms=(int)(Math.random()*25+1);
for(int alike=1;alike<=2;alike++) {
cols=(int)(Math.random()*6+1);
rows=(int)(Math.random()*5+1);
while(grid[cols][rows]!=0) {
cols=(int)(Math.random()*6+1);
rows=(int)(Math.random()*5+1);
}
this.grid[cols][rows]=randoms;
}
}
}
public void fraction(){
fractionLable.setText(String.valueOf(Integer.parseInt(fractionLable.getText())+100));
}
public void reload() {
int save[] = new int[30];
int n=0,cols,rows;
int grid[][]= new int[8][7];
for(int i=0;i<=6;i++) {
for(int j=0;j<=5;j++) {
if(this.grid[i][j]!=0) {
save[n]=this.grid[i][j];
n++;
}
}
}
n=n-1;
this.grid=grid;
while(n>=0) {
cols=(int)(Math.random()*6+1);
rows=(int)(Math.random()*5+1);
while(grid[cols][rows]!=0) {
cols=(int)(Math.random()*6+1);
rows=(int)(Math.random()*5+1);
}
this.grid[cols][rows]=save[n];
n--;
}
mainFrame.setVisible(false);
pressInformation=false; //這里一定要將按鈕點擊信息歸為初始
init();
for(int i = 0;i < 6;i++){
for(int j = 0;j < 5;j++ ){
if(grid[i+1][j+1]==0)
diamondsButton[i][j].setVisible(false);
}
}
}
public void estimateEven(int placeX,int placeY,JButton bz) {
if(pressInformation==false) {
x=placeX;
y=placeY;
secondMsg=grid[x][y];
secondButton=bz;
pressInformation=true;
}
else {
x0=x;
y0=y;
fristMsg=secondMsg;
firstButton=secondButton;
x=placeX;
y=placeY;
secondMsg=grid[x][y];
secondButton=bz;
if(fristMsg==secondMsg && secondButton!=firstButton){
xiao();
}
}
}
public void xiao() { //相同的情況下能不能消去。仔細分析,不一條條注釋
if((x0==x &&(y0==y+1||y0==y-1)) || ((x0==x+1||x0==x-1)&&(y0==y))){ //判斷是否相鄰
remove();
}
else{
for (j=0;j<7;j++ ) {
if (grid[x0][j]==0){ //判斷第一個按鈕同行哪個按鈕為空
if (y>j) { //如果第二個按鈕的Y坐標大於空按鈕的Y坐標說明第一按鈕在第二按鈕左邊
for (i=y-1;i>=j;i-- ){ //判斷第二按鈕左側直到第一按鈕中間有沒有按鈕
if (grid[x][i]!=0) {
k=0;
break;
}
else{ k=1; } //K=1說明通過了第一次驗證
}
if (k==1) {
linePassOne();
}
}
if (y<j){ //如果第二個按鈕的Y坐標小於空按鈕的Y坐標說明第一按鈕在第二按鈕右邊
for (i=y+1;i<=j ;i++ ){ //判斷第二按鈕左側直到第一按鈕中間有沒有按鈕
if (grid[x][i]!=0){
k=0;
break;
}
else { k=1; }
}
if (k==1){
linePassOne();
}
}
if (y==j ) {
linePassOne();
}
}
if (k==2) {
if (x0==x) {
remove();
}
if (x0<x) {
for (n=x0;n<=x-1;n++ ) {
if (grid[n][j]!=0) {
k=0;
break;
}
if(grid[n][j]==0 && n==x-1) {
remove();
}
}
}
if (x0>x) {
for (n=x0;n>=x+1 ;n-- ) {
if (grid[n][j]!=0) {
k=0;
break;
}
if(grid[n][j]==0 && n==x+1) {
remove();
}
}
}
}
}
for (i=0;i<8;i++ ) { //列
if (grid[i][y0]==0) {
if (x>i) {
for (j=x-1;j>=i ;j-- ) {
if (grid[j][y]!=0) {
k=0;
break;
}
else { k=1; }
}
if (k==1) {
rowPassOne();
}
}
if (x<i) {
for (j=x+1;j<=i;j++ ) {
if (grid[j][y]!=0) {
k=0;
break;
}
else { k=1; }
}
if (k==1) {
rowPassOne();
}
}
if (x==i) {
rowPassOne();
}
}
if (k==2){
if (y0==y) {
remove();
}
if (y0<y) {
for (n=y0;n<=y-1 ;n++ ) {
if (grid[i][n]!=0) {
k=0;
break;
}
if(grid[i][n]==0 && n==y-1) {
remove();
}
}
}
if (y0>y) {
for (n=y0;n>=y+1 ;n--) {
if (grid[i][n]!=0) {
k=0;
break;
}
if(grid[i][n]==0 && n==y+1) {
remove();
}
}
}
}
}
}
}
public void linePassOne(){
if (y0>j){ //第一按鈕同行空按鈕在左邊
for (i=y0-1;i>=j ;i-- ){ //判斷第一按鈕同左側空按鈕之間有沒按鈕
if (grid[x0][i]!=0) {
k=0;
break;
}
else { k=2; } //K=2說明通過了第二次驗證
}
}
if (y0<j){ //第一按鈕同行空按鈕在與第二按鈕之間
for (i=y0+1;i<=j ;i++){
if (grid[x0][i]!=0) {
k=0;
break;
}
else{ k=2; }
}
}
}
public void rowPassOne(){
if (x0>i) {
for (j=x0-1;j>=i ;j-- ) {
if (grid[j][y0]!=0) {
k=0;
break;
}
else { k=2; }
}
}
if (x0<i) {
for (j=x0+1;j<=i ;j++ ) {
if (grid[j][y0]!=0) {
k=0;
break;
}
else { k=2; }
}
}
}
public void remove(){
firstButton.setVisible(false);
secondButton.setVisible(false);
fraction();
pressInformation=false;
k=0;
grid[x0][y0]=0;
grid[x][y]=0;
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==newlyButton){
int grid[][] = new int[8][7];
this.grid = grid;
randomBuild();
mainFrame.setVisible(false);
pressInformation=false;
init();
}
if(e.getSource()==exitButton)
System.exit(0);
if(e.getSource()==resetButton)
reload();
for(int cols = 0;cols < 6;cols++){
for(int rows = 0;rows < 5;rows++ ){
if(e.getSource()==diamondsButton[cols][rows])
estimateEven(cols+1,rows+1,diamondsButton[cols][rows]);
}
}
}
public static void main(String[] args) {
lianliankan llk = new lianliankan();
llk.randomBuild();
llk.init();
}
}
//old 998 lines
//new 318 lines
G. 急求俄羅斯方塊等小游戲的源代碼
俄羅斯方塊——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);
}
}
H. 一些資源
<1>解決RaycastTarget勾選過多的煩惱
<2>UGUI背包系統教學視頻
<3>優化之對象池
http://blog.csdn.net/linshuhe1/article/details/50949836
不定時更新。
極客學院
極視教育
https://zhuanlan.hu.com/p/22330268面試題匯總
http://www.cnblogs.com/android-blogs/p/6369271.htmlSun『刺眼的博客
學習網站:
2-30《黑暗靈魂》
鏈接:http://pan..com/s/1dEn0cvf密碼:gi00
2-31《夢幻昆侖》全套源碼
鏈接:http://pan..com/s/1i40jjxz密碼:zddg
2-32《末日浩劫之歌》
鏈接:http://pan..com/s/1i3QruEL密碼:qzil
2-33DNF3D游戲源碼
鏈接:http://pan..com/s/1brAf2A密碼:63h2
2-34《某某軍團》
鏈接:http://pan..com/s/1i4kAo7r密碼:4pu7
2-35《農場模擬經營》游戲
鏈接:http://pan..com/s/1kTUreGv密碼:3czd
2-36《泡泡龍》
鏈接:http://pan..com/s/1mgYsNiS密碼:e7b0
2-37《全民飛機大戰》源碼
鏈接:http://pan..com/s/1boj04cZ密碼:o2wo
2-38三消類游戲《水果樂園》
鏈接:http://pan..com/s/1eRv57oe密碼:bhtb
2-39塔防游戲包
鏈接:http://pan..com/s/1i4lWi7z密碼:lbez
2-40仿神廟逃亡開發包InfiniteRunnerStarterPack新版
鏈接:http://pan..com/s/1pKtl1EV密碼:kyjw
2-41高級塔防開發包TowerDefenseToolKitTDTK
鏈接:http://pan..com/s/1boL6f密碼:vtvj
2-42JewelMatchFramework-PRO1.1.2b消除類游戲框架
鏈接:http://pan..com/s/1boeGOaj密碼:qoni
2-43DeepSpacePlanets太空星球模型源碼
鏈接:http://pan..com/s/1jHz1sYM密碼:4bkc
2-44BottleShot(iPhone.Android)移動版酒吧砸瓶子
鏈接:http://pan..com/s/1o7jKd3k密碼:ixiz
2-45HeliHellPack直升機控制環境地形飛行
鏈接:http://pan..com/s/1eRuPDNc密碼:1s2n
2-46ipad版游戲《DOZ》僵屍游戲源碼
鏈接:http://pan..com/s/1dEgE52L密碼:v7lr
2-47插件跑酷UltimateEndlessRunnerKitv1.03資源包
鏈接:http://pan..com/s/1nuhexnj密碼:q817
2-483DInfiniteRunnerToolkitv1.2跑酷完整項目
鏈接:http://pan..com/s/1i3ZeTxz密碼:68i6
2-492D橫版infiniteRunnerToolkit項目資源包
鏈接:http://pan..com/s/1ntQjts1密碼:0n8i
2-50《最初的幻想》工程文件(源碼)
鏈接:http://pan..com/s/1nuzvfGd密碼:yidn
2-51《最後一戰》UNity4.6
鏈接:http://pan..com/s/1nunPHYL密碼:p27a
2-52《爐石傳說》客戶端加伺服器端
鏈接:http://pan..com/s/1nuo5Pk1
2-53《神廟逃亡之魔境仙蹤》
鏈接:http://pan..com/s/1sk8a39r
2-54《武士2復仇》
鏈接:http://pan..com/s/1mhzxee0
2-55《完美國際》客戶端+伺服器端
鏈接:http://pan..com/s/1o7xXoBC密碼:ul5b
2-56消除游戲源碼內置關卡編譯器
鏈接:http://pan..com/s/1qXeWi9Q密碼:pgxh
2-57永恆冒險
鏈接:http://pan..com/s/1c1sRVPY密碼:r2nl
2-58UltimateMMORPGKit在線mmorpg游戲開發
鏈接:http://pan..com/s/1dEr3Wzv密碼:0gyf
2-59角色扮演開發包ORKOkashiRPGKit源碼1.2.5
鏈接:http
2-1爐石傳說客戶端加伺服器端鏈接:http://pan..com/s/1dDKY3Fr密碼:c03q
2-2新仙劍奇俠傳鏈接:http://pan..com/s/1jH0fIuU密碼:k5xp
2-3unity3d戰斗卡牌《變身吧主公》客戶端+伺服器源碼鏈接:http://pan..com/s/1kUpot51密碼:i02u
2-4降臨OL-U3D全套源碼鏈接:http://pan..com/s/1sktLQ5v密碼:we0g
2-5武士2復仇鏈接:http://pan..com/s/1i4dhjRB密碼:a1yb
2-6《神啟》全套源碼+資源鏈接:http://pan..com/s/1o6UP9rG密碼:1vbg
2-7神廟逃亡之魔境仙蹤鏈接:http://pan..com/s/1c1umpUS密碼:na83
2-8《超級馬里奧64》HD版unity源碼鏈接:http://pan..com/s/1pKpMmwj密碼:d5d8
2-9暗影之槍ShadowGun鏈接:http://pan..com/s/1qWYPnxa密碼:wsqo
2-10unity3d跑酷游戲源碼鏈接:http://pan..com/s/1kU1z8FX密碼:nm3t
2-11地牢破壞者001-Dungeon+Breaker+Starter+Kit+beta3鏈接:http://pan..com/s/1gez5Wv5密碼:l98y
2-12LOL塔防項目源碼、DNF格鬥核心鏈接:http://pan..com/s/1sk07yCt密碼:zw5t
2-13火炬之光demo第四代鏈接:http://pan..com/s/1i4k8L4x密碼:mbqb
2-14AdvanceSniperStarterKit射擊游戲源碼鏈接:http://pan..com/s/1XrsVc密碼:66qt
2-15UnityRpg《江湖路》鏈接:http://pan..com/s/1PjJSM密碼:6gl2
2-16Unity3D《極品狂暴飛車》游戲源碼鏈接:http://pan..com/s/1qXrN2kk密碼:yj01
2-17CS源碼(支持區域網+廣域網)鏈接:http://pan..com/s/1hqY0XW4密碼:hecp
2-18《眾神之光》卡牌u3d全套完整源碼+資源鏈接:http://pan..com/s/1baEXqu密碼:s7en
2-19《暗黑天使》源碼http://pan..com/s/1jGN8Rbw
2-20《暗黑戰神》工程鏈接:http://pan..com/s/1kTSph5t密碼:736b
2-21大型ARPG手游《降臨》客戶端服務端全套源碼鏈接:http://pan..com/s/1nuvXnhn密碼:1t3e
2-22大型《天神傳》RPG(客戶端+伺服器)鏈接:http://pan..com/s/1nuhKf9Z密碼:58vo
2-23《口袋三國》客戶端+伺服器+設計文檔鏈接:http://pan..com/s/1kUtZFsN密碼:58vo
2-24商業項目-《暗影之槍ShadowGun》鏈接:http://pan..com/s/1ntXARyT密碼:l3u5
2-25商業項目-《降臨OL》-U3D全套源碼鏈接:http://pan..com/s/1c0Y9j3M密碼:gzmb
2-26《捕魚達人》源碼鏈接:http://pan..com/s/1pKzravL密碼:gas3
2-27《沉睡魔咒》鏈接:http://pan..com/s/1c1aAZQg密碼:c5j9
2-28《單機鬥地主》鏈接:http://pan..com/s/1hqL9Gmo密碼:qp6d
2-29《合金彈頭》Unity4.6.2鏈接:http://pan..com/s/1o7jemme密碼:6ngd
2-30《黑暗靈魂》鏈接:http:
I. 用C++編寫的小游戲源代碼
五子棋的代碼:
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
usingnamespacestd;
constintN=15;//15*15的棋盤
constcharChessBoardflag='';//棋盤標志
constcharflag1='o';//玩家1或電腦的棋子標志
constcharflag2='X';//玩家2的棋子標志
typedefstructCoordinate//坐標類
{
intx;//代錶行
inty;//代表列
}Coordinate;
classGoBang//五子棋類
{
public:
GoBang()//初始化
{
InitChessBoard();
}
voidPlay()//下棋
{
CoordinatePos1;//玩家1或電腦
CoordinatePos2;//玩家2
intn=0;
while(1)
{
intmode=ChoiceMode();
while(1)
{
if(mode==1)//電腦vs玩家
{
ComputerChess(Pos1,flag1);//電腦下棋
if(GetVictory(Pos1,0,flag1)==1)//0表示電腦,真表示獲勝
break;
PlayChess(Pos2,2,flag2);//玩家2下棋
if(GetVictory(Pos2,2,flag2))//2表示玩家2
break;
}
else//玩家1vs玩家2
{
PlayChess(Pos1,1,flag1);//玩家1下棋
if(GetVictory(Pos1,1,flag1))//1表示玩家1
break;
PlayChess(Pos2,2,flag2);//玩家2下棋
if(GetVictory(Pos2,2,flag2))//2表示玩家2
break;
}
}
cout<<"***再來一局***"<<endl;
cout<<"yorn:";
charc='y';
cin>>c;
if(c=='n')
break;
}
}
protected:
intChoiceMode()//選擇模式
{
inti=0;
system("cls");//系統調用,清屏
InitChessBoard();//重新初始化棋盤
cout<<"***0、退出1、電腦vs玩家2、玩家vs玩家***"<<endl;
while(1)
{
cout<<"請選擇:";
cin>>i;
if(i==0)//選擇0退出
exit(1);
if(i==1||i==2)
returni;
cout<<"輸入不合法"<<endl;
}
}
voidInitChessBoard()//初始化棋盤
{
for(inti=0;i<N+1;++i)
{
for(intj=0;j<N+1;++j)
{
_ChessBoard[i][j]=ChessBoardflag;
}
}
}
voidPrintChessBoard()//列印棋盤,這個函數可以自己調整
{
system("cls");//系統調用,清空屏幕
for(inti=0;i<N+1;++i)
{
for(intj=0;j<N+1;++j)
{
if(i==0)//列印列數字
{
if(j!=0)
printf("%d",j);
else
printf("");
}
elseif(j==0)//列印行數字
printf("%2d",i);
else
{
if(i<N+1)
{
printf("%c|",_ChessBoard[i][j]);
}
}
}
cout<<endl;
cout<<"";
for(intm=0;m<N;m++)
{
printf("--|");
}
cout<<endl;
}
}
voidPlayChess(Coordinate&pos,intplayer,intflag)//玩家下棋
{
PrintChessBoard();//列印棋盤
while(1)
{
printf("玩家%d輸入坐標:",player);
cin>>pos.x>>pos.y;
if(JudgeValue(pos)==1)//坐標合法
break;
cout<<"坐標不合法,重新輸入"<<endl;
}
_ChessBoard[pos.x][pos.y]=flag;
}
voidComputerChess(Coordinate&pos,charflag)//電腦下棋
{
PrintChessBoard();//列印棋盤
intx=0;
inty=0;
while(1)
{
x=(rand()%N)+1;//產生1~N的隨機數
srand((unsignedint)time(NULL));
y=(rand()%N)+1;//產生1~N的隨機數
srand((unsignedint)time(NULL));
if(_ChessBoard[x][y]==ChessBoardflag)//如果這個位置是空的,也就是沒有棋子
break;
}
pos.x=x;
pos.y=y;
_ChessBoard[pos.x][pos.y]=flag;
}
intJudgeValue(constCoordinate&pos)//判斷輸入坐標是不是合法
{
if(pos.x>0&&pos.x<=N&&pos.y>0&&pos.y<=N)
{
if(_ChessBoard[pos.x][pos.y]==ChessBoardflag)
{
return1;//合法
}
}
return0;//非法
}
intJudgeVictory(Coordinatepos,charflag)//判斷有沒有人勝負(底層判斷)
{
intbegin=0;
intend=0;
intbegin1=0;
intend1=0;
//判斷行是否滿足條件
(pos.y-4)>0?begin=(pos.y-4):begin=1;
(pos.y+4)>N?end=N:end=(pos.y+4);
for(inti=pos.x,j=begin;j+4<=end;j++)
{
if(_ChessBoard[i][j]==flag&&_ChessBoard[i][j+1]==flag&&
_ChessBoard[i][j+2]==flag&&_ChessBoard[i][j+3]==flag&&
_ChessBoard[i][j+4]==flag)
return1;
}
//判斷列是否滿足條件
(pos.x-4)>0?begin=(pos.x-4):begin=1;
(pos.x+4)>N?end=N:end=(pos.x+4);
for(intj=pos.y,i=begin;i+4<=end;i++)
{
if(_ChessBoard[i][j]==flag&&_ChessBoard[i+1][j]==flag&&
_ChessBoard[i+2][j]==flag&&_ChessBoard[i+3][j]==flag&&
_ChessBoard[i+4][j]==flag)
return1;
}
intlen=0;
//判斷主對角線是否滿足條件
pos.x>pos.y?len=pos.y-1:len=pos.x-1;
if(len>4)
len=4;
begin=pos.x-len;//橫坐標的起始位置
begin1=pos.y-len;//縱坐標的起始位置
pos.x>pos.y?len=(N-pos.x):len=(N-pos.y);
if(len>4)
len=4;
end=pos.x+len;//橫坐標的結束位置
end1=pos.y+len;//縱坐標的結束位置
for(inti=begin,j=begin1;(i+4<=end)&&(j+4<=end1);++i,++j)
{
if(_ChessBoard[i][j]==flag&&_ChessBoard[i+1][j+1]==flag&&
_ChessBoard[i+2][j+2]==flag&&_ChessBoard[i+3][j+3]==flag&&
_ChessBoard[i+4][j+4]==flag)
return1;
}
//判斷副對角線是否滿足條件
(pos.x-1)>(N-pos.y)?len=(N-pos.y):len=pos.x-1;
if(len>4)
len=4;
begin=pos.x-len;//橫坐標的起始位置
begin1=pos.y+len;//縱坐標的起始位置
(N-pos.x)>(pos.y-1)?len=(pos.y-1):len=(N-pos.x);
if(len>4)
len=4;
end=pos.x+len;//橫坐標的結束位置
end1=pos.y-len;//縱坐標的結束位置
for(inti=begin,j=begin1;(i+4<=end)&&(j-4>=end1);++i,--j)
{
if(_ChessBoard[i][j]==flag&&_ChessBoard[i+1][j-1]==flag&&
_ChessBoard[i+2][j-2]==flag&&_ChessBoard[i+3][j-3]==flag&&
_ChessBoard[i+4][j-4]==flag)
return1;
}
for(inti=1;i<N+1;++i)//棋盤有沒有下滿
{
for(intj=1;j<N+1;++j)
{
if(_ChessBoard[i][j]==ChessBoardflag)
return0;//0表示棋盤沒滿
}
}
return-1;//和棋
}
boolGetVictory(Coordinate&pos,intplayer,intflag)//對JudgeVictory的一層封裝,得到具體那個玩家獲勝
{
intn=JudgeVictory(pos,flag);//判斷有沒有人獲勝
if(n!=0)//有人獲勝,0表示沒有人獲勝
{
PrintChessBoard();
if(n==1)//有玩家贏棋
{
if(player==0)//0表示電腦獲勝,1表示玩家1,2表示玩家2
printf("***電腦獲勝*** ");
else
printf("***恭喜玩家%d獲勝*** ",player);
}
else
printf("***雙方和棋*** ");
returntrue;//已經有人獲勝
}
returnfalse;//沒有人獲勝
}
private:
char_ChessBoard[N+1][N+1];
};
(9)消除類游戲源碼擴展閱讀:
設計思路
1、進行問題分析與設計,計劃實現的功能為,開局選擇人機或雙人對戰,確定之後比賽開始。
2、比賽結束後初始化棋盤,詢問是否繼續比賽或退出,後續可加入復盤、悔棋等功能。
3、整個過程中,涉及到了棋子和棋盤兩種對象,同時要加上人機對弈時的AI對象,即涉及到三個對象。