[c++简单小游戏]东搞西搞第三弹——伪的不能再伪的字符游戏库。。。

最近,看着满百度的游戏库而用不了,有点不爽。。。

于是。。。这个神奇的游戏库就诞生了。。。

但我真没想到就这么点代码。。。


贴代码:

#include<iostream>
#include<windows.h>
#include<conio.h>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<vector>
#include<string>

#define up 72 
#define left 75 
#define right 77 
#define down 80

#define UP 0
#define DOWN 1
#define LEFT 2
#define RIGHT 3

using namespace std;

struct picture{
	char pic[150][150];
};

FILE*in;

class sprite{
	public:
		sprite(){//创建时内置
			hitbox_x=0;
			hitbox_y=0;
                        visible=0; 
               }
		
		void print_sprite(int leftx,int lefty,int animate_num){//打印精灵,但不会把原来的精灵图片擦除。。。擦除的后面有
			visible=1;
			posx=leftx;
			posy=lefty;
			HANDLE out;
			COORD pos={leftx,lefty};
			now_pic=animate_num;
			out=GetStdHandle(STD_OUTPUT_HANDLE);
			SetConsoleCursorPosition(out,pos);
			for(int i=0;i<=height;i++){
				printf("%s",animation[animate_num].pic[i]);
				pos.Y++;
				SetConsoleCursorPosition(out,pos);
			}
		}
		
		bool isinhitbox(int x,int y){//探测某坐标是否在碰撞箱内
			if(posx+hitbox_x<=x && x<=hitbox_x+posx+width && posy+hitbox_y<=y && y<=posy+hitbox_y+hitbox_height)
				return true;
			else
			    return false;
		}
		
		bool is_collision(){//探测是否被碰撞
			char now[width];
			HANDLE out=GetStdHandle(STD_OUTPUT_HANDLE);
			COORD c={posx,posy+1};
			DWORD read;
			for(int i=1;i<=height;i++){
				SetConsoleCursorPosition(out,c);
				ReadConsoleOutputCharacter(out,now,width,c,&read);
				for(int j=0;j<width;j++){
					if(now[j]!=animation[now_pic].pic[i][j])
						return true;
				}
				c.Y++;
			}
			return false;
		}
		
		bool collision_with(char a){//探测是否和某字符碰撞,这个是最快的。。。
			char now[width];
			HANDLE out=GetStdHandle(STD_OUTPUT_HANDLE);
			COORD c={posx,posy+1};
			DWORD read;
			for(int i=1;i<=height;i++){
				SetConsoleCursorPosition(out,c);
				ReadConsoleOutputCharacter(out,now,width,c,&read);
				for(int j=0;j<width;j++){
					if(now[j]==a && a!=animation[now_pic].pic[i][j])
						return true;
				}
				c.Y++;
			}
			return false;
		}
		
		bool detect_collision(sprite a){//是否与某精灵碰撞,最慢。。。
			int x=hitbox_x+posx;
			int y=hitbox_y+posy;
			for(int i=x;i<=x+hitbox_width;i++){
 				for(int j=y;j<=y+hitbox_height;j++){
					if(a.isinhitbox(i,j))
					    return true;
				}
			}
			return false;
		}

		void hide_sprite(){//隐藏精灵
			HANDLE out;
			visible=0;
			COORD lpos={posx,posy};
			out=GetStdHandle(STD_OUTPUT_HANDLE);
			SetConsoleCursorPosition(out,lpos);
			for(int i=0;i<=height;i++){
				for(int j=0;j<=width;j++)
					putchar(0);
				lpos.Y++;
				SetConsoleCursorPosition(out,lpos);
			}
		}
		
		int getnow_pic(){//当前图片编号
			return now_pic;
		}
		
		void move_sprite(int delta_x,int delta_y,int animate_num){//按距离移动精灵
			HANDLE out;
			if(animate_num==-1){
				animate_num=getnow_pic();
			}
			if(visible==1){
				COORD lpos={posx,posy};
				out=GetStdHandle(STD_OUTPUT_HANDLE);
				SetConsoleCursorPosition(out,lpos);
				for(int i=0;i<=height;i++){
					for(int j=0;j<=width;j++)
						putchar(0);
					lpos.Y++;
					SetConsoleCursorPosition(out,lpos);
				}
			}
			if(posx+delta_x>=0 && posy+delta_y>=0){
				posx+=delta_x;
				posy+=delta_y;
			}
			if(visible==1){
				COORD pos={posx,posy};
				now_pic=animate_num;
				SetConsoleCursorPosition(out,pos);
				for(int i=0;i<=height;i++){
					printf("%s",animation[animate_num].pic[i]);
					pos.Y++;
					SetConsoleCursorPosition(out,pos);
				}
			}
		}
		
		void addimage(char* filename){//为精灵加入图片
			picture pics;
			in=fopen(filename,"r");
			fscanf(in,"%d%d",&height,&width);
			for(int i=0;i<=height;i++){
				fgets(pics.pic[i],1000,in);
			}
			animation.push_back(pics);
		}
		
		void spirite_set(int hei,int wid){//设置精灵长宽
			height=hei;
			width=wid;
		}
		
		int getposx(){//取得X坐标
			return posx;
		}
		
		int getposy(){//取得Y坐标
			return posy;
		}
		
		int setposx(int x){//设置X坐标
			posx=x;
		}
		
		int setposy(int y){//设置Y坐标
			posy=y;
		}
		
		bool ishide(){//是否被隐藏
		    return visible;
		}
		
		int hitbox_set(int startx,int starty,int height,int width){//设置碰撞箱
			hitbox_x=startx;
			hitbox_y=starty;
			hitbox_width=width;
			hitbox_height=height;
		}
		
		void move_to(int posx,int posy){//按坐标移动精灵
			hide_sprite();
			print_sprite(posx,posy,now_pic);
		}
		
		picture get_animation(int animate_num){//返回当前图片(是一整张图)
			return animation[animate_num];
		}
	private:
		int posx;//X坐标
		int posy;//Y坐标
		int height;//长
		int width;//宽
		int hitbox_x;//碰撞箱相对X坐标时起点的X值
		int hitbox_y;//碰撞箱相对Y坐标时起点的Y值
		int hitbox_width;//碰撞箱宽
		int hitbox_height;//碰撞箱高
		bool visible;//是否隐藏,0为隐藏,1为显示
		vector<picture> animation;//图片
		int now_pic;//当前图;
};

这个东西真的还挺好用。。。

测试图:


透明的那个就是我。。。


使用:开一个.h文件,复制粘贴,编译运行即可。。。


。。。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值