dev C++俄罗斯方块

作者&投稿:茌庾 (若有异议请与网页底部的电邮联系)
c++俄罗斯方块代码~

#include
#include
#include
#include
#include

#pragma comment(lib, "winmm.lib")
using namespace std;

#define GameW 10
#define GameH 20
const int CtrlLeft = GameW*2+4 + 3;

struct Point {
Point(){}
Point(int x, int y) {_x = x, _y = y;}
int _x, _y;
};

HANDLE g_hOutput = GetStdHandle(STD_OUTPUT_HANDLE);
HANDLE g_hInput = GetStdHandle(STD_INPUT_HANDLE);

Point g_ptCursor(0,0);
BOOL isChecking = FALSE;
BOOL g_bGameOver = FALSE;
int g_nGameBack[GameH][GameW], Case;
int nowKeyInfo = -1;
int g_nDiff = 1;
int g_nLife = 2;
int g_nScore = 0;

void SetCursor(COORD cd) {
SetConsoleCursorPosition(g_hOutput, cd);
}
void SetCursor(int x, int y){
COORD cd = {x, y};
SetCursor(cd);
}
void SetBlockCursor(int x, int y){
COORD cd = {2*x + 2, y + 1};
SetCursor(cd);
}

void SetBack(int x, int y, BOOL bk) {
SetBlockCursor(x, y);
if (bk)
printf("%s", "■");
else
printf(" ");
}

bool Out(int x, int y) {
return x = GameW || y >= GameH;
}

struct xBlock {
public:
int len;
int nowRotateID;
BOOL mask[4][4][4];
static vector List;

xBlock() { len = 0; }
xBlock(int l, char *str) {
int i, j, k;
len = l;
memset(mask, FALSE, sizeof(mask));
for (i = 0; i < l; i++) {
for (j = 0; j < l; j++) {
mask[0][i][j] = str[i*l + j] - '0';
}
}
for (k = 1; k < 4; k++) {
for (i = 0; i < len; i++) {
for (j = 0; j < len; j++) {
mask[k][i][j] = mask[k-1][j][len-1-i];
}
}
}
nowRotateID = rand() % 4;
}

void rotate() {
nowRotateID ++;
if (nowRotateID >= 4)
nowRotateID = 0;
}

BOOL getUnit(int x, int y, int roID) {
if (roID == -1) {
roID = nowRotateID;
}
return mask[roID][y][x];
}
};

vector xBlock::List;

class Block {
public:
int x, y;
int ID;
xBlock bk;

void reset(xBlock *pbk) {
bk = *pbk;

x = 4, y = 0;
ID = ++ Case;

if (collide(0,0)) {
lifeDown();
}
draw();

*pbk = xBlock::List[rand() % xBlock::List.size()];
}

void lifeDown() {
int i, j;
for (i = 0; i < GameH; i++) {
for (j = 0; j < GameW; j++) {
SetBack(j, i, TRUE);
Sleep(10);
}
}
if (g_nLife) {
g_nLife --;
for (i = g_nLife; i < 6; i++) {
SetCursor(CtrlLeft + i, 15);
printf("%c", ' ');
}
for (i = GameH-1; i >= 0; i--) {
for (j = GameW-1; j >= 0; j--) {
SetBack(j, i, FALSE);
Sleep(10);
g_nGameBack[i][j] = 0;
}
}
}else {
g_bGameOver = TRUE;
}
}

void erase() {
int i, j;
for (i = 0; i < bk.len; i++) {
for (j = 0; j < bk.len; j++) {
if (bk.getUnit(j, i, -1)) {
if (! Out(j+x, i+y) && g_nGameBack[i+y][j+x]) {
SetBack(j+x, i+y, FALSE);
g_nGameBack[i+y][j+x] = 0;
}
}
}
}
}
void draw() {
int i, j;
for (i = 0; i < bk.len; i++) {
for (j = 0; j < bk.len; j++) {
if (bk.getUnit(j, i, -1)) {
if (! Out(j+x, i+y) && ! g_nGameBack[i+y][j+x]) {
SetBack(j+x, i+y, TRUE);
g_nGameBack[i+y][j+x] = ID;
}
}
}
}
}
void draw(int x, int y) {
int i, j;
for (i = 0; i < 4; i++) {
for (j = 0; j < 4; j++) {
SetCursor(x + 2*j, y + i);
if (bk.getUnit(j, i, -1)) {
printf("%s", "■");
}else
printf(" ");
}
}
}
bool collide(int dx, int dy, int roID = -1) {
int i, j;
for (i = 0; i < bk.len; i++) {
for (j = 0; j < bk.len; j++) {
if (bk.getUnit(j, i, roID)) {
Point ptPos(j + x + dx, i + y + dy);
if (Out(ptPos._x, ptPos._y)
|| g_nGameBack[ptPos._y][ptPos._x] && ID != g_nGameBack[ptPos._y][ptPos._x]) {
return TRUE;
}
}
}
}
return FALSE;
}

void rotate(int nTimes = 1) {
int nextro = (bk.nowRotateID + nTimes) % 4;
if (collide(0, 0, nextro)) {
return ;
}
Beep(12000, 50);
erase();
bk.nowRotateID = nextro;
draw();
}

BOOL changepos(int dx, int dy) {
if (collide(dx, dy)) {
return FALSE;
}
erase();
x += dx;
y += dy;
draw();
return TRUE;
}
};

void GameInit() {
CONSOLE_CURSOR_INFO cursor_info;
cursor_info.bVisible = FALSE;
cursor_info.dwSize = 100;
SetConsoleCursorInfo(g_hOutput, &cursor_info);
xBlock::List.push_back(xBlock(3, "010111000"));
xBlock::List.push_back(xBlock(3, "110110000"));
xBlock::List.push_back(xBlock(3, "111001000"));
xBlock::List.push_back(xBlock(3, "111100000"));
xBlock::List.push_back(xBlock(3, "110011000"));
xBlock::List.push_back(xBlock(3, "011110000"));
xBlock::List.push_back(xBlock(4, "1000100010001000"));
}

void DrawFrame(int x, int y, int nWidth, int nHeight) {
int i;
for (i = 0; i < nWidth; i++) {
SetCursor(x + 2*i + 2, y);
printf("%s", "一");
SetCursor(x + 2*i + 2, y + nHeight+1);
printf("%s", "┄");
}
for (i = 0; i < nHeight; i++) {
SetCursor(x, y + i + 1);
printf("%s", "┆");
SetCursor(x + nWidth*2+2, y + i + 1);
printf("%s", "┆");
}
SetCursor(x, y);
printf("%s", "┌");
SetCursor(x, y + nHeight+1);
printf("%s", "└");
SetCursor(x + nWidth*2+2, y);
printf("%s", "┐");
SetCursor(x + nWidth*2+2, y + nHeight+1);
printf("%s", "┘");
}

void MissionInit() {
memset(g_nGameBack, FALSE, sizeof(g_nGameBack));
Case = 1;
int i;
DrawFrame(0, 0, GameW, GameH);
DrawFrame(GameW*2+4, 0, 4, GameH);
SetCursor(CtrlLeft, 2);
printf("Next");

SetCursor(CtrlLeft, 8);
printf("Speed");
for (i = 0; i < g_nDiff; i++) {
SetCursor(CtrlLeft + i, 9);
printf("%c", 1);
}

SetCursor(CtrlLeft, 11);
printf("Score");
SetCursor(CtrlLeft, 12);
printf("%d", g_nScore);

SetCursor(CtrlLeft, 14);
printf("Life");
for (i = 0; i < g_nLife; i++) {
SetCursor(CtrlLeft + i, 15);
printf("%c", 3);
}
}

void Check() {
isChecking = TRUE;
int i, j, k;
vector line;
for (i = 0; i < GameH; i++) {
for (j = 0; j < GameW; j++) {
if (! g_nGameBack[i][j])
break;
}
if (j == GameW) {
line.push_back(i);
}
}
if (line.size()) {
int nCount = 7;
while (nCount --) {
for (i = 0; i < line.size(); i++) {
for (j = 0; j < GameW; j++) {
SetBack(j, line[i], nCount&1);
}
}
Sleep(70);
}
for (i = 0; i < line.size(); i++) {
for (j = 0; j < GameW; j++) {
g_nGameBack[line[i]][j] = 0;
}
}

for (i = 0; i < GameW; i++) {
int next = GameH-1;
for (j = GameH-1; j >= 0; j--) {
for (k = next; k >= 0; k--) {
if (g_nGameBack[k][i])
break;
}
next = k - 1;
BOOL is = (k >= 0);
SetBack(i, j, is);
g_nGameBack[j][i] = is;
}
}

g_nScore += 2*line.size()-1;
SetCursor(CtrlLeft, 12);
printf("%d", g_nScore);

if ( g_nScore >= g_nDiff * g_nDiff * 10) {
if (g_nDiff <= 6)
g_nDiff ++;
}
if ( g_nScore >= 50 * (g_nLife+1)) {
if (g_nLife <= 6)
g_nLife ++;
}
}

isChecking = FALSE;
}
int main() {
Block* obj = new Block();
Block* buf = new Block();


BOOL bCreateNew = FALSE;
int nTimer = GetTickCount();
int LastKeyDownTime = GetTickCount();


GameInit();
MissionInit();

buf -> bk = xBlock::List[rand() % xBlock::List.size()];
while (1) {
if (! bCreateNew) {
bCreateNew = TRUE;
obj -> reset(&buf -> bk);
if (g_bGameOver)
break;
buf -> draw(CtrlLeft - 1, 4);
}
if (GetTickCount() - nTimer >= 1000 / g_nDiff) {
nTimer = GetTickCount();
if (! obj -> collide(0, 1))
obj -> changepos(0, 1);
else {
Check();
bCreateNew = FALSE;
}
}
if (GetTickCount() - LastKeyDownTime >= 100) {
if (FALSE == isChecking) {
LastKeyDownTime = GetTickCount();
if (GetAsyncKeyState(VK_UP)) {
obj -> rotate();
}
if (GetAsyncKeyState(VK_LEFT)) {
obj -> changepos(-1, 0);
}
if (GetAsyncKeyState(VK_RIGHT)) {
obj -> changepos(1, 0);
}
if (GetAsyncKeyState(VK_DOWN)) {
if ( FALSE == obj -> changepos(0, 2) )
obj -> changepos(0, 1);
}
}
}
}
SetCursor(8, 10);
printf("Game Over!");

SetCursor(0, GameH + 3);

while (1) {
if (GetAsyncKeyState(VK_ESCAPE))
break;
}
return 0;
}

1111111111111111111111111

1,看看,编译要求的路径,头文件及资源和绝对路径和相对路径对不?文件尽量放在英文的目录下。2,看编译过程中的提示,是不是会有一些第三方的资源代码需要编译进来,按提示的信息查找,然后编译。3,先查错误,再查警告;一个个的来,一定可以编译通过的。。 :)

如果有问题,可以去尝试着调试,因为编译的环境不一样,不可能一下子就能编译通过。

// RussionRect.cpp : 定义应用程序的入口点。
//

#include "stdafx.h"
#include "RussionRect.h"
#include <time.h>
#define MAX_LOADSTRING 100
#define idbutton 1 //按钮id
#define BTN_WIDTH (10 * cxChar)
#define BTN_HEIGHT (6* cyChar/4) //
#define ID_TIMER 1 //计时器id
int point;
TCHAR szBuff[20];

// 全局变量:
HINSTANCE hInst; // 当前实例
TCHAR szTitle[MAX_LOADSTRING]; // 标题栏文本
TCHAR szWindowClass[MAX_LOADSTRING]; // 主窗口类名
int cxBlock=20; //行距离
int cyBlock=20; //列距离
int const line=15; //总的行数
int const clom=20; //总的列数

bool ctrl; //按钮控制 暂时无用

class mapstruct
{
public :
BOOL visible[line][clom];
BOOL enable[line][clom+1];
int color[line][clom];
mapstruct()
{
for(int i=0;i<line;i++)
enable[i][clom]=1;
}
};

mapstruct map;

class shape //形状的父类 以便产生动态连编,所有的函数均为虚函数
{
public:
virtual void left(){}; // 向左移动
virtual void right(){};
virtual void down(){};
virtual void turn(){}; //变幻形状
virtual bool judge(){ return 0;}; //计算是否到底
virtual RECT* jRect(RECT*){return 0;};//计算无效矩形
virtual ~shape(){};
};
///////////////////////////////////////////////////////////////
// 以下定义三种形状 则有三个类
//每个类包括 左右下 移动函数 还有变换形状函数,判断自己占用格子最大的矩形函数(为了产生可能小的无效矩形一面刷新整个屏幕)
class E : public shape
{
private:
int state; //形状状态号
bool complete;
POINT top;
POINT lbottom;
POINT mbottom;
POINT rbottom;

public:
E() //形状初始化位置 ,在客户区的中间上面
{
top.x=7;top.y=0;
lbottom.x=6;lbottom.y=1;
rbottom.x=8;rbottom.y=1;
mbottom.x=7;mbottom.y=1;state=1;
map.visible[top.x][top.y]=map.visible[lbottom.x][lbottom.y]=map.visible[mbottom.x][mbottom.y]=map.visible[rbottom.x][rbottom.y]=1;
map.color[top.x][top.y]=map.color[lbottom.x][lbottom.y]=map.color[mbottom.x][mbottom.y]=map.color[rbottom.x][rbottom.y]=1;
}
void turn();
void down();
void left();
void right();
bool judge();
RECT* jRect(RECT*); //判断无效矩形函数
~E();

};
RECT* E::jRect(RECT* rec)
{
int x,y,ix,iy;
x=min(min(top.x,lbottom.x),min(mbottom.x,rbottom.x));
y=min(min(top.y,lbottom.y),min(mbottom.y,rbottom.y));
rec->left=x*cxBlock;
rec->top=y*cyBlock;
x=max(max(top.x,lbottom.x),max(mbottom.x,rbottom.x));
y=max(max(top.y,lbottom.y),max(mbottom.y,rbottom.y));
rec->right=(x+4)*cxBlock;
rec->bottom=(y+4)*cyBlock;

return rec;
}
void E::turn() //转换形状函数
{
complete=1; //是否转换完毕 一面执行其他行语句
map.visible[top.x][top.y]=map.visible[lbottom.x][lbottom.y]=map.visible[rbottom.x][rbottom.y]=0;// 将上一次可视标记去除
map.color[top.x][top.y]=map.color[lbottom.x][lbottom.y]=map.color[rbottom.x][rbottom.y]=0;

if(state==1 && complete && top.x-1>=0 && top.y+1<clom && lbottom.x+1<line && lbottom.y+1<clom && rbottom.x-1>=0 && rbottom.y-1>=0)
if(!map.enable[top.x-1][top.y+1] && !map.enable[lbottom.x+1][lbottom.y+1] && !map.enable[rbottom.x-1][rbottom.y-1])
{top.x--;top.y++;lbottom.x++;lbottom.y++;rbottom.x--;rbottom.y--;state=2;complete=0;}

if(state==2 && complete && top.x+1<line && top.y+1<clom && lbottom.x+1<line && lbottom.y-1>=0 && rbottom.x-1>=0 && rbottom.y+1<clom)
if(!map.enable[top.x+1][top.y+1] && !map.enable[lbottom.x+1][lbottom.y-1] && !map.enable[rbottom.x-1][rbottom.y+1])
{top.x++;top.y++;lbottom.x++;lbottom.y--;rbottom.x--;rbottom.y++;state=3;complete=0;}

if(state==3 && complete && top.x+1<line && top.y-1>=0 && lbottom.x-1>=0 && lbottom.y-1>=0 && rbottom.x+1< line && rbottom.y+1<clom)
if(!map.enable[top.x+1][top.y-1] && !map.enable[lbottom.x-1][lbottom.y-1] && !map.enable[rbottom.x+1][rbottom.y+1])
{top.x++;top.y--;lbottom.x--;lbottom.y--;rbottom.x++;rbottom.y++;state=4;complete=0;}

if(state==4 && complete && top.x-1>=0 && top.y-1>=0 && lbottom.x-1>=0 && lbottom.y+1<clom && rbottom.x+1<line && rbottom.y-1>=0)
if(!map.enable[top.x-1][top.y-1] && !map.enable[lbottom.x-1][lbottom.y+1] && !map.enable[rbottom.x+1][rbottom.y-1])
{top.x--;top.y--;lbottom.x--;lbottom.y++;rbottom.x++;rbottom.y--;state=1;complete=0;}
map.color[top.x][top.y]=map.color[lbottom.x][lbottom.y]=map.color[rbottom.x][rbottom.y]=1;
map.visible[top.x][top.y]=map.visible[lbottom.x][lbottom.y]=map.visible[rbottom.x][rbottom.y]=1; //设置新的可视标志
}

void E::down()
{
map.color[top.x][top.y]=map.color[lbottom.x][lbottom.y]=map.color[mbottom.x][mbottom.y]=map.color[rbottom.x][rbottom.y]=0;
map.visible[top.x][top.y]=map.visible[lbottom.x][lbottom.y]=map.visible[mbottom.x][mbottom.y]=map.visible[rbottom.x][rbottom.y]=0;
top.y++;lbottom.y++;mbottom.y++;rbottom.y++;
map.visible[top.x][top.y]=map.visible[lbottom.x][lbottom.y]=map.visible[mbottom.x][mbottom.y]=map.visible[rbottom.x][rbottom.y]=1;
map.color[top.x][top.y]=map.color[lbottom.x][lbottom.y]=map.color[mbottom.x][mbottom.y]=map.color[rbottom.x][rbottom.y]=1;

}


长宁县15718215700: dev C++俄罗斯方块
徐唐吡嘧: 如果有问题,可以去尝试着调试,因为编译的环境不一样,不可能一下子就能编译通过.

长宁县15718215700: 跪求一个能在VC++6.0里能运行的俄罗斯方块代码 -
徐唐吡嘧: #include<iostream> #include<stdlib.h> #include<windows.h> #include<time.h> #include<conio.h> using namespace std;#define A1 0//A代表长条型,B为方块,C为L型,D为闪电型(实在无法描述那个形状) #define A2 1#define B ...

长宁县15718215700: 用C++做俄罗斯方块需要哪些软硬件 -
徐唐吡嘧: 你是说游戏还是玩具? 写程序的话,电脑(带键盘,显示器),C++开发工具(VC或其它软件),你完成程序就可以在电脑上玩了.

长宁县15718215700: C#中如何用按钮和数组做出俄罗斯方块 -
徐唐吡嘧: 我曾经用C++实现了简单Dos版的俄罗斯方块,大致方法就是通过接收上下左右的按键来作为动作,然后在移动过程中不断对这一个小块进行擦除以及重画,只不过是位置的区别,以二维数组作为游戏界面,设定边界,并可以顺便在边界填充些星号什么的,最后就是对上下左右控制时的一些细节问题,比如方块不能出边界,方块遇到方块需要停止,方块满一行需要清空的操作.希望能够帮到你.

长宁县15718215700: 用C语言在VC++里编写俄罗斯方块,需要掌握哪些知识? -
徐唐吡嘧: DIRECTdraw ,如果你不想用这个,用PAINTDC也能搞,但花样就不多了,满足功能不成问题,如果你真的会C的话,其实非常简单,就是几个图形的旋转算法,而且用PAINTDC 的话就是矩阵变化而已.然后就是搁住后判断消行,这个更简单了.

长宁县15718215700: 我想用C++ 做一个俄罗斯方块 但是不想用MFC 我得创建什么工程呢 具体点的啊 -
徐唐吡嘧: 不用MFC的话,可以考虑直接使用win api,这样的话建立的工程类型选择win32应用程序就行,绘图部分gdi,gdi+,directx,或者别的什么开源库都可以.

长宁县15718215700: C语言写的俄罗斯方块 写好了 老师提的问题如下 求解答 -
徐唐吡嘧: (1) 版本管理一般在实际工作中才会用到,比如每天写的代码,每天都进行代码提交,生成一个版本.比较简单的版本管理是 svn;(2) 单元测试一般是针对某一个函数进行的,比如你写了一个函数 int max(int a, int b); 单元测试就是编写一个测试程序,传入各种参数,来测试 max 这个函数返回值与我们设计的预期返回值是否相同.单元测试也有框架,我们公司用的是 cppunit, 你也可以上网去学习一下.

长宁县15718215700: 我用vc++6.0搞一个俄罗斯方块的游戏,我的步骤:文件>新建>工程>win32consoleApplication>文件>新建>文件> -
徐唐吡嘧: 你这个头文件它不属于VC,有些书上介绍这些游戏时,它们用的编译器是Turbo C,所以会有graphic.h这个头文件,如果你想按书上的那样做一个游戏,请下载Turbo C

长宁县15718215700: 想用VC++6.0动手写俄罗斯方块 但不知到如何下手 请各位给点思路 -
徐唐吡嘧: 简单说一下算法: 俄罗斯方块由四个小方块组成,假设你定义没每个小方块大小为20 * 20px,方块活动区域大小为:300宽 * 400高,那么你需要定义一个bool型二维数组来标记该活动区域方块的填充情况(以填记1或未填记0),那么该数组可...

长宁县15718215700: 用c语言写俄罗斯方块要学哪些东西 -
徐唐吡嘧: 首先要学习C语言,这个是基础. 然后如果必须用C的话,还要学习C语言的图形界面编程. 网上资料做多,而且容易得到的就是Turbo C的,因为Turbo C可以写图形的程序. 如果你不想限制在Turbo C,想用其他编译器也可以编译的程序的话,你可以学习SDL.SDL的教程里,也可以找到俄罗斯方块.当然,你也可以学习OpenGL的俄罗斯方块.你百度 SDL 俄罗斯方块或者 OpenGL 俄罗斯方块都很容易找到例程. 总之, 第一,你必须先学好C语言; 然后,你要学习一种C语言的图形库; 最后,你要学习俄罗斯方块的程序设计方法. 这样,你就可以编写一个自己的,有声有色的俄罗斯方块了.

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 星空见康网