C语言游戏源代码
用C语言编写的Tic-Tac-Toe游戏源代码(井字过三关)
board[row][colume] = (player == 1) ? 'X' :'O'; //Check for a winning line - diagonals first if((board[0][0]==board[1][1] && board[0][0]==board[2][2]) || (board[0][2]==board[1][1] && board[0][2]==board[2][0])) winner = player; else { //Check rows and columes for a winning line for(unsigned int line = 0; line <= 2; ++line) { if((board[line][0]==board[line][1] && board[line][0]==board[line][2]) || (board[0][line]==board[1][line] && board[0][line]==board[2][line])) winner = player; } } } //Game is over so display the final board printf("\n"); printf(" %c | %c | %c\n", board[0][0], board[0][1], board[0][2]); printf("---+---+---\n"); printf(" %c | %c | %c\n", board[1][0], board[1][1], board[1][2]); printf("---+---+---\n"); printf(" %c | %c | %c\n", board[2][0], board[2][1], board[2][2]); //Display result message if(winner) printf("\nCongratulations, player %d, YOU ARE THE WINNER!\n", winner); else printf("\nHow boring, it is a draw\n"); return 0; }
魂斗罗c语言源代码
"魂斗罗"(Contra)是一款经典的横版射击游戏,由Konami公司开发。
编写一个完整的魂斗罗游戏是一个复杂的过程,涉及图形渲染、物理模拟、碰撞检测、音效处理等多个方面。
通常,这样的游戏会使用专业的游戏引擎或者特定的框架来开发,而不是直接用C语言从头开始。
c#include<stdio.h>#include<stdbool.h>// 定义玩家和敌人的结构体typedef struct {int x;int y;bool isAlive;} Player;typedef struct {int x;int y;bool isAlive;int speed; // 敌人的移动速度} Enemy;// 定义游戏的常量const int SCREEN_WIDTH = 800;const int SCREEN_HEIGHT = 600;const int PLAYER_SPEED = 5;const int BULLET_SPEED = 10;const int ENEMY_SPEED = 3;// 初始化玩家和敌人Player player = {SCREEN_WIDTH / 2, SCREEN_HEIGHT - 50, true};Enemy enemy = {SCREEN_WIDTH, SCREEN_HEIGHT / 2, true, ENEMY_SPEED};// 游戏主循环void gameLoop() {while (player.isAlive && enemy.isAlive) {// 处理玩家输入char input;printf("Move player (w/a/s/d): ");scanf(" %c", &input);switch (input) {case'w':if (player.y > 0) {player.y -= PLAYER_SPEED;}break;case'a':if (player.x > 0) {player.x -= PLAYER_SPEED;}break;case's':if (player.y < SCREEN_HEIGHT - 50) {player.y += PLAYER_SPEED;}break;case'd':if (player.x < SCREEN_WIDTH - 50) {player.x += PLAYER_SPEED;}break;}// 移动敌人enemy.x -= enemy.speed;// 检查碰撞if (player.x < enemy.x + 50 && player.x + 50 > enemy.x && player.y < enemy.y + 50 && player.y + 50 > enemy.y) {player.isAlive = false;printf("Game Over!\n");}// 打印游戏状态(仅用于演示)printf("Player position: (%d, %d)\n", player.x, player.y);printf("Enemy position: (%d, %d)\n", enemy.x, enemy.y);}}int main() {printf("Welcome to Contra Demo!\n");gameLoop();return0;}这。
C语言小游戏源代码《贪吃蛇》
void init(void){/*构建图形驱动函数*/ int gd=DETECT,gm; initgraph(&gd,&gm,""); cleardevice(); }
欢迎您阅读该资料希望该资料能给您的学习和生活带来帮助如果您还了解更多的相关知识也欢迎您分享出来让我们大家能共同进步共同成长
C 语言小游戏源代码《贪吃பைடு நூலகம்》
#define N 200/*定义全局常量*/ #define m 25 #include <graphics.h> #include <math.h> #include <stdlib.h> #include <dos.h> #define LEFT 0x4b00 #define RIGHT 0x4d00 #define DOWN 0x5000 #define UP 0x4800 #define Esc 0x011b int i,j,key,k; struct Food/*构造食物结构体*/ { int x; int y; int yes; }food; struct Goods/*构造宝贝结构体*/ { int x; int y; int yes; }goods; struct Block/*构造障碍物结构体*/ { int x[m]; int y[m]; int yes; }block; struct Snake{/*构造蛇结构体*/ int x[N]; int y[N]; int node; int direction; int life; }snake; struct Game/*构建游戏级别参数体*/ { int score; int level; int speed;
俄罗斯方块c语言源代码
俄罗斯方块c语言源代码俄罗斯方块游戏是一款非常受欢迎的游戏,使用C语言编写源代码实现其功能。
下面是俄罗斯方块游戏的C语言源代码:1. 创建窗口函数: // 创建窗口函数 void CreateWindow(int width, int height) { // 使用SDL库创建窗口 SDL_Init(SDL_INIT_EVERYTHING); SDL_Window *window = SDL_CreateWindow("Tetris",SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,width, height, 0); // 设置刷新时间SDL_SetHint(SDL_HINT_RENDER_VSYNC, "1"); }2. 创建游戏函数: // 创建游戏函数 void CreateGame() { // 设置随机数种子srand((unsigned int)time(NULL)); // 加载游戏资源 LoadResources(); // 初始化游戏数据InitGameData(); // 初始化游戏界面InitGameUI(); // 开始游戏循环 GameLoop(); // 清理游戏资源 CleanupGame(); }3. 绘图函数: // 绘图函数 void Draw(int x, inty, Color color) { // 使用SDL库在指定位置绘制指定颜色的矩形 SDL_Rect rect; rect.x = x;rect.y = y; rect.w = BLOCK_SIZE; rect.h = BLOCK_SIZE; SDL_SetRenderDrawColor(renderer, color.r, color.g, color.b, color.a);SDL_RenderFillRect(renderer, &rect); }。
C语言游戏源代码
C语言游戏源代码1、简单的开机密码程序#include "conio.h"#include "string.h"#include "stdio.h"void error(){window(12,10,68,10);textbackground(15);textcolor(132);clrscr();cprintf("file or system error! you can't enter the system");while(1); /*若有错误不能通过程序*/}void look(){FILE *fauto,*fbak;char *pass="c:\\windows\\password.exe"; /*本程序的位置*/char a[25],ch;char *au="autoexec.bat",*bname="hecfback.^^^"; /*bname 是autoexec.bat 的备份*/setdisk(2); /*set currently disk c:*/chdir("\\"); /*set currently directory \*/fauto=fopen(au,"r+");if (fauto==NULL){fauto=fopen(au,"w+");if (fauto==NULL) error();}fread(a,23,1,fauto); /*读取autoexec.bat前23各字符*/a[23]='\0';if (strcmp(a,pass)==0) /*若读取的和pass指针一样就关闭文件,不然就添加*/fclose(fauto);else{fbak=fopen(bname,"w+");if (fbak==NULL) error();fwrite(pass,23,1,fbak);fputc('\n',fbak);rewind(fauto);while(!feof(fauto)){ch=fgetc(fauto);fputc(ch,fbak);}rewind(fauto);rewind(fbak);while(!feof(fbak)){ch=fgetc(fbak);fputc(ch,fauto);}fclose(fauto);fclose(fbak);remove(bname); /*del bname file*/}}void pass(){char *password="88888888";char input[60];int n;while(1){window(1,1,80,25);textbackground(0);textcolor(15);clrscr();n=0;window(20,12,60,12);textbackground(1);textcolor(15);clrscr();cprintf("password:");while(1){input[n]=getch();if (n>58) {putchar(7); break;} /*若字符多于58个字符就结束本次输入*/ if (input[n]==13) break;if (input[n]>=32 && input[n]<=122) /*若字符是数字或字母才算数*/ {putchar('*');n++;}if (input[n]==8) /*删除键*/if (n>0){cprintf("\b \b");input[n]='\0';n--;}}input[n]='\0';if (strcmp(password,input)==0)break;else{putchar(7);window(30,14,50,14);textbackground(15);textcolor(132);clrscr();cprintf("password error!");getch();}}}main(){look();pass();}2、彩色贪吃蛇#include <graphics.h>#include <stdlib.h>#define N 200#define up 0x4800#define down 0x5000#define left 0x4b00#define right 0x4d00#define esc 0x011b#define Y 0x1579#define n 0x316eint gamespeed; /* 游戏速度*/int i, key, color;int score = 0; /* 游戏分数*/char cai48H[] ={0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,0x04, 0x00, 0x18, 0x00, 0x00, 0x00, 0x0E, 0x00,0x1C, 0x00, 0x00, 0x00, 0x1C, 0x00, 0x1C, 0x00,0x00, 0x00, 0x20, 0x00, 0x38, 0x00, 0x00, 0x00,0x40, 0x00, 0x78, 0x00, 0x00, 0x01, 0x80, 0x40,0x70, 0x00, 0x00, 0x03, 0x80, 0xC0, 0xE0, 0x00,0x00, 0x07, 0x80, 0x80, 0xC0, 0x00, 0x00, 0x0E,0x11, 0x81, 0xC0, 0x00, 0x00, 0x08, 0x61, 0x01,0x80, 0x00, 0x00, 0x00, 0x23, 0x03, 0x04, 0x00,0x00, 0x02, 0x02, 0x00, 0x06, 0x00, 0x00, 0x1E,0x04, 0x00, 0x0F, 0x00, 0x00, 0x1C, 0x1F, 0x80,0x1E, 0x00, 0x00, 0x08, 0x3F, 0x80, 0x3C, 0x00,0x00, 0x00, 0xFF, 0x80, 0x38, 0x00, 0x00, 0x03,0xFF, 0x80, 0x78, 0x00, 0x00, 0x0F, 0xF8, 0x00,0xF0, 0x00, 0x00, 0x7F, 0xF0, 0x00, 0xE0, 0x00,0x03, 0xFF, 0xFC, 0x01, 0x80, 0x00, 0x03, 0xC0,0x03, 0x80, 0x00, 0x01, 0x3F, 0x00, 0x07, 0x80, 0x00, 0x02, 0x11, 0x00, 0x07, 0x00, 0x00, 0x00, 0x10, 0x00, 0x07, 0x00, 0x00, 0x00, 0x10, 0x00, 0x0E, 0x00, 0x00, 0x08, 0x10, 0x00, 0x1C, 0x00, 0x00, 0x30, 0x10, 0x00, 0x18, 0x00, 0x00, 0x70, 0x10, 0x00, 0x30, 0x00, 0x01, 0xE0, 0x10, 0x00, 0x70, 0x00, 0x03, 0x80, 0x10, 0x00, 0x60, 0x00, 0x00, 0x00, 0x30, 0x00, 0xE0, 0x00, 0x00, 0x00, 0xF0, 0x01, 0xC0, 0x00, 0x00, 0x00, 0x70, 0x03, 0xC0, 0x00, 0x00, 0x00, 0x10, 0x07, 0x80, 0x00, 0x00, 0x00, 0x00, 0x0F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1E, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x70, 0x00, 0x00, 0x00, 0x00, 0x01, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, };char she48H[] ={0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0E, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0E, 0x00, 0x00, 0x00, 0x03, 0x00, 0x07, 0x00, 0x00, 0x00, 0x02, 0x00, 0x03, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0xF8, 0x00, 0x00, 0x02, 0x00, 0x07, 0x86, 0x00, 0x00, 0x02, 0x00, 0x18, 0x03, 0x00, 0x00, 0x02, 0x00, 0x00, 0x07, 0x80, 0x00, 0x03, 0xF0, 0x00, 0x07, 0x80, 0x00, 0x0F, 0xFC, 0x00, 0x0C, 0x00, 0x00, 0x7E, 0x3F, 0x80, 0x00, 0x00, 0x01, 0xFE, 0x1F, 0x80, 0x00, 0x00, 0x01, 0xE2, 0x39, 0x8C, 0x00, 0x00, 0x00, 0xC2, 0x30, 0x08, 0x00, 0x00, 0x00, 0xC2, 0x60, 0x08, 0x00, 0x00, 0x00, 0xC3, 0xE0, 0x08, 0x60, 0x00, 0x00, 0x7F, 0xE0, 0x01, 0xE0, 0x00, 0x00, 0x3F, 0x80, 0x1F, 0xE0, 0x00, 0x00, 0x1E, 0x00, 0x1F, 0x80, 0x00, 0x00, 0x1E, 0x00, 0x1F, 0x00, 0x00, 0x00, 0x02, 0x38, 0x1E, 0x00, 0x00, 0x00, 0x07, 0xFC, 0x1C,0x00, 0x1F, 0x0C, 0x10, 0x00, 0x20, 0x00, 0x7C, 0x04, 0x10, 0x00, 0x60, 0x01, 0xF0, 0x00, 0x10, 0x00, 0x60, 0x01, 0xE0, 0x00, 0x08, 0x00, 0xF0, 0x00, 0x80, 0x00, 0x08, 0x03, 0xF0, 0x00, 0x00, 0x00, 0x07, 0xFF, 0xF0, 0x00, 0x00, 0x00, 0x07, 0xFF, 0xF0, 0x00, 0x00, 0x00, 0x03, 0xFF, 0xE0, 0x00, 0x00, 0x00, 0x01, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, };char tun48H[] ={0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0E, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3E, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7F, 0x00, 0x00, 0x00, 0x00, 0x00, 0xE0, 0x00, 0x00, 0x00, 0x00, 0x03, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x1F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7C, 0x00, 0x00, 0x00, 0x00, 0x01, 0xF8, 0x00, 0x00, 0x00, 0x00, 0x03, 0xF8, 0x00, 0x40, 0x00, 0x00, 0x00, 0x06, 0x07, 0xC0, 0x00, 0x00, 0x00, 0x07, 0xFF, 0xE0, 0x00, 0x00, 0x00, 0x07, 0xFF, 0xE0, 0x00, 0x00, 0x00, 0x0F, 0xFF, 0x80, 0x00, 0x00, 0x00, 0x7F, 0xF8, 0x00, 0x00, 0x00, 0x1F, 0xFF, 0xF8, 0x00, 0x00, 0x00, 0x1F, 0xFF, 0xF8, 0x00, 0x00, 0x00, 0x1F, 0xFC, 0x3C, 0x00, 0x00, 0x00, 0x0F, 0xF8, 0x0E, 0x00, 0x00, 0x00, 0x04, 0x70, 0x07, 0x00, 0x00, 0x00, 0x00, 0x60, 0x03, 0x80, 0x00, 0x00, 0x00, 0xC0, 0x00, 0xC0, 0x00, 0x00, 0x01, 0x80, 0x00, 0x30, 0x00, 0x00, 0x01, 0x00, 0x3C, 0x18, 0x00, 0x00, 0x02, 0x03, 0xFF, 0x0C, 0x00, 0x00, 0x0C, 0x7F, 0xFF, 0x8E, 0x00, 0x00, 0x18, 0xFF, 0xFF, 0xC7, 0x80, 0x00, 0x78, 0xFE, 0x07, 0x87, 0xE0, 0x01, 0xF0, 0x70, 0x07, 0x03, 0xF8, 0x07, 0xE0, 0x70, 0x0E, 0x03, 0xFE, 0x00, 0x00, 0x38, 0x1E, 0x01, 0xFE, 0x00, 0x00, 0x3F, 0xFE, 0x00, 0x0C, 0x00, 0x00, 0x1F, 0xFE, 0x00, 0x00, 0x00, 0x00,0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, };char dan48H[] ={0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFC, 0x00, 0x00, 0x00, 0x00, 0x07, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x7F, 0xC0, 0x80, 0x00, 0x00, 0x03, 0xFF, 0x80, 0x40, 0x00, 0x00, 0x01, 0xF1, 0x80, 0x40, 0x00, 0x00, 0x01, 0x81, 0x80, 0xE0, 0x00, 0x00, 0x00, 0x01, 0x93, 0xF0, 0x00, 0x00, 0x00, 0x01, 0xFF, 0xF0, 0x00, 0x00, 0x00, 0x21, 0xFF, 0xF0, 0x00, 0x00, 0x00, 0x21, 0xF8, 0x00, 0x00, 0x00, 0x00, 0x61, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x61, 0x80, 0x00, 0x00, 0x00, 0x00, 0xF3, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x01, 0xFF, 0xC0, 0x00, 0x00, 0x00, 0x03, 0xFF, 0xF8, 0x00, 0x00, 0x00, 0x02, 0x00, 0xFC, 0x00, 0x00, 0x00, 0x04, 0x02, 0x1F, 0x00, 0x00, 0x00, 0x08, 0x03, 0x01, 0xC0, 0x00, 0x00, 0x38, 0x03, 0x00, 0x7C, 0x00, 0x00, 0xF8, 0x07, 0xF8, 0x3F, 0xC0, 0x01, 0xF0, 0x3F, 0xFE, 0x3F, 0xF8, 0x03, 0xC1, 0xFF, 0x0F, 0x1F, 0xF8, 0x00, 0x01, 0xE3, 0x0F, 0x0F, 0xF0, 0x00, 0x01, 0xC3, 0x0E, 0x00, 0x00, 0x00, 0x01, 0x83, 0xFC, 0x00, 0x00, 0x00, 0x00, 0xC7, 0xF8, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xF8, 0x00, 0x00, 0x00, 0x00, 0x7F, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x3F, 0x03, 0x80, 0x00, 0x00, 0x00, 0x03, 0x04, 0x00, 0x00, 0x00, 0x00, 0x03, 0xF8, 0x00, 0x00, 0x00, 0x00, 0x1F, 0xF8, 0x20, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xE0, 0x00, 0x00, 0x07, 0xFF, 0x81, 0xE0, 0x00, 0x00, 0x07, 0xE0, 0x00, 0xE0, 0x00, 0x00, 0x03, 0x00, 0x00, 0x60, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, };char zuo16H[] ={0x18, 0xC0, 0x18, 0xC0, 0x19, 0x80, 0x31, 0xFE, 0x33, 0xFE, 0x76, 0xC0, 0xF0, 0xFC, 0xB0, 0xFC, 0x30, 0xC0, 0x30, 0xC0, 0x30, 0xFE, 0x30, 0xFE, 0x30, 0xC0, 0x30, 0xC0, 0x30, 0xC0, 0x00, 0x00, };char zhe16H[] ={0x03, 0x00, 0x03, 0x0C, 0x1F, 0xCC, 0x1F, 0xD8, 0x03, 0x30, 0xFF, 0xFE, 0xFF, 0xFE, 0x03, 0x00, 0x0F, 0xF8, 0x3F, 0xF8, 0xEC, 0x18, 0xCF, 0xF8, 0x0C, 0x18, 0x0F, 0xF8, 0x0F, 0xF8, 0x00, 0x00, };char tian16H[] ={0x00, 0x00, 0x3F, 0xFC, 0x3F, 0xFC, 0x31, 0x8C, 0x31, 0x8C, 0x31, 0x8C, 0x3F, 0xFC, 0x3F, 0xFC, 0x31, 0x8C, 0x31, 0x8C, 0x31, 0x8C, 0x3F, 0xFC, 0x3F, 0xFC, 0x30, 0x0C, 0x00, 0x00, 0x00, 0x00, };char xue16H[] ={0x33, 0x18, 0x19, 0x98, 0x08, 0xB0, 0x7F, 0xFC, 0x7F, 0xFC, 0x60, 0x0C, 0x1F, 0xF0, 0x1F, 0xF0, 0x00, 0xC0, 0x7F, 0xFC, 0x7F, 0xFC, 0x01, 0x80, 0x01, 0x80, 0x07, 0x80, 0x03, 0x00, 0x00, 0x00, };char ke16H[] ={0x00, 0x00, 0x0C, 0x18, 0xFD, 0x98, 0xF8, 0xD8, 0x18, 0x58, 0xFE, 0x18, 0xFE, 0x98, 0x18, 0xD8, 0x3C, 0x58, 0x7E, 0x1E, 0xDB, 0xFE, 0x9B, 0xF8, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x00, 0x00, };struct Food/*定义结构体存储食物的属性*/{int x; /* 食物的坐标*/int y;int yes; /* 值为0表示屏幕上没有食物,值为1表示屏幕上有食物*/ int color; /* 食物颜色*/} food;struct Snake/*定义结构体存储蛇的属性*/{int x[N]; /* 每一节蛇的坐标*/int y[N];int color[N];/*存储每一节蛇的颜色*/int node; /* 蛇的节数*/int direction; /* 蛇移动的方向*/int life; /* 蛇的生命,如果为1,蛇死,游戏结束*/} snake;void init(void)/*图形驱动*/{int driver = DETECT, mode = 0;registerbgidriver(EGAVGA_driver);initgraph(&driver, &mode, "");}void drawmat(char *mat, int matsize, int x, int y, int color) /*汉字点阵*/ {int i, j, k, m;m = (matsize - 1) / 8 + 1;for(j = 0; j < matsize; j++)for(i = 0; i < m; i++)for(k = 0; k < 8; k++)if(mat[j*m+i]&(0x80 >> k))putpixel(x + i * 8 + k, y + j, color);}void showword(void){/* 调用汉字点阵输出程序,显示标题和作者信息*/drawmat(cai48H, 48, 249, -4, 7);drawmat(she48H, 48, 329, -4, 7);drawmat(tun48H, 48, 409, -4, 7);drawmat(dan48H, 48, 489, -4, 7);drawmat(cai48H, 48, 250, -5, 4);drawmat(she48H, 48, 330, -5, 4);drawmat(tun48H, 48, 410, -5, 4);drawmat(dan48H, 48, 490, -5, 4);/*作者田学科*/drawmat(zuo16H, 16, 515, 465, 7);drawmat(zhe16H, 16, 530, 465, 7);drawmat(tian16H, 16, 550, 465, 7);drawmat(xue16H, 16, 565, 465, 7);drawmat(ke16H, 16, 580, 465, 7); }void draw(void)/*画出四周的墙*/ {if(color == 15)color = 0;setcolor(++color);setlinestyle(SOLID_LINE, 0, 1);for(i = 30; i <= 600; i += 10){rectangle(i, 40, i + 10, 49);rectangle(i, 451, i + 10, 460);}for(i = 40; i < 450; i += 10){rectangle(30, i, 39, i + 10);rectangle(601, i, 610, i + 10);}}void prscore(void){/* 打印游戏分数*/char str[10];setfillstyle(SOLID_FILL, YELLOW);bar(50, 10, 200, 30);setcolor(6);settextstyle(0, 0, 2);sprintf(str, "score:%d", score);outtextxy(55, 15, str);}void gameover(void){cleardevice(); /* 清屏函数*/for(i = 0; i < snake.node; i++) /* 画出蛇死时的位置*/{setcolor(snake.color[i]);rectangle(snake.x[i], snake.y[i], snake.x[i] + 10, snake.y[i] + 10);}prscore(); /* 显示分数*/draw();showword();settextstyle(0, 0, 6);setcolor(7);outtextxy(103, 203, "GAME OVER");setcolor(RED);outtextxy(100, 200, "GAME OVER");}void gameplay(void)/* 玩游戏的具体过程*/{int flag, flag1;randomize();prscore();gamespeed = 50000;food.yes = 0; /* food.yes=0表示屏幕上没有食物*/snake.life = 1; /* snake.life=1表示蛇是活着的*/snake.direction = 4; /* 表示蛇的初始方向为向右*/snake.node = 2; /* 蛇的初始化为两节*/snake.color[0] = 2; /*两节蛇头初始化为绿色*/snake.color[1] = 2;snake.x[0] = 100;snake.y[0] = 100;snake.x[1] = 110;snake.y[1] = 100;food.color = random(15) + 1;while(1){while(1){if(food.yes == 0) /* 如果蛇活着*/{while(1){flag = 1;food.x = random(56) * 10 + 40;food.y = random(40) * 10 + 50;for(i = 0; i < snake.node; i++){if(food.x == snake.x[i] && food.y == snake.y[i])flag = 0;}if(flag) break;}}if(food.yes){setcolor(food.color);rectangle(food.x, food.y, food.x + 10, food.y + 10);}for(i = snake.node - 1; i > 0; i--){snake.x[i] = snake.x[i-1];snake.y[i] = snake.y[i-1];}switch(snake.direction){case 1:snake.y[0] -= 10;break;case 2:snake.y[0] += 10;break;case 3:snake.x[0] -= 10;break;case 4:snake.x[0] += 10;break;}for(i = 3; i < snake.node; i++){if(snake.x[i] == snake.x[0] && snake.y[i] == snake.y[0]) {gameover();break;}}if(snake.x[0] < 40 || snake.x[0] > 590 || snake.y[0] < 50 || snake.y[0] > 440) {gameover();snake.life = 0;}if(snake.life == 0)break;if(snake.x[0] == food.x && snake.y[0] == food.y) /*蛇吃掉食物*/{setcolor(0);rectangle(food.x, food.y, food.x + 10, food.y + 10);snake.x[snake.node] = -20;snake.y[snake.node] = -20;snake.color[snake.node] = food.color;snake.node++;food.yes = 0;food.color = random(15) + 1;score += 10;prscore();if(score % 100 == 0 && score != 0){for(i = 0; i < snake.node; i++) /* 画出蛇*/{setcolor(snake.color[i]);rectangle(snake.x[i], snake.y[i], snake.x[i] + 10, snake.y[i] + 10);}sound(200);delay(50000);delay(50000);delay(50000);delay(50000);delay(50000);delay(50000);nosound();gamespeed -= 5000;draw();}else{sound(500);delay(500);nosound();}}for(i = 0; i < snake.node; i++) /* 画出蛇*/{setcolor(snake.color[i]);rectangle(snake.x[i], snake.y[i], snake.x[i] + 10, snake.y[i] + 10);}delay(gamespeed);delay(gamespeed);flag1 = 1;setcolor(0);rectangle(snake.x[snake.node-1], snake.y[snake.node-1],snake.x[snake.node-1] + 10, snake.y[snake.node-1] + 10);if(kbhit() && flag1 == 1) /*如果没按有效键就重新开始循环*/{flag1 = 0;key = bioskey(0);if(key == esc)exit(0);else if(key == up && snake.direction != 2)snake.direction = 1;else if(key == down && snake.direction != 1)snake.direction = 2;else if(key == left && snake.direction != 4)snake.direction = 3;else if(key == right && snake.direction != 3)snake.direction = 4;}}if(snake.life == 0) /*如果蛇死了就退出循环*/break;}}void main(void){while(1){color = 0;init();cleardevice();showword();draw();gameplay();setcolor(15);settextstyle(0, 0, 2);outtextxy(200, 400, "CONTINUE(Y/N)?");while(1){key = bioskey(0);if(key == Y || key == n || key == esc)break;}if(key == n || key == esc)break;}closegraph();}3、c语言实现移动电话系统#include <stdio.h>#define GRID-SIZE 5#define SELECTED -1 /*低于矩阵中所有元素*/#define TRAFFIC-FILE “traffic.dat”/*关于交通数据的文件*/#define NUM-TRANSMITTERS 10 /*可用的发射器数量*/void get-traffic-data(int commuters[GRID-SIZE][GRID-SIZE],int salesforce[GRID-SIZE][GRID-SIZE],int weekends [GRID-SIZE][GRID-SIZE];voide print-matrix[GRID-SIZE][GRID-SIZE];intmain(void){int commuters[GRID-SIZE][GRID-SIZE];/*上午8:30的交通数据*/int salesforce[GRID-SIZE][GRID-SIZE]; /*上午11:00的交通数据*/int weekend[GRID-SIZE][GRID-SIZE];/*周末交通数据*/int commuter-weight, /*通勤人员数据的权重因子*/sale-weight,/*营销人员数据的权重因子*/weekend-weight;/*周末数据的权重因子*/int location-i,/*每个发射器的位置*/location-j;int current-max;/*和数据中当前的最大值*/int i,j,/*矩阵的循环计数器*/tr;/*发射器的循环计数器*//*填入并显示交通数据*/Get-traffic-data (commuters,salesforce,weekend);Printf(“8:30 A.M.WEEKDAY TRAFFIC DATA 、\n\n”)print-matrix(commuters);printf(“\n\n WEEKEND TRAFFIC DATA\n\n”);print-matrix(salesforce);printf(“\n\n WEEKEND TRAFFIC DATA\n\n”);printf_matrix(weekeng);/*请用户输入权重因子*/printf(“\n\nPlease input the following value:\n”);printf(“Weight (an interger>=0) for the 8:muter data>”)scanf(“%d”,&commuter_weight);printf(“weight(an integer>=0) for the weekeng data>”);scanf(“%d”,&weekend_weight);scanf(“%d”,&weekend_weight);/*计算并显示加权后求和的数据*/for (i=0;i<GRID_SIZE;++i)for (j=0;j<GRID_SIZE;++j)summed_data[i][j]=commuter_weight*commuter[i][j]+salesforce_weight*salesforce[i][j]+weekend_weight*weekend[i][j];printf(“\n\nThe weighted,summed data is :\n\n”);printf_matrix(summed_data);/*在summed_data矩阵中找出NUM_TRANSMITTERS个最大值,将坐标临时存储在location_i和location_j中,然后把最后的结果坐标输出*/printf(“\n\nLocations of the %d transmitters:\n\n”,NUM_TRANSMITTERS);for (tr=1;tr<=NUM_TRANSMITTERS;++tr){current_max=SELECTED;/*以一个过低的值为起点开始查找*/for (i=0;i<GRID_SIZE;++i){for(j=0;j<GRID_SIZE;++j){if(current_max<summed_data[i][j]){current_max=summed_data[i][j]){location_i=i;location_j=j;}}}/*将选中的单元赋一较低的值以避免下次再选中这一元素,显示查找结果*/ summed_data[location_i][location_j]=SELECTED;print f(“Transmitter %3d:at location %3d %3d\n”,tr,location_i,location_j);}return (0);}/**把TRAFFIC_FILE中的交通数据填充到3个GRID_SIZE×GRID_SIZE数组中*/voidget_traffic_data(int commuters[GRID_SIZE],/*输出*/int salesforce[GRID_SIZE][GRID_SIZE],/*输出*/int weekend[GRID_SIZE][GRID_SIZE],/*输出*/{int i,j; /*循环计数器*/FILE *fp; /*文件指针*/fq=fopen(TRAFFIC_FILE,“r”);for(i=0;i<GRID_SIZE;++i)for(j=0;j<GRID_SIZE;++j)fs canf(fp,“%d”,&commnters[i][j];for(i=0;i<GRID_SIZE;++j)for(j=0;j<GRID_SIZE;++j)fscanf(fq,“%d”,&weekend[i][j]);fclose(fq);}/**显示一个GRID_SIZE×GRID_SIZE整数矩阵的内容*/voidprint_matrix(int matrix[GRID_SIZE][GRID_SIZE]){int i,j; /*循环计数器*/for(i=0;i<GRID_SIZE;++j){ for(j=0;j<GRID_SIZE;++J)printf(“%3d”,matrix[i][j]);printf(“\n”);}}4、扑克牌游戏/*************************************Copyright(C) 2004-2005 vision,math,NJU.File Name: guess_card.cppAuthor: vision Version: 1.0 Data: 23-2-2004Description: 给你9张牌,然后让你在心中记住那张牌,然后电脑分组让你猜你记住的牌在第几组,然后猜出你记住的那张牌.Other: 出自儿童时的一个小魔术History:修改历史**************************************/#include <stdio.h>#include <stdlib.h>#include <string.h>#include <time.h>#include <assert.h>#define CARDSIZE 52 /*牌的总张数*/#define SUITSIZE 13 /*一色牌的张数*//*扑克牌结构*/typedef struct Card{char val;/*扑克牌面上的大小*/int kind :4; /*扑克牌的花色*/}Card;/*************************************************Function: // riffleDescription: // 洗牌,然后随机的得到9张牌,要求九张牌不能有重复. Calls: //Called By: // main()Table Accessed: //被修改的表(此项仅对于牵扯到数据库操作的程序)Table Updated: // 被修改的表(此项仅对于牵扯到数据库操作的程序)Input: //Card card[] 牌结构, int size 结构数组的大小Output: //Return: // voidOthers: // 此函数修改card[]的值,希望得到九张随机牌Bug: //此函数有bug,有时会产生两个相同的牌,有待修订*************************************************/void riffle(Card *cards, int size);/*************************************************Function: // showDescription: // 显示数组的内容Calls: //Called By: // main()Table Accessed: //被修改的表(此项仅对于牵扯到数据库操作的程序)Table Updated: // 被修改的表(此项仅对于牵扯到数据库操作的程序)Input: //Card *card 牌结构指针, int size 结构数组的大小Output: //Return: // voidOthers: //*************************************************/void show(const Card *cards, int size);/*************************************************Function: // groupingDescription: //把9张牌分别放到3个数组中,每组3张,a.e分组Calls: //Called By: // main()Table Accessed: //被修改的表(此项仅对于牵扯到数据库操作的程序)Table Updated: // 被修改的表(此项仅对于牵扯到数据库操作的程序)Input: //Card *card 牌结构指针, int size 结构数组的大小Output: //Return: // voidOthers: // 此函数修改*carr1,*carr2,* carr3的值*************************************************/void grouping(const Card *cards, Card *carr1, Card *carr2, Card *carr3);/*************************************************Function: // result_processDescription: //用递归计算,所选的牌Calls: // rshiftCalled By: // main()Table Accessed: //被修改的表(此项仅对于牵扯到数据库操作的程序)Table Updated: // 被修改的表(此项仅对于牵扯到数据库操作的程序)Input: //Card *carr1, Card *carr2, Card *carr3Output: //Return: // voidOthers: // 此函数修改*carr1,*carr2,* carr3的值*************************************************/Card* result_process(Card *carr1, Card *carr2, Card *carr3, int counter);/*************************************************Function: // rshiftDescription: //右移操作Calls: //Called By: // result_processTable Accessed: //被修改的表(此项仅对于牵扯到数据库操作的程序)Table Updated: // 被修改的表(此项仅对于牵扯到数据库操作的程序)Input: //Card *carr1, Card *carr2, Card *carr3 ,int counterOutput: //Return: // Card*Others: // 此函数修改*carr1,*carr2,* carr3的值*************************************************/void rshift(Card *carr1, Card *carr2, Card *carr3, int counter);void main(){Card cards[9]; /*存放九张牌*/Card carr1[3]; /*第一组牌,cards array 1*/Card carr2[3]; /*第二组牌,cards array 2*/Card carr3[3]; /*第三组牌,cards array 3*/int select = 0; /*玩家的选择*/Card *selected_card;/*存放玩家所记住(选)的牌*/int counter = 0;riffle(cards, 9); /*洗牌,得到九张牌*/puts("请记住一张牌千万别告诉我!最多经过下面三次我与你的对话,我就会知道你所记的那张牌!");puts("如果想继续玩,请准确的回答我问你的问题,根据提示回答!");puts("请放心,我不会问你你选了哪张牌的!");grouping(cards, carr1, carr2, carr3); /*把9张牌分别放到3个数组中,每组3张,a.e分组*/show(carr1, 3);show(carr2, 3);show(carr3, 3);puts("请告诉我你记住的那张牌所在行数");select = getchar();switch(select)/*分支猜你玩家记住的牌*/{case '1':selected_card = result_process(carr1, carr2, carr3, counter);break;case '2':selected_card = result_process(carr2, carr3, carr1, counter);break;case '3':selected_card = result_process(carr3, carr1, carr2, counter);break;default:puts("你在撒谎!不和你玩了!");fflush(stdin);getchar();exit(0);}if( selected_card ==NULL){fflush(stdin);getchar();exit(0);}puts("你猜的牌为:");show(selected_card, 1);puts("我猜的对吧,哈哈~~~~");fflush(stdin);getchar();}/*riffle的原代码*/void riffle(Card *cards, int size){char deck[CARDSIZE];/*临时数组,用于存储牌*/unsigned int seed;/*最为产生随机数的种的*/int deckp = 0; /*在牌的产生中起着指示作用*/seed = (unsigned int)time(NULL);srand(seed);/*洗牌*/while (deckp < CARDSIZE){char num = rand() % CARDSIZE;if ((memchr(deck, num, deckp)) == 0){assert(!memchr(deck,num,deckp));deck[deckp] = num;deckp++;}}/*找9张牌给card*/for (deckp = 0; deckp < size; deckp++){div_t card = div(deck[deckp], SUITSIZE);cards[deckp].val = "A23456789TJQK"[card.rem]; /*把余数给card.val*/ cards[deckp].kind = "3456"[card.quot]; /*把商给card.kind*/}}/*show的原代码,将会自动换行*/void show(const Card *cards, int size){for(int i = 0; i < size; i++){printf("%c%c ",cards[i].kind,cards[i].val);if( (i !=0) && (((i+1 ) % 3) == 0))puts("");}puts(""); /*自动换行*/}/*grouping 的原代码*/void grouping(const Card *cards, Card *carr1, Card *carr2, Card *carr3) {int i = 0;/*循环参数*//*分给carr1三个数*/while (i < 3){carr1[i].val = cards[i].val;carr1[i].kind = cards[i].kind;i++;}/*分给carr2接下来的三个数*/while (i < 6){carr2[i-3].val = cards[i].val;carr2[i-3].kind = cards[i].kind;i++;}/*分给carr3接下来的三个数*/while (i < 9){carr3[i-6].val = cards[i].val;carr3[i-6].kind = cards[i].kind;i++;}}/*rshift的实现*/void rshift(Card *carr1, Card *carr2, Card *carr3, int counter){Card temp2;/*用于存放carr2[counter]*/Card temp3;/*用于存放carr3[counter]*//*temp = carr2*/temp2.val = carr2[counter].val;temp2.kind = carr2[counter].kind;/*carr2 = carr1*/carr2[counter].val = carr1[counter].val;carr2[counter].kind = carr1[counter].kind;/*temp3 = carr3*/temp3.val = carr3[counter].val;temp3.kind = carr3[counter].kind;/*carr3 = carr2*/carr3[counter].val = temp2.val;carr3[counter].kind = temp2.kind;/*carr1 = carr3*/carr1[counter].val = temp3.val;carr1[counter].kind = temp3.kind;}Card* result_process(Card *carr1, Card *carr2, Card *carr3, int counter){rshift(carr1, carr2, carr3, counter); /* 把数组的第一个元素依次右移*/if(counter == 2){return(&carr2[2]);}show(carr1, 3);show(carr2, 3);show(carr3, 3);puts("请给出你记住的牌所在行数:");fflush(stdin);int input = 1;input = getchar(); /*获取你选的组*/switch(input){case '1':return(result_process(carr1, carr2, carr3, ++counter));break;case '2':return(&carr2[counter]);break;default:puts("你在撒谎!不和你玩了!");return NULL;}}5、C语言实现打字游戏#include "stdio.h"#include "time.h"#include "stdlib.h"#include "conio.h"#include "dos.h"#define xLine 70#define yLine 20#define full 100#define true 1#define false 0/*---------------------------------------------------------------------*/void printScreen(int level,int right,int sum,char p[yLine][xLine])/* 刷新屏幕的输出图像 */{int i,j;clrscr();printf("level:%d Press 0 to exit;1 topause score:%d/%d\n",level,right,sum);/* 输出现在的等级,击中数和现在已下落总数 */printf("----------------------------------------------------------------------\n");for (i=0;i<yLine;i++){for(j=0;j<xLine;j++)printf ("%c",p[i][j]);printf("\n");}/* for (i) */printf("----------------------------------------------------------------------\n");}/* printScreen *//*---------------------------------------------------------------------*/void leave()/* 离开程序时,调用该函数结束程序。
贪吃蛇游戏c语言源代码
#include <stdlib.h>#include <graphics.h>#include <bios.h>#include <dos.h>#include <conio.h>#define Enter 7181#define ESC 283#define UP 18432#define DOWN 20480#define LEFT 19200#define RIGHT 19712#ifdef __cplusplus#define __CPPARGS ...#else#define __CPPARGS#endifvoid interrupt (*oldhandler)(__CPPARGS);void interrupt newhandler(__CPPARGS);void SetTimer(void interrupt (*IntProc)(__CPPARGS));void KillTimer(void);void Initgra(void);void TheFirstBlock(void);void DrawMap(void);void Initsnake(void);void Initfood(void);void Snake_Headmv(void);void Flag(int,int,int,int);void GameOver(void);void Snake_Bodymv(void);void Snake_Bodyadd(void);void PrntScore(void);void Timer(void);void Win(void);void TheSecondBlock(void);void Food(void);void Dsnkorfd(int,int,int);void Delay(int);struct Snake{int x;int y;int color;}Snk[12];struct Food{int x;int y;int color;}Fd;int flag1=1,flag2=0,flag3=0,flag4=0,flag5=0,flag6=0,checkx,checky,num,key=0,Times,Score,Hscore,Snkspeed,TimerCounter,TureorFalse; char Sco[2],Time[6];void main(){ Initgra();SetTimer(newhandler);TheFirstBlock();while(1){DrawMap();Snake_Headmv();GameOver();Snake_Bodymv();Snake_Bodyadd();PrntScore();Timer();Win();if(key==ESC)break;if(key==Enter){cleardevice();TheFirstBlock();}TheSecondBlock();Food();Delay(Snkspeed);}closegraph();KillTimer();}void interrupt newhandler(__CPPARGS){TimerCounter++;oldhandler();}void SetTimer(void interrupt (*IntProc)(__CPPARGS)) {oldhandler=getvect(0x1c);disable();setvect(0x1c,IntProc);enable();}void KillTimer(){disable();setvect(0x1c,oldhandler);enable();}void Initgra(){int gd=DETECT,gm;initgraph(&gd,&gm,"d:\\tc");}void TheFirstBlock(){setcolor(11);settextstyle(0,0,4);outtextxy(100,220,"The First Block");loop:key=bioskey(0);if(key==Enter){cleardevice();Initsnake();Initfood();Score=0;Hscore=1;Snkspeed=10;num=2;Times=0;key=0;TureorFalse=1;TimerCounter=0;Time[0]='0';Time[1]='0';Time[2]=':';Time[3]='1';Time[4]='0';Time[5]='\0'; }else if(key==ESC) cleardevice();else goto loop;}void DrawMap(){line(10,10,470,10);line(470,10,470,470);line(470,470,10,470);line(10,470,10,10);line(480,20,620,20);line(620,20,620,460);line(620,460,480,460);line(480,460,480,20);}void Initsnake(){randomize();num=2;Snk[0].x=random(440);Snk[0].x=Snk[0].x-Snk[0].x%20+50;Snk[0].y=random(440);Snk[0].y=Snk[0].y-Snk[0].y%20+50;Snk[0].color=4;Snk[1].x=Snk[0].x;Snk[1].y=Snk[0].y+20;Snk[1].color=4;}void Initfood(){randomize();Fd.x=random(440);Fd.x=Fd.x-Fd.x%20+30;Fd.y=random(440);Fd.y=Fd.y-Fd.y%20+30;Fd.color=random(14)+1;}void Snake_Headmv(){if(bioskey(1)){key=bioskey(0);switch(key){case UP:Flag(1,0,0,0);break;case DOWN:Flag(0,1,0,0);break;case LEFT:Flag(0,0,1,0);break;case RIGHT:Flag(0,0,0,1);break;default:break;}}if(flag1){checkx=Snk[0].x;checky=Snk[0].y;Dsnkorfd(Snk[0].x,Snk[0].y,0);Snk[0].y-=20;Dsnkorfd(Snk[0].x,Snk[0].y,Snk[0].color); }if(flag2){checkx=Snk[0].x;checky=Snk[0].y;Dsnkorfd(Snk[0].x,Snk[0].y,0);Snk[0].y+=20;Dsnkorfd(Snk[0].x,Snk[0].y,Snk[0].color); }if(flag3){checkx=Snk[0].x;checky=Snk[0].y;Dsnkorfd(Snk[0].x,Snk[0].y,0);Snk[0].x-=20;Dsnkorfd(Snk[0].x,Snk[0].y,Snk[0].color);}if(flag4){checkx=Snk[0].x;checky=Snk[0].y;Dsnkorfd(Snk[0].x,Snk[0].y,0);Snk[0].x+=20;Dsnkorfd(Snk[0].x,Snk[0].y,Snk[0].color);}}void Flag(int a,int b,int c,int d){flag1=a;flag2=b;flag3=c;flag4=d;}void GameOver(){int i;if(Snk[0].x<20||Snk[0].x>460||Snk[0].y<20||Snk[0].y>460) {cleardevice();setcolor(11);settextstyle(0,0,4);outtextxy(160,220,"Game Over");loop1:key=bioskey(0);if(key==Enter){cleardevice();TheFirstBlock();}elseif(key==ESC)cleardevice();elsegoto loop1;}for(i=3;i<num;i++){if(Snk[0].x==Snk[i].x&&Snk[0].y==Snk[i].y) {cleardevice();setcolor(11);settextstyle(0,0,4);outtextxy(160,220,"Game Over");loop2:key=bioskey(0);if(key==Enter){cleardevice();TheFirstBlock();}elseif(key==ESC)cleardevice();else goto loop2;}}}void Snake_Bodymv(){int i,s,t;for(i=1;i<num;i++){Dsnkorfd(checkx,checky,Snk[i].color); Dsnkorfd(Snk[i].x,Snk[i].y,0);s=Snk[i].x;t=Snk[i].y;Snk[i].x=checkx;Snk[i].y=checky;checkx=s;checky=t;}}void Food(){if(flag5){randomize();Fd.x=random(440);Fd.x=Fd.x-Fd.x%20+30;Fd.y=random(440);Fd.y=Fd.y-Fd.y%20+30;Fd.color=random(14)+1;flag5=0;}Dsnkorfd(Fd.x,Fd.y,Fd.color);}void Snake_Bodyadd(){if(Snk[0].x==Fd.x&&Snk[0].y==Fd.y) {if(Snk[num-1].x>Snk[num-2].x){num++;Snk[num-1].x=Snk[num-2].x+20;Snk[num-1].y=Snk[num-2].y;Snk[num-1].color=Fd.color;}elseif(Snk[num-1].x<Snk[num-2].x){num++;Snk[num-1].x=Snk[num-2].x-20;Snk[num-1].y=Snk[num-2].y;Snk[num-1].color=Fd.color;}elseif(Snk[num-1].y>Snk[num-2].y) {num++;Snk[num-1].x=Snk[num-2].x; Snk[num-1].y=Snk[num-2].y+20; Snk[num-1].color=Fd.color;}elseif(Snk[num-1].y<Snk[num-2].y) {num++;Snk[num-1].x=Snk[num-2].x; Snk[num-1].y=Snk[num-2].y-20; Snk[num-1].color=Fd.color;}flag5=1;Score++;}}void PrntScore(){if(Hscore!=Score){setcolor(11);settextstyle(0,0,3); outtextxy(490,100,"SCORE"); setcolor(2);setfillstyle(1,0);rectangle(520,140,580,180); floodfill(530,145,2);Sco[0]=(char)(Score+48);Sco[1]='\0';Hscore=Score;setcolor(4);settextstyle(0,0,3); outtextxy(540,150,Sco);}}void Timer(){if(TimerCounter>18){Time[4]=(char)(Time[4]-1); if(Time[4]<'0'){Time[4]='9';Time[3]=(char)(Time[3]-1);}if(Time[3]<'0'){Time[3]='5';Time[1]=(char)(Time[1]-1);}if(TureorFalse){setcolor(11);settextstyle(0,0,3);outtextxy(490,240,"TIMER");setcolor(2);setfillstyle(1,0);rectangle(490,280,610,320);floodfill(530,300,2);setcolor(11);settextstyle(0,0,3);outtextxy(495,290,Time);TureorFalse=0;}if(Time[1]=='0'&&Time[3]=='0'&&Time[4]=='0') {setcolor(11);settextstyle(0,0,4);outtextxy(160,220,"Game Over");loop:key=bioskey(0);if(key==Enter){cleardevice();TheFirstBlock();}else if(key==ESC) cleardevice();else goto loop;}TimerCounter=0;TureorFalse=1;}}void Win(){if(Score==3)Times++;if(Times==2){cleardevice();setcolor(11);settextstyle(0,0,4);outtextxy(160,220,"You Win");loop:key=bioskey(0);if(key==Enter){cleardevice();TheFirstBlock();key=0;}else if(key==ESC) cleardevice();else goto loop;}}void TheSecondBlock(){if(Score==3){cleardevice();setcolor(11);settextstyle(0,0,4);outtextxy(100,220,"The Second Block"); loop:key=bioskey(0);if(key==Enter){cleardevice();Initsnake();Initfood();Score=0;Hscore=1;Snkspeed=8;num=2;key=0;}else if(key==ESC) cleardevice();else goto loop;}}void Dsnkorfd(int x,int y,int color) {setcolor(color);setfillstyle(1,color);circle(x,y,10);floodfill(x,y,color);}void Delay(int times){int i;for(i=1;i<=times;i++)delay(15000);}。
C语言编程游戏代码
#include <stdio.h>#include <stdlib.h>#include <dos.h>#include <conio.h>#include <time.h>#define L 1#define LX 15#define L Y 4static struct BLOCK{int x0,y0,x1,y1,x2,y2,x3,y3;int color,next;intb[]={{0,1,1,1,2,1,3,1,4,1},{1,0,1,3,1,2,1,1,4,0},{1,1,2,2,1,2,2,1,1,2},{0,1,1,1,1,0,2,0,2,4}, {0,0,0,1,1,2,1,1,2,3},{0,0,1,0,1,1,2,1,3,8},{1,0,1,1,2,2,2,1,2,5},{0,2,1,2,1,1,2,1,2,6},{0,1,0,2,1,1,1 ,0,3,9},{0,1,1,1,1,2,2,2,3,10},{1,1,1,2,2,1,2,0,3,7},{ 1,0,1,1,1,2,2,2,7,12},{0,1,1,1,2,1,2,0,7,13},{0 ,0,1,2,1,1,1,0,7,14},{0,1,0,2,1,1,2,1,7,11},{0,2,1,2,1,1,1,0,5,16},{0,1,1,1,2,2,2,1,5,17},{1,0,1,1,1, 2,2,0,5,18},{0,0,0,1,1,11,2,1,5,15},{0,1,1,1,1,0,2,1,6,2,0},{0,1,1,2,1,1,1,0,6,21},{0,1,1,2,1,1,2,1,6 ,22},{1,0,1,1,1,2,2,1,6,19}};static int d[10]={33000,3000,1600,1200,900,800,600,400,300,200};int Llevel,Lcurrent,Lnext,Lable,lx,ly,Lsum;unsigned Lpoint;int La[19][10],FLAG,sum;unsigned ldelay;void scrint(),datainit(),dispb(),eraseeb();void throw(),judge(),delayp(),move(0,note(0,show();int Ldrop(),Ljudge(),nextb(),routejudge();}main(){char c;datainit();Label=nextb();Label=Ldrop();while(1){delayp();if(Label!=0){Ljudge();Lable=nextb();}ldelay--;if(ldelay==0){Label=Ldrop();ldelay=d[0];}if(FLAG!=0) break;while(getch()!='\r');goto xy(38,16);cputs("again?");c=getch();while(c!='n'&&c!='N')c lscr();}int nextb(){if(La[(b[Lnext].y0)][(3+b[Lnext].x0)]!=0||La[(b[Lnext].y1)][(3+b[Lnext].x1)]!=0|| La[(b[Lnext].y2)][(3+b[Lnext].x2)]!=0||La[(b[Lnext].y3)][3+b[Lnext].x3)]!=0) {FLAG=L;return (-1);}erase b(0,3,5,Lnext);Lcurrent=Lnext;lx=3;ly=0;Label=0;ldelay=d[0];Lsum ++;Lpoint+=1;Lnext=random(23);dispb(0,3,5,Lnext);text color(7);goto xy(3,14);printf("%#5d",Lsum);goto xy(3,17);printf("%#5d",Lpoint);return(0);}void delayp(){char key;if(kbhit()!=0){key=getch();move(key);if(key=='\\')getch();}}void move(funkey)char funkey;{int tempcode;case 'k';if(lx+b[current].x0>0){if(La[ly+(b[Lcurrent].y0)][lx-1+(b[Lcurrent].x0)]==0&&La[(ly+b[current].y1)][(lx-1+b[curr ent].x1]==0&&La[ly+b[current].y2)][lx-1+b[Lcurrent].x2)]==0&&La[ly+(b[current].y3)][lx-1+(b [Lcurrent].x3)]==0){eraseb(L,lx,lyLcurrent);lx--;dispb(L,lx,ly,Lcurrent);}}break;case 0x20;tempcode=b[Lcurrent].next;if (lx+b[tempcode].x0>=0 && lx+b[tempcode].x3<=9 && ly+b[tempcode].y1<=19 && ly+b[tempcode].y2<=19){if(routejudge()!+-1){if(La+(b[tempcode].y0)][lx+(b[tempcode].x0)]==0 && La[ly+(b[tempcode].y1)][lx+(b[tempcode].x1)]==0 && La[ly+(b[tempcode].y2)][lx+(b[tempcode].x2)]==0 && La[ly+(b[tempcode].y3)][lx+(b[tempcode].x3)]==0)eraseb(L,lx,ly,Lcurrent);Lcurrent=tempcode;dispb(L,lx,ly,Lcurrent);}}break;case 'M';if(lx+b[Lcurrent].x3<9){if(La[ly+(b[Lcurrent].y0)][lx+(b[Lcurrent].x0)]==0 &&La[ly+(b[Lcurrent].y1)][lx+(b[Lcurrent].x1)]==0 && La[ly+(b[Lcurrent].y2)][lx+(b[Lcurrent].x2)]==0 && La[ly+(b[Lcurrent].y3)][lx+(b[Lcurrent].x3)]==0){eraseb(L,lx,ly,Lcurrent);lx++;disb(L,lx,ly,Lcurrent);}}break;case 'p';throw();break;case 0x1b;clrscr();exit(0);break;default:break;}void throw(){int tempy;tempy=ly;while(ly+b[Lcurrent].y1<19 && ly+b[current].y2<19&&La[ly+(b[Lcurrent].y0)][lx+(b[Lcurrent].x0)]==0 && La[ly+(b[Lcurrent].y1)][lx+(b[Lcurrent].x1)]==0 && La[ly+(b[Lcurrent].y2)][lx+(b[Lcurrent].x2)]==0 && La[ly+(b[Lcurrent].y3)][lx+(b[Lcurrent].x3)]==0)ly++;ly--;eraseb(L,lx,tempy,Lcurrent);dispb(L,lx,ly,Lcurrent);La[ly+b[Lcurrent].y0)][lx+(b[current].x0)]=La[ly+b[Lcurrent].y1)][lx+(b[current].x1)]=La[l y+b[Lcurrent].y2)][lx+(b[current].x2)]=La[ly+b[Lcurrent].y3)][lx+(b[current].x3)]=b[Lcurrent].c olor;Label=-1;}int routejudge(){int i,j;for(i=0;i<3;i++)for(j=0;j<3;j++)if(La[ly+i][lx+j]!=0)return(-1);else return(1);}int Ldrop(){if(ly+b[Lcurrent].y1>=18||ly+b[Lcurrent].y2>=18{La[ly+b[Lcurrent].y0)][lx+(b[current].x0)]=3;La[ly+b[Lcurrent].y1)][lx+(b[current].x1)]=1;La[ly+b[Lcurrent].y2)][lx+(b[current].x2)]=5;La[ly+b[Lcurrent].y3)][lx+(b[current].x3)]=b[Lcurrent].color;return(-1);}if(La(ly+1+(b[Lcurrent].y0)][lx+(b[Lcurrent].x0)]!=0||La(ly+1+(b[Lcurrent].y1)][lx+(b[Lcur rent].x1)]!=0||La(ly+1+(b[Lcurrent].y2)][lx+(b[Lcurrent].x2)]!=0||La(ly+1+(b[Lcurrent].y3)][lx+( b[Lcurrent].x3)]!=0){La[ly+b[Lcurrent].y0)][lx+(b[current].x0)]=La[ly+b[Lcurrent].y1)][lx+(b[cu rrent].x1)]=0BLa[ly+b[Lcurrent].y2)][lx+(b[current].x2)]=La[ly+b[Lcurrent].y3)][lx+(b[current].x3)]=b[Lcurren t].color){return(-1);eraseb(L,lx,ly,Lcurrent);dispb(L,lx,++ly,Lcurrent);return(0);}}int Ljudge(){int i,j,k,lines,f;static int p[5]={0,1,3,6,10};lines=0;for(k=0;k<=3;k++){f=0;if((ly+k)>18)continue;for(i=0;i<10;i++){if(La[ly+k]==0);i>0;i--){f++;break;}if(f==0){movetext(LX,L Y,LX+19,L Y+ly+k-1,LX,L Y+1);for(i=(ly+k);i>0;i--){for(j=0;j<10;j++)La[j]=La[i-1][j];{for(j=0;j<10;j++)La[0][j]=0;lines++;}}}}}Lpoint+=p[lines]*10;return(0);}void scrint(){int i;char lft[20];textbackground(1);clrscr();goto xy(30,9);cputs("enter your name");scanf("%s",lft);goto xy(25,14);scanf("%s",lft);textbackground(0);clrscr();goto xy(17,1);printf("%s",lft);goto xy(5,3);puts("next");goto xy(4,13);cputs("block");goto xy(4,16);cputs("point");for(i=0;i<19;i++){goto xy(LX-2,L Y+1);cputs("** **");}goto xy(LX-2,L Y+19);cputs("**********************");void datainit(){int i,j;)for(i=0;i<19;i++){for(j=0;j<10;j++){La[j]=0;Label=0;FLAG=0;ldelay=d[0];Lsum=0;Lpoint=0;randomize();Lnext=random(23);}}}void dispb(LRflag,x,y,blockcode){int realx,realy;if(LRflag==L){realx=LX+x*2;realy=L Y+y;}realx=x;raly=y;textcolor(b[blockcode].color);goto xy(realx+2*b[blockcode].x0,realy+b[blockcode].y0); cputs("**");goto xy(realx+2*b[blockcode].x1,realy+b[blockcode].y1); cputs("**");goto xy(realx+2*b[blockcode].x2,realy+b[blockcode].y2); cputs("**");goto xy(realx+2*b[blockcode].x3,realy+b[blockcode].y3); cputs("**");}void eraseb(LRflag,x,y,blockcode)int LRflag,x,y,blockcode;int realx,realy;if(LRflag==L){realx=LX+x*2;realy=L Y+y;}else{realx=Lx+x*2;realy=L Y+y;}textcolor(0);goto xy(realx+2*b[blockcode].x0,realy+b[blockcode].y0);cputs("**");goto xy(realx+2*b[blockcode].x1,realy+b[blockcode].y1);cputs("**");goto xy(realx+2*b[blockcode].x2,realy+b[blockcode].y2);cputs("**");goto xy(realx+2*b[blockcode].x3,realy+b[blockcode].y3);cputs("**");}。
C语言小游戏源代码
b=(a+2)%8; /*得到下第二个的数组下标*/
change(a,b); /*交换*/
}
update();
}
update(){ /*在屏幕上画出整个九格图像,关于定位的问题自己分析*/
pos=GetTheNull(); /*得到空格*/
switch(key){ /*测试按键*/
case 72: /*按下*/
if(pos<=5) change(pos,pos+3);
int i;
for(i=0;i<=8;i++){
if (num[i]==0)
return i;
}
}
change(int a,int b){ /*交换num[a],num[b]*/
int temp;
}
if(key==27) break; /*退出*/
}
clrscr(); /*清屏*/
}
newGame(){ /*新建游戏,实际上就是把有数字的八个方格打散,
这种效果的实现就是随机的两两交换,而怎么不与空的那个交换呢?
而这些的前提是其中的0不能与其第奇数个作奇数次交换.
为了省事,不处理0...
其中产道理,请自己思考
*/
int i,a,b;
for(i=0;i<8;i++) num[i]=i+1;
num[8]=0; /*将数组复原*/
for(i=0;i<300;i++){
这涉及有没有解的问题
不管怎么样,九格游戏最后的结果只有两个:
C语言小游戏源代码《打砖块》
C 语言小游戏源代码《打砖块》#include "graphics.h"#include "stdio.h"#include "conio.h" /* 所需的头文件*/int on; /* 声明具有开关作用的全局变量*/ static int score;/* 声明静态的记分器变量*//* 定义开始界面函数*/int open(){setviewport(100,100,500,380,1); /* 设置图形窗口区域*/setcolor(4); /* 设置作图色*/rectangle(0,0,399,279); /* 以矩形填充所设的图形窗口区域*/ setfillstyle(SOLID_FILL,7); /* 设置填充方式*/floodfill(50,50,4); /* 设置填充范围*/setcolor(8);settextstyle(0,0,9); /* 文本字体设置*/outtextxy(90,80,"BALL"); /* 输出文本内容*/settextstyle(0,0,1);outtextxy(110,180,"version 1.0");outtextxy(110,190,"made by ddt");setcolor(128);settextstyle(0,0,1);outtextxy(120,240,"Press any key to continue "); }/* 定义退出界面函数*/int quitwindow(){char s[100]; /* 声明用于存放字符串的数组*/setviewport(100,150,540,420,1);setcolor(YELLOW);rectangle(0,0,439,279);setfillstyle(SOLID_FILL,7);floodfill(50,50,14);setcolor(12);settextstyle(0,0,8);outtextxy(120,80,"End");settextstyle(0,0,2);outtextxy(120,200,"quit? Y/N");sprintf(s,"Your score is:%d",score);/* 格式化输出记分器的值*/outtextxy(120,180,s);on=1; /* 初始化开关变量*/}/* 主函数*/main(){int gdriver,gmode;gdriver=DETECT; /* 设置图形适配器*/gmode=VGA; /* 设置图形模式*/registerbgidriver(EGAVGA_driver); /* 建立独立图形运行程序*/ initgraph(&gdriver,&gmode,""); /* 图形系统初试化*/ setbkcolor(14);open(); /* 调用开始界面函数*/getch(); /* 暂停*/while(1) /* 此大循环体控制游戏的反复重新进行*/{int driver,mode,l=320,t=400,r,a,b,dl=5,n,x=200,y=400,r1=10,dx=- 2,dy=-2;/* 初始化小球相关参数*/intleft[100],top[100],right[100],bottom[100],i,j,k,off=1,m,num[100][10 0];/*方砖阵列相关参数*/static int pp;static int phrase; /* 一系列起开关作用的变量*/ int oop=15;pp=1;score=0;driver=DETECT;mode=VGA;registerbgidriver(EGAVGA_driver);initgraph(&driver,&mode,"");setbkcolor(10);cleardevice(); /**/ clearviewport(); /* 清除现行图形窗口内容*/b=t+6;r=l+60;setcolor(1);rectangle(0,0,639,479);setcolor(4);rectangle(l,t,r,b);setfillstyle(SOLID_FILL,1);floodfill(l+2,t+2,4);for(i=0,k=0;i<=6;i++) /* 此循环绘制方砖阵列*/ {top[i]=k;bottom[i]=top[i]+20;k=k+21;oop--;for(j=0,m=0;j<=7;j++){left[j]=m;right[j]=left[j]+80;m=m+81;setcolor(4);rectangle(left[j],top[i],right[j],bottom[i]);setfillstyle(SOLID_FILL,j+oop); floodfill(left[j]+1,top[i]+1,4);num[i][j]=pp++;}}while(1) /* 此循环控制整个动画*/ {while(!kbhit()){x=x+dx; /* 小球运动的圆心变量控制*/ y=y+dy;if(x+r1>r||x+r1<r)phrase=0;} {if((x-r1<=r||x+r1<=r)&&x+r1>=l){if(y<t)phrase=1;if(y+r1>=t&&phrase==1){dy=-dy;y=t-1-r1;}}if(off==0)continue;for(i=0;i<=6;i++) /*for(j=0;j<=7;j++) 此循环用于判断、控制方砖阵列的撞击、擦除* /if((x+r1<=right[j]&&x+r1>=left[j])||(x-r1<=right[j]&&x- r1>=left[j])){if(( y-r1>top[i]&&y-r1<=bottom[i])||(y+r1>=top[i]&&y+r1<=bottom[i] )) {if(num[i][j]==0){continue; }setcolor(10);rectangle(left[j],top[i],right[j],bottom[i]);setfillstyle(SOLID_FILL,10);floodfill(left[j]+1,top[i]+1,10); dy=-dy;num[i][j]=0;score=score+10;printf("%d\b\b\b",score);}}if((y+r1>=top[i]&&y+r1<=bottom[i])||(y-r1>=top[i]&&y-r1<=bottom[i])){if((x+r1>=left[j]&&x+r1<right[j])||(x-r1<=right[j]&&x-r1>left[j])){if(num[i][j]==0){ continue;}setcolor(10);rectangle(left[j],top[i],right[j],bottom[i]);setfillstyle(SOLID_FILL,10);floodfill(left[j]+1,top[i]+1,10);dx=-dx;num[i][j]=0;score=score+10;printf("%d\b\b\b",score);}}}if(x+r1>639) /* 控制小球的弹射范围*/{dx=-dx;x=638-r1;}if(x<=r1){dx=-dx;x=r1+1;}if(y+r1>=479){off=0;quitwindow();break;}if(y<=r1){dy=-dy;y=r1+1;}if(score==560){off=0;quitwindow();break;} setcolor(6); circle(x,y,r1);setfillstyle(SOLID_FILL,14);floodfill(x,y,6);delay(1000);setcolor(10);circle(x,y,r1);setfillstyle(SOLID_FILL,10);floodfill(x,y,10); }a=getch();setcolor(10);rectangle(l,t,r,b);setfillstyle(SOLID_FILL,10);floodfill(l+2,t+2,10);if(a==77&&l<=565) /* 键盘控制设定*/ {dl=20;l=l+dl;}if(a==75&&l>=15){dl=-20;l=l+dl;}if(a=='y'&&on==1)break;if(a=='n'&&on==1)break;if(a==27){quitwindow();off=0;}r=l+60;setcolor(4);rectangle(l,t,r,b);setfillstyle(SOLID_FILL,1);floodfill(l+5,t+5,4);delay(100);}if(a=='y'&&on==1) /* 是否退出游戏*/ {break;}if(a=='n'&&on==1){ continue;}}closegraph();}。
五子棋游戏 C语言 代码 源代码
#include <stdio.h>#include <stdlib.h>#define m 30int main (void){int count;//计数器算横纵行的结果int w,h;int u;int l;int i,size;//i声明步数。
size声明int r[m][m] = {0};//数组声明(棋子位置)int x, y;//声明落子坐标int n;//声明棋盘大小nchar a[20],b[20];printf ("请输入棋盘大小n\n");//编辑棋盘直到棋盘长度宽度大于4小于30 scanf ("%d", &n);if (n<=4 || n>m){do{printf ("输入的棋盘大小:4<n<%d\n", m);scanf ("%d", &n);}while (n<=4 || n>m);}getchar ();//声明玩家printf ("请输入玩家1姓名:\n");gets(a);printf ("请输入玩家2姓名:\n");gets(b);for ( i = 1, size = n*n;i <= size; i++)//编辑棋盘{if (i%2 == 1)//如果i能被2整除,为玩家a相关信息{do//玩家a棋子信息{printf ("%s该你下棋了,第%d个棋子\n", a, i);scanf ("%d%d", &x, &y);if (x > n || x < 0)//判断坐标是否在棋盘内,如果不是则重新输入{do{printf ("0<=横坐标<=%d请重新输入横坐标\n", n);scanf ("%d", &x);}while (x>m || x<0);}if (y > n || y < 0)//判断坐标是否在棋盘内,如果不是则重新输入{do{printf ("0<=纵坐标<=%d请重新输入纵坐标\n", n);scanf ("%d", &y);}while (y < 0 || y > n);}}while ((r[x][y] == 1 && (printf ("这个位置上已经有棋子了,请重新输入\n")))|| r[x][y] == 2&& (printf ("这个位置上已经有棋子了,请重新输入\n")) );r[x][y] = 1;for (u = 0;u < n; u++)//不同情况下判断玩家a获胜方式{for (l = 0;l < n;l++){count = 0;for (w = u,h = l;r[w][h] == 1 && h < n; h++)count++;if (count == 5){printf ("%s是胜利者\n", a);goto e;//直接跳转,其余代码不在运行count = 0;for (w = u, h = l; r[w][h] == 1 && w < n; w++)count ++;if (count == 5){printf ("%s是胜利者\n", a);goto e;}count = 0;for (w = u,h = l; r[w][h] == 1 && w < n && h<n;w++,h++)count++;if (count == 5){printf ("%s是胜利者\n", a);goto e;}count = 0;for (w =u ,h =l;r[w][h] == 1 && h > 0;h--)count++;if (count == 5){printf ("%s是胜利者\n", a);goto e;}}}}system("cls");for (int j = n;j>=0;j--){printf ("%-2d", j);for (int k = 0;k < n;k++)//画棋盘,声明两玩家棋子图片{if (r[k][j] == 0)printf ("╋");else if(r[k][j] == 1)printf ("○");else if (r[k][j] == 2)printf ("●"); }printf ("\n");}printf (" ");for (int k = 0;k < n;k++)printf ("%-2d", k);}else if (i%2 == 0)//如果i不能被2整除,为玩家b相关信息{do{printf ("\n%s该你下棋了,第%d个棋子\n", b, i);scanf ("%d%d", &x, &y);if (x > n || x < 0){do{printf ("0<=横坐标<=%d请重新输入横坐标\n", n);scanf ("%d", &x);}while (x>n || x<0);}if (y >n|| y < 0){do{printf ("0<=纵坐标<=%d请重新输入纵坐标\n", n);scanf ("%d", &y);}while (y < 0 || y > n);}}while ((r[x][y] == 1 && (printf ("这个位置上已经有棋子了,请重新输入\n")))|| r[x][y] == 2&& (printf ("这个位置上已经有棋子了,请重新输入\n")) );r[x][y] = 2;system("cls");for (int j = n;j>=0;j--){printf ("%-2d", j);for (int k = 0;k < n;k++){if (r[k][j] == 0)printf ("╋");else if(r[k][j] == 1)printf ("○");else if (r[k][j] == 2)printf ("●");}printf ("\n");}printf (" ");for (int k = 0;k < n;k++)printf ("%-2d", k); printf ("\n");count = 0;for (u = 0;u < n; u++){for (l = 0;l < n;l++){count = 0;for (w = u,h = l;r[w][h] == 2 && h < n; h++)count++;if (count == 5){printf ("%s是胜利者\n", b);goto e;}count = 0;for (w = u, h = l; r[w][h] == 2 && w < n; w++)count ++;if (count == 5){printf ("%s是胜利者\n", b);goto e;}count = 0;for (w = u,h = l; r[w][h] == 2 && w < n && h<n;w++,h++)count++;if (count == 5){printf ("%s是胜利者\n", b);goto e;}count = 0;for (w =u ,h =l;r[w][h] == 2 && h > 0;h--)count++;if (count == 5){printf ("%s是胜利者\n", b);goto e;}}}}}e: for (int j = n;j>=0;j--)//游戏结束界面棋盘固定重新显示{printf ("%-2d", j);for (int k = 0;k < n;k++){if (r[k][j] == 0)printf ("╋");else if(r[k][j] == 1)printf ("○");else if (r[k][j] == 2)printf ("●");}printf ("\n");}printf (" ");for (int k = 0;k < n;k++)printf ("%-2d", k); printf ("\n");printf ("\a游戏愉快,Powered by Techmessager\n");//结束语句return 0;}。
简单的迷宫小游戏C语言程序源代码
简单的迷宫小游戏C语言程序源代码#include <stdio.h>#include <conio.h>#include <windows.h> #include <time.h>#define Height 31 //迷宫的高度,必须为奇数 #define Width 25 //迷宫的宽度,必须为奇数 #define Wall 1#define Road 0#define Start 2#define End 3#define Esc 5#define Up 1#define Down 2#define Left 3#define Right 4int map[Height+2][Width+2]; void gotoxy(int x,int y) //移动坐标{COORD coord;coord.X=x;coord.Y=y;SetConsoleCursorPosition( GetStdHandle( STD_OUTPUT_HANDLE ), coord );}void hidden()//隐藏光标{HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);CONSOLE_CURSOR_INFO cci; GetConsoleCursorInfo(hOut,&cci);cci.bVisible=0;//赋1为显示,赋0为隐藏SetConsoleCursorInfo(hOut,&cci);}void create(int x,int y) //随机生成迷宫 {int c[4][2]={0,1,1,0,0,-1,-1,0}; //四个方向 int i,j,t;//将方向打乱for(i=0;i<4;i++){j=rand()%4;t=c[i][0];c[i][0]=c[j][0];c[j][0]=t;t=c[i][1];c[i][1]=c[j][1];c[j][1]=t;}map[x][y]=Road;for(i=0;i<4;i++)if(map[x+2*c[i][0]][y+2*c[i][1]]==Wall){map[x+c[i][0]][y+c[i][1]]=Road;create(x+2*c[i][0],y+2*c[i][1]);}}int get_key() //接收按键{char c;while(c=getch()) {if(c==27) return Esc; //Escif(c!=-32)continue; c=getch();if(c==72) return Up; //上if(c==80) return Down; //下if(c==75) return Left; //左if(c==77) return Right; //右}return 0;}void paint(int x,int y) //画迷宫 { gotoxy(2*y-2,x-1); switch(map[x][y]) { case Start:printf("入");break; //画入口case End:printf("出");break; //画出口case Wall:printf("※");break; //画墙case Road:printf(" ");break; //画路}}void game(){int x=2,y=1; //玩家当前位置,刚开始在入口处 int c; //用来接收按键while(1){gotoxy(2*y-2,x-1); printf("?"); //画出玩家当前位置if(map[x][y]==End) //判断是否到达出口{gotoxy(30,24); printf("到达终点,按任意键结束");getch();break;}c=get_key(); if(c==Esc){gotoxy(0,24); break;}switch(c){case Up: //向上走if(map[x-1][y]!=Wall){paint(x,y); x--;}break;case Down: //向下走if(map[x+1][y]!=Wall){paint(x,y); x++;}break;case Left: //向左走if(map[x][y-1]!=Wall){paint(x,y); y--;}break;case Right: //向右走if(map[x][y+1]!=Wall){paint(x,y); y++;}break;}}}int main(){int i,j;srand((unsigned)time(NULL)); //初始化随即种子 hidden(); //隐藏光标for(i=0;i<=Height+1;i++) for(j=0;j<=Width+1;j++)if(i==0||i==Height+1||j==0||j==Width+1) //初始化迷宫 map[i][j]=Road;else map[i][j]=Wall;create(2*(rand()%(Height/2)+1),2*(rand()%(Width/2)+1)); //从随机一个点开始生成迷宫,该点行列都为偶数for(i=0;i<=Height+1;i++) //边界处理 {map[i][0]=Wall;map[i][Width+1]=Wall; }for(j=0;j<=Width+1;j++) //边界处理{map[0][j]=Wall;map[Height+1][j]=Wall; }map[2][1]=Start; //给定入口map[Height-1][Width]=End; //给定出口 for(i=1;i<=Height;i++)for(j=1;j<=Width;j++) //画出迷宫paint(i,j);game(); //开始游戏getch();return 0;}。
C语言俄罗斯方块游戏源代码
return(Low==0?Hig+256:Low); } void Select() { int OldStaus,acts=ActS; switch(GetKey()) { case ESC :Quit();break; case DOWN :Seconds+=14500;break; case LEFT :Display(0); if (ActH>0 && Touch(ActH,ActS,-1,0)==CAN) { ActH--;} Display(Act+1);break; case RIGHT :Display(0); if (ActH<Heng && Touch(ActH,ActS,1,0)==CAN) { ActH++;} Display(Act+1);break; case BLANK : Display(0); ksdown(); Display(Act+1); break; case F1 :Help();break; case EQUAL : case ADD :if (Delays>300) Delays-=100;break; case DEC :if (Delays<3000) Delays+=100;break; case PAUSEP : case PAUSEp :getch();break; case SOUNபைடு நூலகம்S : case SOUNDs :if (Sounds==CAN) Sounds=CANNOT; else Sounds=CAN;break; case UP :if(Act==7) { while(acts<Shu-1&&Position[ActH][acts]!=1) acts++; Position[ActH][acts]=0; DrawBox(ActH,acts,0); acts=ActS; break; } else { Display(0);
飞机大战c语言代码
飞机大战这种游戏较为复杂,无法直接给出完整的C语言源代码。
但是,我可以提供一个简单的基本构架,以便你了解如何利用C语言开始编写一个飞机大战游戏。
若要构建完整游戏,你需要进一步完善该基本结构并添加更多功能。
以下是C语言实现一个简易飞机大战游戏的基本雏形:#include <stdio.h>#include <stdlib.h>#include <conio.h>#include <windows.h>// 常量,用于界定屏幕尺寸#define SCREEN_WIDTH 50#define SCREEN_HEIGHT 20// 玩家结构体typedef struct {int x, y;} Player;// 敌人结构体typedef struct {int x, y;} Enemy;// 子弹结构体typedef struct {int x, y;} Bullet;// 按ASCII码清屏void clearScreen() {system("cls");}// 跳转到指定坐标void gotoxy(int x, int y) {COORD coord;coord.X = x;coord.Y = y;SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);}// 游戏循环void gameLoop() {Player player;Enemy enemy[10];Bullet bullet;int i;// 初始化游戏player.x = SCREEN_WIDTH / 2;player.y = SCREEN_HEIGHT - 1;// 敌人的初始位置for (i = 0; i < 10; i++) {enemy[i].x = i * 5 + 1;enemy[i].y = 1;}// 游戏主循环while (1) {clearScreen();// 绘制玩家gotoxy(player.x, player.y);printf("^");// 绘制敌人for (i = 0; i < 10; i++) {gotoxy(enemy[i].x, enemy[i].y);printf("V");}// 绘制子弹 (若存在)if (bullet.y >= 0) {gotoxy(bullet.x, bullet.y);printf("|");// 更新子弹位置bullet.y -= 1;// 判断是否击中敌人for (i = 0; i < 10; i++) {if (bullet.x == enemy[i].x && bullet.y == enemy[i].y) { enemy[i].y = -1; // 隐藏已经被击中的敌人}}}// 检测操作if (_kbhit()) {char input = _getch();switch (input) {case 'a': // 往左移动player.x -= 1;break;case 'd': // 往右移动player.x += 1;break;case ' ': // 发射子弹bullet.x = player.x;bullet.y = player.y - 1;break;case 'q': // 退出游戏exit(0);break;}}// 刷新画布Sleep(100);}}int main() {gameLoop();return 0;}这个代码示例使用了C语言(在Windows操作系统下运行),为玩家提供了移动(A,D键)和射击(空格键)功能。
c语言经典游戏代码
c语⾔经典游戏代码C语⾔精品游戏主⾓和怪物源码//C语⾔多线程-主⾓和怪物#include#include#define bool int //定义int变量为bool变量,bool不是真就是假int a=0,b=20;//主⾓的坐标int x=1,y=0;//怪物的坐标int i=1;//i值为真HANDLE hMutex;//1.坐标void GamePosition(HANDLE g_hout,int x,int y){COORD pos;//点的结构体pos.X=x;//横坐标pos.Y=y;//纵坐标SetConsoleCursorPosition(g_hout,pos);//设置控制平台光标位置}DWORD WINAPI Func(LPVOID lpParamter)//多线程的功能函数6.线程是画怪物{HANDLE hout=GetStdHandle(STD_OUTPUT_HANDLE);//7.拿到这张纸WaitForSingleObject(hMutex, INFINITE);//13.⾃⼰进来,⾃⼰⽤洗⼿间GamePosition(hout,x,y),printf('●');//8.在纸上画怪物ReleaseMutex(hMutex);//14.放弃使⽤权while(1)//9.怪物在横坐标为从0-10依次循环移动{if(x>=0&&i==1){printf(' ');GamePosition(hout,++x,y);printf('●');Sleep(1000);if(x==10)}else if(x<>{printf(' ');GamePosition(hout,--x,y);printf('●');Sleep(1000);if(x==0)i=1;}}return 0;}int main(){HANDLE hThread = CreateThread(NULL, 0, Func, NULL, 0, NULL);//5.创建线程hMutex = CreateMutexA(NULL, FALSE, '123');//创建互斥锁(量)//10.关上洗⼿间HANDLE hout=GetStdHandle(STD_OUTPUT_HANDLE);//2.拿到这张纸WaitForSingleObject(hMutex, INFINITE);//11.等待你的同事出来 15步接着GamePosition(hout,a,b),printf('☆');//3.在纸上画主⾓ReleaseMutex(hMutex);//12.同事出来了,放弃了洗⼿间的使⽤权while(1){if(kbhit())switch(getch())//控制左右 4.主⾓上下左右移动{case 'w':case 'W':if(b>3)GamePosition(hout,a,b),printf(' '),GamePosition(hout,a,--b),printf('☆'); break;case 's':case 'S':if(b<20)gameposition(hout,a,b),printf('>break;col = rand() % 10;if(map[row][col] == '0'){map[row][col] = 'x';num++;}}while(num <>for (row = 0;row <=>for (col = 0;col <=>if(map[row][col] != 'x'){int cnt = 0;for (num = 0;num <=>if(row + delta[num][0] <>continue;}if(row + delta[num][0] > 9){continue;}if(col + delta[num][1] <>continue;}if(col + delta[num][1] > 9){continue;}if(map[row + delta[num][0]][col + delta[num][1]]== 'x'){ cnt++;}}map[row][col] = '0' + cnt;}}}for (row = 0;row <>for(col = 0;col < 10;col="">printf('* ');}num = 0;int x,y;do{printf('please enter the coordinate of array:'); scanf('%d%d',&x,&y);show[x-1][y-1] = 1;if(map[x-1][y-1] == '0'){for (num = 0;num <=>if(x-1 + delta[num][0] <>continue;}if(x-1 + delta[num][0] > 9){continue;}if(y -1+ delta[num][1] <>continue;}if(y-1 + delta[num][1] > 9){continue;}show[x-1+delta[num][0]][y-1+delta[num][1]] = 1; }}if (map[x-1][y-1]!= 'x'&&map[x-1][y-1] != '0'){for (num = 0;num <=>int cnt = 0;if(x-1 + delta[num][0] <>continue;}if(x-1 + delta[num][0] > 9){continue;}if(y-1 + delta[num][1] <>if(y-1 + delta[num][1] > 9){continue;}if( map[x -1 + delta[num][0]][y -1+ delta[num][1]] != 'x'){ show[x-1 + delta[num][0]][y -1+ delta[num][1]] = 1 ; }}}if(map[x-1][y-1] == 'x') {printf('game over!\n');for (row = 0;row <>for(col = 0;col < 10;col="">printf('%c ',map[row][col]);}printf('\n');}return 0;}system('cls');printf('mine sweeping:\n');for (row = 0;row <>for(col = 0;col < 10;col="">if (show[row][col] == 1){printf('%c ', map[row][col]);}else{printf('* ');}}printf('\n');}for (row = 0;row <>for(col = 0;col < 10;col=""> if (show[row][col] == 1 ){num++;}}}printf('num:%d\n',num);}while(num <>printf('you win!');return 0;}。
c语言小游戏代码
c语言小游戏代码#include <stdio.h>#include <stdlib.h>#include <windows.h>// 定义元素类型#define ELEMENT char// 游戏行数#define ROW 10// 游戏显示延迟#define SLEEPTIME 100int main(int argc, char *argv[]){// 定义游戏的棋盘,用数组存放ELEMENT array[ROW][ROW];// 定义获胜条件int winCondition = 5;// 初始化,把棋盘清空system("cls");int i,j;for(i = 0; i < ROW; i++){for(j = 0; j < ROW; j++){array[i][j] = ' ';}}// 循环游戏,当有一方满足胜利条件时终止int tmp;int count = 0; // 存放棋子数while(1){// 依次取出玩家记录的棋子int x, y;// 如果已经有子落下,则计算是第几步if(count > 0){printf("第%d步:\n", count);}// 显示游戏棋盘for(i = 0; i < ROW; i++){printf(" ");for(j = 0; j < ROW; j++){printf("---");}printf("\n|");for(j = 0; j < ROW; j++){printf("%c |", array[i][j]);}printf("\n");}printf(" ");for(j = 0; j < ROW; j++){printf("---");}printf("\n");// 要求玩家输入放下棋子的位置printf("请玩家输入要放弃棋子的位置(1-%d)\n", ROW); printf("横坐标:");scanf("%d", &x);printf("纵坐标:");scanf("%d", &y);// 判断棋子位置是否有效if(x < 1 || x > ROW || y < 1 || y > ROW || array[x-1][y-1] != ' '){printf("输入错误!\n");system("pause");system("cls");continue;}// 把棋子记录,并计数if(count % 2 == 0){array[x-1][y-1] = 'X';}else{array[x-1][y-1] = 'O';}count++;// 判断是否有获胜者int i, j, k;int tempx, tempy;for(i = 0; i < ROW; i++){for(j = 0; j < ROW; j++){if(array[i][j] == 'X' || array[i][j] == 'O') {// 判断横向是否有获胜者tmp = 1;for(k = 1; k < winCondition; k++){// 注意边界,必须验证范围有效if(j + k > ROW - 1) break;// 如果和前一个位置的棋子相同,则计数加1,否则跳出if(array[i][j+k] == array[i][j])tmp++;else break;}// 如果计数满足获胜条件,则显示获胜者if(tmp >= winCondition){printf("玩家 %c 获胜!\n", array[i][j]);system("pause");return 0;}// 判断纵向是否有获胜者tmp = 1;for(k。
2048游戏C语言源代码(后缀txt改成cpp可直接运行)
{
temp[j]=code[i][j];/*把一行数移到中间变量*/
}
temp[4]=0;
{
code[i][j]=2;/*随机选一个空格填上2或4*/
}
move++;/*增加次数*/
}
print();/*显示*/
input=getch();/*输入方向*/
{
for(j=0;j<=3;j++)
{
if(code[i][j]==0)
{
printf("| ");/*0显示空格*/
}
else
printf("Score:%d Move:%d\n",score,move);
printf("Made by Yanjisheng\n");
printf("|-------------------|\n");/*显示横向分隔线*/
for(i=0;i<=3;i++)
}
}
break;
case 'S':
case 's':/*下*/
for(j=0;j<=3;j++)
{
}while(code[i][j]!=0);
if(((unsigned)rand())%4==0)
{
code[i][j]=4;
}
else
{
int i,j;
// clrscr();/*清屏*/
俄罗斯方块C语言源代码
}
}
/* 在图形模式下的清屏 */
void cls()
{
setfillstyle(SOLID_FILL,0);
setcolor(0);
bar(0,0,640,480);
}
/*在图形模式下的高级清屏*/
void clscr(int a,int b,int c,int d,int color){
{0,0,1,0,1,1,1,0,0,0,0,0,0,0,0,0}},
{
{1,1,0,0,1,0,0,0,1,0,0,0,0,0,0,0},
{1,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0},
{0,1,0,0,0,1,0,0,1,1,0,0,0,0,0,0},
{1,1,1,0,0,0,1,0,0,0,0,0,(SOLID_FILL,color);
setcolor(color);
bar(a,b,c,d);
}
/*最小方块的绘制*/
void minbox(int asc,int bsc,int color,int bdcolor){
int a=0,b=0;
a=SCCOL+asc;
#define VK_ESC 0x011b
#define VK_ENTER 0x1c0d
/* 定义俄罗斯方块的方向(我定义他为4种)*/
#define F_DONG 0
#define F_NAN 1
#define F_XI 2
#define F_BEI 3
#define NEXTCOL 20 /* 要出的下一个方块的纵坐标*/
C语言小游戏源代《俄罗斯方块》
C语言小游戏源代码《俄罗斯方块》#include <stdlib.h>#include <stdio.h>#include <graphics.h>#define ESC 27#define UP 328#define DOWN 336#define LEFT 331#define RIGHT 333#define BLANK 32#define BOTTOM 2#define CANNOT 1#define CAN 0#define MAX 30#define F1 315#define ADD 43#define EQUAL 61#define DEC 45#define SOUNDs 115#define SOUNDS 83#define PAUSEP 80#define PAUSEp 112void Init();void Down();void GoOn();void ksdown();void Display(int color);void Give();int Touch(int x,int y,int dx,int dy);int GeyKey();void Select();void DetectFill();void GetScores();void Fail();void Help();void Quit();void DrawBox(int x,int y,int Color);void OutTextXY(int x,int y,char *String); void DispScore(int x,int y,char Ch);void DrawNext(int Color);int Heng=12,Shu=20; /*横竖*/int Position[MAX][MAX];int middle[MAX][MAX];int ActH,ActS;int Act,Staus;int i,j,k;int Wid=10;int NoPass=CAN;float Delays=15000;int BeginH=250,BeginS=7;float Seconds=0;int Scores=0;int flag=1;int Sounds=CAN;int PreAct,NextAct;int a[8][4][4][4]={{{1,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0}, {1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0},{1,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0},{1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0}},{{1,1,0,0,1,1,0,0,0,0,0,0,0,0,0,0},{1,1,0,0,1,1,0,0,0,0,0,0,0,0,0,0},{1,1,0,0,1,1,0,0,0,0,0,0,0,0,0,0},{1,1,0,0,1,1,0,0,0,0,0,0,0,0,0,0}},{{1,1,1,0,0,1,0,0,0,0,0,0,0,0,0,0},{0,1,0,0,1,1,0,0,0,1,0,0,0,0,0,0},{0,1,0,0,1,1,1,0,0,0,0,0,0,0,0,0},{1,0,0,0,1,1,0,0,1,0,0,0,0,0,0,0}},{{1,0,0,0,1,1,0,0,0,1,0,0,0,0,0,0},{0,1,1,0,1,1,0,0,0,0,0,0,0,0,0,0},{1,0,0,0,1,1,0,0,0,1,0,0,0,0,0,0},{0,1,1,0,1,1,0,0,0,0,0,0,0,0,0,0}},{{0,1,0,0,1,1,0,0,1,0,0,0,0,0,0,0},{1,1,0,0,0,1,1,0,0,0,0,0,0,0,0,0},{0,1,0,0,1,1,0,0,1,0,0,0,0,0,0,0},{1,1,0,0,0,1,1,0,0,0,0,0,0,0,0,0}},{{1,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0},{1,1,0,0,1,0,0,0,1,0,0,0,0,0,0,0},{1,1,1,0,0,0,1,0,0,0,0,0,0,0,0,0}, {0,1,0,0,0,1,0,0,1,1,0,0,0,0,0,0}}, {{0,0,1,0,1,1,1,0,0,0,0,0,0,0,0,0}, {1,0,0,0,1,0,0,0,1,1,0,0,0,0,0,0}, {1,1,1,0,1,0,0,0,0,0,0,0,0,0,0,0}, {1,1,0,0,0,1,0,0,0,1,0,0,0,0,0,0}}, {{1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0}}}; int b[4][4];main(int argc,char *argv[]){if (argc!=1){if (argv[1]!="")Heng=atoi(argv[1]);if (argv[2]!="")Shu=atoi(argv[2]);}Init(); /*初始化界面*/PreAct=random(8); /*取得当前的方块*/ for(;;) /*以下是游戏流程*/{NextAct=random(8); /*取得下一个方块*/ DrawNext(1); /*画出下一个方块*/Act=PreAct;if (Heng%2==0) ActH=Heng/2;else ActH=(Heng-1)/2;ActS=0; /*方块开始从游戏空间的中间下落*/Staus=0; /*取开始的状态*/NoPass=CAN; /*物体可以下落*/Give(); /*取得当前的方块*/Display(Act+1); /*显示当前的方块,每种方块的颜色不同*/ GoOn(); /*游戏的算法精髓所在*/PreAct=NextAct; /*方块下落完毕,取得下一个方块*/ DrawNext(0);}}void Init(){int GraphDriver=DETECT,GraphMode;registerbgidriver(EGAVGA_driver);initgraph(&GraphDriver,&GraphMode,"");if (kbhit()) Sounds=CANNOT;setcolor(1);OutTextXY(10,10,"Tetris");OutTextXY(30,30,"Version 2.0");OutTextXY(10,120,"Help:");OutTextXY(20,140,"+ :Faster");OutTextXY(20,160,"- :Slower");OutTextXY(20,180,"Esc :Quit");OutTextXY(20,200,"F1 :Help");OutTextXY(10,310,"Copyright(c) 1998.2.22");OutTextXY(10,320,"By Mr. Unique");outtextxy(10,250,"Score: 00000");rectangle(BeginH-3,BeginS-3,BeginH+Heng*(Wid+2)+2,BeginS+Shu*(Wid+2)+2);rectangle(BeginH-5,BeginS-5,BeginH+Heng*(Wid+2)+4,BeginS+Shu*(Wid+2)+4);rectangle(BeginH+(Heng+4)*(Wid+2)-2,BeginS+10,BeginH+(Heng+8)*(Wid+2)+2,BeginS+12+4*( Wid+2));for (i=0;i<MAX;i++)for (j=0;j<MAX;j++){Position[i][j]=1;middle[i][j]=-1;}for (i=0;i<Heng;i++)for (j=0;j<Shu;j++)Position[i][j]=0;for (i=0;i<Heng;i++)for (j=0;j<Shu;j++)DrawBox(i,j,0);randomize();}void GoOn(){for(;;){Seconds+=0.2; /*控制方块的下落速度*/if (Seconds>=Delays){Down();Seconds=0;if (NoPass==BOTTOM){DetectFill();middle[ActH][ActS]=Act;if (ActS==0)Fail();return;}}if (kbhit())Select();}}void Down() /*方块下降*/{Display(0);if (Touch(ActH,ActS,0,1)==CAN) ActS++;elsemiddle[ActH][ActS]=Act; Display(Staus+1);}int Touch(int x,int y,int dx,int dy) {NoPass=CAN;for (i=0;i<4;i++)for (j=0;j<4;j++)Position[x+dx+i][y+dy+j]+=b[i][j];for (i=0;i<MAX;i++)for (j=0;j<MAX;j++)if (Position[i][j]>1)NoPass=CANNOT;for (i=0;i<4;i++)for (j=0;j<4;j++){Position[x+dx+i][y+dy+j]-=b[i][j]; middle[x+dx+i][y+dy+j]=Act;}if (NoPass==CANNOT && dx==0 && dy==1) {for (i=0;i<4;i++)for (j=0;j<4;j++)Position[x+i][y+j]+=b[i][j];NoPass=BOTTOM;}return NoPass;}int GetKey(void){int Ch,Low,Hig;Ch=bioskey(0);Low=Ch&0x00ff;Hig=(Ch&0xff00)>>8;return(Low==0?Hig+256:Low);}void Select(){int OldStaus,acts=ActS;switch(GetKey()){case ESC :Quit();break;case DOWN :Seconds+=14500;break;case LEFT :Display(0);if (ActH>0 && Touch(ActH,ActS,-1,0)==CAN) { ActH--;}Display(Act+1);break;case RIGHT :Display(0);if (ActH<Heng && Touch(ActH,ActS,1,0)==CAN) { ActH++;}Display(Act+1);break;case BLANK : Display(0);ksdown();Display(Act+1);break;case F1 :Help();break;case EQUAL :case ADD :if (Delays>300) Delays-=100;break; case DEC :if (Delays<3000) Delays+=100;break; case PAUSEP :case PAUSEp :getch();break;case SOUNDS :case SOUNDs :if (Sounds==CAN)Sounds=CANNOT;elseSounds=CAN;break;case UP :if(Act==7){while(acts<Shu-1&&Position[ActH][acts]!=1) acts++;Position[ActH][acts]=0;DrawBox(ActH,acts,0);acts=ActS;break;}else{Display(0);OldStaus=Staus;switch(Act){case 0:case 3:case 4:if (Staus==1) Staus=0;else Staus=1;break; case 1:break;case 2:case 5:case 6:if (Staus==3) Staus=0;else Staus++;break; }Give();if (Touch(ActH,ActS,0,0)==CANNOT){Staus=OldStaus;Give();}Display(Act+1);break;}}}void ksdown(){while(flag){if(Touch(ActH,ActS,0,0)==CAN){ActS++;}else {ActS--;flag=0;}}flag=1;}void Quit(){int ch,TopScore;FILE *fp;if ((fp=fopen("Russian.scr","r+"))!=NULL) {fscanf(fp,"%d",&TopScore);if (Scores>TopScore){setcolor(1);outtextxy(470,80,"Hello !");outtextxy(470,100,"In all the players,"); outtextxy(470,120,"You are the First !"); outtextxy(470,140,"And your score will"); outtextxy(470,160,"be the NEW RECORD !"); fseek(fp,0L,0);fprintf(fp,"%d",Scores);}fclose(fp);}setcolor(1);OutTextXY(470,220,"Are You Sure (Yes/no)?");ch=getch();if (ch=='y'||ch=='Y'){closegraph();delay(20);exit(0);}setcolor(0);outtextxy(470,220,"Are You Sure (Yes/no)?"); }void OutTextXY(int x,int y,char *String) {int i=0;char a[2];moveto(x,y);a[1]='\0';while (*(String+i)!='\0'){a[0]=*(String+i);outtext(a);if (Sounds==CAN && a[0]!=' '){sound(3000);delay(50);nosound();}i++;}}void Help(){unsigned Save;void *Buf;Save=imagesize(160,120,500,360);Buf=malloc(Save);getimage(160,120,500,360,Buf);setfillstyle(1,1);bar(160,120,500,280);setcolor(0);OutTextXY(170,130," About & Help");OutTextXY(170,150," # # # ########## # # # "); OutTextXY(170,160," # ## # # # # # # ###### ### "); OutTextXY(170,170," ########## ########## ## # # "); OutTextXY(170,180," # # # # # # # ## #### "); OutTextXY(170,190," # ## # #### ## # # # "); OutTextXY(170,200," # ## # # # # # ## # # # "); OutTextXY(170,210," # # # ## ## # ###### # # # "); OutTextXY(170,220," ## # ## # ## # # # # "); OutTextXY(170,230," # ## # #### # ## # "); OutTextXY(170,260," Good Luckly to You !!! ");getch();putimage(160,120,Buf,0);free(Buf);}void GetScores(){int Sec10000,Sec1000,Sec100,Sec10,Sec1; setfillstyle(0,1);bar(60,250,109,260);Sec1=Scores%10;Sec10=(Scores%100-Scores%10)/10;Sec100=(Scores%1000-Scores%100)/100;Sec1000=(Scores%10000-Scores%1000)/1000; Sec10000=(Scores%100000-Scores%10000)/10000; DispScore(60,250,'0'+Sec10000);DispScore(70,250,'0'+Sec1000);DispScore(80,250,'0'+Sec100);DispScore(90,250,'0'+Sec10);DispScore(100,250,'0'+Sec1);DispScore(110,250,'0');DispScore(120,250,'0');}void DispScore(int x,int y,char Ch){char a[2];a[1]='\0';a[0]=Ch;outtextxy(x,y,a);void Give(){for (i=0;i<4;i++)for (j=0;j<4;j++)b[i][j]=a[Act][Staus][i][j];}void Display(int color){for (i=0;i<4;i++)for (j=0;j<4;j++)if (b[i][j]==1) DrawBox(ActH+i,ActS+j,color); }void DrawBox(int x,int y,int Color){x=BeginH+x*(Wid+2);y=BeginS+y*(Wid+2);setfillstyle(1,Color);bar(x+2,y+2,x+Wid-1,y+Wid-1);if (Color==0)setcolor(9);elsesetcolor(Act+1);rectangle(x+4,y+4,x+Wid-4,y+Wid-4);}void DrawNext(int Color)for (i=0;i<4;i++)for (j=0;j<4;j++)if (a[NextAct][0][i][j]==1) DrawBox(Heng+4+i,1+j,Color); }void DetectFill(){int Number,Fall,FallTime=0;for (i=Shu-1;i>=0;i--){Number=0;for (j=0;j<Heng;j++)if (Position[j][i]==1) Number++;if (Number==Heng){FallTime++;if (Sounds==CAN){sound(500);delay(500);nosound();}for (Fall=i;Fall>0;Fall--)for (j=0;j<Heng;j++){Position[j][Fall]=Position[j][Fall-1];middle[j][Fall]=middle[j][Fall-1];if (Position[j][Fall]==0) DrawBox(j,Fall,0); else DrawBox(j,Fall,middle[j][Fall]+1);}i++;}}switch(FallTime){case 0:break;case 1:Scores+=1;break;case 2:Scores+=3;break;case 3:Scores+=6;break;case 4:Scores+=10;break;}if (FallTime!=0){GetScores();if (Scores%100==0) Delays-=100;}}void Fail(){if (Sounds==CAN){for (k=0;k<3;k++){sound(300);delay(200);nosound();}}setcolor(1);OutTextXY(440,200,"Game over!"); Quit();closegraph();exit(0);}。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
语言游戏源代码1、简单地开机密码程序""""""(){()()()()(" ! ' !!!");(); *若有错误不能通过程序*}(){ *,*;*":\\\\"; *本程序地位置*[];*"",*".^^^"; *是地备份*(); * :*("\\"); * \*(,"");(){(,"");() ();}(); *读取前各字符*[]'\';(()) *若读取地和指针一样就关闭文件,不然就添加*();{(,"");() ()()('\');();(()){()();}();();(()){()();}()();(); * *}}(){ *"";[] ;(){()()()();;()()()()(":");(){[]();(>) {(); ;} *若字符多于个字符就结束本次输入*([]) ;([]> []<) *若字符是数字或字母才算数*{('*');;}([]) *删除键*(>){("\ \");[]'\';;}}[]'\';(());{();()()()()(" !")();}}}(){();();}2、彩色贪吃蛇<><>; * 游戏速度*, , ; * 游戏分数*[]{, , , , , , , ,, , , , , , , ,, , , , , , , ,, , , , , , , ,, , , , , , , ,, , , , , , , ,, , , , , , , ,, , , , , , , ,, , , , , , , ,, , , , , , , ,, , , , , , , ,, , , , , , , ,, , , , , , , ,, , , , , , , ,, , , , , , , ,, , , , , , , ,, , , , , , , ,, , , , , , , , , , , , , , , ,, , , , , , , ,, , , , , , , ,, , , , , , , ,, , , , , , , ,, , , , , , , ,, , , , , , , ,, , , , , , , ,, , , , , , , ,, , , , , , , ,, , , , , , , ,, , , , , , , ,, , , , , , , ,, , , , , , , ,, , , , , , , ,, , , , , , , ,, , , , , , , ,};[]{, , , , , , , ,, , , , , , , ,, , , , , , , ,, , , , , , , ,, , , , , , , ,, , , , , , , ,, , , , , , , ,, , , , , , , ,, , , , , , , ,, , , , , , , ,, , , , , , , ,, , , , , , , ,, , , , , , , ,, , , , , , , ,, , , , , , , ,, , , , , , , ,, , , , , , , ,, , , , , , , ,, , , , , , , ,, , , , , , , ,, , , , , , , ,, , , , , , , , , , , , , , , ,, , , , , , , ,, , , , , , , ,, , , , , , , ,, , , , , , , ,, , , , , , , ,, , , , , , , ,, , , , , , , ,, , , , , , , ,, , , , , , , ,, , , , , , , ,, , , , , , , ,, , , , , , , ,};[]{, , , , , , , ,, , , , , , , ,, , , , , , , ,, , , , , , , ,, , , , , , , ,, , , , , , , ,, , , , , , , ,, , , , , , , ,, , , , , , , ,, , , , , , , ,, , , , , , , ,, , , , , , , ,, , , , , , , ,, , , , , , , ,, , , , , , , ,, , , , , , , ,, , , , , , , ,, , , , , , , ,, , , , , , , ,, , , , , , , ,, , , , , , , ,, , , , , , , ,, , , , , , , ,, , , , , , , ,, , , , , , , ,, , , , , , , ,, , , , , , , , , , , , , , , ,, , , , , , , ,, , , , , , , ,, , , , , , , ,, , , , , , , ,, , , , , , , ,, , , , , , , ,, , , , , , , ,};[]{, , , , , , , ,, , , , , , , ,, , , , , , , ,, , , , , , , ,, , , , , , , ,, , , , , , , ,, , , , , , , ,, , , , , , , ,, , , , , , , ,, , , , , , , ,, , , , , , , ,, , , , , , , ,, , , , , , , ,, , , , , , , ,, , , , , , , ,, , , , , , , ,, , , , , , , ,, , , , , , , ,, , , , , , , ,, , , , , , , ,, , , , , , , ,, , , , , , , ,, , , , , , , ,, , , , , , , ,, , , , , , , ,, , , , , , , ,, , , , , , , ,, , , , , , , ,, , , , , , , ,, , , , , , , ,, , , , , , , ,, , , , , , , , , , , , , , , ,, , , , , , , ,, , , , , , , ,};[]{, , , , , , , ,, , , , , , , ,, , , , , , , ,, , , , , , , ,};[]{, , , , , , , ,, , , , , , , ,, , , , , , , ,, , , , , , , ,};[]{, , , , , , , ,, , , , , , , ,, , , , , , , ,, , , , , , , ,};[]{, , , , , , , ,, , , , , , , ,, , , , , , , ,, , , , , , , ,};[]{, , , , , , , ,, , , , , , , ,, , , , , , , ,, , , , , , , ,} *定义结构体存储食物地属性*{; * 食物地坐标*;; * 值为表示屏幕上没有食物,值为表示屏幕上有食物* ; * 食物颜色*} *定义结构体存储蛇地属性*{[]; * 每一节蛇地坐标*[];[]*存储每一节蛇地颜色*; * 蛇地节数*; * 蛇移动地方向*; * 蛇地生命,如果为,蛇死,游戏结束*} ;()*图形驱动*{, ;();(, , "");}( *, , , , ) *汉字点阵*{, , , ;( ) ;( ; < ; )( ; < ; )( ; < ; )([*]( >> ))( * , , );}(){* 调用汉字点阵输出程序,显示标题和作者信息*(, , , , );(, , , , );(, , , , );(, , , , ); (, , , , );(, , , , );(, , , , );(, , , , );*作者田学科*(, , , , );(, , , , );(, , , , );(, , , , );(, , , , );}()*画出四周地墙*{( );();(, , );( ; < ; ){(, , , );(, , , );}( ; < ; ){(, , , );(, , , );}}(){* 打印游戏分数*[];(, );(, , , );();(, , );(, "", );(, , );} (){(); * 清屏函数*( ; < ; ) * 画出蛇死时地位置* {([]);([], [], [] , [] );}(); * 显示分数*();();(, , );();(, , " ");();(, , " ");}()* 玩游戏地具体过程*{, ;();();;; * 表示屏幕上没有食物*; * 表示蛇是活着地*; * 表示蛇地初始方向为向右*; * 蛇地初始化为两节*[] ; *两节蛇头初始化为绿色*[] ;[] ;[] ;[] ;[] ;() ;(){(){( ) * 如果蛇活着*{(){; ;() * ;() * ;( ; < ; ){( [] []);}() ;}}(){();(, , , );}( ; > ; ){[] [];[] [];}(){:[] ;;:[] ;;:[] ;;:[] ;;}( ; < ; ){([] [] [] []){(); ;;}}([] < [] > [] < [] > ) {();;}( );([] [] ) *蛇吃掉食物* {();(, , , );[] ;[] ;[] ;;;;();( ){( ; < ; ) * 画出蛇*{([]);([], [], [] , [] );}();();();();();();();();;();}{();();();}}( ; < ; ) * 画出蛇*{([]);([], [], [] , [] );}();();;();([], [],[] , [] );(() ) *如果没按有效键就重新开始循环* {;();( )();;( );( );( );}}( ) *如果蛇死了就退出循环*;}}(){ (){;();();();();();();(, , );(, , "()?");(){();( );}( );}();}3、语言实现移动电话系统<>*低于矩阵中所有元素*“”*关于交通数据地文件**可用地发射器数量*( [][],[][],[][] [][];(){[][]*上午:地交通数据*[][]; *上午:地交通数据*[][]*周末交通数据*, *通勤人员数据地权重因子**营销人员数据地权重因子*;*周末数据地权重因子*,*每个发射器地位置* ;;*和数据中当前地最大值*,,*矩阵地循环计数器*;*发射器地循环计数器**填入并显示交通数据*(,,);(“:、\\”)()(“\\ \\”);()(“\\ \\”)();*请用户输入权重因子*(“\\ :\”)(“ ( >) >”)(“”)(“( >) >”)(“”)(“”);*计算并显示加权后求和地数据*(<)(<)[][]*[][]*[][]*[][](“\\ :\\”)();*在矩阵中找出个最大值,将坐标临时存储在和中,然后把最后地结果坐标输出*(“\\ :\\”);(<){;*以一个过低地值为起点开始查找*(<){(<){(<[][]){[][]){;;}}}*将选中地单元赋一较低地值以避免下次再选中这一元素,显示查找结果*[][](“\”,);}();}**把中地交通数据填充到个×数组中*( [],*输出*[][]*输出*[][],*输出*{; *循环计数器**; *文件指针*(,“”);(;<)(<)(,“”[][];(<)(<)(,“”,[][]);();}**显示一个×整数矩阵地内容*( [][]){; *循环计数器*(<){ (<)(“”[][]);(“\”);}}4、扑克牌游戏*************************************() .:: : :: 给你张牌,然后让你在心中记住那张牌,然后电脑分组让你猜你记住地牌在第几组,然后猜出你记住地那张牌.: 出自儿童时地一个小魔术:修改历史**************************************<><><><><>*牌地总张数**一色牌地张数**扑克牌结构*{*扑克牌面上地大小* ; *扑克牌地花色*};*************************************************:: 洗牌,然后随机地得到张牌,要求九张牌不能有重复. :: (): 被修改地表(此项仅对于牵扯到数据库操作地程序): 被修改地表(此项仅对于牵扯到数据库操作地程序): [] 牌结构, 结构数组地大小::: 此函数修改[]地值,希望得到九张随机牌: 此函数有,有时会产生两个相同地牌,有待修订*************************************************( *, );*************************************************:: 显示数组地内容:: (): 被修改地表(此项仅对于牵扯到数据库操作地程序): 被修改地表(此项仅对于牵扯到数据库操作地程序): * 牌结构指针, 结构数组地大小:::*************************************************( *, );*************************************************:: 把张牌分别放到个数组中,每组张分组:: (): 被修改地表(此项仅对于牵扯到数据库操作地程序): 被修改地表(此项仅对于牵扯到数据库操作地程序): * 牌结构指针, 结构数组地大小::: 此函数修改*,*,* 地值*************************************************( *, *, *, *);*************************************************:: 用递归计算,所选地牌:: (): 被修改地表(此项仅对于牵扯到数据库操作地程序): 被修改地表(此项仅对于牵扯到数据库操作地程序): *, *, *::: 此函数修改*,*,* 地值************************************************** ( *, *, *, );*************************************************:: 右移操作::: 被修改地表(此项仅对于牵扯到数据库操作地程序): 被修改地表(此项仅对于牵扯到数据库操作地程序): *, *, *:: *: 此函数修改*,*,* 地值*************************************************( *, *, *, ) (){[]; *存放九张牌*[]; *第一组牌*[]; *第二组牌*[]; *第三组牌* ; *玩家地选择***存放玩家所记住(选)地牌* ;(, ); *洗牌,得到九张牌*("请记住一张牌千万别告诉我!最多经过下面三次我与你地对话,我就会知道你所记地那张牌!");("如果想继续玩,请准确地回答我问你地问题,根据提示回答!");("请放心,我不会问你你选了哪张牌地!");(, , , ); *把张牌分别放到个数组中,每组张分组*(, );(, );(, );("请告诉我你记住地那张牌所在行数");();()*分支猜你玩家记住地牌*{'':(, , , );;'':(, , , );;'':(, , , );;:("你在撒谎!不和你玩了!");();();();}( ){();();();}("你猜地牌为:");(, );("我猜地对吧,哈哈")()();}*地原代码*( *, ){[]*临时数组,用于存储牌**最为产生随机数地种地* ; *在牌地产生中起着指示作用* ( )()();*洗牌*( < ){() ;(((, , )) )(());[] ;;}}*找张牌给*( ; < ; ){([], );[] ""[]; *把余数给*[] ""[]; *把商给*}}*地原代码,将会自动换行* ( *, ){( ; < ; ){(" "[][]);( ( ) ((( ) ) )) ("");}(""); *自动换行*}* 地原代码*( *, *, *, *){ *循环参数**分给三个数*( < ){[] [];[] [];}*分给接下来地三个数* ( < ){[] [];[] [];;}*分给接下来地三个数* ( < )[] [];[] [];;}}*地实现*( *, *, *, ){*用于存放[]**用于存放[]** *[];[];* *[] [];[] [];* *[];[];* *[] ;[] ;* *[] ;[] ;}* ( *, *, *, ){(, , , ); * 把数组地第一个元素依次右移* ( ){([]);}(, );(, );(, );("请给出你记住地牌所在行数:");();;(); *获取你选地组*(){'':((, , , ));;'':([]);;:("你在撒谎!不和你玩了!");;}}、语言实现打字游戏""""""""""**( [][])* 刷新屏幕地输出图像 *{;();(" \")* 输出现在地等级,击中数和现在已下落总数 *("\");(<){(<)(""[][]);("\");}* () *("\");}* ***()* 离开程序时,调用该函数结束程序. *{();("\\\\ .");();}**( )* 进入游戏时选择游戏等级 *{()* *{();(" .\");();;(><) ();()();(" !\");}* () *}* ***( [][])* 随生成一个新地字符并将其加入数组地首行 *{;(){(()());(());[][];();}* *();}* ***( [][])* 将最后一行置空,并使所有在数组中其他行地字符下降一行 *{;;(<)* 遍历最后一行地所有字符,如果该字符非空则将其置空并使加一 * {([][]' '){;[][]' ';}(>)* 从倒数第二行地最后一个字符开始开始向前遍历该数组内地元素,如果该位置非空则将该字符移动至下一行 *{(>){([][]' '){[][];[][]' ';[][];}* *}* () *}* () *();}* ***( [][])*判断是否有字符从键盘键入.如果有,则从最后一行地最后一个元素开始遍历该数组,找出该字符,并把对应位置置空,且返回.如果有输入,但屏幕上无对应项,或无输入则返回*{;;(())* 判断用户是否从键盘键入字符.如果返回值为 *();(){('') ();(''){();(" .");();}(>){(>){([][]){[][]' ';();}* *}* () *}* () *();}* () *();}* ***( )*判断该次地成绩并输出对应地结果,询问用户是否继续,若继续,判断是否可以进入下一级别.*{;;*;();();()("!\");(>)("!\");(>)("' !\");(" .\");(" ? \");(;;)* *{();(){'' :{(>) ();();}* *'' : ();(" :");}* *}* *}* ***()* 进入程序时调用该函数,提示其操作. *{;(){();("\\\\\ !\\\ \ \ ");();(){'' : ()* 用户选择退出,退出主程序 *'' :{();("\\\\\ .\ .\ \ \ ");(){();('') ();('') ();}}'' : ();}}}**(){***函数声明*( [][]);();( );( [][]);( [][]);( [][]);( );();**[][];;(());();(<)* 初始化屏显数组 *{(<)()[][];[][]' ';}* () *();(;;)* 开始运行主程序 *{ ;;;(" !");();();(){()* 延迟 *();()* 当为地整数倍,即上一个字母下落行时生成一个新地字母在首行并刷新屏幕 *{();;();}*()*(())* 如果用户输入了字符并且正确,则使答对地数加一,并刷新屏幕 *{;();();}* () *((*))* 当*时,屏幕上地字母下落一行,并刷新屏幕,若最后一行有字母,则使错过数加一 *{;;();();}* ()*() * 当正确数加错误数等于预置地总字母个数时跳出该循环 * }( ()<)* 当用户成绩在以上,并且等级小于时,等级加一,即进入下一级别 *;}* () *}。