1.使用pthread,调用的函数的参数是class中的,该如何处理?
#include<Windows.h>
#include <pthread.h>
#include<stdlib.h>
#include <time.h>
#include<stdio.h>
#include<conio.h>
#define KEY_UP 'w'
#define KEY_DOWN 's'
#define KEY_LEFT 'a'
#define KEY_RIGHT 'd'
#define FIRE 'j'
class Player {
private:
int positionX;
int positionY;
int positionA;
int positionB;
public:
void SetPositionX(int x) { positionX = x; }
void SetPositionY(int y) { positionY = y; }
int GetPositionX() { return positionX; }
int GetPositionY() { return positionY; }
int m, n; //子弹坐标
};
void SetPosition(int x, int y)
{
COORD coord;
coord.X = 2 * x;
coord.Y = y;
HANDLE handle;
handle = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(handle,coord);
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_CURSOR_INFO cursor_info = {1, 0};
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}
void *Controller(Player &pl)
{
int direction;
if (_kbhit()) { //_kbhit()为true当且仅当用户按下键盘
switch (_getch()) { //_getch()返回用户按下的那个按键,注意使用小写
//根据玩家的输入进行移动
case KEY_UP:
if (pl.GetPositionY() == 1 ) {
break;
}
else {
SetPosition(pl.GetPositionX(), pl.GetPositionY());
printf(" ");
pl.SetPositionY(pl.GetPositionY() - 1); //
SetPosition(pl.GetPositionX(), pl.GetPositionY()); //改变光标位置到改变后的位置
printf("○"); //进行玩家的移动
direction = 1;
}
break;
case KEY_DOWN:
if (pl.GetPositionY() == 21 ) {
break;
}
else {
SetPosition(pl.GetPositionX(), pl.GetPositionY());
printf(" ");
pl.SetPositionY(pl.GetPositionY() + 1);
SetPosition(pl.GetPositionX(), pl.GetPositionY());
printf("○");
direction = 2;
}
break;
case KEY_LEFT:
if (pl.GetPositionX() == 1 ) {
break;
}
else {
SetPosition(pl.GetPositionX(), pl.GetPositionY());
printf(" ");
pl.SetPositionX(pl.GetPositionX() - 1);
SetPosition(pl.GetPositionX(), pl.GetPositionY());
printf("○");
direction = 3;
}
break;
case KEY_RIGHT:
if (pl.GetPositionX() == 37 ) {
break;
}
else {
SetPosition(pl.GetPositionX(), pl.GetPositionY());
printf(" ");
pl.SetPositionX(pl.GetPositionX() + 1);
SetPosition(pl.GetPositionX(), pl.GetPositionY());
printf("○");
direction = 4;
}
break;
default:break;
}
}
}
int main()
{
int pth1, loop01;
pthread_t threads;
Player player, enemy; //声明一个自定义的玩家类型变量
srand((unsigned)time(NULL));
player.SetPositionX(rand()%30+2);
player.SetPositionY(rand()%15+2); //设置初始坐标
enemy.SetPositionX(rand()%30+2);
enemy.SetPositionY(rand()%15+2);
while (1) //游戏循环
{ //问题在此
pth1 = pthread_create(&threads, NULL, Controller, (void*)Player &player);
} //参数该如何调用?
system("pause");
return 0;
}
3.报错如下
E:\Workspace\C\Game test\problem.cpp: In function 'int main()':
E:\Workspace\C\Game test\problem.cpp:119:67: error: expected primary-expression
before '&' token
pth1 = pthread_create(&threads, NULL, Controller, (void*)Player &player);
^
リーリー
私の場合はコンパイルできますが、少し見にくいです
pth1 = pthread_create(&threads, NULL, Controller, &player);