才知道EGE是可以做3D游戲呢,不過仔細想一想我做游戲的目的又是啥?現階段無非就是搞出一個邏輯復雜,能鍛煉編程思維的東西,最好是2.5D俯視角而且要多借鑒github等復雜的項目,變成自己的東西。最好做出新一個饑荒,元氣騎士,合金彈頭,僵尸危機,環世界,矮人要塞之類的。
#include
#include
#include
#include
#include
using namespace std;
#define SCREEN_WIDTH 900
#define SCREEN_HEIGHT 700
class Object{
int life;
PIMAGE img;
int x;
int y;
float speed;
void border(){
if(this->x < 0){
this->x = SCREEN_WIDTH-getwidth(this->img);
}else if(this->x > SCREEN_WIDTH-getwidth(this->img)){
this->x = 0;
}else if(this->y < 0){
this->y = SCREEN_HEIGHT-getheight(this->img);
}else if(this->y >SCREEN_HEIGHT-getheight(this->img)){
this->y = 0;
}
}
public:
Object(LPCSTR file,float speed){
random_device rd; // 隨機數種子
mt19937 gen(rd()); // 使用Mersenne Twister 19937算法生成隨機數引擎
uniform_real_distribution dis(0.0, 1.0); // 定義在[0, 1)范圍內的均勻分布隨機數
double random = dis(gen); // 生成0到1之間的隨機數
this->x = randomSCREEN_WIDTH;
this->y = randomSCREEN_HEIGHT;
this->life = 0;
this->speed = speed;
this->img = newimage();
getimage(this->img,file);
//LPCSTR為圖片路徑
}
void move(char k){
border();
if (k == 'w') {
this->y -= this->speed;
} else if (k == 's') {
this->y += this->speed;
} else if (k == 'a') {
this->x -= this->speed;
} else if (k == 'd') {
this->x += this->speed;
}
}
void display(){
putimage(this->x,this->y,this->img);
}
};
class Managemet{
public:
Managemet(){
initgraph(SCREEN_WIDTH,SCREEN_HEIGHT);
setcaption("Magic World");
setbkcolor(WHITE);
}
Object create_player(LPCSTR s="player.png",float speed = 3){
Object p(s,speed);
return p;
}
void start(){
Object p = create_player();
// //60幀(FPS)
// for (; is_run(); delay_fps(60)){
//
// }
// 當沒有按下ESC鍵時循環
while (!kbhit() || getch() != 27) {
cleardevice(); // 清空窗口
if (keystate(key_W)) {
p.move('w');
}else if(keystate(key_S)){
p.move('s');
}else if(keystate(key_A)){
p.move('a');
}else if(keystate(key_D)){
p.move('d');
}
p.display();
Sleep(10);
}
}
~Managemet(){
closegraph();
// 關閉圖形窗口
}
};
int main()
{
Managemet m;
m.start();
return 0;
}
// getch();暫停程序,等待用戶按下任意鍵(除暫停作用外,不建議使用)
// 使用newimage() 創建的圖像是動態內存分配的圖像,不使用時需要使用 delimage(pimg) 進行銷毀
class Object{
int life;
PIMAGE img;
int x;
int y;
float speed;
void border(){
if(this->x < 0){
this->x = SCREEN_WIDTH-getwidth(this->img);
}else if(this->x > SCREEN_WIDTH-getwidth(this->img)){
this->x = 0;
}else if(this->y < 0){
this->y = SCREEN_HEIGHT-getheight(this->img);
}else if(this->y >SCREEN_HEIGHT-getheight(this->img)){
this->y = 0;
}
}
public:
Object(LPCSTR file,float speed){
random_device rd; // 隨機數種子
mt19937 gen(rd()); // 使用Mersenne Twister 19937算法生成隨機數引擎
uniform_real_distribution
double random = dis(gen); // 生成0到1之間的隨機數
this->x = randomSCREEN_WIDTH;
this->y = randomSCREEN_HEIGHT;
this->life = 0;
this->speed = speed;
this->img = newimage();
getimage(this->img,file);
//LPCSTR為圖片路徑
}
void move(char k){
border();
if (k == 'w') {
this->y -= this->speed;
} else if (k == 's') {
this->y += this->speed;
} else if (k == 'a') {
this->x -= this->speed;
} else if (k == 'd') {
this->x += this->speed;
}
}
void display(){
putimage(this->x,this->y,this->img);
}
};
class Managemet{
public:
Managemet(){
initgraph(SCREEN_WIDTH,SCREEN_HEIGHT);
setcaption("Magic World");
setbkcolor(WHITE);
}
Object create_player(LPCSTR s="player.png",float speed = 3){
Object p(s,speed);
return p;
}
void start(){
Object p = create_player();
// //60幀(FPS)
// for (; is_run(); delay_fps(60)){
//
// }
// 當沒有按下ESC鍵時循環
while (!kbhit() || getch() != 27) {
cleardevice(); // 清空窗口
if (keystate(key_W)) {
p.move('w');
}else if(keystate(key_S)){
p.move('s');
}else if(keystate(key_A)){
p.move('a');
}else if(keystate(key_D)){
p.move('d');
}
p.display();
Sleep(10);
}
}
~Managemet(){
closegraph();
// 關閉圖形窗口
}
};
int main()
{
Managemet m;
m.start();
return 0;
}
// getch();暫停程序,等待用戶按下任意鍵(除暫停作用外,不建議使用)
// 使用newimage() 創建的圖像是動態內存分配的圖像,不使用時需要使用 delimage(pimg) 進行銷毀
浙公網安備 33010602011771號