当前位置:首页 » 编程语言 » 一个游戏解说带你了解c语言
扩展阅读
webinf下怎么引入js 2023-08-31 21:54:13
堡垒机怎么打开web 2023-08-31 21:54:11

一个游戏解说带你了解c语言

发布时间: 2023-02-02 00:26:56

Ⅰ 用c语言编写一个游戏 游戏规则用户选择出拳的数字(1、石头 2、剪刀 3、布), 而计算机随机

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
intmain()
{

intplayer;
intcomputer;
intplayerWin=0,computerWin=0,balance=0;
scanf_s("%d",&player);
while(player!=0)
{
srand(time(0));
computer=rand()%3+1;
if(computer==player)
{
printf("%s ","平局");
balance++;
}
elseif((player==1&&computer==2)||(player==2&&computer==3)||(player==3&&computer==1))
{
printf("%s ","玩家胜利");
playerWin++;
}
else
{
printf("%s ","电脑胜利");
computerWin++;
}
scanf_s("%d",&player);
}
printf("玩家胜利%d次,电脑胜利%d次,平局%d次 ",playerWin,computerWin,balance);
return0;
}

Ⅱ 如何用C++编写一个小游戏

这类游戏,你可以去图书馆借一本 ”C语言课程设计”

这本书真的认认真真学完,敲出每一行代码,你的面向过程编程能力,基本可以放心了。

有什么代码细节搞不定,上 CSDN, 博客园,overflow 去问,去发帖问。

Ⅲ 求几C语言个小游戏代码,简单的,要注释、、谢谢了、

// Calcu24.cpp : Defines the entry point for the console application.
//
/*
6-6
24点游戏
*/
#include "conio.h"
#include "stdlib.h"
#include "time.h"
#include "math.h"
#include "string.h"/*
从一副扑克牌中,任取4张。
2-10 按其点数计算(为了表示方便10用T表示),J,Q,K,A 统一按 1 计算
要求通过加减乘除四则运算得到数字 24。
本程序可以随机抽取纸牌,并用试探法求解。
*/void GivePuzzle(char* buf)
{
char card[] = {'A','2','3','4','5','6','7','8','9','T','J','Q','K'}; for(int i=0; i<4; i++){
buf[i] = card[rand() % 13];
}
}
void shuffle(char * buf)
{
for(int i=0; i<5; i++){
int k = rand() % 4;
char t = buf[k];
buf[k] = buf[0];
buf[0] = t;
}
}
int GetCardValue(int c)
{
if(c=='T') return 10;
if(c>='0' && c<='9') return c - '0';
return 1;
}
char GetOper(int n)
{
switch(n)
{
case 0:
return '+';
case 1:
return '-';
case 2:
return '*';
case 3:
return '/';
} return ' ';
}double MyCalcu(double op1, double op2, int oper)
{
switch(oper)
{
case 0:
return op1 + op2;
case 1:
return op1 - op2;
case 2:
return op1 * op2;
case 3:
if(fabs(op2)>0.0001)
return op1 / op2;
else
return 100000;
} return 0;
}
void MakeAnswer(char* answer, int type, char* question, int* oper)
{
char p[4][3];
for(int i=0; i<4; i++)
{
if( question[i] == 'T' )
strcpy(p[i], "10");
else
sprintf(p[i], "%c", question[i]);
}

switch(type)
{
case 0:
sprintf(answer, "%s %c (%s %c (%s %c %s))",
p[0], GetOper(oper[0]), p[1], GetOper(oper[1]), p[2], GetOper(oper[2]), p[3]);
break;
case 1:
sprintf(answer, "%s %c ((%s %c %s) %c %s)",
p[0], GetOper(oper[0]), p[1], GetOper(oper[1]), p[2], GetOper(oper[2]), p[3]);
break;
case 2:
sprintf(answer, "(%s %c %s) %c (%s %c %s)",
p[0], GetOper(oper[0]), p[1], GetOper(oper[1]), p[2], GetOper(oper[2]), p[3]);
break;
case 3:
sprintf(answer, "((%s %c %s) %c %s) %c %s",
p[0], GetOper(oper[0]), p[1], GetOper(oper[1]), p[2], GetOper(oper[2]), p[3]);
break;
case 4:
sprintf(answer, "(%s %c (%s %c %s)) %c %s",
p[0], GetOper(oper[0]), p[1], GetOper(oper[1]), p[2], GetOper(oper[2]), p[3]);
break;
}
}
bool TestResolve(char* question, int* oper, char* answer)
{
// 等待考生完成
int type[5]={0,1,2,3,4};//计算类型
double p[4];
double sum=0;
//
for(int i=0; i<4; i++) //循环取得点数
{
p[i]=GetCardValue(int(question[i]));
} for(i=0;i<5;i++)
{
MakeAnswer(answer,type[i],question,oper); //获取可能的答案
switch(type[i])
{
case 0:
sum=MyCalcu(p[0],MyCalcu( p[1],MyCalcu(p[2], p[3], oper[2]),oper[1]),oper[0]); //A*(B*(c*D))
break;
case 1:
sum=MyCalcu(p[0],MyCalcu(MyCalcu(p[1], p[2], oper[1]),p[3],oper[2]),oper[0]); //A*((B*C)*D)
break;
case 2:
sum=MyCalcu(MyCalcu(p[0], p[1], oper[0]),MyCalcu(p[2], p[3], oper[2]),oper[1]); // (A*B)*(C*D)
break;
case 3:
sum=MyCalcu(MyCalcu(MyCalcu(p[0], p[1], oper[0]),p[2],oper[1]),p[3],oper[2]); //((A*B)*C)*D
break;
case 4:
sum=MyCalcu(MyCalcu(p[0],MyCalcu(p[1], p[2], oper[1]),oper[0]),p[3],oper[2]); //(A*(B*C))*D
break;
}
if(sum==24) return true;
}
return false;
}
/*
采用随机试探法:就是通过随机数字产生 加减乘除的 组合,通过大量的测试来命中的解法
提示:
1. 需要考虑用括号控制计算次序的问题 比如:( 10 - 4 ) * ( 3 + A ), 实际上计算次序的数目是有限的:
A*(B*(c*D))
A*((B*C)*D)
(A*B)*(C*D)
((A*B)*C)*D
(A*(B*C))*D
2. 需要考虑计算结果为分数的情况:( 3 + (3 / 7) ) * 7
3. 题目中牌的位置可以任意交换
*/
bool TryResolve(char* question, char* answer)
{
int oper[3]; // 存储运算符,0:加法 1:减法 2:乘法 3:除法
for(int i=0; i<1000 * 1000; i++)
{
// 打乱纸牌顺序
shuffle(question);

// 随机产生运算符
for(int j=0; j<3; j++)
oper[j] = rand() % 4; if( TestResolve(question, oper, answer) ) return true;
} return false;
}
int main(int argc, char* argv[])
{
// 初始化随机种子
srand( (unsigned)time( NULL ) ); char buf1[4]; // 题目
char buf2[30]; // 解答
printf("***************************\n");
printf("计算24\n");
printf("A J Q K 均按1计算,其它按牌点计算\n");
printf("目标是:通过四则运算组合出结果:24\n");
printf("***************************\n\n");
for(;;)
{
GivePuzzle(buf1); // 出题
printf("题目:");
for(int j=0; j<4; j++){
if( buf1[j] == 'T' )
printf("10 ");
else
printf("%c ", buf1[j]);
} printf("\n按任意键参考答案...\n");
getch(); if( TryResolve(buf1, buf2) ) // 解题
printf("参考:%s\n", buf2);
else
printf("可能是无解...\n"); printf("按任意键出下一题目,x 键退出...\n");
if( getch() == 'x' ) break;
} return 0;
}

Ⅳ 怎样用C语言编写一个小游戏

“贪吃蛇”C代码:

#include <stdio.h>

#include <stdlib.h>

#include <conio.h>

#include <time.h>

#include <Windows.h>

#define W 78 //游戏框的宽,x轴

#define H 26 //游戏框的高,y轴

int dir=3; //方向变量,初值3表示向“左”

int Flag=0; //吃了食物的标志(1是0否)

int score=0; //玩家得分

struct food{ int x; //食物的x坐标

int y; //食物的y坐标

}fod; //结构体fod有2个成员

struct snake{ int len; //身长

int speed; //速度

int x[100];

int y[100];

}snk; //结构体snk有4个成员

void gtxy( int x,int y) //控制光标移动的函数

{ COORD coord;

coord.X=x;

coord.Y=y;

SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);

}

void gtxy( int x,int y); //以下声明要用到的几个自编函数

void csh( ); //初始化界面

void keymove( ); //按键操作移动蛇

void putFod( ); //投放食物

int Over( ); //游戏结束(1是0否)

void setColor(unsigned short p, unsigned short q); //设定显示颜色

int main( ) //主函数

{ csh( );

while(1)

{ Sleep(snk.speed);

keymove( );

putFod( );

if(Over( ))

{system(“cls”);

gtxy(W/2+1,H/2); printf(“游戏结束!T__T”);

gtxy(W/2+1,H/2+2); printf(“玩家总分:%d分”,score);

getch( );

break;

}

}

return 0;

}

void csh( ) //初始化界面

{ int i;

gtxy(0,0);

CONSOLE_CURSOR_INFO cursor_info={1,0}; //以下两行是隐藏光标的设置

SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&cursor_info);

for(i=0;i<=W;i=i+2) //横坐标要为偶数,因为这个要打印的字符占2个位置

{ setColor(2, 0); //设定打印颜色为绿字黑底

gtxy(i,0); printf("■"); //打印上边框

gtxy(i,H); printf("■"); //打印下边框

}

for(i=1;i<H;i++)

{ gtxy(0,i); printf("■"); //打印左边框

gtxy(W,i); printf("■"); //打印右边框

}

while(1)

{ srand((unsigned)time(NULL)); //初始化随机数发生器srand( )

fod.x=rand()%(W-4)+2; //随机函数rand( )产生一个从0到比”(W-4)”小1的数再加2

fod.y=rand()%(H-2)+1; //随机函数rand( )产生一个从0到比”(H-2)”小1的数再加1

if (fod.x%2==0) break; //fod.x是食物的横坐标,要是2的倍数(为偶数)

}

setColor(12, 0); //设定打印颜色为淡红字黑底

gtxy(fod.x,fod.y); printf("●"); //到食物坐标处打印初试食物

snk.len=3; //蛇身长

snk.speed=350; //刷新蛇的时间,即是移动速度

snk.x[0]=W/2+1; //蛇头横坐标要为偶数(因为W/2=39)

snk.y[0]=H/2; //蛇头纵坐标

setColor(9, 0); //设定打印颜色为淡蓝字黑底

gtxy(snk.x[0], snk.y[0]); printf("■"); //打印蛇头

for(i=1;i<snk.len;i++)

{ snk.x[i]=snk.x[i-1]+2; snk.y[i]=snk.y[i-1];

gtxy(snk.x[i],snk.y[i]); printf("■"); //打印蛇身

}

setColor(7, 0); //恢复默认的白字黑底

return;

}

void keymove( ) //按键操作移动蛇

{ int key;

if( kbhit( ) ) //如有按键输入才执行下面操作

{ key=getch( );

if (key==224) //值为224表示按下了方向键,下面要再次获取键值

{ key=getch( );

if(key==72&&dir!=2)dir=1; //72表示按下了向上方向键

if(key==80&&dir!=1)dir=2; //80为向下

if(key==75&&dir!=4)dir=3; //75为向左

if(key==77&&dir!=3)dir=4; //77为向右

}

if (key==32)

{ while(1) if((key=getch( ))==32) break; } //32为空格键,这儿用来暂停

}

if (Flag==0) //如没吃食物,才执行下面操作擦掉蛇尾

{ gtxy(snk.x[snk.len-1],snk.y[snk.len-1]); printf(" "); }

int i;

for (i = snk.len - 1; i > 0; i--) //从蛇尾起每节存储前一节坐标值(蛇头除外)

{ snk.x[i]=snk.x[i-1]; snk.y[i]=snk.y[i-1]; }

switch (dir) //判断蛇头该往哪个方向移动,并获取最新坐标值

{ case 1: snk.y[0]--; break; //dir=1要向上移动

case 2: snk.y[0]++; break; //dir=2要向下移动

case 3: snk.x[0]-=2; break; //dir=3要向左移动

case 4: snk.x[0]+=2; break; //dir=4要向右移动

}

setColor(9, 0);

gtxy(snk.x[0], snk.y[0]); printf("■"); //打印蛇头

if (snk.x[0] == fod.x && snk.y[0] == fod.y) //如吃到食物则执行以下操作

{ printf("07"); snk.len++; score += 100; snk.speed -= 5; Flag = 1; } //007是响铃

else Flag = 0; //没吃到食物Flag的值为0

if(snk.speed<150) snk.speed= snk.speed+5; //作弊码,不让速度无限加快

}

void putFod( ) //投放食物

{ if (Flag == 1) //如吃到食物才执行以下操作,生成另一个食物

{ while (1)

{ int i,n= 1;

srand((unsigned)time(NULL)); //初始化随机数发生器srand( )

fod.x = rand( ) % (W - 4) + 2; //产生在游戏框范围内的一个x坐标值

fod.y = rand( ) % (H - 2) + 1; //产生在游戏框范围内的一个y坐标值

for (i = 0; i < snk.len; i++) //随机生成的食物不能在蛇的身体上

{ if (fod.x == snk.x[i] &&fod.y == snk.y[i]) { n= 0; break;} }

if (n && fod.x % 2 == 0) break; //n不为0且横坐标为偶数,则食物坐标取值成功

}

setColor(12, 0);

gtxy(fod.x, fod.y); printf("●"); //光标到取得的坐标处打印食物

}

return;

}

int Over( ) //判断游戏是否结束的函数

{ int i;

setColor(7, 0);

gtxy(2,H+1); printf(“暂停键:space.”); //以下打印一些其它信息

gtxy(2,H+2); printf(“游戏得分:%d”,score);

if (snk.x[0] == 0 || snk.x[0] == W) return 1; //蛇头触碰左右边界

if (snk.y[0] == 0 || snk.y[0] == H) return 1; //蛇头触碰上下边界

for (i = 1; i < snk.len; i++)

{ if (snk.x[0] == snk.x[i] && snk.y[0] == snk.y[i]) return 1; } //蛇头触碰自身

return 0; //没碰到边界及自身时就返回0

}

void setColor(unsigned short ForeColor = 7, unsigned short BackGroundColor = 0)

{ HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);

SetConsoleTextAttribute( handle, ForeColor + BackGroundColor * 0x10 );

} //用来设定颜色的函数

Ⅳ 求一个用C语言编写的小游戏代码

#include <graphics.h>
#include <conio.h>
#include <time.h>

/////////////////////////////////////////////
// 定义常量、枚举量、结构体、全局变量
/////////////////////////////////////////////

#define WIDTH 10 // 游戏区宽度
#define HEIGHT 22 // 游戏区高度
#define SIZE 20 // 每个游戏区单位的实际像素

// 定义操作类型
enum CMD
{
CMD_ROTATE, // 方块旋转
CMD_LEFT, CMD_RIGHT, CMD_DOWN, // 方块左、右、下移动
CMD_SINK, // 方块沉底
CMD_QUIT // 退出游戏
};

// 定义绘制方块的方法
enum DRAW
{
SHOW, // 显示方块
HIDE, // 隐藏方块
FIX // 固定方块
};

// 定义七种俄罗斯方块
struct BLOCK
{
WORD dir[4]; // 方块的四个旋转状态
COLORREF color; // 方块的颜色
} g_Blocks[7] = { {0x0F00, 0x4444, 0x0F00, 0x4444, RED}, // I
{0x0660, 0x0660, 0x0660, 0x0660, BLUE}, // 口
{0x4460, 0x02E0, 0x0622, 0x0740, MAGENTA}, // L
{0x2260, 0x0E20, 0x0644, 0x0470, YELLOW}, // 反L
{0x0C60, 0x2640, 0x0C60, 0x2640, CYAN}, // Z
{0x0360, 0x4620, 0x0360, 0x4620, GREEN}, // 反Z
{0x4E00, 0x4C40, 0x0E40, 0x4640, BROWN}}; // T

// 定义当前方块、下一个方块的信息
struct BLOCKINFO
{
byte id; // 方块 ID
char x, y; // 方块在游戏区中的坐标
byte dir:2; // 方向
} g_CurBlock, g_NextBlock;

// 定义游戏区
BYTE g_World[WIDTH][HEIGHT] = {0};

/////////////////////////////////////////////
// 函数声明
/////////////////////////////////////////////

void Init(); // 初始化游戏
void Quit(); // 退出游戏
void NewGame(); // 开始新游戏
void GameOver(); // 结束游戏
CMD GetCmd(); // 获取控制命令
void DispatchCmd(CMD _cmd); // 分发控制命令
void NewBlock(); // 生成新的方块
bool CheckBlock(BLOCKINFO _block); // 检测指定方块是否可以放下
void DrawBlock(BLOCKINFO _block, DRAW _draw = SHOW); // 画方块
void OnRotate(); // 旋转方块
void OnLeft(); // 左移方块
void OnRight(); // 右移方块
void OnDown(); // 下移方块
void OnSink(); // 沉底方块

/////////////////////////////////////////////
// 函数定义
/////////////////////////////////////////////

// 主函数
void main()
{
Init();

CMD c;
while(true)
{
c = GetCmd();
DispatchCmd(c);

// 按退出时,显示对话框咨询用户是否退出
if (c == CMD_QUIT)
{
HWND wnd = GetHWnd();
if (MessageBox(wnd, _T("您要退出游戏吗?"), _T("提醒"), MB_OKCANCEL | MB_ICONQUESTION) == IDOK)
Quit();
}
}
}

// 初始化游戏
void Init()
{
initgraph(640, 480);
srand((unsigned)time(NULL));

// 显示操作说明
setfont(14, 0, _T("宋体"));
outtextxy(20, 330, _T("操作说明"));
outtextxy(20, 350, _T("上:旋转"));
outtextxy(20, 370, _T("左:左移"));
outtextxy(20, 390, _T("右:右移"));
outtextxy(20, 410, _T("下:下移"));
outtextxy(20, 430, _T("空格:沉底"));
outtextxy(20, 450, _T("ESC:退出"));

// 设置坐标原点
setorigin(220, 20);

// 绘制游戏区边界
rectangle(-1, -1, WIDTH * SIZE, HEIGHT * SIZE);
rectangle((WIDTH + 1) * SIZE - 1, -1, (WIDTH + 5) * SIZE, 4 * SIZE);

// 开始新游戏
NewGame();
}

// 退出游戏
void Quit()
{
closegraph();
exit(0);
}

// 开始新游戏
void NewGame()
{
// 清空游戏区
setfillstyle(BLACK);
bar(0, 0, WIDTH * SIZE - 1, HEIGHT * SIZE - 1);
ZeroMemory(g_World, WIDTH * HEIGHT);

// 生成下一个方块
g_NextBlock.id = rand() % 7;
g_NextBlock.dir = rand() % 4;
g_NextBlock.x = WIDTH + 1;
g_NextBlock.y = HEIGHT - 1;

// 获取新方块
NewBlock();
}

// 结束游戏
void GameOver()
{
HWND wnd = GetHWnd();
if (MessageBox(wnd, _T("游戏结束。\n您想重新来一局吗?"), _T("游戏结束"), MB_YESNO | MB_ICONQUESTION) == IDYES)
NewGame();
else
Quit();
}

// 获取控制命令
DWORD m_oldtime;
CMD GetCmd()
{
// 获取控制值
while(true)
{
// 如果超时,自动下落一格
DWORD newtime = GetTickCount();
if (newtime - m_oldtime >= 500)
{
m_oldtime = newtime;
return CMD_DOWN;
}

// 如果有按键,返回按键对应的功能
if (kbhit())
{
switch(getch())
{
case 'w':
case 'W': return CMD_ROTATE;
case 'a':
case 'A': return CMD_LEFT;
case 'd':
case 'D': return CMD_RIGHT;
case 's':
case 'S': return CMD_DOWN;
case 27: return CMD_QUIT;
case ' ': return CMD_SINK;
case 0:
case 0xE0:
switch(getch())
{
case 72: return CMD_ROTATE;
case 75: return CMD_LEFT;
case 77: return CMD_RIGHT;
case 80: return CMD_DOWN;
}
}
}

// 延时 (降低 CPU 占用率)
Sleep(20);
}
}

// 分发控制命令
void DispatchCmd(CMD _cmd)
{
switch(_cmd)
{
case CMD_ROTATE: OnRotate(); break;
case CMD_LEFT: OnLeft(); break;
case CMD_RIGHT: OnRight(); break;
case CMD_DOWN: OnDown(); break;
case CMD_SINK: OnSink(); break;
case CMD_QUIT: break;
}
}

// 生成新的方块
void NewBlock()
{
g_CurBlock.id = g_NextBlock.id, g_NextBlock.id = rand() % 7;
g_CurBlock.dir = g_NextBlock.dir, g_NextBlock.dir = rand() % 4;
g_CurBlock.x = (WIDTH - 4) / 2;
g_CurBlock.y = HEIGHT + 2;

// 下移新方块直到有局部显示
WORD c = g_Blocks[g_CurBlock.id].dir[g_CurBlock.dir];
while((c & 0xF) == 0)
{
g_CurBlock.y--;
c >>= 4;
}

// 绘制新方块
DrawBlock(g_CurBlock);

// 绘制下一个方块
setfillstyle(BLACK);
bar((WIDTH + 1) * SIZE, 0, (WIDTH + 5) * SIZE - 1, 4 * SIZE - 1);
DrawBlock(g_NextBlock);

// 设置计时器,用于判断自动下落
m_oldtime = GetTickCount();
}

// 画方块
void DrawBlock(BLOCKINFO _block, DRAW _draw)
{
WORD b = g_Blocks[_block.id].dir[_block.dir];
int x, y;

int color = BLACK;
switch(_draw)
{
case SHOW: color = g_Blocks[_block.id].color; break;
case HIDE: color = BLACK; break;
case FIX: color = g_Blocks[_block.id].color / 3; break;
}
setfillstyle(color);

for(int i=0; i<16; i++)
{
if (b & 0x8000)
{
x = _block.x + i % 4;
y = _block.y - i / 4;
if (y < HEIGHT)
{
if (_draw != HIDE)
bar3d(x * SIZE + 2, (HEIGHT - y - 1) * SIZE + 2, (x + 1) * SIZE - 4, (HEIGHT - y) * SIZE - 4, 3, true);
else
bar(x * SIZE, (HEIGHT - y - 1) * SIZE, (x + 1) * SIZE - 1, (HEIGHT - y) * SIZE - 1);
}
}
b <<= 1;
}
}

// 检测指定方块是否可以放下
bool CheckBlock(BLOCKINFO _block)
{
WORD b = g_Blocks[_block.id].dir[_block.dir];
int x, y;

for(int i=0; i<16; i++)
{
if (b & 0x8000)
{
x = _block.x + i % 4;
y = _block.y - i / 4;
if ((x < 0) || (x >= WIDTH) || (y < 0))
return false;

if ((y < HEIGHT) && (g_World[x][y]))
return false;
}
b <<= 1;
}

return true;
}

// 旋转方块
void OnRotate()
{
// 获取可以旋转的 x 偏移量
int dx;
BLOCKINFO tmp = g_CurBlock;
tmp.dir++; if (CheckBlock(tmp)) { dx = 0; goto rotate; }
tmp.x = g_CurBlock.x - 1; if (CheckBlock(tmp)) { dx = -1; goto rotate; }
tmp.x = g_CurBlock.x + 1; if (CheckBlock(tmp)) { dx = 1; goto rotate; }
tmp.x = g_CurBlock.x - 2; if (CheckBlock(tmp)) { dx = -2; goto rotate; }
tmp.x = g_CurBlock.x + 2; if (CheckBlock(tmp)) { dx = 2; goto rotate; }
return;

rotate:
// 旋转
DrawBlock(g_CurBlock, HIDE);
g_CurBlock.dir++;
g_CurBlock.x += dx;
DrawBlock(g_CurBlock);
}

// 左移方块
void OnLeft()
{
BLOCKINFO tmp = g_CurBlock;
tmp.x--;
if (CheckBlock(tmp))
{
DrawBlock(g_CurBlock, HIDE);
g_CurBlock.x--;
DrawBlock(g_CurBlock);
}
}

// 右移方块
void OnRight()
{
BLOCKINFO tmp = g_CurBlock;
tmp.x++;
if (CheckBlock(tmp))
{
DrawBlock(g_CurBlock, HIDE);
g_CurBlock.x++;
DrawBlock(g_CurBlock);
}
}

// 下移方块
void OnDown()
{
BLOCKINFO tmp = g_CurBlock;
tmp.y--;
if (CheckBlock(tmp))
{
DrawBlock(g_CurBlock, HIDE);
g_CurBlock.y--;
DrawBlock(g_CurBlock);
}
else
OnSink(); // 不可下移时,执行“沉底方块”操作
}

// 沉底方块
void OnSink()
{
int i, x, y;

// 连续下移方块
DrawBlock(g_CurBlock, HIDE);
BLOCKINFO tmp = g_CurBlock;
tmp.y--;
while (CheckBlock(tmp))
{
g_CurBlock.y--;
tmp.y--;
}
DrawBlock(g_CurBlock, FIX);

// 固定方块在游戏区
WORD b = g_Blocks[g_CurBlock.id].dir[g_CurBlock.dir];
for(i = 0; i < 16; i++)
{
if (b & 0x8000)
{
if (g_CurBlock.y - i / 4 >= HEIGHT)
{ // 如果方块的固定位置超出高度,结束游戏
GameOver();
return;
}
else
g_World[g_CurBlock.x + i % 4][g_CurBlock.y - i / 4] = 1;
}

b <<= 1;
}

// 检查是否需要消掉行,并标记
int row[4] = {0};
bool bRow = false;
for(y = g_CurBlock.y; y >= max(g_CurBlock.y - 3, 0); y--)
{
i = 0;
for(x = 0; x < WIDTH; x++)
if (g_World[x][y] == 1)
i++;
if (i == WIDTH)
{
bRow = true;
row[g_CurBlock.y - y] = 1;
setfillstyle(WHITE, DIAGCROSS2_FILL);
bar(0, (HEIGHT - y - 1) * SIZE + SIZE / 2 - 2, WIDTH * SIZE - 1, (HEIGHT - y - 1) * SIZE + SIZE / 2 + 2);
}
}

if (bRow)
{
// 延时 200 毫秒
Sleep(200);

// 擦掉刚才标记的行
IMAGE img;
for(i = 0; i < 4; i++)
{
if (row[i])
{
for(y = g_CurBlock.y - i + 1; y < HEIGHT; y++)
for(x = 0; x < WIDTH; x++)
{
g_World[x][y - 1] = g_World[x][y];
g_World[x][y] = 0;
}

getimage(&img, 0, 0, WIDTH * SIZE, (HEIGHT - (g_CurBlock.y - i + 1)) * SIZE);
putimage(0, SIZE, &img);
}
}
}

// 产生新方块
NewBlock();
}

Ⅵ C语言是什么课程【请详细解说】

C语言是一种计算机程序设计语言。它既具有高级语言的特点,又具有汇编语言的特点。它可以作为系统设计语言,编写工作系统应用程序,也可以作为应用程序设计语言,编写不依赖计算机硬件的应用程序。因此,它的应用范围广泛。主要有以下特点: C语言在很多方面都可以用,不仅仅是在软件开发上,各类科研都是需要用到C语言的。具体应用比如我是学硬件的,单片机以及嵌入式系统都可以用C来开发。 C 语言发展如此迅速, 而且成为最受欢迎的语言之一, 主要因为它具有强大的功能。许多着名的系统软件, 如DBASE Ⅲ PLUS、DBASE Ⅳ 都是由C 语言编写的。用C 语言加上一些汇编语言子程序, 就更能显示C 语言的优势了, 像PC- DOS 、WORDSTAR等就是用这种方法编写的。归纳起来C 语言具有下列特点:1. C是中级语言,它把高级语言的基本结构和语句与低级语言的实用性结合起来。C 语言可以象汇编语言一样对位、字节和地址进行操作, 而这三者是计算机最基本的工作单元。 2. C是结构式语言.结构式语言的显着特点是代码及数据的分隔化, 即程序的各个部分除了必要的信息交流外彼此独立。这种结构化方式可使程序层次清晰, 便于使用、维护以及调试。C 语言是以函数形式提供给用户的, 这些函数可方便的调用, 并具有多种循环、条件语句控制程序流向, 从而使程序完全结构化。 3. C语言功能齐全C 语言具有各种各样的数据类型, 并引入了指针概念, 可使程序效率更高。另外C 语言也具有强大的图形功能, 支持多种显示器和驱动器。而且计算功能、逻辑判断功能也比较强大, 可以实现决策目的编游戏,编3D游戏,做数据库,做联众世界,做聊天室,做PHOTOSHOP做FLASH,做3DMAX。 4. C语言适用范围大C 语言还有一个突出的优点就是适合于多种操作系统, 如DOS、UNIX,也适用于多种机型。 C语言对操作系统和系统使用程序以及需要对硬件进行操作的场合,用C语言明显优于其它解释型高级语言,有一些大型应用软件也是用C语言编写的。 C语言具有绘图能力强,可移植性,并具备很强的数据处理能力,因此适于编写系统软件,三维,二维图形和动画。它是数值计算的高级语言。 常用的C语言IDE(集成开发环境)有Microsoft Visual C++,Borland C++,Watcom C++ ,Borland C++ ,Borland C++ Builder,Borland C++ 3.1 for DOS,Watcom C++ 11.0 for DOS,GNU DJGPP C++ ,Lccwin32 C Compiler 3.1,Microsoft C,High C,Turbo C,Dev-C++,C-Free, win-tc 等等...... 对于一个初学者,Turboc2.01是一个比较好的软件。Turboc2.01是为了照顾不熟悉DOS环境软件操作的广大初学者,让大家能在Windows环境下方便地安装TurboC。下载该改良版后就像普通软件一样安装既可使用,十分方便。 近年来,大多数人用Windows操作系统的图形界面,用鼠标进行操作,感到用TurboC2.0不太方便,建议用TurboC++3.0作为编译工具!