一、c++俄罗斯方块代码
#include <iostream>
#include <windows.h>
#include <vector>
#include <mmsystem.h>
#include <cstdio>
#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 < 0 || y < 0 || x >= gamew || y >= gameh;
}
struct xblock {
public:
int len;
int nowrotateid;
bool mask[4][4][4];
static vector <xblock> 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> 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 <int> 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;
}
二、寻求c语言版的俄罗斯方块程序
楼上的人说的很好,但bios.h 是tc 自带的,其它编译器通不过,现在用的较多的编译器是vc6.0 或vs 2008或c++builder,这些编译器通不过bios.h编译,若要完整的代码,把邮箱号留下,我发给你一个压缩包,最好学会用工具才能顺利通过编译,如果只学了c程序设计而没有真正会操作工具,俄罗斯方块程序
是调试不出来的。