首页 技术 正文
技术 2022年11月21日
0 收藏 330 点赞 3,940 浏览 1233 个字

easyx是一个针对VC++编译器的图形化插件。使用它,可以使得在C++中编写图形程序。

小球移动代码:

#include"stdafx.h"
#include<graphics.h>
#include<conio.h>
#include<stdlib.h>
#include<time.h>int main(){
initgraph(640, 480);
char ch; int now_x=200,now_y=200; for(;;){
circle(now_x,now_y,15);
ch=_getch();
if(ch=='A')now_x-=10;
else if(ch=='S')now_y+=10;
else if(ch=='W')now_y-=10;
else if(ch=='D')now_x+=10;
cleardevice(); } closegraph();
return 0;
}

注:

1.stdafx.h是VC++新建工程时默认加入的头文件

2.initgraph用于创建窗口

3.根据getch()的结果进行移动小球

4.每次得到按键之后清屏(即cleardevice()函数),然后使用circle画新的圆

效果演示:

简单小游戏:

小球移动,掉落到地面上就GAME OVER,如果球碰到右上角的球就胜利。(球在没有控制的情况下会不断向下掉落)

使用WASD控制

#include"stdafx.h"
#include<graphics.h>
#include<conio.h>
#include<stdlib.h>
#include<time.h>int main(){
initgraph(640, 480);
char ch; int now_x=200,now_y=200; int t=clock(); circle(30,400,15);
for(;;){
circle(now_x,now_y,15);
if(_kbhit()){
ch=_getch();
if(ch=='A')now_x-=10;
else if(ch=='S')now_y+=10;
else if(ch=='W')now_y-=10;
else if(ch=='D')now_x+=10;
}
else if(clock()-t>100){
t=clock();
now_y+=10;
if(now_y>480)now_y=480;
}
cleardevice();
circle(400,30,15); if(now_x==400 && now_y==30)goto win;
if(now_y>=480)goto lose;
}win:
cleardevice();
TCHAR s[]=_T("YOU WIN");
outtextxy(200,200,s);
for(;;);
closegraph();
return 0;
lose:
cleardevice();
TCHAR st[]=_T("GAME OVER");
outtextxy(200,200,st);
for(;;);
closegraph();
return 0;
}

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,108
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,584
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,431
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,202
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,837
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,920