c游戲編程視頻
生命游戲
/* ------------------------------------------------------ */
/* PROGRAM game of life : */
/* This is a finite implementation of John H. Conway's */
/* Game of Life. Refere to my book for detail please. */
/* */
/* Copyright Ching-Kuang Shene July/25/1989 */
/* ------------------------------------------------------ */
#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 50 /* board size */
#define OCCUPIED 1 /* occupied flag */
#define UNOCCUPIED 0
#define YES 1
#define NO 0
char cell[MAXSIZE][MAXSIZE]; /* the board */
char work[MAXSIZE][MAXSIZE]; /* a working */
int row; /* No. of rows you want */
int column; /* no. of columns you want */
int generations; /* maximum no. of generation*/
/* ------------------------------------------------------ */
/* FUNCTION read_in : */
/* This function reads in the number of generations, */
/* the number of rows, the number of columns and finally */
/* the initial configuration (the generation 0). Then put*/
/* your configuration to the center of the board. */
/* ------------------------------------------------------ */
void read_in(void)
{
int max_row, max_col; /* max # of row and col. */
int col_gap, row_gap; /* incremnet of row and col */
int i, j;
char line[100];
gets(line); /* read in gens, row and col*/
sscanf(line, "%d%d%d", &generations, &row, &column);
for (i = 0; i < row; i++)/* clear the board */
for (j = 0; j < column; j++)
cell[i][j] = UNOCCUPIED;
max_col = 0; /* read in the config. */
for (max_row = 0; gets(line) != NULL; max_row++) {
for (i = 0; line[i] != '\0'; i++)
if (line[i] != ' ')
cell[max_row][i] = OCCUPIED;
max_col = (max_col < i) ? i : max_col;
}
row_gap = (row - max_row)/2; /* the moving gap */
col_gap = (column - max_col)/2;
for (i = max_row + row_gap - 1; i >= row_gap; i--) {
for (j = max_col + col_gap - 1; j >= col_gap; j--)
cell[i][j] = cell[i-row_gap][j-col_gap];
for ( ; j >= 0; j--)
cell[i][j] = UNOCCUPIED;
}
for ( ; i >= 0; i--)
for (j = 0; j < column; j++)
cell[i][j] = UNOCCUPIED;
}
/* ------------------------------------------------------ */
/* FUNCTION display : */
/* Display the board. */
/* ------------------------------------------------------ */
#define DRAW_BOARDER(n) { int i; \
printf("\n+"); \
for (i = 0; i < n; i++) \
printf("-"); \
printf("+"); \
}
void display(int gen_no)
{
int i, j;
if (gen_no == 0)
printf("\n\nInitial Generation :\n");
else
printf("\n\nGeneration %d :\n", gen_no);
DRAW_BOARDER(column);
for (i = 0; i < row; i++) {
printf("\n|");
for (j = 0; j < column; j++)
printf("%c", (cell[i][j] == OCCUPIED) ? '*' : ' ');
printf("|");
}
DRAW_BOARDER(column);
}
/* ------------------------------------------------------ */
/* FUNCTION game_of_life : */
/* This is the main function of Game of Life. */
/* ------------------------------------------------------ */
void game_of_life(void)
{
int stable; /* stable flag */
int iter; /* iteration count */
int top, bottom, left, right; /* neighborhood bound */
int neighbors; /* # of neighbors */
int cell_count; /* # of cells count */
int done;
int i, j, p, q;
display(0); /* display initial config. */
done = NO;
for (iter = 1; iter <= generations && !done; iter++) {
memmove(work, cell, MAXSIZE*MAXSIZE); /**/
stable = YES; /* assume it is in stable */
cell_count = 0; /* # of survived cells = 0 */
for (i = 0; i < row; i++) { /* scan each cell...*/
top = (i == 0) ? 0 : i - 1;
bottom = (i == row - 1) ? row-1 : i + 1;
for (j = 0; j < column; j++) {
left = (j == 0) ? 0 : j - 1;
right = (j == column - 1) ? column-1 : j + 1;
/* compute number of neighbors */
neighbors = 0;
for (p = top; p <= bottom; p++)
for (q = left; q <= right; q++)
neighbors += work[p][q];
neighbors -= work[i][j];
/* determine life or dead */
if (work[i][j] == OCCUPIED)
if (neighbors == 2 || neighbors == 3) {
cell[i][j] = OCCUPIED;
cell_count++;
}
else
cell[i][j] = UNOCCUPIED;
else if (neighbors == 3) {
cell[i][j] = OCCUPIED;
cell_count++;
}
else
cell[i][j] = UNOCCUPIED;
stable = stable && (work[i][j] == cell[i][j]);
}
}
if (cell_count == 0) {
printf("\n\nAll cells die out.");
done = YES;
}
else if (stable) {
printf("\n\nSystem enters a stable state.");
done = YES;
}
else
display(iter);
}
}
/* ------------------------------------------------------ */
void main(void)
{
read_in();
game_of_life();
}
❷ 一種C語言小游戲程序設計(程序已經附上)
"掃雷"小游戲C代碼
#include<stdio.h>
#include<math.h>
#include<time.h>
#include<stdlib.h>
main( )
{char a[102][102],b[102][102],c[102][102],w;
int i,j; /*循環變數*/
int x,y,z[999]; /*雷的位置*/
int t,s; /*標記*/
int m,n,lei; /*計數*/
int u,v; /*輸入*/
int hang,lie,ge,mo; /*自定義變數*/
srand((int)time(NULL)); /*啟動隨機數發生器*/
leb1: /*選擇模式*/
printf("
請選擇模式:
1.標准 2.自定義
");
scanf("%d",&mo);
if(mo==2) /*若選擇自定義模式,要輸入三個參數*/
{do
{t=0; printf("請輸入
行數 列數 雷的個數
");
scanf("%d%d%d",&hang,&lie,&ge);
if(hang<2){printf("行數太少
"); t=1;}
if(hang>100){printf("行數太多
");t=1;}
if(lie<2){printf("列數太少
");t=1;}
if(lie>100){printf("列數太多
");t=1;}
if(ge<1){printf("至少要有一個雷
");t=1;}
if(ge>=(hang*lie)){printf("雷太多了
");t=1;}
}while(t==1);
}
else{hang=10,lie=10,ge=10;} /*否則就是選擇了標准模式(默認參數)*/
for(i=1;i<=ge;i=i+1) /*確定雷的位置*/
{do
{t=0; z[i]=rand( )%(hang*lie);
for(j=1;j<i;j=j+1){if(z[i]==z[j]) t=1;}
}while(t==1);
}
for(i=0;i<=hang+1;i=i+1) /*初始化a,b,c*/
{for(j=0;j<=lie+1;j=j+1) {a[i][j]='1'; b[i][j]='1'; c[i][j]='0';} }
for(i=1;i<=hang;i=i+1)
{for(j=1;j<=lie;j=j+1) {a[i][j]='+';} }
for(i=1;i<=ge;i=i+1) /*把雷放入c*/
{x=z[i]/lie+1; y=z[i]%lie+1; c[x][y]='#';}
for(i=1;i<=hang;i=i+1) /*計算b中數字*/
{for(j=1;j<=lie;j=j+1)
{m=48;
if(c[i-1][j-1]=='#')m=m+1; if(c[i][j-1]=='#')m=m+1;
if(c[i-1][j]=='#')m=m+1; if(c[i+1][j+1]=='#')m=m+1;
if(c[i][j+1]=='#')m=m+1; if(c[i+1][j]=='#')m=m+1;
if(c[i+1][j-1]=='#')m=m+1; if(c[i-1][j+1]=='#')m=m+1;
b[i][j]=m;
}
}
for(i=1;i<=ge;i=i+1) /*把雷放入b中*/
{x=z[i]/lie+1; y=z[i]%lie+1; b[x][y]='#';}
lei=ge; /*以下是游戲設計*/
do
{leb2: /*輸出*/
system("cls");printf("
");
printf(" ");
for(i=1;i<=lie;i=i+1)
{w=(i-1)/10+48; printf("%c",w);
w=(i-1)%10+48; printf("%c ",w);
}
printf("
|");
for(i=1;i<=lie;i=i+1){printf("---|");}
printf("
");
for(i=1;i<=hang;i=i+1)
{w=(i-1)/10+48; printf("%c",w);
w=(i-1)%10+48; printf("%c |",w);
for(j=1;j<=lie;j=j+1)
{if(a[i][j]=='0')printf(" |");
else printf(" %c |",a[i][j]);
}
if(i==2)printf(" 剩餘雷個數");
if(i==3)printf(" %d",lei);
printf("
|");
for(j=1;j<=lie;j=j+1){printf("---|");}
printf("
");
}
scanf("%d%c%d",&u,&w,&v); /*輸入*/
u=u+1,v=v+1;
if(w!='#'&&a[u][v]=='@')
goto leb2;
if(w=='#')
{if(a[u][v]=='+'){a[u][v]='@'; lei=lei-1;}
else if(a[u][v]=='@'){a[u][v]='?'; lei=lei+1;}
else if(a[u][v]=='?'){a[u][v]='+';}
goto leb2;
}
a[u][v]=b[u][v];
leb3: /*打開0區*/
t=0;
if(a[u][v]=='0')
{for(i=1;i<=hang;i=i+1)
{for(j=1;j<=lie;j=j+1)
{s=0;
if(a[i-1][j-1]=='0')s=1; if(a[i-1][j+1]=='0')s=1;
if(a[i-1][j]=='0')s=1; if(a[i+1][j-1]=='0')s=1;
if(a[i+1][j+1]=='0')s=1; if(a[i+1][j]=='0')s=1;
if(a[i][j-1]=='0')s=1; if(a[i][j+1]=='0')s=1;
if(s==1)a[i][j]=b[i][j];
}
}
for(i=1;i<=hang;i=i+1)
{for(j=lie;j>=1;j=j-1)
{s=0;
if(a[i-1][j-1]=='0')s=1; if(a[i-1][j+1]=='0')s=1;
if(a[i-1][j]=='0')s=1; if(a[i+1][j-1]=='0')s=1;
if(a[i+1][j+1]=='0')s=1; if(a[i+1][j]=='0')s=1;
if(a[i][j-1]=='0')s=1; if(a[i][j+1]=='0')s=1;
if(s==1)a[i][j]=b[i][j];
}
}
for(i=hang;i>=1;i=i-1)
{for(j=1;j<=lie;j=j+1)
{s=0;
if(a[i-1][j-1]=='0')s=1; if(a[i-1][j+1]=='0')s=1;
if(a[i-1][j]=='0')s=1; if(a[i+1][j-1]=='0')s=1;
if(a[i+1][j+1]=='0')s=1; if(a[i+1][j]=='0')s=1;
if(a[i][j-1]=='0')s=1; if(a[i][j+1]=='0')s=1;
if(s==1)a[i][j]=b[i][j];
}
}
for(i=hang;i>=1;i=i-1)
{for(j=lie;j>=1;j=j-1)
{s=0;
if(a[i-1][j-1]=='0')s=1; if(a[i-1][j+1]=='0')s=1;
if(a[i-1][j]=='0')s=1; if(a[i+1][j-1]=='0')s=1;
if(a[i+1][j+1]=='0')s=1;if(a[i+1][j]=='0')s=1;
if(a[i][j-1]=='0')s=1; if(a[i][j+1]=='0')s=1;
if(s==1)a[i][j]=b[i][j];
}
}
for(i=1;i<=hang;i=i+1) /*檢測0區*/
{for(j=1;j<=lie;j=j+1)
{if(a[i][j]=='0')
{if(a[i-1][j-1]=='+'||a[i-1][j-1]=='@'||a[i-1][j-1]=='?')t=1;
if(a[i-1][j+1]=='+'||a[i-1][j+1]=='@'||a[i-1][j+1]=='?')t=1;
if(a[i+1][j-1]=='+'||a[i+1][j-1]=='@'||a[i+1][j-1]=='?')t=1;
if(a[i+1][j+1]=='+'||a[i+1][j+1]=='@'||a[i+1][j+1]=='?')t=1;
if(a[i+1][j]=='+'||a[i+1][j]=='@'||a[i+1][j]=='?')t=1;
if(a[i][j+1]=='+'||a[i][j+1]=='@'||a[i][j+1]=='?')t=1;
if(a[i][j-1]=='+'||a[i][j-1]=='@'||a[i][j-1]=='?')t=1;
if(a[i-1][j]=='+'||a[i-1][j]=='@'||a[i-1][j]=='?')t=1;
}
}
}
if(t==1)goto leb3;
}
n=0; /*檢查結束*/
for(i=1;i<=hang;i=i+1)
{for(j=1;j<=lie;j=j+1)
{if(a[i][j]!='+'&&a[i][j]!='@'&&a[i][j]!='?')n=n+1;}
}
}
while(a[u][v]!='#'&&n!=(hang*lie-ge));
for(i=1;i<=ge;i=i+1) /*游戲結束*/
{x=z[i]/lie+1; y=z[i]%lie+1; a[x][y]='#'; }
printf(" ");
for(i=1;i<=lie;i=i+1)
{w=(i-1)/10+48; printf("%c",w);
w=(i-1)%10+48; printf("%c ",w);
}
printf("
|");
for(i=1;i<=lie;i=i+1){printf("---|");}
printf("
");
for(i=1;i<=hang;i=i+1)
{w=(i-1)/10+48; printf("%c",w);
w=(i-1)%10+48; printf("%c |",w);
for(j=1;j<=lie;j=j+1)
{if(a[i][j]=='0')printf(" |");
else printf(" %c |",a[i][j]);
}
if(i==2)printf(" 剩餘雷個數");
if(i==3)printf(" %d",lei); printf("
|");
for(j=1;j<=lie;j=j+1) {printf("---|");}
printf("
");
}
if(n==(hang*lie-ge)) printf("你成功了!
");
else printf(" 游戲結束!
");
printf(" 重玩請輸入1
");
t=0;
scanf("%d",&t);
if(t==1)goto leb1;
}
/*註:在DEV c++上運行通過。行號和列號都從0開始,比如要確定第0行第9列不是「雷」,就在0和9中間加入一個字母,可以輸入【0a9】三個字元再按回車鍵。3行7列不是雷,則輸入【3a7】回車;第8行第5列是雷,就輸入【8#5】回車,9行0列是雷則輸入【9#0】並回車*/
❸ 猜數字游戲C語言編程
有個幾年前編好的猜數字,但是代碼找不到了,沒有沒有記憶成績的功能,樓主要不?
❹ c語言游戲編程,下落的小鳥 求代碼
下落的小鳥
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<time.h>
#include<Windows.h>
int Grade = 1, Score = 0, Max_blank = 9, Distance = 18;
struct Birds{int x; int y;}; //定義一種Birds數據類型(含3個成員)
Birds *Bird = (Birds*)malloc(sizeof(Birds)); //定義Birds類型 指針變數Bird並賦初值
struct Bg{int x, y; int l_blank; Bg *pri; Bg *next;}; //定義一種Bg數據類型(含5個成員)
Bg *Bg1 = (Bg*)malloc(sizeof(Bg)); //定義Bg類型 指針變數Bg1並賦初值
void Position(int x, int y) //游標定位函數(用於指定位置輸出)
{COORD pos = { x - 1, y - 1 };
HANDLE Out = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(Out, pos);
}
void Csh( ) //初始化界面
{
printf("══════════════════════════════════════ ");
printf(" ■■ ■■ C語言版 Flappy Bird ");
printf(" ■■ ■■ ");
printf(" ■■ ■■ ");
printf(" ■■ ■■ 瞎搞人:yyposs原創 ");
printf(" ■■ ■■ 瞎搞日期:2014.2 ");
printf(" ■■ ■■ ");
printf(" ■■ ■■ 改編:鳴蟬百2021.7 ");
printf(" ■■ ■■ 操作:按向上方向鍵讓小鳥起飛 ");
printf(" ■■ ");
printf(" ■■ ");
printf(" ■■ ■■ ");
printf(" ■■ ■■ ");
printf(" ■■ ■■ ");
printf(" ■■ ■■ ");
printf(" ■■ ■■ DEVc++運行通過 ");
printf("══════════════════════════════════════ ");
printf(" 按鍵繼續…");
getch( );
system("cls");
}
void PrFK( ) //輸出方框(游戲范圍區)
{int i;
Position(1, 1); printf("╔"); Position(79, 1); printf("╗");
Position(1, 24); printf("╚"); Position(79, 24); printf("╝");
for (i = 3; i <= 78; i += 2){Position(i, 1); printf("═"); Position(i, 24); printf("═");}
for(i=2;i<=23;i++)
{ Position(1,i); printf("║");if(i<11)printf("0%d",i-1);else printf("%d",i-1);
Position(79,i); printf("║");
}
Position(4, 25); printf("小鳥即將出現,請准備按鍵起飛… ");
getch( );
Position(4, 25); printf(" ");
}
void CreatBg( ) //創建障礙物坐標(便於列印輸出)
{Bg *Bg2 = (Bg*)malloc(sizeof(Bg));
Bg1->x = 90; Bg1->y = 8; //確定障礙物的一對基本坐標(此時值是在游戲框之外)
Bg2->x = Bg1->x + Distance; Bg2->y = 9; //下一障礙物的基本坐標x、y
Bg1->l_blank = Max_blank - Grade; //障礙物上下兩部分之間的空白距離l_blank
Bg2->l_blank = Max_blank - Grade;
Bg1->next = Bg2; Bg1->pri = Bg2;
Bg2->next = Bg1; Bg2->pri = Bg1;
}
void InsertBg(Bg *p) //隨機改變障礙物的y坐標,讓空白通道有上下變化
{int temp;
Bg *Bgs = (Bg*)malloc(sizeof(Bg));
Bgs->x = p->pri->x + Distance;
Bgs->l_blank = Max_blank - Grade;
srand((int)time(0)); //啟動隨機數發生器
temp = rand( ); //產生一個隨機數並賦值給temp
if (temp % 2 == 0)
{if ((temp % 4 + p->pri->y + Max_blank - Grade)<21)
Bgs->y = p->pri->y + temp % 4;
else Bgs->y = p->pri->y;
}
else
{if ((p->pri->y - temp % 4)>2)Bgs->y = p->pri->y - temp % 4;
else Bgs->y = p->pri->y;
}
Bgs->pri = p->pri; Bgs->next = p;
p->pri->next = Bgs; p->pri = Bgs;
}
void CreatBird( ) //建立小鳥的坐標(初始列印輸出小鳥的位置)
{Bird->x = 41; Bird->y = 10;}
int CheckYN(Bg *q) //判斷游戲結束與否(值為0是要結束,為1沒有要結束)
{Bg *p = q; int i = 0;
while (++i <= 5)
{if (Bird->y>23)return 0;
if (Bird->x == p->x&&Bird->y <= p->y)return 0;
if ((Bird->x == p->x || Bird->x == p->x + 1 || Bird->x == p->x + 2) && Bird->y == p->y)return 0;
if (Bird->x == p->x&&Bird->y>p->y + p->l_blank)return 0;
if ((Bird->x == p->x || Bird->x == p->x + 1 || Bird->x == p->x + 2) && Bird->y == p->y + p->l_blank)
return 0;
p = p->next;
}
return 1;
}
void Check_Bg(Bg *q) //核查開頭的障礙物坐標是否在游戲區內
{Bg *p = q; int i = 0, temp;
while (++i <= 5)
{if (p->x>-4)p = p->next;
else
{srand((int)time(0)); temp = rand();
if (temp % 2 == 0)
{if ((temp % 4 + p->y + Max_blank - Grade)<21)p->y = p->y + temp % 4;
else p->y = p->y; p->x = p->pri->x + Distance;
p->l_blank = Max_blank - Grade;
}
else
{if ((p->y - temp % 4)>2)p->y = p->y - temp % 4;
else p->y = p->y; p->x = p->pri->x + Distance;
p->l_blank = Max_blank - Grade;
}
}
}
}
void Prt_Bg(Bg *q) //列印輸出障礙物(依據其x、y坐標進行相應輸出)
{Bg *p = q; int i = 0, k, j;
while (++i <= 5)
{if (p->x>0 && p->x <= 78)
{for (k = 2; k<p->y; k++){Position(p->x + 1, k); printf("■"); printf("■"); printf(" ");}
Position(p->x, p->y);
printf("■"); printf("■"); printf("■"); printf(" ");
Position(p->x, p->y + p->l_blank);
printf("■"); printf("■"); printf("■"); printf(" ");
k = k + p->l_blank + 1;
for (k; k <= 23; k++){Position(p->x + 1, k); printf("■"); printf("■"); printf(" ");}
}
p = p->next;
if (p->x == 0)
{for (j = 2; j<p->y; j++){Position(p->x + 1, j); printf(" "); printf(" ");}
Position(p->x + 1, p->y);
printf(" "); printf(" "); printf(" ");
Position(p->x + 1, p->y + Max_blank - Grade);
printf(" "); printf(" "); printf(" ");
j = j + Max_blank - Grade + 1;
for (j; j <= 22; j++){Position(p->x + 1, j); printf(" "); printf(" ");}
}
}
}
void PrtBird( ) //列印輸出小鳥
{Position(Bird->x, Bird->y - 1); printf(" ");
Position(Bird->x, Bird->y); printf("Ю");
Position(38, 2); printf("Score:%d", Score);
}
void Loop_Bg(Bg *q) //障礙物x坐標左移,並記錄成績
{Bg *p = q; int i = 0;
while (++i <= 5)
{p->x = p->x - 1; p = p->next;
if (Bird->x == p->x)
{Score += 1;
if (Score % 4 == 0 && Grade<4)Grade++;
}
}
}
int main( )
{int i = 0; int t;
while (1)
{
Csh( );PrFK( );CreatBg( );
InsertBg(Bg1);InsertBg(Bg1);InsertBg(Bg1);
CreatBird( );
while (1)
{if (!CheckYN(Bg1))break;
Check_Bg(Bg1);Prt_Bg(Bg1);
PrtBird( );Loop_Bg(Bg1);
Bird->y = Bird->y + 1;
if (GetAsyncKeyState(VK_UP)) //按下了向上方向鍵
{Position(Bird->x, Bird->y - 1);printf(" ");
Bird->y = Bird->y - 4;
}
Sleep(200); //程序延時200毫秒(數值大小決定游戲速度快慢)
i = 0;
}
Position(6, 25);
printf("游戲結束! 請輸入:0.退出 1.重玩");
scanf("%d",&t);
if (t==0)break;
system("cls"); Score = 0;
}
return 0;
}
❺ 貪吃蛇游戲的C語言編程
#include <windows.h>
#include <ctime>
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
#ifndef SNAKE_H
#define SNAKE_H
class Cmp
{
friend class Csnake;
int rSign; //橫坐標
int lSign; //豎坐標
public:
// friend bool isDead(const Cmp& cmp);
Cmp(int r,int l){setPoint(r,l);}
Cmp(){}
void setPoint(int r,int l){rSign=r;lSign=l;}
Cmp operator-(const Cmp &m)const
{
return Cmp(rSign-m.rSign,lSign-m.lSign);
}
Cmp operator+(const Cmp &m)const
{
return Cmp(rSign+m.rSign,lSign+m.lSign);
}
};
const int maxSize = 5; //初始蛇身長度
class Csnake
{
Cmp firstSign; //蛇頭坐標
Cmp secondSign;//蛇頸坐標
Cmp lastSign; //蛇尾坐標
Cmp nextSign; //預備蛇頭
int row; //列數
int line; //行數
int count; //蛇身長度
vector<vector<char> > snakeMap;//整個游戲界面
queue<Cmp> snakeBody; //蛇身
public:
int GetDirections()const;
char getSymbol(const Cmp& c)const
//獲取指定坐標點上的字元
{
return snakeMap[c.lSign][c.rSign];
}
Csnake(int n)
//初始化游戲界面大小
{
if(n<20)line=20+2;
else if(n>30)line=30+2;
else line=n+2;
row=line*3+2;
}
bool isDead(const Cmp& cmp)
{
return ( getSymbol(cmp)=='@' || cmp.rSign == row-1
|| cmp.rSign== 0 || cmp.lSign == line-1 ||
cmp.lSign == 0 );
}
void InitInstance(); //初始化游戲界面
bool UpdataGame(); //更新游戲界面
void ShowGame(); //顯示游戲界面
};
#endif // SNAKE_H
using namespace std;
//測試成功
void Csnake::InitInstance()
{
snakeMap.resize(line); // snakeMap[豎坐標][橫坐標]
for(int i=0;i<line;i++)
{
snakeMap[i].resize(row);
for(int j=0;j<row;j++)
{
snakeMap[i][j]=' ';
}
}
for(int m=1;m<maxSize+1;m++)
{
//初始蛇身
snakeMap[line/2][m]='@';
//將蛇身坐標壓入隊列
snakeBody.push(Cmp(m,(line/2)));
//snakeBody[橫坐標][豎坐標]
}
//鏈表頭尾
firstSign=snakeBody.back();
secondSign.setPoint(maxSize-1,line/2);
}
//測試成功
int Csnake::GetDirections()const
{
if(GetKeyState(VK_UP)<0) return 1; //1表示按下上鍵
if(GetKeyState(VK_DOWN)<0) return 2; //2表示按下下鍵
if(GetKeyState(VK_LEFT)<0) return 3; //3表示按下左鍵
if(GetKeyState(VK_RIGHT)<0)return 4; //4表示按下右鍵
return 0;
}
bool Csnake::UpdataGame()
{
//-----------------------------------------------
//初始化得分0
static int score=0;
//獲取用戶按鍵信息
int choice;
choice=GetDirections();
cout<<"Total score: "<<score<<endl;
//隨機產生食物所在坐標
int r,l;
//開始初始已經吃食,產生一個食物
static bool eatFood=true;
//如果吃了一個,才再出現第2個食物
if(eatFood)
{
do
{
//坐標范圍限制在(1,1)到(line-2,row-2)對點矩型之間
srand(time(0));
r=(rand()%(row-2))+1; //橫坐標
l=(rand()%(line-2))+1;//豎坐標
//如果隨機產生的坐標不是蛇身,則可行
//否則重新產生坐標
if(snakeMap[l][r]!='@')
{snakeMap[l][r]='*';}
}while (snakeMap[l][r]=='@');
}
switch (choice)
{
case 1://向上
//如果蛇頭和社頸的橫坐標不相同,執行下面操作
if(firstSign.rSign!=secondSign.rSign)nextSign.setPoint(firstSign.rSign,firstSign.lSign-1);
//否則,如下在原本方向上繼續移動
else nextSign=firstSign+(firstSign-secondSign);
break;
case 2://向下
if(firstSign.rSign!=secondSign.rSign)nextSign.setPoint(firstSign.rSign,firstSign.lSign+1);
else nextSign=firstSign+(firstSign-secondSign);
break;
case 3://向左
if(firstSign.lSign!=secondSign.lSign)nextSign.setPoint(firstSign.rSign-1,firstSign.lSign);
else nextSign=firstSign+(firstSign-secondSign);
break;
case 4://向右
if(firstSign.lSign!=secondSign.lSign)nextSign.setPoint(firstSign.rSign+1,firstSign.lSign);
else nextSign=firstSign+(firstSign-secondSign);
break;
default:
nextSign=firstSign+(firstSign-secondSign);
}
//----------------------------------------------------------
if(getSymbol(nextSign)!='*' && !isDead(nextSign))
//如果沒有碰到食物(且沒有死亡的情況下),刪除蛇尾,壓入新的蛇頭
{
//刪除蛇尾
lastSign=snakeBody.front();
snakeMap[lastSign.lSign][lastSign.rSign]=' ';
snakeBody.pop();
//更新蛇頭
secondSign=firstSign;
//壓入蛇頭
snakeBody.push(nextSign);
firstSign=snakeBody.back();
snakeMap[firstSign.lSign][firstSign.rSign]='@';
//沒有吃食
eatFood=false;
return true;
}
//-----吃食-----
else if(getSymbol(nextSign)=='*' && !isDead(nextSign))
{
secondSign=firstSign;
snakeMap[nextSign.lSign][nextSign.rSign]='@';
//只壓入蛇頭
snakeBody.push(nextSign);
firstSign=snakeBody.back();
eatFood=true;
//加分
score+=20;
return true;
}
//-----死亡-----
else {cout<<"Dead"<<endl;cout<<"Your last total score is "<<score<<endl; return false;}
}
void Csnake::ShowGame()
{
for(int i=0;i<line;i++)
{
for(int j=0;j<row;j++)
cout<<snakeMap[i][j];
cout<<endl;
}
Sleep(1);
system("cls");
}
int main()
{
Csnake s(20);
s.InitInstance();
//s.ShowGame();
int noDead;
do
{
s.ShowGame();
noDead=s.UpdataGame();
}while (noDead);
system("pause");
return 0;
}
這個代碼可以運行的,記得給分啦
❻ 用C語言編寫小游戲
用c語言編寫一個五子棋吧,不怎麼難,給你程序,自己參考一下
/*3.3.4 源程序*/
#include "graphics.h" /*圖形系統頭文件*/
#define LEFT 0x4b00 /*游標左鍵值*/
#define RIGHT 0x4d00 /*游標右鍵值*/
#define DOWN 0x5000 /*游標下鍵值*/
#define UP 0x4800 /*游標上鍵值*/
#define ESC 0x011b /* ESC鍵值*/
#define ENTER 0x1c0d /* 回車鍵值*/
int a[8][8]={0},key,score1,score2;/*具體分數以及按鍵與存放棋子的變數*/
char playone[3],playtwo[3];/*兩個人的得分轉換成字元串輸出*/
void playtoplay(void);/*人人對戰函數*/
void DrawQp(void);/*畫棋盤函數*/
void SetPlayColor(int x);/*設置棋子第一次的顏色*/
void MoveColor(int x,int y);/*恢復原來棋盤狀態*/
int QpChange(int x,int y,int z);/*判斷棋盤的變化*/
void DoScore(void);/*處理分數*/
void PrintScore(int n);/*輸出成績*/
void playWin(void);/*輸出勝利者信息*/
/******主函數*********/
void main(void)
{
int gd=DETECT,gr;
initgraph(&gd,&gr,"c:\\tc"); /*初始化圖形系統*/
DrawQp();/*畫棋盤*/
playtoplay();/*人人對戰*/
getch();
closegraph();/*關閉圖形系統*/
}
void DrawQp()/*畫棋盤*/
{
int i,j;
score1=score2=0;/*棋手一開始得分都為0*/
setbkcolor(BLUE);
for(i=100;i<=420;i+=40)
{
line(100,i,420,i);/*畫水平線*/
line(i,100,i,420); /*畫垂直線*/
}
setcolor(0);/*取消圓周圍的一圈東西*/
setfillstyle(SOLID_FILL,15);/*白色實體填充模式*/
fillellipse(500,200,15,15); /*在顯示得分的位置畫棋*/
setfillstyle(SOLID_FILL,8); /*黑色實體填充模式*/
fillellipse(500,300,15,15);
a[3][3]=a[4][4]=1;/*初始兩個黑棋*/
a[3][4]=a[4][3]=2;/*初始兩個白棋*/
setfillstyle(SOLID_FILL,WHITE);
fillellipse(120+3*40,120+3*40,15,15);
fillellipse(120+4*40,120+4*40,15,15);
setfillstyle(SOLID_FILL,8);
fillellipse(120+3*40,120+4*40,15,15);
fillellipse(120+4*40,120+3*40,15,15);
score1=score2=2; /*有棋後改變分數*/
DoScore();/*輸出開始分數*/
}
void playtoplay()/*人人對戰*/
{
int x,y,t=1,i,j,cc=0;
while(1)/*換棋手走棋*/
{
x=120,y=80;/*每次棋子一開始出來的坐標,x為行坐標,y為列坐標*/
while(1) /*具體一個棋手走棋的過程*/
{
PrintScore(1);/*輸出棋手1的成績*/
PrintScore(2);/*輸出棋手2的成績*/
SetPlayColor(t);/*t變數是用來判斷棋手所執棋子的顏色*/
fillellipse(x,y,15,15);
key=bioskey(0);/*接收按鍵*/
if(key==ESC)/*跳出遊戲*/
break;
else
if(key==ENTER)/*如果按鍵確定就可以跳出循環*/
{
if(y!=80&&a[(x-120)/40][(y-120)/40]!=1
&&a[(x-120)/40][(y-120)/40]!=2)/*如果落子位置沒有棋子*/
{
if(t%2==1)/*如果是棋手1移動*/
a[(x-120)/40][(y-120)/40]=1;
else/*否則棋手2移動*/
a[(x-120)/40][(y-120)/40]=2;
if(!QpChange(x,y,t))/*落子後判斷棋盤的變化*/
{
a[(x-120)/40][(y-120)/40]=0;/*恢復空格狀態*/
cc++;/*開始統計嘗試次數*/
if(cc>=64-score1-score2) /*如果嘗試超過空格數則停步*/
{
MoveColor(x,y);
fillellipse(x,y,15,15);
break;
}
else
continue;/*如果按鍵無效*/
}
DoScore();/*分數的改變*/
break;/*棋盤變化了,則輪對方走棋*/
}
else/*已經有棋子就繼續按鍵*/
continue;
}
else /*四個方向按鍵的判斷*/
if(key==LEFT&&x>120)/*左方向鍵*/
{
MoveColor(x,y);
fillellipse(x,y,15,15);
SetPlayColor(t);
x-=40;
fillellipse(x,y,15,15);
}
else
if(key==RIGHT&&x<400&&y>80)/*右方向鍵*/
{
MoveColor(x,y);
fillellipse(x,y,15,15);
SetPlayColor(t);
x+=40;
fillellipse(x,y,15,15);
}
else
if(key==UP&&y>120)/*上方向鍵*/
{
MoveColor(x,y);
fillellipse(x,y,15,15);
SetPlayColor(t);
y-=40;
fillellipse(x,y,15,15);
}
else
if(key==DOWN&&y<400)/*下方向鍵*/
{
MoveColor(x,y);
fillellipse(x,y,15,15);
SetPlayColor(t);
y+=40;
fillellipse(x,y,15,15);
}
}
if(key==ESC)/*結束游戲*/
break;
if((score1+score2)==64||score1==0||score2==0)/*格子已經占滿或一方棋子為0判斷勝負*/
{
playWin();/*輸出最後結果*/
break;
}
t=t%2+1; /*一方走後,改變棋子顏色即輪對方走*/
cc=0; /*計數值恢復為0*/
} /*endwhile*/
}
void SetPlayColor(int t)/*設置棋子顏色*/
{
if(t%2==1)
setfillstyle(SOLID_FILL,15);/*白色*/
else
setfillstyle(SOLID_FILL,8);/*灰色*/
}
void MoveColor(int x,int y)/*走了一步後恢復原來格子的狀態*/
{
if(y<100)/*如果是從起點出發就恢復藍色*/
setfillstyle(SOLID_FILL,BLUE);
else/*其他情況如果是1就恢復白色棋子,2恢復黑色棋子,或恢復藍色棋盤*/
switch(a[(x-120)/40][(y-120)/40])
{
case 1:
setfillstyle(SOLID_FILL,15);break; /*白色*/
case 2:
setfillstyle(SOLID_FILL,8);break; /*黑色*/
default:
setfillstyle(SOLID_FILL,BLUE); /*藍色*/
}
}
int QpChange(int x,int y,int t)/*判斷棋盤的變化*/
{
int i,j,k,kk,ii,jj,yes;
yes=0;
i=(x-120)/40; /*計算數組元素的行下標*/
j=(y-120)/40; /*計算數組元素的列下標*/
SetPlayColor(t);/*設置棋子變化的顏色*/
/*開始往8個方向判斷變化*/
if(j<6)/*往右邊*/
{
for(k=j+1;k<8;k++)
if(a[i][k]==a[i][j]||a[i][k]==0)/*遇到自己的棋子或空格結束*/
break;
if(a[i][k]!=0&&k<8)
{
for(kk=j+1;kk<k&&k<8;kk++)/*判斷右邊*/
{
a[i][kk]=a[i][j]; /*改變棋子顏色*/
fillellipse(120+i*40,120+kk*40,15,15);
}
if(kk!=j+1) /*條件成立則有棋子改變過顏色*/
yes=1;
}
}
if(j>1)/*判斷左邊*/
{
for(k=j-1;k>=0;k--)
if(a[i][k]==a[i][j]||!a[i][k])
break;
if(a[i][k]!=0&&k>=0)
{
for(kk=j-1;kk>k&&k>=0;kk--)
{
a[i][kk]=a[i][j];
fillellipse(120+i*40,120+kk*40,15,15);
}
if(kk!=j-1)
yes=1;
}
}
if(i<6)/*判斷下邊*/
{
for(k=i+1;k<8;k++)
if(a[k][j]==a[i][j]||!a[k][j])
break;
if(a[k][j]!=0&&k<8)
{
for(kk=i+1;kk<k&&k<8;kk++)
{
a[kk][j]=a[i][j];
fillellipse(120+kk*40,120+j*40,15,15);
}
if(kk!=i+1)
yes=1;
}
}
if(i>1)/*判斷上邊*/
{
for(k=i-1;k>=0;k--)
if(a[k][j]==a[i][j]||!a[k][j])
break;
if(a[k][j]!=0&&k>=0)
{
for(kk=i-1;kk>k&&k>=0;kk--)
{
a[kk][j]=a[i][j];
fillellipse(120+kk*40,120+j*40,15,15);
}
if(kk!=i-1)
yes=1;
}
}
if(i>1&&j<6)/*右上*/
{
for(k=i-1,kk=j+1;k>=0&&kk<8;k--,kk++)
if(a[k][kk]==a[i][j]||!a[k][kk])
break;
if(a[k][kk]&&k>=0&&kk<8)
{
for(ii=i-1,jj=j+1;ii>k&&k>=0;ii--,jj++)
{
a[ii][jj]=a[i][j];
fillellipse(120+ii*40,120+jj*40,15,15);
}
if(ii!=i-1)
yes=1;
}
}
if(i<6&&j>1)/*左下*/
{
for(k=i+1,kk=j-1;k<8&&kk>=0;k++,kk--)
if(a[k][kk]==a[i][j]||!a[k][kk])
break;
if(a[k][kk]!=0&&k<8&&kk>=0)
{
for(ii=i+1,jj=j-1;ii<k&&k<8;ii++,jj--)
{
a[ii][jj]=a[i][j];
fillellipse(120+ii*40,120+jj*40,15,15);
}
if(ii!=i+1)
yes=1;
}
}
if(i>1&&j>1)/*左上*/
{
for(k=i-1,kk=j-1;k>=0&&kk>=0;k--,kk--)
if(a[k][kk]==a[i][j]||!a[k][kk])
break;
if(a[k][kk]!=0&&k>=0&&kk>=0)
{
for(ii=i-1,jj=j-1;ii>k&&k>=0;ii--,jj--)
{
a[ii][jj]=a[i][j];
fillellipse(120+ii*40,120+jj*40,15,15);
}
if(ii!=i-1)
yes=1;
}
}
if(i<6&&j<6)/* 右下*/
{
for(k=i+1,kk=j+1;kk<8&&kk<8;k++,kk++)
if(a[k][kk]==a[i][j]||!a[k][kk])
break;
if(a[k][kk]!=0&&kk<8&&k<8)
{
for(ii=i+1,jj=j+1;ii<k&&k<8;ii++,jj++)
{
a[ii][jj]=a[i][j];
fillellipse(120+ii*40,120+jj*40,15,15);
}
if(ii!=i+1)
yes=1;
}
}
return yes;/*返回是否改變過棋子顏色的標記*/
}
void DoScore()/*處理分數*/
{
int i,j;
score1=score2=0;/*重新開始計分數*/
for(i=0;i<8;i++)
for(j=0;j<8;j++)
if(a[i][j]==1)/*分別統計兩個人的分數*/
score1++;
else
if(a[i][j]==2)
score2++;
}
void PrintScore(int playnum)/*輸出成績*/
{
if(playnum==1)/*清除以前的成績*/
{
setfillstyle(SOLID_FILL,BLUE);
bar(550,100,640,400);
}
setcolor(RED);
settextstyle(0,0,4);/*設置文本輸出樣式*/
if(playnum==1)/*判斷輸出哪個棋手的分,在不同的位置輸出*/
{
sprintf(playone,"%d",score1);
outtextxy(550,200,playone);
}
else
{
sprintf(playtwo,"%d",score2);
outtextxy(550,300,playtwo);
}
setcolor(0);
}
void playWin()/*輸出最後的勝利者結果*/
{
settextstyle(0,0,4);
setcolor(12);
if(score2>score1)/*開始判斷最後的結果*/
outtextxy(100,50,"black win!");
else
if(score2<score1)
outtextxy(100,50,"white win!");
else
outtextxy(60,50,"you all win!");
}
❼ C語言 要學多久 才可以編程 大型游戲
大型游戲得2年左右吧,其實先不用考慮那麼遠,先把基礎的學會了。然後再學其他語言也更容易上手。其實C並不難,關鍵你得找個適合自己的材料,如果是自學的話,以我自身經歷來看,感覺看視頻比看書效率高,畢竟理科知識不像是文科的,看書效率太低了。可能一個很簡單的知識點,自己看書得半個小時,但是懂的人就講幾句話,就能明白了。所以相比之下,還是視頻效率比看書高。選擇個適合自己的就能學懂C了。我當初看的是夏老師的,感覺挺適合我這樣初學者的。他講的不繁瑣啰嗦,都是重點,而且思維原理講的最好。能讓我理解,我感覺這點很重要。比之前看的什麼郝斌曾怡金文的那些繁瑣啰嗦聽不出重點的好多了。
❽ 我想學游戲編程要怎麼學呢~(我一點都不會...)
先送你一個猜數游戲:
program c_k;
var a,b,c:integer;
begin
randomize;
a:=random(100);
c:=0;
repeat
c:=c+1;
write('please input b:');
readln(b);
if b>a then writeln('Too big')
else if b<a then writeln('Too small')
else if b=a tehn writeln('you are right');
until (c=10) or (a=b);
if c=10 then writeln('you false,the number is:',a);
readln;readln;
end.
再送你一個坦克大戰程序:
program tanke;
uses crt,graph;
type
type1=record
h,l,f:integer;
end;
time=record
ms,s,m:integer;
end;
const fx:array[1..4,1..2] of -1..1=((-1,0),(0,-1),(1,0),(0,1));
var t:time;
tank:type1;
bomb:array[1..20]of type1;
diren:array[1..8]of type1;
fire:array[1..20]of type1;
u,d,l,r,gu,gd,gl,gr:char;
lf:boolean;
procere initg;
var gd,gm:integer;
begin
gd:=detect;
initgraph(gd,gm,'');
end;
procere pushtype1(l,h,f:integer;var a:array of type1);
var i:integer;
begin
for i:=1 to high(a) do
if a[i].f<=0 then
begin
a[i].h:=h;a[i].l:=l;a[i].f:=f;
exit;
end;
end;
procere print;
var i,j:integer;
begin
setfillstyle(white,1);
bar(0,0,600,400);
for i:=1 to high(fire) do
with fire[i] do
if f>0 then
begin
setcolor(red);
line(l*40-20,h*40-20,l*40-1,h*40-1);
line(l*40-20,h*40-20,l*40-1,h*40-10);
line(l*40-20,h*40-20,l*40-1,h*40-20);
line(l*40-20,h*40-20,l*40-1,h*40-30);
line(l*40-20,h*40-20,l*40-1,h*40-40);
line(l*40-20,h*40-20,l*40-40,h*40-1);
line(l*40-20,h*40-20,l*40-40,h*40-10);
line(l*40-20,h*40-20,l*40-40,h*40-20);
line(l*40-20,h*40-20,l*40-40,h*40-30);
line(l*40-20,h*40-20,l*40-40,h*40-40);
line(l*40-20,h*40-20,l*40-20,h*40-1);
line(l*40-20,h*40-20,l*40-30,h*40-1);
line(l*40-20,h*40-20,l*40-10,h*40-40);
line(l*40-20,h*40-20,l*40-10,h*40-1);
line(l*40-20,h*40-20,l*40-20,h*40-40);
line(l*40-20,h*40-20,l*40-30,h*40-40);
end;
for i:=1 to high(bomb) do
with bomb[i] do
if f>0 then
begin
setcolor(blue);
circle((l-1)*40+20,(h-1)*40+20,5);
end;
for i:=1 to high(diren) do
with diren[i] do
if f>0 then
begin
setcolor(11);
line((l-1)*40+5,(h-1)*40+1,(l-1)*40+15,(h-1)*40+10);
line((l-1)*40+15,(h-1)*40+10,(l-1)*40+10,(h-1)*40+15);
line((l-1)*40+10,(h-1)*40+15,(l-1)*40+1,(h-1)*40+5);
line((l-1)*40+1,(h-1)*40+5,(l-1)*40+5,(h-1)*40+1);
line((l-1)*40+35,(h-1)*40+1,(l-1)*40+25,(h-1)*40+10);
line((l-1)*40+25,(h-1)*40+10,(l-1)*40+30,(h-1)*40+15);
line((l-1)*40+30,(h-1)*40+15,(l-1)*40+40,(h-1)*40+5);
line((l-1)*40+40,(h-1)*40+5,(l-1)*40+35,(h-1)*40+1);
line((l-1)*40+5,(h-1)*40+40,(l-1)*40+15,(h-1)*40+30);
line((l-1)*40+15,(h-1)*40+30,(l-1)*40+10,(h-1)*40+25);
line((l-1)*40+10,(h-1)*40+25,(l-1)*40+1,(h-1)*40+35);
line((l-1)*40+1,(h-1)*40+35,(l-1)*40+5,(h-1)*40+40);
line((l-1)*40+35,(h-1)*40+40,(l-1)*40+25,(h-1)*40+30);
line((l-1)*40+25,(h-1)*40+30,(l-1)*40+30,(h-1)*40+25);
line((l-1)*40+30,(h-1)*40+25,(l-1)*40+40,(h-1)*40+35);
line((l-1)*40+40,(h-1)*40+35,(l-1)*40+35,(h-1)*40+40);
line((l-1)*40+15,(h-1)*40+10,(l-1)*40+25,(h-1)*40+10);
line((l-1)*40+30,(h-1)*40+15,(l-1)*40+30,(h-1)*40+25);
line((l-1)*40+10,(h-1)*40+15,(l-1)*40+15,(h-1)*40+25);
line((l-1)*40+15,(h-1)*40+30,(l-1)*40+25,(h-1)*40+30);
setcolor(red);
circle((l-1)*40+20,(h-1)*40+20,6);
end;
with tank do
begin
if f>0 then setcolor(green)
else setcolor(red);
case f of
0,1:begin
setcolor(green);
line((l-1)*40+1,(h-1)*40+10,(l-1)*40+1,(h-1)*40+40);
line((l-1)*40+10,(h-1)*40+10,(l-1)*40+10,(h-1)*40+40);
line((l-1)*40+1,(h-1)*40+10,(l-1)*40+10,(h-1)*40+10);
line((l-1)*40+1,(h-1)*40+40,(l-1)*40+10,(h-1)*40+40);
line((l-1)*40+30,(h-1)*40+10,(l-1)*40+30,(h-1)*40+40);
line((l-1)*40+40,(h-1)*40+10,(l-1)*40+40,(h-1)*40+40);
line((l-1)*40+30,(h-1)*40+10,(l-1)*40+40,(h-1)*40+10);
line((l-1)*40+30,(h-1)*40+40,(l-1)*40+40,(h-1)*40+40);
line((l-1)*40+10,(h-1)*40+15,(l-1)*40+30,(h-1)*40+15);
line((l-1)*40+10,(h-1)*40+35,(l-1)*40+30,(h-1)*40+35);
line((l-1)*40+1,(h-1)*40+20,(l-1)*40+10,(h-1)*40+20);
line((l-1)*40+1,(h-1)*40+30,(l-1)*40+10,(h-1)*40+30);
line((l-1)*40+30,(h-1)*40+20,(l-1)*40+40,(h-1)*40+20);
line((l-1)*40+30,(h-1)*40+30,(l-1)*40+40,(h-1)*40+30);
line((l-1)*40+15,(h-1)*40+1,(l-1)*40+25,(h-1)*40+1);
line((l-1)*40+15,(h-1)*40+1,(l-1)*40+15,(h-1)*40+15);
line((l-1)*40+25,(h-1)*40+1,(l-1)*40+25,(h-1)*40+15);
circle((l-1)*40+20,(h-1)*40+25,5);
end;
2:begin
line((l-1)*40+10,(h-1)*40+1,(l-1)*40+40,(h-1)*40+1);
line((l-1)*40+10,(h-1)*40+10,(l-1)*40+40,(h-1)*40+10);
line((l-1)*40+10,(h-1)*40+1,(l-1)*40+10,(h-1)*40+10);
line((l-1)*40+40,(h-1)*40+1,(l-1)*40+40,(h-1)*40+10);
line((l-1)*40+10,(h-1)*40+30,(l-1)*40+40,(h-1)*40+30);
line((l-1)*40+10,(h-1)*40+40,(l-1)*40+40,(h-1)*40+40);
line((l-1)*40+10,(h-1)*40+30,(l-1)*40+10,(h-1)*40+40);
line((l-1)*40+40,(h-1)*40+30,(l-1)*40+40,(h-1)*40+40);
line((l-1)*40+15,(h-1)*40+10,(l-1)*40+15,(h-1)*40+30);
line((l-1)*40+35,(h-1)*40+10,(l-1)*40+35,(h-1)*40+30);
line((l-1)*40+20,(h-1)*40+1,(l-1)*40+20,(h-1)*40+10);
line((l-1)*40+30,(h-1)*40+1,(l-1)*40+30,(h-1)*40+10);
line((l-1)*40+20,(h-1)*40+30,(l-1)*40+20,(h-1)*40+40);
line((l-1)*40+30,(h-1)*40+30,(l-1)*40+30,(h-1)*40+40);
line((l-1)*40+1,(h-1)*40+15,(l-1)*40+1,(h-1)*40+25);
line((l-1)*40+1,(h-1)*40+15,(l-1)*40+15,(h-1)*40+15);
line((l-1)*40+1,(h-1)*40+25,(l-1)*40+15,(h-1)*40+25);
circle((l-1)*40+25,(h-1)*40+20,5);
end;
3:begin
setcolor(green);
line((l-1)*40+40,(h-1)*40+30,(l-1)*40+40,(h-1)*40+1);
line((l-1)*40+30,(h-1)*40+30,(l-1)*40+30,(h-1)*40+1);
line((l-1)*40+40,(h-1)*40+30,(l-1)*40+30,(h-1)*40+30);
line((l-1)*40+40,(h-1)*40+1,(l-1)*40+30,(h-1)*40+1);
line((l-1)*40+40,(h-1)*40+1,(l-1)*40+30,(h-1)*40+1);
line((l-1)*40+10,(h-1)*40+30,(l-1)*40+10,(h-1)*40+1);
line((l-1)*40+1,(h-1)*40+30,(l-1)*40+1,(h-1)*40+1);
line((l-1)*40+10,(h-1)*40+30,(l-1)*40+1,(h-1)*40+30);
line((l-1)*40+10,(h-1)*40+1,(l-1)*40+1,(h-1)*40+1);
line((l-1)*40+30,(h-1)*40+25,(l-1)*40+10,(h-1)*40+25);
line((l-1)*40+30,(h-1)*40+25,(l-1)*40+10,(h-1)*40+25);
line((l-1)*40+30,(h-1)*40+5,(l-1)*40+10,(h-1)*40+5);
line((l-1)*40+40,(h-1)*40+20,(l-1)*40+30,(h-1)*40+20);
line((l-1)*40+40,(h-1)*40+10,(l-1)*40+30,(h-1)*40+10);
line((l-1)*40+10,(h-1)*40+20,(l-1)*40+1,(h-1)*40+20);
line((l-1)*40+10,(h-1)*40+10,(l-1)*40+1,(h-1)*40+10);
line((l-1)*40+25,(h-1)*40+40,(l-1)*40+15,(h-1)*40+40);
line((l-1)*40+25,(h-1)*40+40,(l-1)*40+25,(h-1)*40+25);
line((l-1)*40+15,(h-1)*40+40,(l-1)*40+15,(h-1)*40+25);
circle((l-1)*40+20,(h-1)*40+15,5);
end;
4:begin
setcolor(green);
line((l-1)*40+30,(h-1)*40+40,(l-1)*40+1,(h-1)*40+40);
line((l-1)*40+30,(h-1)*40+30,(l-1)*40+1,(h-1)*40+30);
line((l-1)*40+30,(h-1)*40+40,(l-1)*40+30,(h-1)*40+30);
line((l-1)*40+1,(h-1)*40+40,(l-1)*40+1,(h-1)*40+30);
line((l-1)*40+30,(h-1)*40+10,(l-1)*40+1,(h-1)*40+10);
line((l-1)*40+30,(h-1)*40+1,(l-1)*40+1,(h-1)*40+1);
line((l-1)*40+30,(h-1)*40+10,(l-1)*40+30,(h-1)*40+1);
line((l-1)*40+1,(h-1)*40+10,(l-1)*40+1,(h-1)*40+1);
line((l-1)*40+25,(h-1)*40+30,(l-1)*40+25,(h-1)*40+10);
line((l-1)*40+5,(h-1)*40+30,(l-1)*40+5,(h-1)*40+10);
line((l-1)*40+20,(h-1)*40+40,(l-1)*40+20,(h-1)*40+30);
line((l-1)*40+10,(h-1)*40+40,(l-1)*40+10,(h-1)*40+30);
line((l-1)*40+20,(h-1)*40+10,(l-1)*40+20,(h-1)*40+1);
line((l-1)*40+10,(h-1)*40+10,(l-1)*40+10,(h-1)*40+1);
line((l-1)*40+40,(h-1)*40+25,(l-1)*40+40,(h-1)*40+15);
line((l-1)*40+40,(h-1)*40+25,(l-1)*40+25,(h-1)*40+25);
line((l-1)*40+40,(h-1)*40+15,(l-1)*40+25,(h-1)*40+15);
circle((l-1)*40+15,(h-1)*40+20,5);
end;
end;{case}
end;{with}
end;
procere runtank(ch:char);
begin
case ch of
'i':if tank.h>1 then dec(tank.h);
'k':if tank.h<10 then inc(tank.h);
'j':if tank.l>1 then dec(tank.l);
'l':if tank.l<15 then inc(tank.l);
'8':tank.f:=1;
'4':tank.f:=2;
'5':tank.f:=3;
'6':tank.f:=4;
'0':pushtype1(tank.l,tank.h,tank.f,bomb);
'q':halt;
end;
end;
function win:boolean;
var i:integer;
begin
for i:=1 to high(diren) do
if diren[i].f>0 then begin win:=false;exit;end;
win:=true;
end;
function lost:boolean;
var i:integer;
begin
for i:=1 to high(diren) do
if (diren[i].h=tank.h) and (diren[i].l=tank.l) and (diren[i].f>0) then
begin lost:=true;exit;end;
lost:=false;
end;
procere runbomb;
var i,hh,hl:integer;
begin
for i:=1 to high(bomb) do
with bomb[i] do
if f>0 then
begin
hh:=hh+fx[f,1];
hl:=l+fx[f,2];
if(hl in [1..15])and
(hh in [1..10]) then
begin
h:=hh;
l:=hl;
end
else begin f:=0;pushtype1(l,h,5,fire);end;
end;
end;
procere runfire;
var i:integer;
begin
for i:=1 to high(fire) do
begin
if fire[i].f>0 then dec(fire[i].f);
end;
end;
procere rundiren;
var s:set of 1..4;
i,j,k,hh1,hh2,hl1,hl2:integer;
begin
for i:=1 to high(diren) do
with diren[i] do
if f>0 then
begin
if h>tank.h then dec(h)
else if h<tank.h then inc(h);
if l<tank.l then inc(l)
else if l>tank.l then dec(l);
end;
end;
procere check;
var i,j:integer;
begin
for i:=1 to high(diren) do
with diren[i] do
if f>0 then
begin
for j:=1 to high(bomb) do
if (bomb[j].h=h) and (bomb[j].l=l) and (bomb[j].f>0)
and ((bomb[j].h<>tank.h) or (bomb[j].l<>tank.l)) then
begin
bomb[j].f:=0;f:=0;break;
end;
end;
end;
procere inittank;
var y:integer;
ch:char;
begin
settextstyle(7,horizdir,6);
setcolor(white);
outtextxy(10,10,'Tank2:Crazy tank');
settextstyle(1,horizdir,1);
y:=100;
outtextxy(50,y,'Mase by WangYu');
inc(y,textheight('H')+1);
outtextxy(50,y,'Ctrl key:');
inc(y,textheight('H')+1);
outtextxy(50,y,'Up:i Left:j Right:l Down:k Exit:q');
inc(y,textheight('H')+1);
outtextxy(50,y,'GunUp:8 GunLeft:4 GunRight:6 Fire:0');
inc(y,textheight('H')+100);
outtextxy(50,y,'Pressed Enter to continue');
repeat ch:=readkey;until ch=#13;
end;
procere init(ds:integer);
var i:integer;
st:string;
begin
fillchar(t,sizeof(t),0);
fillchar(diren,sizeof(diren),0);
fillchar(fire,sizeof(fire),0);
fillchar(bomb,sizeof(bomb),0);
tank.h:=5;tank.l:=8;tank.f:=1;
with diren[1] do begin h:=1;l:=7;f:=0;end;
with diren[2] do begin h:=10;l:=7;f:=0;end;
with diren[3] do begin h:=1;l:=1;f:=0;end;
with diren[4] do begin h:=10;l:=15;f:=0;end;
with diren[5] do begin h:=1;l:=15;f:=0;end;
with diren[6] do begin h:=10;l:=1;f:=0;end;
with diren[7] do begin h:=6;l:=1;f:=0;end;
with diren[8] do begin h:=6;l:=15;f:=0;end;
for i:=1 to ds do diren[i].f:=1;
str(ds,st);
print;
setcolor(black);
outtextxy(200,200,'Level'+st);
readkey;
end;
procere main;
var ch:char;
begin
while not lost and not win do
begin
delay(50);
with t do
begin
inc(ms,50);
if ms>=1000 then
begin
ms:=0;
inc(s);
if s>=60 then
s:=0;
inc(m);
end;
end;
if keypressed then
begin
ch:=readkey;
runtank(ch);
end;
if t.ms mod 200=0 then rundiren;
if t.ms mod 100=0 then check;
if t.ms mod 100=0 then begin runbomb;lf:=true;end;
if t.ms mod 100=0 then runfire;
print;
end;
setcolor(black);
if win then outtextxy(200,200,'You win!!!')
else outtextxy(200,200,'You lost!!!');
repeat
ch:=readkey;
until ch=#13;
end;
var level:integer;
begin
initg;
inittank;
for level:=1 to 8 do
begin
init(level);
main;
if lost then dec(level);
end;
end.
這是每個游戲編程FAQ里都有的問題。這個問題每星期都會在游戲開發論壇上被問上好幾次。這是個很好的問題,但是,沒人能給出簡單的答案。在某些應用程序中,總有一些計算機語言優於其他語言。下面是幾種用於編寫游戲的主要編程語言的介紹及其優缺點。希望這篇文章能幫助你做出決定。
可以編一個最簡單的猜數游戲
我這有一個俄羅斯方塊源碼:
USES Crt;
CONST
Change:Array [0..6,0..3,0..7] Of Byte =(((0,1,1,1,2,1,3,1),(1,0,1,1,1,2,1,3),(0,1,1,1,2,1,3,1),(1,0,1,1,1,2,1,3)),
((1,0,0,1,1,1,2,1),(1,0,1,1,1,2,2,1),(0,1,1,1,2,1,1,2),(1,0,0,1,1,1,1,2)),
((1,0,2,0,1,1,2,1),(1,0,2,0,1,1,2,1),(1,0,2,0,1,1,2,1),(1,0,2,0,1,1,2,1)),
((1,0,2,0,0,1,1,1),(0,0,0,1,1,1,1,2),(1,0,2,0,0,1,1,1),(0,0,0,1,1,1,1,2)),
((0,0,1,0,1,1,2,1),(1,0,0,1,1,1,0,2),(0,0,1,0,1,1,2,1),(1,0,0,1,1,1,0,2)),
((1,0,2,0,1,1,1,2),(0,0,0,1,1,1,2,1),(1,0,0,2,1,1,1,2),(2,2,0,1,1,1,2,1)),
((0,0,1,0,1,1,1,2),(2,0,0,1,1,1,2,1),(2,2,1,0,1,1,1,2),(0,2,0,1,1,1,2,1)));
VAR
Board:Array [0..3,0..11,1..25] Of Byte;
Players,N,Nowx,Nowy,Kind,Trans,Speed:Byte;
Time,Score:Word;
Now:Array [0..7] Of Byte;
PROCEDURE Furbish;
VAR B,C:Byte;
Begin
For C:=24 Downto 2 Do Begin
Gotoxy(1,C);
For B:=1 To 10 Do
If Board[0,B,C]=0 Then Write(' ')
Else Write('壙');
End;
End;
PROCEDURE Clear;
Var A,B,C:Byte;D:Boolean;
Begin
For A:=24 Downto 1 Do
Begin
D:=True;
For B:=1 To 10 Do
If Board[0,B,A]=0 Then D:=False;
If D=True Then
Begin
Score:=Score+10;Gotoxy(1,1);Write(Score:5,'0');
For C:=A Downto 2 Do
For B:=1 To 10 Do
Board[0,B,C]:=Board[0,B,C-1];
A:=A+1;
End;
End;
Furbish;
End;
FUNCTION Canmove(X,Y:Byte):Boolean;
BEGIN
Canmove:=True;
If Board[0,X+Now[0],Y+Now[1]]>0 Then Canmove:=False;
If Board[0,X+Now[2],Y+Now[3]]>0 Then Canmove:=False;
If Board[0,X+Now[4],Y+Now[5]]>0 Then Canmove:=False;
If Board[0,X+Now[6],Y+Now[7]]>0 Then Canmove:=False;
End;
PROCEDURE Clean;
Begin
Gotoxy((Nowx+Now[0])*2-1,Nowy+Now[1]);Write(' ');
Gotoxy((Nowx+Now[2])*2-1,Nowy+Now[3]);Write(' ');
Gotoxy((Nowx+Now[4])*2-1,Nowy+Now[5]);Write(' ');
Gotoxy((Nowx+Now[6])*2-1,Nowy+Now[7]);Write(' ');
End;
PROCEDURE Show;
Begin
Gotoxy((Nowx+Now[0])*2-1,Nowy+Now[1]);Write('壙');
Gotoxy((Nowx+Now[2])*2-1,Nowy+Now[3]);Write('壙');
Gotoxy((Nowx+Now[4])*2-1,Nowy+Now[5]);Write('壙');
Gotoxy((Nowx+Now[6])*2-1,Nowy+Now[7]);Write('壙');
End;
BEGIN
Fillchar(Board,Sizeof(Board),0);
Randomize;Score:=0;
For N:=1 To 24 Do
Board[0,0,N]:=1;
For N:=1 To 24 Do
Board[0,11,N]:=1;
For N:=1 To 10 Do
Board[0,N,25]:=1;
Window(31,2,50,25);Textcolor(White);Textbackground(Blue);
Clrscr;Window(31,2,51,25);
Speed:=1;
Kind:=Random(7);Trans:=Random(4);Nowx:=4;Nowy:=1;
For N:=0 To 7 Do
Now[N]:=Change[Kind,Trans,N];
While Canmove(Nowx,Nowy) Do
Begin
Repeat
Clean;Nowy:=Nowy+1;Show;
Repeat
If Keypressed Then
Case Upcase(Readkey) Of
#0:Case Readkey Of
#75:If Canmove(Nowx-1,Nowy) Then Begin Clean;Nowx:=Nowx-1;Show;End;
#77:If Canmove(Nowx+1,Nowy) Then Begin Clean;Nowx:=Nowx+1;Show;End;
#80:Begin Clean;Repeat
If Canmove(Nowx,Nowy+1) Then Nowy:=Nowy+1;
Until Not(Canmove(Nowx,Nowy+1));Show;End;
#61:Begin Gotoxy(9,12);Write('Pause');Repeat Delay(1000);Until Keypressed;Furbish;End;
End;
#27:Exit;
' ',#13:Begin
Clean;Trans:=Trans+1;
If Trans=4 Then Trans:=0;
For N:=0 To 7 Do
Now[N]:=Change[Kind,Trans,N];
If Not(Canmove(Nowx,Nowy)) Then Begin Trans:=Trans-1;For N:=0 To 7 Do
Now[N]:=Change[Kind,Trans,N];Show;End
Else Show;
End;
End;
Until Not(Keypressed);
Delay((10-Speed)*50);
Until Not(Canmove(Nowx,Nowy+1));
Score:=Score+1;Gotoxy(1,1);Write(Score:5,'0');Speed:=(Score Div 300)+1;
Board[0,Nowx+Now[0],Nowy+Now[1]]:=1;
Board[0,Nowx+Now[2],Nowy+Now[3]]:=1;
Board[0,Nowx+Now[4],Nowy+Now[5]]:=1;
Board[0,Nowx+Now[6],Nowy+Now[7]]:=1;
Clear;
Kind:=Random(7);Trans:=Random(4);Nowx:=4;Nowy:=1;
For N:=0 To 7 Do
Now[N]:=Change[Kind,Trans,N];
End;
Gotoxy(7,12);Write('GAME OVER');Readln;
END.
❾ 怎樣才能快速學會C語言編程
資料內容所涉及方面: C/C++/VC++的基礎教程、通訊、協議分析、游戲開發、圖像處理、管理系統、多媒體、加密與解密、控制項ACTIVEX、開發CGI、標准程式庫、技術內幕、Windows核心編程、對象模型、人工智慧、軟體工程、數據結構、WindowsAPI、Visual Basic 、Directx、網路開發、資料庫開發、ADO、神經網路、DLL和內存管理、OLE&DDE、操作系統、埠、數值演算法、等等技術或相關開發資料。 《C語言專區》 C語言實例教程 1.72 http://download.chinaitlab.com/soft/8658.htm 經典編程900例(C語言) http://download.chinaitlab.com/soft/7972.htm C語言實例教程 http://download.chinaitlab.com/soft/6530.htm C語言圖象處理方法 http://download.chinaitlab.com/soft/6514.htm C語言編程實例 http://download.chinaitlab.com/soft/6103.htm C語言書打包下載 http://download.chinaitlab.com/soft/6041.htm 集成學習環境(C語言) http://download.chinaitlab.com/soft/5764.htm C語言聖經 1.0 http://download.chinaitlab.com/soft/5745.htm 用C實現面向對象 http://download.chinaitlab.com/soft/7566.htm C語言高級實用編程技巧 http://download.chinaitlab.com/soft/4593.htm C語言最新編程技巧200例 http://download.chinaitlab.com/soft/4594.htm C語言速成3.0 http://download.chinaitlab.com/soft/4723.htm C語言編程技巧程序集 http://download.chinaitlab.com/soft/4561.htm C語言速成多媒件課件 3.0 http://download.chinaitlab.com/soft/3238.htm C語言多媒體教程 http://download.chinaitlab.com/soft/2893.htm C程序設計培訓 http://download.chinaitlab.com/soft/6340.htm 實用C語言詳解 http://download.chinaitlab.com/soft/3274.htm C語言程序設計及應用實例 http://download.chinaitlab.com/soft/3241.htm C語言程序寶典 http://download.chinaitlab.com/soft/3071.htm C語言教程 http://download.chinaitlab.com/soft/3027.htm 《C語言編程寶典》 http://download.chinaitlab.com/soft/1849.htm C語言常用函數手冊 http://download.chinaitlab.com/soft/1818.htm C語言學習系統 http://download.chinaitlab.com/soft/1816.htm C語言技術文章 http://download.chinaitlab.com/soft/1817.htm C常用演算法程序集 http://download.chinaitlab.com/soft/6225.htm 微軟C編程精粹 http://download.chinaitlab.com/soft/6166.htm 《C++語言專區》 Inside C++ Object Model http://download.chinaitlab.com/soft/7851.htm C++ 入門基礎教程 http://download.chinaitlab.com/soft/7617.htm C++和面向對象的數值計算 http://download.chinaitlab.com/soft/6987.htm 經典C++語言教程 http://download.chinaitlab.com/soft/7399.htm C++編程實例詳解 http://download.chinaitlab.com/soft/6988.htm C_C++深層探索 http://download.chinaitlab.com/soft/6989.htm C_C++程序設計 http://download.chinaitlab.com/soft/6990.htm C++輸入輸出流及本地化 http://download.chinaitlab.com/soft/6986.htm 經典C++語言教程 http://download.chinaitlab.com/soft/6978.htm C++ Primer 3rd Edition http://download.chinaitlab.com/soft/6823.htm C++_Primer_Plus_4th http://download.chinaitlab.com/soft/6819.htm C++應用編程200例 http://download.chinaitlab.com/soft/6820.htm C++面向對象高效編程 http://download.chinaitlab.com/soft/6610.htm Borland C++ 3.0自學培訓教程 http://download.chinaitlab.com/soft/6274.htm 經典C++ 語言教程 http://download.chinaitlab.com/soft/6600.htm C++Builder自學培訓教程 http://download.chinaitlab.com/soft/6317.htm Borland C++ Builder高級編程 http://download.chinaitlab.com/soft/7927.htm 高質量C_C++編程指南 http://download.chinaitlab.com/soft/7916.htm C++程序設計培訓教程 http://download.chinaitlab.com/soft/6339.htm C++程序設計教程 http://download.chinaitlab.com/soft/6338.htm C++Builder核心program http://download.chinaitlab.com/soft/6318.htm C++Builder基礎進階 http://download.chinaitlab.com/soft/6316.htm C++Builder30開發指南 創世紀的C++ Builder教程 http://download.chinaitlab.com/soft/6175.htm http://download.chinaitlab.com/soft/6311.htm
❿ 想做一個Win32下的小游戲編程,但沒什麼經驗,有什麼好一點的老師視頻講解么,要詳細一點的,求~
或許你可以看看相關書籍,比如c
語言游戲
基礎…………
