求助!自己做的小游戏,背景显示不了,主角显示不了

0

Win11 visual studio 2022 背景图片出不来 

#define _CRT_SECURE_NO_WARNINGS
#include <graphics.h>
#include <conio.h>
#include <time.h>
#include <math.h>
#include <stdio.h>
#include<stdlib.h>
#include <easyx.h>
#include<Windows.h>
#include<iostream>
#include<string.h>
#include <cmath>

#define PI 3.1415926
#define BULLET_SPEED 200
MOUSEMSG m;

#define MAX_BULLETS 10000  // 最多子弹数
struct Point
{
	float x, y;
};

struct Bullet {
	Point position;
	Point direction;
	int speed;
	int damage; // 添加伤害属性
};


// 怪物结构体定义
struct Monster {
	int x, y;       // 怪物位置
	int hp;         // 怪物血量
	bool alive;     // 怪物是否存活
};

// 英雄结构体定义
struct Hero {
	int x, y;       // 英雄位置
	int hp;         // 英雄血量
};

// 地图尺寸
const int WIDTH = 1280;
const int HEIGHT = 960;

// 游戏参数
const int HERO_SPEED = 10;         // 英雄移动速度
const int BULLET_DAMAGE = 20;     // 子弹伤害
const int MONSTER_MAX_HP = 500;   // 怪物最大血量
const int MONSTER_SPEED = 50;   // 怪物最大血量
const int HERO_MAX_HP = 100;      // 英雄最大血量

// 地图节点结构体定义
typedef struct mapnode {
	int number;          // 地图编号
	const char* src;           // 地图文件路径
	struct mapnode* next; // 指向下一个地图节点
} mapnode;

Bullet bullets[MAX_BULLETS];  // 子弹数组
int bullet_count = 0;  // 当前子弹数


// 函数声明
Point calculateDirection(Point start, Point end);
void showmainmenu();
mapnode* createmaplist();
void runGame();
void updateMonsterPosition(Monster* monster, float deltaTime, int speed);
void updateBullet();

int main() {

	showmainmenu();
	IMAGE img;

	closegraph();
	return 0;
}


Point calculateDirection(Point start, Point end)
{
	float dx = end.x - start.x;
	float dy = end.y - start.y;
	float length = sqrt(dx * dx + dy * dy);

	return { dx / length, dy / length };
}
void updateBullet()
{
	Bullet bullet;
	bullet.position = { 0, 0 };
	bullet.direction = { 0, 0 };
	bullet.speed = 200;
	m = GetMouseMsg();
	bullet.position.x = m.x;
	bullet.position.y = m.y;
	bullet.position.x += bullet.direction.x * bullet.speed;
	bullet.position.y += bullet.direction.y * bullet.speed;
}

//菜单
void showmainmenu() {

	//初始化窗口
	initgraph(528, 528);
	setbkcolor(WHITE);
	cleardevice();

	//初始化标题字体
	settextcolor(BLACK);
	settextstyle(80, 0, (LPCTSTR)"Times New Roman");
	setbkmode(TRANSPARENT);
	char* arr = (char*)"Kill Monsters";

	int x, y;
	x = 264 - textwidth((LPCTSTR)arr) / 2;
	y = 100 - textheight((LPCTSTR)arr) / 2;
	outtextxy(x, y, (LPCTSTR)"Kill Monsters");
	//加阴影
	settextcolor(RGB(154, 167, 177));
	outtextxy(x + 5, y + 5, (LPCTSTR)arr);
	settextcolor(BLACK);
	//开始游戏按钮和字体初始化
	setfillcolor(WHITE);
	setlinecolor(BLACK);
	setlinestyle(PS_SOLID);
	arr = (char*)"Start Game";
	settextstyle(40, 0, (LPCTSTR)"Arial");  // Changed the font to Arial
	x = 264 - textwidth((LPCTSTR)arr) / 2;
	y = 250 - textheight((LPCTSTR)arr) / 2;
	fillroundrect(x - 20, y - 20, x + textwidth((LPCTSTR)arr) + 20, y + textheight((LPCTSTR)arr) + 20, 20, 20);
	outtextxy(x, y, (LPCTSTR)arr);

	arr = (char*)"version 0.1";
	settextstyle(20, 0, (LPCTSTR)"Arial");  // Changed the font to Arial
	outtextxy(450, 500, (LPCTSTR)arr);
	settextstyle(40, 0, (LPCTSTR)"Arial");  // Changed the font to Arial
	arr = (char*)"Start Game";
	//接收鼠标信息
	ExMessage msg;
	int flag = 0;
	while (1) {
		if (peekmessage(&msg, EM_MOUSE)) {
			switch (msg.message) {
			case WM_LBUTTONUP:
				if (msg.x >= x - 20 && msg.x <= x + textwidth((LPCTSTR)arr) + 20 && msg.y >= y - 20 && msg.y <= y + textheight((LPCTSTR)arr) + 20) {
					flag = 1;
					closegraph();
					runGame();
					break;
				}
			case WM_MOUSEMOVE:
				if (msg.x >= x - 20 && msg.x <= x + textwidth((LPCTSTR)arr) + 20 && msg.y >= y - 20 && msg.y <= y + textheight((LPCTSTR)arr) + 20) {
					setfillcolor(RGB(154, 167, 177));
					fillroundrect(x - 20 + 1, y - 20 + 1, x + textwidth((LPCTSTR)arr) + 21, y + textheight((LPCTSTR)arr) + 21, 20, 20);
					outtextxy(x, y, (LPCTSTR)arr);
				}
				else {
					setfillcolor(WHITE);
					fillroundrect(x - 20, y - 20, x + textwidth((LPCTSTR)arr) + 20, y + textheight((LPCTSTR)arr) + 20, 20, 20);
					outtextxy(x, y, (LPCTSTR)arr);
				}
				break;
			}
		}
		if (flag == 0) {
		}
		else {
			break;
		}
	}

}


mapnode* createmaplist() {
	mapnode* head = (mapnode*)malloc(sizeof(mapnode));
	if (!head) return NULL;
	head->number = 1;
	head->src = "C:/Users/朱玺/Desktop/IMG/MAP1.jpg";
	head->next = NULL;


	mapnode* second = (mapnode*)malloc(sizeof(mapnode));
	if (!second) {
		free(head);
		return NULL;
	}
	second->number = 2;
	second->src = "C:/Users/朱玺/Desktop/IMG/MAP2.jpg";
	second->next = NULL;

	head->next = second;

	return head;
}


void runGame() {
	initgraph(1280, 800);
	
	Hero hero;
	hero.x = 0;
	hero.y = 300;
	hero.hp = HERO_MAX_HP;

	IMAGE xiang;
	loadimage(&xiang, _T("C:\\Users\\朱玺\\Desktop\\IMG\\ren.jpg"));
	putimage(hero.x, hero.y, &xiang);


	Monster monster;
	monster.x = 1280;
	monster.y = 400;
	monster.hp = MONSTER_MAX_HP;
	monster.alive = true;

	IMAGE character1;
	loadimage(&character1, _T("C:\\Users\\朱玺\\Desktop\\IMG\\guai.jpg"));


	if (monster.alive)
	{
		putimage(monster.x, monster.y, &character1);
	}
	
	mapnode* currentMap = createmaplist();

	float deltaTime;
	clock_t lastTime = clock();
	
	while (monster.alive) {
		clock_t currentTime = clock();
		deltaTime = (float)(currentTime - lastTime) / CLOCKS_PER_SEC;

		updateMonsterPosition(&monster, deltaTime, MONSTER_SPEED);

		lastTime = currentTime;
	}

	// 主循环
	while (1) {
		// 如果怪物还活着,显示地图
		if (monster.alive) {
			IMAGE map;
			loadimage(&map, _T(currentMap->src));
			putimage(0, 0, &map);
		}

		// 键盘输入处理
		if (_kbhit()) 
		{
			char key =_getch();
			printf("%c", key);

			key = tolower(key);
			switch (key) {
			case 'w': hero.y -= HERO_SPEED; break;
			case 's': hero.y += HERO_SPEED; break;
			case 'a': hero.x -= HERO_SPEED; break;
			case 'd': hero.x += HERO_SPEED; break;
			}
		}


		
		// 处理鼠标点击事件
		while (MouseHit()) {
			m = GetMouseMsg();
			Bullet bullet;
			Point start = { hero.x, hero.y };  // 初始位置
			bullet.direction.x = m.x;  // 鼠标当前的x坐标
			bullet.direction.y = m.y;  // 鼠标当前的y坐标
			Point end = { bullet.direction.x, bullet.direction.y };  // 目标位置
			bullet.position = { 0, 0 };
			bullet.direction = calculateDirection(start, end);
			bullet.speed = 200;  // 子弹的速度
			bullet.damage = 50;  // 设置子弹伤害为50

			// 子弹在屏幕内移动
			while ((abs(bullet.position.x - end.x) > bullet.speed || abs(bullet.position.y - end.y) > bullet.speed)
				&& bullet.position.x >= 0 && bullet.position.x <= 1800
				&& bullet.position.y >= 0 && bullet.position.y <= 600)
			{
				updateBullet(); // 更新子弹位置
				circle(bullet.position.x, bullet.position.y, 10);

				std::cout << "Bullet position: (" << bullet.position.x << ", " << bullet.position.y << ")\n";

				// 计算子弹和怪物之间的距离
				double distance = sqrt(pow(bullet.position.x - monster.x, 2) + pow(bullet.position.y - monster.y, 2));

				// 如果距离小于10,怪物受到伤害
				if (distance < 10) {
					monster.hp -= bullet.damage;
					if (monster.hp <= 0) {
						// 如果怪物的生命值小于或等于0,怪物死亡
						monster.alive = false;
					}
				}
			}
		}


		// 更新怪物状态
		if (monster.alive) {
			updateMonsterPosition(&monster, deltaTime, MONSTER_SPEED);
		}

		// 如果怪物死亡,更换地图
		if (!monster.alive) {
			currentMap = currentMap->next;
			cleardevice();
		}

		// 刷新屏幕
		FlushBatchDraw();

		// 延迟
		Sleep(20);
	}

	// 关闭图形窗口
	closegraph();



}


void updateMonsterPosition(Monster* monster, float deltaTime, int speed) {
	monster->x += 1800-deltaTime * speed;
	monster->y += (rand() % 21) - 10;
}

ava
Echo

2023-11-26

悬赏金额:¥20元

1. 回答问题后,可以看到提问者联系方式。

2. 如果对问题有不明确的地方,可以在回复中写下你的疑问,就可以看到提问者的联系方式,然后详细沟通。

3. 如果私下沟通解决了问题,建议回答者将解决方案补充到本站,提问者勾选正确答案。每个人回答的问题数量,是能力的象征。

4. 回答问题后,可以通过提问者的联系方式私下协商领取悬赏的方式,本站不做任何干涉,不做担保,不抽取佣金,请双方谨慎操作。

0
int main() {

	showmainmenu();
	IMAGE img;

	closegraph(); // 与 runGame 重复
	return 0;
}
case WM_LBUTTONUP:
	if (msg.x >= x - 20 && msg.x <= x + textwidth((LPCTSTR)arr) + 20 && msg.y >= y - 20 && msg.y <= y + textheight((LPCTSTR)arr) + 20) {
		flag = 1;
		closegraph(); // runGame 有 closegraph,第二次会重复
		runGame();
		// runGame 结束后无窗口,不能交互
		break;
	}
// 如果怪物还活着,显示地图
if (monster.alive) {
	IMAGE map;
	loadimage(&map, _T(currentMap->src)); // 对非字符串字面量使用 _T 宏
	// loadimage(&map, currentMap->src);
	putimage(0, 0, &map);
}
mapnode* createmaplist() {
	mapnode* head = (mapnode*)malloc(sizeof(mapnode));
	if (!head) return NULL;
	head->number = 1;
	head->src = "C:/Users/朱玺/Desktop/IMG/MAP1.jpg"; // 未用 _T 宏
	// head->src = _T("C:\\Users\\朱玺\\Desktop\\IMG\\MAP1.jpg");
	head->next = NULL;


	mapnode* second = (mapnode*)malloc(sizeof(mapnode));
	if (!second) {
		free(head);
		return NULL;
	}
	second->number = 2;
	second->src = "C:/Users/朱玺/Desktop/IMG/MAP2.jpg"; // 未用 _T 宏
	// second->src = _T("C:\\Users\\朱玺\\Desktop\\IMG\\MAP2.jpg");
	second->next = NULL;

	head->next = second;

	return head;
}
typedef struct mapnode {
	int number;          // 地图编号
	const char* src;           // 地图文件路径
	// 使用 const char* 无法接收 const wchar_t* (Unicode 下 _T 宏返回)
	// LPCTSTR src;
	struct mapnode* next; // 指向下一个地图节点
} mapnode;
ava
侧云

2023-12-1

-1

在加载背景图片和输出背景图片的地方打几个断点进行调试。应该就能发现问题,目前没有完整的项目,无法帮你调试。

ava
简单

2023-11-28

技术讨论社区