
别人用我的代码就可以输出图片,我的就不行
图片已放入相应文件夹。
#include<iostream>
#include<conio.h>
#include<stdlib.h>
#include<windows.h>
#include<stdio.h>
#include <graphics.h>
using namespace std;
//全局变量
//图片
//英雄坦克
IMAGE hero;
//敌人坦克
IMAGE emy;
//子弹
IMAGE bulite;
//坦克的横坐标: 左右移动 左x-- 右 x++
int tankx;
//坦克的纵坐标: 上下移动 上y-- 下 y++
int tanky;
//敌人:产生位置,碰到我方之后
//坐标
int emyx;
int emyy;
//英雄子弹
int bx;
int by;
//开火关火
int frie = 1;
//界面的宽度和高度
int high;//界面的高度
int width;//界面的宽度
//计分
int score = 0;
//清屏方法
//清屏
void clear(int x, int y)//类似于清屏函数,光标移动到原点位置进行重画
{
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
COORD pos;
pos.X = x;
pos.Y = y;
SetConsoleCursorPosition(handle, pos);
}
void hdiepos()
{
CONSOLE_CURSOR_INFO cursor_info = { 1,0 };
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}
//模块
//初始数据模块
void setup_data() {
//图片 图片对象 图片地址
loadimage(&hero, "D:\\img\\tanke.gif");//调用图片
loadimage(&emy, "D:\\img\\diren.gif");
loadimage(&bulite, "D:\\img\\zidan.gif");
//界面的宽度高度
high = 600;
width = 800;
initgraph(800, 600);
//坦克的位置:把坦克显示到正中央的位置
tankx = width / 2;
tanky = high / 2;
//分值
score = 0;
//敌人初始位置
emyx = 0;
emyy = 0;
//英雄子弹
bx = tankx;
by = tanky;//是在坦克的上边 #
BeginBatchDraw();//画
}
//显示
void show() {
//清屏
//system("cls");
//clear(0,0);
//图像坐标 图像 原类型
putimage(tankx, tanky, &hero, SRCINVERT);//输出我方坦克
putimage(emyx, emyy, &emy, SRCINVERT);
putimage(bx, by, &bulite, SRCINVERT);
FlushBatchDraw();
}
//操作
//跟用户操作有关的 A S D W J
void update_ok() {
//清楚操作
cleardevice();
//接收变量
char input;
//判断是否按下了键盘
if (kbhit()) {
//输入
input = getch();
//判断 A
if (input == 'a') {
//左移动
tankx--;
cout << by << endl;
}
//判断 D
if (input == 'd' && tankx < width - 1) {
//右移动
tankx++;
}
//判断 S
if (input == 's' && tanky < high - 1) {
//下移动
tanky++;
}
//判断 W
if (input == 'w') {
//上移动
tanky--;
}
//判断 J
if (input == 'j')
{
by = tanky;
bx = tankx;
frie=0;
}
}
}
//与用户操作无关:敌方的移动,子弹的移动
void no_update()
{
//敌人的移动
emyx++;
//到底之后要重新生成
if(emyx>high-1)
{
emyx=0;
//随机出现
emyx=rand()%40;
}
//子弹移动
if(frie==0)
{
by--;
//在坦克上装配子弹
}
else {
by=tanky;
}
//判断是否击中敌人
if(emyx==bx&&emyy==by)
{
emyx=0;
//计分
score++;
}
}
//程序入口模块:说白了就是主方法
int main(){
//调用初始数据
setup_data();
//运行程序
while(1){
//显示页面
show();
//调用用户操作
update_ok();
//自动操作,与用户操作无关
no_update();
}
return 0;
}