分享

别碰白块(游戏笔记)2

 行立坐卧 2020-08-26

方块的绘制和移动

#include<graphics.h>

#include<conio.h>

#include<stdio.h>

int main()

{

int width, height;  // 游戏画面大小 

width = 600;  // 宽

height = 400; // 高

initgraph(width,height); // 重建一个画布

float ball_x,ball_y,radius; //  圆心坐标 半径

float ball_vy,gravity; //y方向速度,重力加速度

radius = 20;

ball_x = width/4;  //  小球x 坐标

ball_y = height-radius;  //  小球y坐标

ball_vy = 0;     // 初始y方向速度为0

gravity = 0.6; // 重力加速度

float rect_left_x,rect_top_y,rect_width,rect_height;

float rect_vx;

rect_height = 100;

rect_width = 20;

rect_left_x = width*3/4;

rect_top_y = height - rect_height;

rect_vx = -3;

while (1)

{

if(kbhit())  // 当按键时

{

char input = getch();  //获得字符

if (input==' ') //当按下空格键时

{

ball_vy = -16; // 给小球一个向上的加速度

}

}

ball_vy = ball_vy + gravity;    // 根据重力速度更新小球y方向速度

ball_y = ball_y + ball_vy;    //  根据小球y方向速度更新其y坐标 

if (ball_y>=height-radius)  // 如果小球落到地面上

{

ball_y = 0;    //  速度为0

ball_y = height-radius; // 规范其y坐标,避免落到地面上

}

rect_left_x = rect_left_x + rect_vx; // 方块往左运动

if (rect_left_x<=0)

{

rect_left_x=width;

}

cleardevice();  // 清空画面

fillrectangle(rect_left_x,height - rect_height,rect_left_x+rect_width,height); // 绘制方块

fillcircle(ball_x,ball_y,radius); // 绘制小球

Sleep(10); // 暂停10毫秒

}

_getch();  // 等待按键

closegraph();  // 关闭窗口

return 0;

}

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多