- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在尝试制作一个在命令提示符下运行的小型 2 人游戏。在我开始研究玩家控制之前,一切都很好。因此,为了捕获键盘按键,我认为最好的解决方案是使用 getch() 函数。那是因为 getch() 在运行中获取键,而不是在屏幕上显示它,等待输入或其他键被按下。
据我所知,实现它的代码相当简单:
c=getch();
switch(c)
{
case 'a': make player 1 go left
break;
case 'd': make player 1 go right
break;
case 's': make player 1 go down
break;
case 'w': make player 1 go up
break;
case 'h': make player 2 go left
break;
case 'k': make player 2 go right
break;
case 'j': make player 2 go down
break;
case 'u': make player 2 go up
break;
}
当然,一切都在 while 循环中。
问题是我需要两个玩家都能够在同时按下控件时移动。例如,当玩家 1 在玩家 2 按下分配给左移动的键后玩家 1 向右移动时,使用 getch() 时,玩家 1 会阻止玩家 2,因为他们都按住移动键。当然,如果玩家 2 松开按键并按下指定的另一个键,他将控制移动,同时阻挡玩家 1,依此类推。
为了让自己更容易理解,代码如下所示:
c=getch();
switch(c)
{
case 'a': cout <<"a";
break;
case 'd': cout <<"b";
break;
}
当某人按住“a”键时,输出将是“aaaaaaaaaaaaaaaa”,而在其他人按下“b”后,即使第一个人按住“a”键,输出也将变为“aaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbb”。如果第一个人释放'a'并再次按下它,则输出将是“aaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbaaaaaaaaaaaaaaa”,因为另一个人按住'b'。为了让我的游戏正常运行,当他们同时按住“a”和“b”时,我相信输出应该类似于“ababababababababab”。
老实说,到目前为止,我已经尝试了所有我能想到的方法,但我是 C/C++ 编程的新手,所以我可能只是没有足够的经验来使用这些编程语言。不必使我的 getch()+switch() 策略起作用,任何其他建议也将受到欢迎。但他们必须恢复到 Windows 控制台。
提前感谢您的宝贵时间
最佳答案
你的想法是正确的。在一天结束时,它可能看起来像这样:
void Keypressed()
{
if (kbhit())
{
keypress =getch();
switch (keypress )
{
case key_F1:
displayHelp();
break;
case key_LEFT:
case key_RIGHT:
break;
case key_UP:
PlayersPad.y-=3;if (PlayersPad.y<0) PlayersPad.y=0;
break;
case key_DOWN:
PlayersPad.y+=3;if (PlayersPad.y>18) PlayersPad.y=18;
break;
case key_ENTER:
break;
case key_SPACE:
break;
case key_TAB:
autoPlay=-autoPlay;
if (autoPlay>0)
{
setcolor(79);gotoxy(62,23);printf("cheat on " );setcolor(15);
}
else
{
setcolor(79);gotoxy(62,23);printf("cheat off " );setcolor(15);
}
break;
}
}
}
作为额外的奖励,这是我编写的 Pong 游戏的代码,它是玩家对玩家或玩家对计算机。
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <time.h>
#include <iostream.h>
#include <math.h>
#include <fstream.h>
#define Black 0
#define Blue 1
#define Green 2
#define Cyan 3
#define Red 4
#define Magenta 5
#define Yellow 6
#define White 7
#define Gray 8
#define LightBlue 9
#define LightGreen 10
#define LightCyan 11
#define LightRed 12
#define LightMagenta 13
#define LightYellow 14
#define BrightWhite 15
#define key_F1 59
#define key_UP 72
#define key_DOWN 80
#define key_LEFT 75
#define key_RIGHT 77
#define key_SPACE 32
#define key_ENTER 13
#define key_ESCAPE 27
#define key_TAB 9
#define key_INSERT 82
#define PONG_WIDTH 78
#define PONG_HEIGHT 22
#define PONG_SCREEN_RIGHT PONG_WIDTH-3
#define PONG_SCREEN_LEFT 5
#define PONG_SCREEN_TOP 2
#define PONG_SCREEN_BOTTOM 22
struct t_ball{ int x,y,headingX,headingY;};
struct t_pad{ int x,y,LEFT,RIGHT;};
int temp,kbChar;
int _key=0;
unsigned long OldTicksPerSecond=GetTickCount(),NewTicksPerSecond=GetTickCount();
unsigned long PreferredFramesPerSecond;
unsigned long DeltaTicksPerSecond;
unsigned int frames=0;
float fps=60.0f;
float Refesh;
float InverseFramesPerSecond;
float OneFramePerSecond;
int keypress=0;
t_ball ball;
t_pad PlayersPad,computersPad;
int autoPlay,playersScore,computersScore;
void moveBall();
void runGame();
void initGame();
void Keypressed();
void gameLogic();
void removeBall();
void displayHelp();
void displayYouMissed();
void displayCheatEnabled();
void Render_Game_At_60_Frames_Per_Second();
void getKey();
void gotoxy(int x, int y);
void setcolor(WORD color);
void textColor(unsigned char fColor,unsigned char bColor);
void clrscr();
void delay(int milliseconds);
void txtPlot( unsigned char x, unsigned char y, unsigned char Color);
void txtLine( int xx1, int yy1, int xx2, int yy2 ,int color);
void clrbox(unsigned char x1,unsigned char y1,unsigned char x2,unsigned char y2,unsigned char bkcol);
void box(unsigned x,unsigned y,unsigned sx,unsigned sy,unsigned char col,unsigned char col2,char text_[]);
void putbox(unsigned x,unsigned y,unsigned sx,unsigned sy,
unsigned char col, unsigned char col2,unsigned char bkcol,char text_[]);
int main()
{
runGame();
return 0;
}
/*********************************
* runGame()
********************************/
void runGame()
{
initGame();
while (keypress !=key_ESCAPE)
{
Keypressed();
gameLogic();
moveBall();
Sleep( 50 );
removeBall();
setcolor(31);gotoxy(7,22);printf("ball X=%d,Y=%d ",ball.x,ball.y );setcolor(15);
setcolor(31);gotoxy(7,23);printf("pad X=%d,Y=%d ",PlayersPad.x,PlayersPad.y );setcolor(15);
setcolor(31);gotoxy(32,22);printf("Your Score: %d ", playersScore );setcolor(15);
setcolor(79);gotoxy(32,23);printf("Computers Score: %d ", computersScore );setcolor(15);
}
}
/*********************************
* moveBall()
********************************/
void moveBall()
{
txtPlot( ball.x,ball.y,15);
txtLine(PlayersPad.x,PlayersPad.y,PlayersPad.x,PlayersPad.y+3,15);
txtLine(computersPad.x,computersPad.y,computersPad.x,computersPad.y+3,15);
}
/*********************************
* removeBall()
********************************/
void removeBall()
{
//Sleep( 9 );
txtPlot( ball.x,ball.y,0);
txtLine(PlayersPad.x,PlayersPad.y,PlayersPad.x,PlayersPad.y+3,0);
txtLine(computersPad.x,computersPad.y,computersPad.x,computersPad.y+3,0);
}
/*********************************
* gameLogic()
********************************/
void gameLogic()
{
/* update ball's x location */
ball.x+=ball.headingX;
/* update ball's y location */
ball.y+=ball.headingY;
/* if ball at most right of screen then reverse ball's x heading */
if( (ball.x>PONG_SCREEN_RIGHT) )
{
ball.headingX=-ball.headingX;
computersScore+=10;
}
/* check if ball's location at top or bottom of screen,if true reverse ball's y heading */
if( (ball.y<PONG_SCREEN_TOP) || (ball.y>PONG_SCREEN_BOTTOM-2) ) ball.headingY=-ball.headingY;
PlayersPad.LEFT=PlayersPad.y-3;
PlayersPad.RIGHT=PlayersPad.y+5;
/* check if ball lands on pad, if true bounce back */
if ( (ball.y>= PlayersPad.LEFT) && (ball.y<= PlayersPad.RIGHT) && (ball.x==PlayersPad.x))
{
ball.headingX=-ball.headingX;
playersScore+=10;
}
/* let computer track ball's movement */
if (ball.x>PONG_SCREEN_RIGHT-18) computersPad.y=ball.y;
/* if cheat enabled,let player track ball's movement */
if (autoPlay>0)
{
if (ball.x<PONG_SCREEN_RIGHT-50) PlayersPad.y=ball.y;
if (ball.x>PONG_SCREEN_LEFT+16)
{
if (PlayersPad.y<10) while (PlayersPad.y<10) PlayersPad.y++;
if (PlayersPad.y>12) while (PlayersPad.y>10) PlayersPad.y--;
}
}
/* check if ball misses pad, if true display you missed */
if (ball.x<PONG_SCREEN_LEFT)
{
displayYouMissed();
ball.x=75;
ball.y=15;
computersScore+=10;
}
}
/*********************************
* initGame()
********************************/
void initGame()
{
playersScore=0;
computersScore=0;
ball.x=15;
ball.y=5;
ball.headingX=1;
ball.headingY=1;
PlayersPad.x=5;
PlayersPad.y=12;
computersPad.x=75;
computersPad.y=12;
displayCheatEnabled();
setcolor(15);
clrscr();
txtLine(6,0,74,0,1);
txtLine(6,22,74,22,1);
txtLine(6,23,74,23,1);
autoPlay=1;
if (autoPlay>0)
{
setcolor(79);gotoxy(62,23);printf("cheat on " );setcolor(15);
}
else
{
setcolor(79);gotoxy(62,23);printf("cheat off " );setcolor(15);
}
setcolor(31);gotoxy(62,22);printf("<F1> Help" );setcolor(15);
}
void displayHelp()
{
clrbox(10,8,70,16,79);
box(10,8,70,16,31,79,"Pong Game, Player vs Computer");
gotoxy(18,10);cprintf("Controls ");
gotoxy(18,11);cprintf("<LEFT> move pad left");
gotoxy(18,12);cprintf("<RIGHT> move pad right");
gotoxy(18,13);cprintf("<TAB> cheat mode ON/OFF");
gotoxy(18,15);cprintf("press any key to continue");
keypress =getch();
clrbox(10,8,75,21,0);
}
void displayYouMissed()
{
clrbox(10,8,70,16,79);
box(10,8,70,16,31,79,"You missed");
gotoxy(18,10);cprintf(" ");
gotoxy(18,11);cprintf("The ball has missed the paddle");
gotoxy(18,12);cprintf("press press space to continue");
gotoxy(18,13);cprintf("");
gotoxy(18,14);cprintf("");
keypress =getch();
clrbox(10,8,75,21,0);
}
void displayCheatEnabled()
{
clrbox(10,8,70,16,79);
box(10,8,70,16,31,79,"Player vs. Computer");
gotoxy(15,10);cprintf("The Cheat' pad will track ball movement' is enabled");
gotoxy(15,11);cprintf("press <TAB> to enable or disable the cheat.");
gotoxy(15,13);cprintf("press press space to continue");
gotoxy(18,14);cprintf("");
gotoxy(18,14);cprintf("");
keypress =getch();
clrbox(10,8,75,21,0);
}
/*********************************
* Keypressed()
********************************/
void Keypressed()
{
if (kbhit())
{
keypress =getch();
switch (keypress )
{
case key_F1:
displayHelp();
break;
case key_LEFT:
case key_RIGHT:
break;
case key_UP:
PlayersPad.y-=3;if (PlayersPad.y<0) PlayersPad.y=0;
break;
case key_DOWN:
PlayersPad.y+=3;if (PlayersPad.y>18) PlayersPad.y=18;
break;
case key_ENTER:
break;
case key_SPACE:
break;
case key_TAB:
autoPlay=-autoPlay;
if (autoPlay>0)
{
setcolor(79);gotoxy(62,23);printf("cheat on " );setcolor(15);
}
else
{
setcolor(79);gotoxy(62,23);printf("cheat off " );setcolor(15);
}
break;
}
}
}
/*********************************
* Render_Game_At_60_Frames_Per_Second()
********************************/
void Render_Game_At_60_Frames_Per_Second()
{
NewTicksPerSecond=GetTickCount();
DeltaTicksPerSecond=NewTicksPerSecond-OldTicksPerSecond;
frames++;
if(DeltaTicksPerSecond>=PreferredFramesPerSecond)
{
OldTicksPerSecond=NewTicksPerSecond;
InverseFramesPerSecond=1/((float)PreferredFramesPerSecond/1000.0f);
OneFramePerSecond=(float)frames*InverseFramesPerSecond;
fps+=OneFramePerSecond;
fps/=2;
setcolor(31);gotoxy(70,22);printf(" %d FPS ",DeltaTicksPerSecond );setcolor(15);
frames=0;
Refesh=60/fps;
}
Sleep(3);
}
//*****************************************************************************
//* *
//*****************************************************************************
void gotoxy(int x, int y)
{
COORD coord;
coord.X = x; coord.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
return;
}
//*****************************************************************************
//* *
//*****************************************************************************
void setcolor(WORD color)
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),color);
return;
}
//
// colors:
// 0 = Black
// 1 = Blue
// 2 = Green
// 3 = Cyan
// 4 = Red
// 5 = Magenta
// 6 = Yellow
// 7 = LightGray
// 8 = DarkGray
// 9 = LightBlue
// 10 = LightGreen
// 11 = LightCyan
// 12 = LightRed
// 13 = LightMagenta
// 14 = LightYellow
// 15 = White
//
void setForeGroundAndBackGroundColor(int ForeGroundColor,int BackGroundColor)
{
int color=16*BackGroundColor+ForeGroundColor;
setcolor(color);
}
//*****************************************************************************
//* *
//*****************************************************************************
void clrscr()
{
COORD coordScreen = { 0, 0 };
DWORD cCharsWritten;
CONSOLE_SCREEN_BUFFER_INFO csbi;
DWORD dwConSize;
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
GetConsoleScreenBufferInfo(hConsole, &csbi);
dwConSize = csbi.dwSize.X * csbi.dwSize.Y;
FillConsoleOutputCharacter(hConsole, TEXT(' '), dwConSize, coordScreen, &cCharsWritten);
GetConsoleScreenBufferInfo(hConsole, &csbi);
FillConsoleOutputAttribute(hConsole, csbi.wAttributes, dwConSize, coordScreen, &cCharsWritten);
SetConsoleCursorPosition(hConsole, coordScreen);
return;
}
//*****************************************************************************
//* *
//*****************************************************************************
void box(unsigned x,unsigned y,unsigned sx,unsigned sy,unsigned char col,unsigned char col2,char text_[])
{ unsigned i,j,m;
{
m=(sx-x); //differential
j=m/8; //adjust
j=j-1; //more adjustment
gotoxy(x,y);cprintf("É"); //Top left corner of box
gotoxy(sx,y);cprintf("»"); //Top right corner of box
gotoxy(x,sy);cprintf("È"); //Bottom left corner of box
gotoxy(sx,sy);cprintf("¼"); //Bottom right corner of box
for (i=x+1;i<sx;i++)
{
gotoxy(i,y);cprintf("Í"); // Top horizontol line
gotoxy(i,sy);cprintf("Í"); // Bottom Horizontal line
}
for (i=y+1;i<sy;i++)
{
gotoxy(x,i);cprintf("º"); //Left Vertical line
gotoxy(sx,i);cprintf("º"); //Right Vertical Line
}
gotoxy(x+j,y);cprintf(text_); //put Title
gotoxy(1,24);
}
}
//*****************************************************************************
//* *
//*****************************************************************************
void clrbox(unsigned char x1,unsigned char y1,unsigned char x2,unsigned char y2,unsigned char bkcol)
{
int x,y;
setcolor(bkcol); //Set to color bkcol
for (y=y1;y<y2;y++) //Fill Y Region Loop
{
for (x=x1;x<x2;x++) //Fill X region Loop
{
gotoxy(x,y);cprintf(" "); //Draw Solid space
}
}
}
//*****************************************************************************
//* *
//*****************************************************************************
void putbox(unsigned x,unsigned y,unsigned sx,unsigned sy,
unsigned char col, unsigned char col2,unsigned char bkcol,char text_[])
{
clrbox(x,y,sx,sy,bkcol);
box(x,y,sx,sy,col,col2,text_);
}
//*****************************************************************************
//* *
//*****************************************************************************
void txtPlot( unsigned char x, unsigned char y, unsigned char Color)
{
setcolor(Color);
gotoxy(x,y);cprintf("Û");
}
//**********************************************
//** **
//**********************************************
void txtSwap(int first, int second )
{
int temp ;
temp = first;
first = second;
second = temp;
}
void txtCircle(int X, int Y, int rad, int col)
{
float deg = 0;
do {
X = (int) (rad * cos(deg));
Y = (int) (rad * sin(deg));
txtPlot (40+X, 12+Y, col);
deg += 0.005;
}
while (deg <= 6.4);
}
//**********************************************
//** **
//**********************************************
void txtLine(int x0, int y0, int x1, int y1, int color)
{
int pix = color;
int dy = y1 - y0;
int dx = x1 - x0;
int stepx, stepy;
if (dy < 0) { dy = -dy; stepy = -1; } else { stepy = 1; }
if (dx < 0) { dx = -dx; stepx = -1; } else { stepx = 1; }
dy <<= 1; // dy is now 2*dy
dx <<= 1; // dx is now 2*dx
txtPlot( x0, y0,pix);
if (dx > dy) {
int fraction = dy - (dx >> 1); // same as 2*dy - dx
while (x0 != x1) {
if (fraction >= 0) {
y0 += stepy;
fraction -= dx; // same as fraction -= 2*dx
}
x0 += stepx;
fraction += dy; // same as fraction -= 2*dy
txtPlot( x0, y0,pix);
}
} else {
int fraction = dx - (dy >> 1);
while (y0 != y1) {
if (fraction >= 0) {
x0 += stepx;
fraction -= dy;
}
y0 += stepy;
fraction += dx;
txtPlot( x0, y0,pix);
}
}
}
//**********************************************
//** **
//**********************************************
void delay(unsigned int milliseconds)
{
clock_t ticks1, ticks2;
unsigned int tic1=0,tic2=0,tick=0;
ticks1=clock();
while(tick<milliseconds)
{
ticks2=clock();
tic1=ticks2/CLOCKS_PER_SEC-ticks1;
tic2=ticks1/CLOCKS_PER_SEC;
tick=ticks2-ticks1;
}
ticks2=clock();
}
关于用于在 Windows 控制台中运行的游戏的 C++ 控件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20019945/
为什么我可以在控制台 window.window.window.window 中执行此操作并无限追加 .window 并返回 DOM 窗口? 最佳答案 因为 window 对象有一个指向它自身的 wi
Windows管理员用户和系统用户之间有什么权限区别吗? 有些时候,我必须将 cmd 窗口提升到系统权限才能删除一些文件。这可能是因为系统用户锁定了文件,或者系统用户可能具有更高的访问权限,我希望找出
按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
Windows 服务和 Windows 进程之间的区别是什么? 最佳答案 服务是真正的 Windows 进程,没有区别。服务的唯一特殊之处在于它由操作系统启动并在单独的 session 中运行。一个独
我有一个 Windows 网络 (peer-2-peer) 以及 Active Directory,我需要记录向服务器发送任何类型打印的用户的名称。我想编写一个程序来记录他们的用户名和/或他们各自的
当我让一个 Windows 服务尝试安装另一个 Windows 服务时遇到问题。 具体来说,我有一个 TeamCity 代理在 Windows 2008 AWS 实例上为我运行测试。这些测试是用 Ja
我创建了一个应用程序来接收广播的 Windows 消息,效果很好。当我把它变成一个服务、安装它并启动服务时,该服务没有收到消息。 最佳答案 服务可能必须被授予访问桌面的权限。从服务属性、“登录”选项卡
我正在使用 Delphi 2010 编写应用程序。我希望在 Windows 启动时启动我的应用程序。我需要它在最新版本的 Windows XP、7.0 和最新的服务器中工作。 将其存储在以下关键工作下
我想开发一个适用于所有三个版本的 Windows XP、Vista 和 7 的应用程序。该应用程序允许人们选择要打开的文件,并允许他们在某些操作后保存文件。三个版本的 Windows 中的每一个都有不
对于\Windows\中的文件类型与\Windows\System32 中的文件类型是否有标准约定? 我正在开发一个 SDK,其中包含各种 DLL、帮助程序 exe 和 Windows 服务 exe。
要求是,必须在 WINDOWS7 机器上配置自动登录,但是这个自动登录应该等待(即延迟)直到另一个 Windows 服务发出继续自动登录的信号。 我使用了自定义凭据提供程序,它在其中等待另一个 Win
很抱歉,这不是一个大问题,而是更多的帮助人们解决这些特定问题的方法。我正在解决的问题要求使用串行I/O,但主要在Windows CE 6.0下运行。但是,最近有人问我是否也可以在Windows下运行该
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 这个问题似乎不是关于 a specific programming problem, a softwar
不幸的是 SC 命令在 W2000 上还不可用,所以我不能使用它。 我正在尝试检查服务是否在 W2000 服务器上运行,如果它没有运行,脚本应该能够启动该服务。 如何在 Windows 2000 上执
如何在登录到 Windows 之前启动 Windows 窗体应用程序?是否可以在登录到 Windows 之前启动 Windows 窗体应用程序?如果不是,我是否有机会在登录前启动 Windows 服务
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 这个问题似乎不是关于 a specific programming problem, a softwar
我想在 XML 文件中区分 Windows XP 和 Windows 7。我想我会在 XML 中为它使用一个环境变量。 但是我找不到在 Windows 中定义的任何系统环境变量来提供此信息。 我看到了
有谁知道我可以在注册表中的哪个位置检查机器上是否安装了这些应用程序: Windows 通讯录 Windows 联系人 最佳答案 来自 Microsoft:我知道它说的是 win 95,但 reg 是一
我正在尝试从我的 Windows 服务器调用放置在远程 Windows 服务器上的批处理文件。我在远程服务器上安装了 freeSSHd。我尝试使用 putty/plink 但没有结果。 我使用的命令语
( 大家好。我是 Windows 编程的新手,所以如果已经有人问过我,我提前道歉,我只是不知道要搜索什么,但这个问题一直让我发疯,我知道有人可能真的很容易回答这个问题。) 我的公司有一个在 Windo
我是一名优秀的程序员,十分优秀!