struct

Why struct?

• 地圖上的x位置
• 地圖上的y位置
• x方向的速度
• y方向的速度
• 目前按住的按鍵
• 是否為撲倒狀態
• ...

double x[60];

double y[60];

double vx[60];

double vy[60];

bool key[60][4];

bool fall[60];

(每個資料沒有直接關係，是散開的)

Why struct?

• 地圖上的x位置
• 地圖上的y位置
• x方向的速度
• y方向的速度
• 目前按住的按鍵
• 是否為撲倒狀態
• ...

double x[60];

double y[60];

double vx[60];

double vy[60];

bool key[60][4];

bool fall[60];

``````struct fall_guys{
double x, y;
double vx, vy;
bool key[4];
bool fall;
};

struct fall_guys players[60];``````

Example Usage

``````#include <stdio.h>
struct fall_guys{
double x, y;
double vx, vy;
bool key[4];
bool fall;
};

struct fall_guys players[60];
int main(){
scanf("%lf%lf", &players[0].x, &players[0].y);
printf("x0: %lf, y0: %lf\n", players[0].x, players[0].y);

return 0;
}``````

typedef

type define - 定義型態名稱

把int換個名字?

``````typedef int I;
typedef double D;``````

struct fall_guys -> fall_guys

``````#include <stdio.h>
typedef struct fall_guys{
double x, y;
double vx, vy;
bool key[4];
bool fall;
}fall_guys;

fall_guys players[60];
int main(){
scanf("%lf%lf", &players[0].x, &players[0].y);
printf("x0: %lf, y0: %lf\n", players[0].x, players[0].y);

return 0;
}``````

Cursor

POINT

``````typedef struct tagPOINT {
LONG x;
LONG y;
} POINT, *PPOINT, *NPPOINT, *LPPOINT;``````

Get Cursor

``````#include <windows.h>
POINT pt;
GetCursorPos(&pt);
// pt.x就會等於現在的滑鼠x座標
// pt.y就會等於現在的滑鼠y座標``````

Set Cursor

``````#include <windows.h>
SetCursorPos(x, y);
``````

Function

Function in math

f(x) = x^2 + x + 1

f(5) = 31

Usage:

f(x, y) = x^2 + y + 1

f(5, 2) = 28

Function Def

}

``````int f(int x){
return x * x + x + 1;
}``````

f(x) = x^2 + x + 1

Function Usage

``````int f(int x){
return x * x + x + 1;
}``````

跟數學的用法一樣。

``````printf("%d", f(5));
// -----
cout << f(5);``````

Function Tips

``````void Hi(){
printf("Hi");
return ;
}``````

Exercise

``````#include <stdio.h>
#include <string.h>
// TODO
int main(){
int pos = 0;
char S[100];
while(scanf("%s", S)!= EOF){
if(strcmp(S, "left")==0){
pos = left(pos);
}else if(strcmp(S, "right")==0){
pos = right(pos);
}else if(strcmp(S, "print")==0){
print(pos);
}
}
return 0;
}``````

Keyboard Control

GetKeyState

偵測左Shift

``GetKeyState('A');``
``GetKeyState(VK_LSHIFT);``

偵測左鍵

``GetKeyState(VK_LEFT);``

keybd_event

按住A

``````keybd_event('A', 0, KEYEVENTF_EXTENDEDKEY|0, 0);
``````
`` keybd_event('A', 0, KEYEVENTF_KEYUP|0, 0);``

By Arvin Liu

• 447