c语言游戏问题

Seffrui_M 2012-12-19 11:43:55
看了一些小游戏的代码,对于键盘控制的上下左右,键盘控制,鼠标控制都不是学校讲的,书里也没有,请问怎么才可用c语言编写一些小程序,现在那些库函数我也知道几个 ,怎么可以看到所有库函数,还有请说一下上下左右的键盘控制函数 最好有实例哈
...全文
384 13 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
ranxufeng 2012-12-21
  • 打赏
  • 举报
回复
引用 6 楼 zhao4zhong1 的回复:
C/C++ code ? 1234567891011121314151617 #include <conio.h> #include <windows.h> int main() { int k; while (1) { if (kbhit()) { k=getch(); if (0==k |……
如果 不要按Esc退出呢? Esc 27==k 27表示Esc? 别的键要怎么弄呢 ?? 求解答
ranxufeng 2012-12-21
  • 打赏
  • 举报
回复
引用 7 楼 zhao4zhong1 的回复:
上帖代码应改为: C/C++ code ? 1234567891011121314151617 #include <conio.h> #include <windows.h> int main() { int k; while (1) { if (kbhit()) { k=getch(); ……
问问大神! cprintf();功能是什么?? if (0==k || 0xE0==k) 中0xE0是什么意思?? 真心不懂 我初学者
赵4老师 2012-12-21
  • 打赏
  • 举报
回复
查MSDN是Windows程序员必须掌握的技能之一。 mk:@MSITStore:C:\MSDN98\98VS\2052\vccore.chm::/html/_crt__getch.2c_._getche.htm _getch, _getche Get a character from the console without echo (_getch) or with echo (_getche). int _getch( void ); int _getche( void ); Routine Required Header Compatibility _getch <conio.h> Win 95, Win NT _getche <conio.h> Win 95, Win NT For additional compatibility information, see Compatibility in the Introduction. Libraries LIBC.LIB Single thread static library, retail version LIBCMT.LIB Multithread static library, retail version MSVCRT.LIB Import library for MSVCRT.DLL, retail version Return Value Both _getch and _getche return the character read. There is no error return. Remarks The _getch function reads a single character from the console without echoing. _getche reads a single character from the console and echoes the character read. Neither function can be used to read CTRL+C. When reading a function key or an arrow key, _getch and _getche must be called twice; the first call returns 0 or 0xE0, and the second call returns the actual key code. Example /* GETCH.C: This program reads characters from * the keyboard until it receives a 'Y' or 'y'. */ #include <conio.h> #include <ctype.h> void main( void ) { int ch; _cputs( "Type 'Y' when finished typing keys: " ); do { ch = _getch(); ch = toupper( ch ); } while( ch != 'Y' ); _putch( ch ); _putch( '\r' ); /* Carriage return */ _putch( '\n' ); /* Line feed */ } Output Type 'Y' when finished typing keys: Y Console and Port I/O Routines See Also _cgets, getc, _ungetch mk:@MSITStore:C:\MSDN98\98VS\2052\vccore.chm::/html/_langref_key_scan_codes.htm Key Scan Codes
  • 打赏
  • 举报
回复


#include <windows.h>
#include <iostream>
using namespace std;
 
HWND g_hWnd = NULL; 
HINSTANCE g_hInst;
volatile bool   bCanExit    =   false;
 
#pragma comment(linker, "/subsystem:\"console\" /entry:\"main\"")
 
LRESULT WndProc(HWND hWnd,UINT wMsg,WPARAM wParam,LPARAM lParam) {
    switch(wMsg){
    case    WM_CLOSE:
        bCanExit    =   true;
        PostQuitMessage(0);
        return (0);
 
    default:
        return  ::DefWindowProcA(hWnd, wMsg, wParam, lParam);
    }
}
 
void CreateWnd(void) {    
    WNDCLASS wc = {0}; 
    wc.style         = 0; 
    wc.lpfnWndProc   = (WNDPROC)WndProc; 
    wc.cbClsExtra    = 0; 
    wc.cbWndExtra    = 0; 
    wc.hInstance     = g_hInst; 
    wc.hIcon         = NULL;
 
    wc.hCursor       = LoadCursor(NULL, IDC_ARROW); 
    wc.hbrBackground = (HBRUSH)GetSysColorBrush(COLOR_GRAYTEXT); 
    wc.lpszMenuName  = NULL; 
    wc.lpszClassName = TEXT("SimpleWindow");
    RegisterClass(&wc);
    g_hWnd = CreateWindowEx(0, 
        TEXT("SimpleWindow"), 
        TEXT("SimpleWindow"), 
        WS_OVERLAPPEDWINDOW|WS_VISIBLE, 
        CW_USEDEFAULT, 
        CW_USEDEFAULT, 
        CW_USEDEFAULT, 
        CW_USEDEFAULT, 
        NULL,  
        NULL,  
        g_hInst,  
        0); 
}
 
 
DWORD MyThread(PVOID pArg) { 
    CreateWnd();   
    MSG msg; 
    while(GetMessage(&msg,NULL,0,0)) { 
        TranslateMessage(&msg); 
        DispatchMessage(&msg); 
    }
    return 0; 
}
 
 
int main(){ 
    g_hInst = GetModuleHandle(NULL);
    HANDLE hThrd = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)MyThread, NULL, 0, NULL);
    CloseHandle(hThrd);
 
    while(!bCanExit){
        Sleep(10);
    }
    cout<<"windows has closed"<<endl;
    getchar();
    return 0; 
}

  • 打赏
  • 举报
回复
学windows编程吧,少年,支持你.
Kuovane 2012-12-20
  • 打赏
  • 举报
回复
LZ要入门游戏,可先不必学opengl,这个opengl挺复杂的,要懂得里面的原理需要数学,物理等知识,一开始弄这个不太好,个人觉得,会让人摸不着头脑。 下面提供一个基于windows的游戏路线: 1,在屏幕画个图 2,用按键控制这个图移动 3,自己写个简单游戏 4,看别人的代码 5,再写个稍微复杂的游戏 6,看开源的游戏引擎及里面的demo 7,根据前面的基础估计此时的你能清楚的思考自己要学什么 游戏领域用的技术很多,可以往移动领域发展,这个是新型行业,未来几年需求量较大。 至于实例,LZ自己得学会怎么找资源,别用依赖别人的想法,自己才是最可靠的。
夏梦c 2012-12-20
  • 打赏
  • 举报
回复
自己写接收按键的函数
Kuovane 2012-12-20
  • 打赏
  • 举报
回复
目标已给出了,为这个目标找资源呗,别人不可能手把手教你
引用 8 楼 Seffrui_M 的回复:
引用 5 楼 lansong4 的回复:LZ要入门游戏,可先不必学opengl,这个opengl挺复杂的,要懂得里面的原理需要数学,物理等知识,一开始弄这个不太好,个人觉得,会让人摸不着头脑。 下面提供一个基于windows的游戏路线: 1,在屏幕画个图 2,用按键控制这个图移动 3,自己写个简单游戏 4,看别人的代码 5,再写个稍微复杂的游戏 6,看开……
Seffrui_M 2012-12-20
  • 打赏
  • 举报
回复
引用 7 楼 zhao4zhong1 的回复:
上帖代码应改为: C/C++ code?1234567891011121314151617#include <conio.h>#include <windows.h>int main() { int k; while (1) { if (kbhit()) { k=getch(); if (0==k ……
windows函数有什么用 请解释一下 赵大师
Seffrui_M 2012-12-20
  • 打赏
  • 举报
回复
引用 5 楼 lansong4 的回复:
LZ要入门游戏,可先不必学opengl,这个opengl挺复杂的,要懂得里面的原理需要数学,物理等知识,一开始弄这个不太好,个人觉得,会让人摸不着头脑。 下面提供一个基于windows的游戏路线: 1,在屏幕画个图 2,用按键控制这个图移动 3,自己写个简单游戏 4,看别人的代码 5,再写个稍微复杂的游戏 6,看开源的游戏引擎及里面的demo 7,根据……
我该怎么学习呢
赵4老师 2012-12-20
  • 打赏
  • 举报
回复
上帖代码应改为:
#include <conio.h>
#include <windows.h>
int main() {
    int k;

    while (1) {
        if (kbhit()) {
            k=getch();
            if (0==k || 0xE0==k) k=k<<8|getch();
            if (27==k) break;//按Esc键退出
            cprintf("\r\n%04X\r\n",k);
        }
        Sleep(200);
        cprintf(".");
    }
    return 0;
}
赵4老师 2012-12-20
  • 打赏
  • 举报
回复
#include <conio.h>
#include <windows.h>
int main() {
    int k;

    while (1) {
        if (kbhit()) {
            k=getch();
            if (0==k || 0xE0==k) getch();
            if (27==k) break;//按Esc键退出
            cprintf("\r\n%04X\r\n",k);
        }
        Sleep(200);
        cprintf(".");
    }
    return 0;
}
运行时按上下左右键试试看。
Proteas 2012-12-19
  • 打赏
  • 举报
回复
C只是一个语言,编写游戏还依赖于平台。 觉得你可以试着学学 OpenGL,然后在编游戏。

33,321

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧