问题标题: 送大家一些好代码

0
0
已解决
被禁言 席清源
席清源
修练者
修练者
#include<bits/stdc++.h>
using namespace std;
int main()
{
	while(1)
	{
	//有键按下
	if(kbhit())
	{
		//保存键码
		int ch=getch();
		//如果是功能键,则再读取一次
		if(ch==224)
		{
			ch=getch();
		}
		//判断
		if(ch=='a')
		{
			cout<<"a\n";
		}
		else if(ch=='s')
		{
			cout<<"s\n";
		}
		else if(ch=='d')
		{
			cout<<"d\n";
		}
		else if(ch=='w')
		{
			cout<<"w\n";
		}
		else if(ch=='A')
		{
			cout<<"Shift+a\n";
		}
		else if(ch=='S')
		{
			cout<<"Shift+s\n";
		}
		else if(ch=='D')
		{
			cout<<"Shift+d\n";
		}
		else if(ch=='W')
		{
			cout<<"Shift+w\n";
		}
	}
	}
	return 0;
}

事件绑定

席清源在2021-10-02 11:09:33追加了内容

ASCII:

Ctrl+a:1
Ctrl+b:2
Ctrl+c:3
Ctrl+d:4
Ctrl+e:5
Ctrl+f:6
Ctrl+g:7
Ctrl+h:8
Ctrl+i:9
Ctrl+j:10
Ctrl+k:11
Ctrl+l:12
Ctrl+m:13
Ctrl+n:14
Ctrl+o:15
Ctrl+p:16
Ctrl+q:17
Ctrl+r:18
Ctrl+s:19
Ctrl+t:20
Ctrl+u:21
Ctrl+v:22
Ctrl+w:23
Ctrl+x:24
Ctrl+y:25
Ctrl+z:26

A:65
B:66
C:67
D:68
E:69
F:70
G:71
H:72
I:73
J:74
K:75
L:76
M:77
N:78
O:79
P:80
Q:81
R:82
S:83
T:84
U:85
V:86
W:87
X:88
Y:89
Z:90

a:97
b:98
c:99
d:100
e:101
f:102
g:103
h:104
i:105
j:106
k:107
l:108
m:109
n:110
o:111
p:112
q:113
r:114
s:115
t:116
u:117
v:118
w:119
x:120
y:121
z:122

席清源在2021-10-02 11:50:46追加了内容

我的世界生存第一稿1.0.1.0版

Human.h

#ifndef HUMAN_H_
#define HUMAN_H_

#include<Window.h>
/*
ASCII:

Ctrl+a:1
Ctrl+b:2
Ctrl+c:3
Ctrl+d:4
Ctrl+e:5
Ctrl+f:6
Ctrl+g:7
Ctrl+h:8
Ctrl+i:9
Ctrl+j:10
Ctrl+k:11
Ctrl+l:12
Ctrl+m:13
Ctrl+n:14
Ctrl+o:15
Ctrl+p:16
Ctrl+q:17
Ctrl+r:18
Ctrl+s:19
Ctrl+t:20
Ctrl+u:21
Ctrl+v:22
Ctrl+w:23
Ctrl+x:24
Ctrl+y:25
Ctrl+z:26

A:65
B:66
C:67
D:68
E:69
F:70
G:71
H:72
I:73
J:74
K:75
L:76
M:77
N:78
O:79
P:80
Q:81
R:82
S:83
T:84
U:85
V:86
W:87
X:88
Y:89
Z:90

a:97
b:98
c:99
d:100
e:101
f:102
g:103
h:104
i:105
j:106
k:107
l:108
m:109
n:110
o:111
p:112
q:113
r:114
s:115
t:116
u:117
v:118
w:119
x:120
y:121
z:122
*/
class Human
{
	public:
		Point p;
		Human()
		{p.x=p.y=0;}
		void up()
		{
			p.y--;
		}
		void down()
		{
			p.y++;
		}
		void left()
		{
			p.x--;
		}
		void right()
		{
			p.x++;
		}
		void draw(Window*window)
		{
			window->window[p.x][p.y]=' ';
			if(kbhit())
			{
				//保存键码
				int ch=getch();
				//如果是功能键,则再读取一次
				if(ch==224)
				{
					ch=getch();
				}
				//判断
				if(ch=='a')
				{
					left();
				}
				else if(ch=='s')
				{
					down();
				}
				else if(ch=='d')
				{
					right();
				}
				else if(ch=='w')
				{
					up();
				}
				else if(ch==1)
				{
					window->window[p.y][p.x-1]=' ';
				}
				else if(ch==19)
				{
					window->window[p.y+1][p.x]=' ';
				}
				else if(ch==4)
				{
					window->window[p.y][p.x+1]=' ';
				}
				else if(ch==23)
				{
					window->window[p.y-1][p.x]=' ';
				}
			}
			window->drawtext(p,'#'/*,MAN*/);
		}
};

#endif

Window.h

#ifndef WINDOW_H_
#define WINDOW_H_

#include<bits/stdc++.h>
struct Point
{
	int x=0;
	int y=0;
};
//enum BlockType{GRASS=2,SOIL=8,LEAVES=10,TREE=0,MAN=1,DEFAULT=0,NULLN=15};
/*颜色代号*/
/*
0 = 黑色 8 = 灰色
1 = 蓝色 9 = 淡蓝色
2 = 绿色 10 = 淡绿色
3 = 浅绿色 11 = 淡浅绿色
4 = 红色 12 = 淡红色
5 = 紫色 13 = 淡紫色
6 = ** 14 = 淡**
7 = 白色 15 = 亮白色
*//*
void color_print(const char s, int front_color,int back_color)
{
 HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
 SetConsoleTextAttribute(handle, BACKGROUND_INTENSITY | back_color*16 |FOREGROUND_INTENSITY | front_color);
 std::cout<<s;
 SetConsoleTextAttribute(handle, FOREGROUND_INTENSITY | 7);
}
*/
class Window
{
	private:
		int*width_max=NULL;
		int*heigh_max=NULL;
	public:
//		char window[1024][1024];
//		BlockType window_type[1024][1024];
		char window[64][64];
//		BlockType window_type[64][64];
		Window(int width,int heigh,std::string title)
		{
			std::string t="title ";
			std::string ti=t+title;
			system(ti.c_str());
			memset(window,' ',sizeof window);
//			memset(window_type,NULLN,sizeof window_type);
			width_max=new int(width);
			heigh_max=new int(heigh);
		}
		~Window()
		{
			delete width_max;
			delete heigh_max;
			width_max=NULL;
			heigh_max=NULL;
		}
		int drawline(Point p1,Point p2)
		{
			if(p1.x!=p2.x&&p1.y!=p2.y)
			{
				return 255;
			}
			if(p1.x==p2.x&&p1.y==p2.y)
			{
				return 255;
			}
			if(p1.x!=p2.x)
			{
				for(int i=p1.x;i<=p2.x;i++)
				{
					window[p1.y][i]='X';
//					window_type[p1.y][i]=DEFAULT;
				}
				return 0;
			}
			if(p1.y!=p2.y)
			{
				for(int i=p1.y;i<=p2.y;i++)
				{
					window[i][p1.x]='X';
//					window_type[i][p1.x]=DEFAULT;
				}
				return 0;
			}
		}
		int fillrect(Point p1,Point p2)
		{
			for(int i=p1.y;i<=p2.y;i++)
			{
				for(int j=p1.x;j<=p2.x;j++)
				{
					window[i][j]='X';
//					window_type[i][j]=DEFAULT;
				}
			}
			return 0;
		}
		int drawpoint(Point p)
		{
			window[p.y][p.x]='X';
//			window_type[p.y][p.x]=DEFAULT;
			return 0;
		}
		int drawtext(Point p,char text/*,BlockType type*/)
		{
			window[p.y][p.x]=text;
//			window_type[p.y][p.x]=type;
		}
		void update()
		{
			for(int i=0;i<*width_max;i++)
			{
				for(int j=0;j<*heigh_max;j++)
				{
//					color_print(window[i][j],window_type[i][j],15);
					std::cout<<window[i][j]<<" ";
//					std::cout<<"  ";
				}
				std::cout<<std::endl;
			}
		}
		void cls()
		{
			memset(window,' ',sizeof(window));
//			memset(window_type,NULLN,sizeof(window_type));
		}
};


#endif

main.cpp

#include<bits/stdc++.h>
#include<Window.h>
#include<Human.h>
using namespace std;
int main()
{
//	system("mode con cols=200 lines=200");
	Window window(20,20,"游戏");
	Human man;
/*	Point p1,p2;
	p1.x=p1.y=0;
	p2.x=p2.y=9;
	window.fillrect(p1,p2);*/
	while(1)
	{
		man.draw(&window);
		window.update();
		system("cls");
	}
	return 0;
}

暂时不能建造物品,只能移动

席清源在2021-10-02 14:00:01追加了内容

注:本人的bits/stdc++.h加上了#include<windows.h>和#include<conio.h>

应加上#include<windows.h>和#include<conio.h>

席清源在2021-10-02 14:00:56追加了内容

@王文博 你也来看看

席清源在2021-10-03 13:35:15追加了内容

请给bits/stdc++.h加上以上的头文件,它位于

Dev-Cpp\MinGW64\lib\gcc\x86_64-w64-mingw32\4.8.1\include\c++\x86_64-w64-mingw32\32\bits\stdc++.h

谢谢,如果不加,以后大量的代码将非法

席清源在2021-10-03 14:06:57追加了内容

养动物游戏来啦!!

Animals.h

#ifndef ANIMALS_H_
#define ANIMALS_H_

#include<iostream>
using namespace std;
class Animals
{
	private:
		int m_**ger=0,m_tired=0;
		void Passtime(int time=1)
		{
			m_**ger+=time;
			m_tired+=time;
		}
		int Getfood()const
		{
			return m_**ger+m_tired;
		}
	public:
		void Eat(int food=4)
		{
			m_**ger-=food;
			if(m_**ger<0)
			{
				m_**ger=0;
			}
			Passtime();
		}
		void Play(int happy=4)
		{
			m_tired=-happy;
			if(m_tired<0)
			{
				m_tired=0;
			}
			Passtime();
		}
		void Talk()
		{
			int food=Getfood();
			cout<<"我很";
			if(food<=5)
			{
				cout<<"开心\n";
			}
			else if(food<=10)
			{
				cout<<"\b\b还好\n";
			}
			else if(food<=15)
			{
				cout<<"沮丧\n"; 
			}
			else
			{
				cout<<"愤怒\n";
			}
			Passtime();
		}
};

#endif

main.cpp

#include<bits/stdc++.h>
#include<Animals.h>
using namespace std; 
int main()
{
	Animals an;
	int choose=1;
	while(choose!=0)
	{
		cout<<"       MENU\n";
		cout<<"0.     EXIT\n";
		cout<<"1.  听动物讲话\n";
		cout<<"2.  喂动物吃饭\n";
		cout<<"3.  和动物玩耍\n";
		cout<<"选择:";
		cin>>choose;
		switch(choose)
		{
			case 0:
				cout<<"Bye";
				break;
			case 1:
				an.Talk();
				break;
			case 2:
				an.Eat();
				break;
			case 3:
				an.Play();
				break;
			default:
				cout<<"选择有误\n";
		}
		Sleep(1000);
		system("cls");
	}
	return 0;
}

 

席清源在2021-10-17 21:01:03追加了内容
string toString(int a)
{
	int numberLen=0,copy_a=a;
	while(copy_a!=0)
	{
		numberLen++;
		copy_a/=10;
	}
	string s;
	for(int i=1;i<=numberLen;i++)
	{
		s+=" ";
		s[i-1]=(a/(index(10,numberLen-i))%10)+'0';
	}
	return s; 
}

 


0
0
0
0
0
0
0
我要回答