当前位置:首页 » 操作系统 » 消除类游戏源码

消除类游戏源码

发布时间: 2023-02-17 18:56:27

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

密码:nxkf

2-53《神庙逃亡之魔境仙踪》

链接:http://pan..com/s/1sk8a39r

密码:hb3j

2-54《武士2复仇》

链接:http://pan..com/s/1mhzxee0

密码:gy8o

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对象,即涉及到三个对象。

热点内容
电脑服务器忙原因 发布:2025-07-30 13:35:29 浏览:839
linuxipmac 发布:2025-07-30 13:18:48 浏览:318
微信小程序开发源码 发布:2025-07-30 13:08:39 浏览:63
求累加和c语言 发布:2025-07-30 12:56:03 浏览:49
asp在线加密 发布:2025-07-30 12:51:06 浏览:937
java完全手册 发布:2025-07-30 12:22:05 浏览:614
支付宝接口java 发布:2025-07-30 12:21:58 浏览:77
做脚本需要把主机零件拆了吗 发布:2025-07-30 12:02:55 浏览:493
邮政解压视频完整版 发布:2025-07-30 12:00:54 浏览:491
301重定向php 发布:2025-07-30 11:52:34 浏览:22