请写一段打怪升级有画面的devc++代码
时间: 2023-08-18 15:07:57 浏览: 165
qt下用c++语言写的 打怪发育game,其中英雄可以体验到打怪发育,打工做任务发育等级,当然,人物还可以随时查看自己的状态装备
好的,这里是一个简单的带图形界面的打怪升级游戏的示例代码:
```
#include <graphics.h>
#include <conio.h>
#include <stdlib.h>
#include <time.h>
int main() {
initgraph(640, 480); // 初始化图形界面,大小为640x480像素
setbkcolor(WHITE); // 设置背景颜色为白色
cleardevice(); // 清屏
// 加载图片
IMAGE player, enemy, background;
loadimage(&player, "player.jpg", 100, 100);
loadimage(&enemy, "enemy.jpg", 100, 100);
loadimage(&background, "background.jpg", 640, 480);
// 初始化游戏参数
srand(time(NULL));
int player_health = 100;
int player_attack = 10;
int enemy_health = 50;
int enemy_attack = 5;
int experience = 0;
int level = 1;
while (player_health > 0) {
// 绘制背景和角色
putimage(0, 0, &background);
putimage(100, 300, &player);
putimage(400, 300, &enemy);
// 绘制文字
settextcolor(BLACK);
settextstyle(20, 0, "宋体");
char player_health_str[50], player_attack_str[50], experience_str[50], level_str[50];
sprintf(player_health_str, "你的血量:%d", player_health);
sprintf(player_attack_str, "你的攻击力:%d", player_attack);
sprintf(experience_str, "当前经验值:%d", experience);
sprintf(level_str, "当前等级:%d", level);
outtextxy(10, 10, player_health_str);
outtextxy(10, 50, player_attack_str);
outtextxy(10, 90, experience_str);
outtextxy(10, 130, level_str);
// 开始战斗
while (enemy_health > 0) {
if (kbhit()) { // 按下空格键攻击
char key = getch();
if (key == ' ') {
int player_damage = rand() % player_attack + 1;
enemy_health -= player_damage;
settextcolor(RED);
outtextxy(400, 260, "你攻击了敌人!");
Sleep(500);
settextcolor(BLACK);
outtextxy(400, 260, "你攻击了敌人!");
char player_damage_str[50];
sprintf(player_damage_str, "造成了%d点伤害。", player_damage);
outtextxy(400, 300, player_damage_str);
Sleep(500);
clearrectangle(400, 260, 640, 320);
clearrectangle(400, 300, 640, 320);
}
}
if (enemy_health > 0) { // 敌人攻击
int enemy_damage = rand() % enemy_attack + 1;
player_health -= enemy_damage;
settextcolor(RED);
outtextxy(100, 260, "敌人攻击了你!");
Sleep(500);
settextcolor(BLACK);
outtextxy(100, 260, "敌人攻击了你!");
char enemy_damage_str[50];
sprintf(enemy_damage_str, "造成了%d点伤害。", enemy_damage);
outtextxy(100, 300, enemy_damage_str);
Sleep(500);
clearrectangle(100, 260, 400, 320);
clearrectangle(100, 300, 400, 320);
}
if (player_health <= 0) { // 玩家死亡
settextcolor(RED);
outtextxy(100, 200, "你被敌人打败了,游戏结束。");
Sleep(2000);
cleardevice();
return 0;
}
}
// 玩家胜利
experience += rand() % 10 + 1;
if (experience >= level * 10) {
level++;
player_attack += 5;
settextcolor(GREEN);
outtextxy(10, 170, "恭喜你升到了新的等级!");
Sleep(2000);
clearrectangle(10, 170, 640, 200);
}
enemy_health = 50;
settextcolor(GREEN);
outtextxy(400, 260, "你打败了敌人!");
Sleep(1000);
clearrectangle(400, 260, 640, 320);
clearrectangle(400, 300, 640, 400);
}
return 0;
}
```
这个示例代码实现了一个简单的带图形界面的打怪升级游戏,玩家需要通过战斗来获取经验值,提升等级和攻击力,最终打败敌人。这个示例代码只是一个简单的实现,可以根据具体需求进行修改和扩展。
阅读全文