编了个小程序,从屏幕上方下落随机生成的一个4*4方块,由于程序是顺序执行的,只有等第一个方框完成下落循环之后才能下落第二个方块,能否实现4个方块同时下落(这个用结构体数组应该可以实现),但是要完成在不同的横坐标,按照随机时间下落不同的方块 可能是同时下落也可能有时间差,请大家帮助看看,谢谢
#include<stdio.h>
#include<time.h>
#include<Windows.h>
#include<conio.h>
#include<stdlib.h>
#define COL 40
#define ROW 40
//4*4小方块结构体,用来存储方块信息,值为1显示方块, 值为0不显示方块
typedef struct Graph {
int block[4][4];
} graph;
//隐藏光标
void hidecursor() {
CONSOLE_CURSOR_INFO cursorinfo;
cursorinfo.dwSize=1;
cursorinfo.bVisible=FALSE;
HANDLE handle=GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorInfo(handle,&cursorinfo);
}
//跳转光标
void cursorjump(int x,int y) {
COORD pos;
pos.X=x;
pos.Y=y;
HANDLE handle=GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(handle,pos);
}
//初始化4*4方块信息
void init(graph * p) {
int i=0,j=0;
for(i=0; i<4; i++)
for(j=0; j<4; j++)
p->block[i][j]=rand()%2;
}
//打印方块信息,值为1打印方块,值为零打印空格,flag变量用于控制覆盖方块最上一行和打印方块用
void print(int x,int y,int flag,graph * p) {
hidecursor();
int i=0,j=0;
if(flag) {
for(i=0; i<4; i++)
for(j=0; j<4; j++) {
if(p->block[i][j]==1) {
cursorjump(2*(x+j),y+i);
printf("■");
} else {
cursorjump(2*(x+j),y+i);
printf(" ");
}
}
}
else {
for(j=0; j<4; j++) {
cursorjump(2*(x+j),y);
printf(" ");
}
}
}
//方块向下移动,先覆盖现有方块最后一行,然后打印移动后的方块
void move(int x,int y,graph * p) {
int t=3000;
while(y<=ROW-15) {
if(t==-1)
t=90000000;
print(x,y,0,p);
y++;
print(x,y,1,p);
while(t--);
}
}
int main() {
//随机初始化4个方块,然后顺序向下移动,请指点如何随机在不同的列位置,按照随机时间下落多个随机方块
//目前程序是顺序执行的,只有第一个方块循环结束后才能第二个方块下落
srand((unsigned int)time(NULL));
graph a,b,c,d;
while(1){
init(&a);
init(&b);
init(&c);
init(&d);
move(0,0,&a);
move(4,0,&b);
move(8,0,&c);
move(12,0,&d);
}
return 0;
}
是这样的效果么?
#include<stdio.h>
#include<time.h>
#include<Windows.h>
#include<conio.h>
#include<stdlib.h>
#define COL 40
#define ROW 40
//4*4小方块结构体,用来存储方块信息,值为1显示方块, 值为0不显示方块
typedef struct Graph {
int block[4][4];
} graph;
//隐藏光标
void hidecursor() {
CONSOLE_CURSOR_INFO cursorinfo;
cursorinfo.dwSize = 1;
cursorinfo.bVisible = FALSE;
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorInfo(handle, &cursorinfo);
}
//跳转光标
void cursorjump(int x, int y) {
COORD pos;
pos.X = x;
pos.Y = y;
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(handle, pos);
}
//初始化4*4方块信息
void init(graph* p) {
int i = 0, j = 0;
for (i = 0; i < 4; i++)
for (j = 0; j < 4; j++)
p->block[i][j] = rand() % 2;
}
//打印方块信息,值为1打印方块,值为零打印空格,flag变量用于控制覆盖方块最上一行和打印方块用
void print(int x, int y, int flag, graph* p) {
hidecursor();
int i = 0, j = 0;
if (flag) {
for (i = 0; i < 4; i++)
for (j = 0; j < 4; j++) {
if (p->block[i][j] == 1) {
cursorjump(2 * (x + j), y + i);
printf("■");
}
else {
cursorjump(2 * (x + j), y + i);
printf(" ");
}
}
}
else {
for (j = 0; j < 4; j++) {
cursorjump(2 * (x + j), y);
printf(" ");
}
}
}
//方块向下移动,先覆盖现有方块最后一行,然后打印移动后的方块
void move(int x, int y, graph* p1, graph* p2, graph* p3, graph* p4) {
int t = 3000;
int offset = 4;
while (y <= ROW - 15) {
if (t == -1)
t = 90000000;
print(x, y, 0, p1);
print(x+ offset, y, 0, p2);
print(x + offset * 2, y, 0, p3);
print(x + offset * 3, y, 0, p4);
y++;
print(x, y, 1, p1);
print(x+ offset, y, 1, p2);
print(x + offset * 2, y, 1, p3);
print(x + offset * 3, y, 1, p4);
while (t--);
}
}
int main() {
//随机初始化4个方块,然后顺序向下移动,请指点如何随机在不同的列位置,按照随机时间下落多个随机方块
//目前程序是顺序执行的,只有第一个方块循环结束后才能第二个方块下落
srand((unsigned int)time(NULL));
graph a, b, c, d;
while (1) {
init(&a);
init(&b);
init(&c);
init(&d);
move(0, 0, &a, &b, &c, &d);
/* move(4, 0, &b);
move(8, 0, &c);
move(12, 0, &d);*/
}
return 0;
}
可以采用随机横坐标加随机降落速度还有随机偏移角度实现,可以站内搜索雪花飘落c语言代码实现
想要在不同的横坐标,在不同的时间段下落,可以用随机算法控制降落速度