修练者
#include "codingtang.h"
#include <cstdlib>
using namespace std;
struct din{
int now_up=12,now_y=160,now_xx=50; //现在的每帧上升像素数
bool up; //是否正在进行跳跃
int blood=20;
}D;
struct bird{
int Y=110,now_X=550,spe=4;
}bir,bir2;
struct goad_apple{
int xx=440,yy=100;
}app;
struct water{
int XX=130,YY=100;
int go_x,go_flag;
}wat;
int cnt=1,speed=3;
int little_xrz_x=440,big_xrz_x=250,little_cnt,big_cnt,flag1,flag2;
bool flag;
int main(){
Window window = createWindow(500,200);
Color fontColor = {255,255,255,0};
Color fontColor1 = {0,255,0,0};
Color fontColor2 = {255,0,0,0};
Color fontColor3 = {255,255,0,0};
Color fontColor4 = {0,0,255,0};
Event event; //定义事件
while(true){
while(true){
little_xrz_x-=speed; //更新障碍的位置
big_xrz_x-=speed;
wat.XX--;
wat.YY+=rand()%6-3;
if(wat.go_flag==1)wat.go_x+=5;
if(cnt>=1000){
bir.now_X-=bir.spe;
bir2.now_X-=bir.spe;
}
if(cnt%100==0){
wat.XX=rand()%100+100;
wat.YY=rand()%50+60;
}
app.xx-=speed;
if(little_xrz_x<10){
little_xrz_x=440-rand()%80;
}
if(big_xrz_x<10){
big_xrz_x=440-rand()%80;
}
if(bir.now_X<=10){
bir.now_X=300+rand()%150;
bir.Y=90+rand()%50;
bir.spe=4+rand()%3;
}
if(bir2.now_X<=10){
bir2.now_X=300+rand()%150;
bir2.Y=90+rand()%50;
bir2.spe=4+rand()%3;
}
if(app.xx<10){
app.xx=440-rand()%80;
app.yy=80+rand()%40;
}
if(cnt%1000==0)speed++;
window.drawText( "blood:" , 15 , fontColor , 10 , 10); //打印血条
for(int i=1;i<=D.blood;i++){
if(D.blood<10&&D.blood>=5) window.drawText( "|" , 10 , fontColor3 , 60+i*3 , 10 );//显示血量
else if(D.blood<5)window.drawText( "|" , 10 , fontColor2 , 60+i*3 , 10 );
else window.drawText( "|" , 10 , fontColor1 , 60+i*3 , 10 );
}
if(wat.go_flag==1){
for(int i=1;i<=200;i+=10){
window.drawText( "[]" , 10 , fontColor4 , wat.go_x , i);
if(big_xrz_x>=wat.go_x-5&&big_xrz_x<=wat.go_x+5&&i>=140&&i<=160){
big_xrz_x=650; //检测大仙人掌的碰撞
flag2=cnt;
}
if(little_xrz_x>=wat.go_x-5&&little_xrz_x<=wat.go_x+5&&i>=140&&i<=160){
little_xrz_x=570; //检测小仙人掌的碰撞
flag2=cnt;
}
if(bir.now_X>=wat.go_x-5&&bir.now_X<=wat.go_x+5&&D.now_y>=i-5&&D.now_y<=i+5){
bir.now_X=640; //检测飞鸟的碰撞
flag2=cnt;
}
if(bir2.now_X>=wat.go_x-7&&bir2.now_X<=wat.go_x+7&&i>=bir2.Y-5&&i<=bir2.Y+5){
bir2.now_X=600; //检测飞鸟的碰撞
flag2=cnt;
}
}
}
window.drawText( "/--" , 20 , fontColor , bir2.now_X , bir2.Y);
window.drawText( "/--" , 20 , fontColor , bir.now_X , bir.Y);
window.drawText( "水" , 15 , fontColor4 , wat.XX ,wat.YY);
window.drawText( "•" , 20 , fontColor3 , app.xx , app.yy);
window.drawText( "K" , 20 , fontColor1 , little_xrz_x , 160);
window.drawText( "B" , 20 , fontColor1 , big_xrz_x , 160);
window.drawText( "B" , 20 , fontColor1 , big_xrz_x , 140);
window.drawText( "I" , 20 , fontColor2 , D.now_xx , D.now_y);
window.drawText( "========================================================================================" , 10 , fontColor , 0 , 180);//绘制出地面
if(pollEvent(&event)){ //监听
if(event.type == CDT_KEYDOWN){ //是KEYDOWN事件 键盘按钮按下事件
switch(event.keyCode){ //判断是什么按钮被按下
case CDT_KEY_w: //w键被按下
D.up=1; //开始一次跳跃
break;
case CDT_KEY_a: //a键被按下
D.now_xx-=8; //左移8个像素
break;
case CDT_KEY_d: //d键被按下
D.now_xx+=8; //右移8个像素
break;
}
}
}
if(D.up==1){
D.now_y-=D.now_up; //更新恐龙的位置
D.now_up-=1; //更新下一秒上升(或下降)的高度
if(D.now_up==-12){ //跳跃结束
D.now_y=160;
D.now_up=12;
D.up=0;
}
}
if(big_xrz_x>=D.now_xx-5&&big_xrz_x<=D.now_xx+5&&D.now_y>=140&&D.now_y<=160&&cnt>=flag2+10){
D.blood-=5; //检测大仙人掌的碰撞
flag2=cnt;
}
if(little_xrz_x>=D.now_xx-5&&little_xrz_x<=D.now_xx+5&&D.now_y>=150&&D.now_y<=160&&cnt>=flag2+10){
D.blood-=3; //检测小仙人掌的碰撞
flag2=cnt;
}
if(bir.now_X>=D.now_xx-5&&bir.now_X<=D.now_xx+5&&D.now_y>=bir.Y-5&&D.now_y<=bir.Y+5&&cnt>=flag2+10){
D.blood-=4; //检测飞鸟的碰撞
flag2=cnt;
}
if(bir2.now_X>=D.now_xx-7&&bir2.now_X<=D.now_xx+7&&D.now_y>=bir2.Y-5&&D.now_y<=bir2.Y+5&&cnt>=flag2+10){
D.blood-=4; //检测飞鸟的碰撞
flag2=cnt;
}
if(wat.XX>=D.now_xx-7&&wat.XX<=D.now_xx+7&&D.now_y>=wat.YY-5&&D.now_y<=wat.YY+5&&cnt>=flag2+10){
wat.go_flag=1;//检测水的碰撞
wat.go_x=D.now_xx;
flag2=cnt;
}
if(app.xx>=D.now_xx-5&&app.xx<=D.now_xx+5&&D.now_y>=app.yy-5&&D.now_y<=app.yy+5&&cnt>=flag2+10){
if(D.blood<=20)D.blood+=5; //检测金苹果的碰撞
flag2=cnt;
}
if(D.blood<=0){ //血量为0
window.drawText( "YOU DIED..." , 30 , fontColor2 , 150 , 50);
sleep(500);
break;
}
if(cnt%100==0&&D.blood<20)D.blood++;
cnt++;
if(cnt==2000){
window.drawText( "YOU WIN!" , 30 , fontColor3 , 150 , 50);
sleep(500);
break;
}
window.present();
sleep(3);
}
cnt=1;
flag1=0;
flag2=0;
flag=0;
speed=3;
big_xrz_x=250;
D.now_xx=50;
little_xrz_x=440;
D.blood=20;
bir.Y=110;
bir.now_X=440;
bir.spe=3;
bir2.Y=110;
bir2.now_X=440;
bir2.spe=3;
while(1){
sleep(1);
window.drawText( "press 's' to start a new game or press 'n' to exit" , 15 , fontColor , 100 , 50);
if(pollEvent(&event)){
if(event.type == CDT_KEYDOWN){ //是KEYDOWN事件 键盘按钮按下事件
switch(event.keyCode){ //判断是什么按钮被按下
case CDT_KEY_s: //s键被按下
flag1=1; //开始一次新游戏
break;
case CDT_KEY_n: //n键被按下
flag1=2; //结束游戏
break;
}
}
}
if(flag1==1)break;
if(flag1==2){
flag=1;
break;
}
}
if(flag==1)break;
}
return 0;
}
代码,请复制到酷丁平台上运行。
修练者
操作方法:那个红色的竖杠就是你,你可以按"W"键跳跃; 屏幕上方的"blood"后面的是你的血条,初始值是20,每过50游戏秒恢复1; 那个绿色的,由两个"B"叠起来的是大仙人掌,碰上去扣5滴血; 那个绿色的,由一个"K"叠起来的是小仙人掌,碰上去扣3滴血; 那两个飞在天上的白东西,是飞鸟,碰上去扣4滴血; 那个也是飞在天上的金点,是金苹果,碰上去加5滴血; 在血量归0后你就会死,死后你可以按"s"键开始一局新游戏,也可以按"n"键退出。 当你坚持了3000游戏秒后你就会赢。