自己做的2D minecraft1.0
2024-09-17 14:20:31
发布于:广东
游戏说明:
1.开局会要输入两个数,分别是地图的长和宽。
2.正式开始之后,按A,S,D,W可以操控上下左右移动。
3.J键可以放置方块,K键可以拆除方块(水方块不能拆,草方块不能直接在上面建,要先拆掉)
4.I,O,P,9 四个按键可以控制光标上下左右移动。
游戏代码:
#include<iostream>
#include<windows.h>
#include<conio.h>
#include<time.h>
#define ll long long
using namespace std;
char cmap[1005][1005];
char DNGI[10] = {'.','#'},DNM[10] = {'.','u'};
int dngi_max = 2,dnm_max = 2;
int camx,camy;
int WID,HIG;
class Player{
public:
int x,y;
bool check_position(int x,int y){
char tmp1 = cmap[x][y];
for(int i = 0;i < dngi_max;i ++){
if(tmp1 == DNGI[i]){
return 0;
}
}
return 1;
}
void move(char key,int &ax,int &ay){
if(key == 'a' and check_position(x,y - 1)){
y --;
ay --;
} else if(key == 'd' and check_position(x,y + 1)){
y ++;
ay ++;
} else if(key == 'w' and check_position(x - 1,y)){
x --;
ax --;
} else if(key == 's' and check_position(x + 1,y)){
x ++;
ax ++;
}
}
};
Player player;
class Cursor{
public:
int x,y;
void avoid(){
if(x == player.x and y == player.y){
x -= 1;
}
}
void place_block(char k,int x,int y){
if(cmap[x][y] == 'u' or cmap[x][y] == '.') cmap[x][y] = k;
}
bool check_mine(int x,int y){
char tmp2 = cmap[x][y];
for(int i = 0;i < dnm_max;i ++){
if(tmp2 == DNM[i]){
return 0;
}
}
return 1;
}
void mine_block(int x,int y){
if(check_mine(x,y)) cmap[x][y] = 'u';
}
void move(char key){
if(key == 'i'){
y --;
} else if(key == 'o'){
x ++;
} else if(key == 'p'){
y ++;
} else {
x --;
}
}
};
Cursor cursor;
void setCursorPosition(int x, int y) {
COORD coord;
coord.X = x;
coord.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}
void show_cursor(bool visible) {
CONSOLE_CURSOR_INFO cursor_info = {20, visible};
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}
int tmp[1005][1005];
int h = 9,z = 3;
void smooth(){
for(int i = 0;i < 1000;i ++) for(int j = 0;j < 1000;j ++) tmp[i][j] = (tmp[i - 1][j] + tmp[i + 1][j] + tmp[i][j - 1] + tmp[i][j + 1] + tmp[i][j]) / 5;
}
void generate_world(){
srand(time(0));
int cnt = 1;
for(int i = 0;i < 1000;i ++){
for(int j = 0;j < 1000;j ++){
if((rand() - cnt) % 2 == 0) cnt += rand() - cnt * h;
else cnt -= rand() - cnt * h;
tmp[i][j] = (rand() + cnt * h) % 100;
if(tmp[i][j] < 0) tmp[i][j] = 0;
cnt ++;
}
}
while(z --) smooth();
for(int i = 0;i < 1000;i ++){
for(int j = 0;j < 1000;j ++){
if(tmp[i][j] > 20) cmap[i][j] = '.';
else cmap[i][j] = 'w';
}
}
}
void print_screen(){
setCursorPosition(0, 0);
for(int i = camx;i <= camx + 20;i ++){
for(int j = camy;j <= camy + 20;j ++){
if(i < 0 or j < 0 or i >= WID or j >= HIG){
printf("[]");
continue;
} else if(i == player.x and j == player.y){
printf("P ");
continue;
} else if(i == cursor.x and j == cursor.y){
printf("%c]",cmap[i][j]);
continue;
}
printf("%c ",cmap[i][j]);
}
printf("\n");
}
}
void check_key(char key){
if(key == 'a' or key == 's' or key == 'w' or key == 'd'){
player.move(key,cursor.x,cursor.y);
} else if(key == 'j'){
cursor.place_block('#',cursor.x,cursor.y);
} else if(key == 'k'){
cursor.mine_block(cursor.x,cursor.y);
} else if(key == 'i' or key == 'o' or key =='p' or key == '9'){
cursor.move(key);
}
}
void game(){
player.x = WID / 2,player.y = HIG / 2;
show_cursor(0);
generate_world();
cursor.x = player.x - 1,cursor.y = player.y;
while(true){
camx = player.x - 10,camy = player.y - 10;
print_screen();
char key;
key = getch();
check_key(key);
}
}
int main(){
show_cursor(1);
scanf("%d %d",&WID,&HIG);
game();
return 0;
}
全部评论 1
这还不是成品,有bug的话记得提出,有需要改进的地方也记得提出。谢谢。
2024-09-17 来自 广东
0
有帮助,赞一个