事件
分类
键盘事件 和 鼠标事件
键盘事件函数:
bool IsKeyDown(int key); //确定按键按下
bool IsKeyReleased(int key);
bool IsKeyUp(int key);
int GetKeyPressed();
最简单的逻辑控制
思路:我们运用IsKeyDown(Key的状态) 判断键盘是否按下这个键,然后按照上下左右的键位对小球进行控制
#include <stdio.h>
#include <raylib.h>
#include <time.h>
#include <stdlib.h>
void test_database()
{
Vector2 p;
p.x = 11;
p.y = 2;
Vector3 o;
o.x = 10;
o.y = 11;
o.z = 10;
}
int main()
{
srand((unsigned int)time(NULL));
int x = 10, y = 10;
InitWindow(800, 600,"Alaso_shuang");
while (!WindowShouldClose())
{
BeginDrawing();
DrawCircle(x, y, 10, RED);
ClearBackground(BLACK);
EndDrawing();
if (IsKeyDown(KEY_UP)) y -= 1;
if (IsKeyDown(KEY_DOWN)) y += 1;
if (IsKeyDown(KEY_LEFT)) x -= 1;
if (IsKeyDown(KEY_RIGHT)) x += 1;
}
CloseWindow();
return 0;
}
你会发现这个小球一下子就出去了,我们需要设置帧率才会正常运转,优化上述程序
#include <stdio.h>
#include <raylib.h>
#include <time.h>
#include <stdlib.h>
void test_database()
{
Vector2 p;
p.x = 11;
p.y = 2;
Vector3 o;
o.x = 10;
o.y = 11;
o.z = 10;
}
int main()
{
srand((unsigned int)time(NULL));
int x = 10, y = 10;
InitWindow(800, 600,"Alaso_shuang");
//需要设置帧率,帧率越低,移动速度越慢
SetTargetFPS(60);
while (!WindowShouldClose())
{
BeginDrawing();
DrawCircle(x, y, 10, RED);
ClearBackground(BLACK);
EndDrawing();
if (IsKeyDown(KEY_UP)) y -= 1;
if (IsKeyDown(KEY_DOWN)) y += 1;
if (IsKeyDown(KEY_LEFT)) x -= 1;
if (IsKeyDown(KEY_RIGHT)) x += 1;
if (IsKeyDown(KEY_ESCAPE)) break;
}
CloseWindow();
return 0;
}
鼠标事件函数:
bool IsMouseButtonPressed(int button);
bool IsMouseButtonDown(int button);
bool IsMouseButtonReleased(int button);
int GetMouse(); //获取鼠标坐标
int GetMouse(void);
最简单的逻辑控制
#include <stdio.h>
#include <raylib.h>
#include <time.h>
#include <stdlib.h>
void test_database()
{
Vector2 p;
p.x = 11;
p.y = 2;
Vector3 o;
o.x = 10;
o.y = 11;
o.z = 10;
}
int main()
{
srand((unsigned int)time(NULL));
int x = 10, y = 10;
InitWindow(800, 600,"Alaso_shuang");
//需要设置帧率,帧率越低,移动速度越慢
SetTargetFPS(60);
while (!WindowShouldClose())
{
BeginDrawing();
DrawCircle(x, y, 10, RED);
EndDrawing();
if (IsMouseButtonDown(MOUSE_BUTTON_LEFT))
{
DrawCircle(GetMouseX(),GetMouseY(), 10, GREEN);
}
if (IsMouseButtonDown(MOUSE_BUTTON_RIGHT))
{
DrawRectangle(GetMouseX()-5, GetMouseY()- 5, 20, 10, RED);
}
}
CloseWindow();
return 0;
}
824

被折叠的 条评论
为什么被折叠?



