首页 > 其他分享 >【江鸟中原】————简单小游戏

【江鸟中原】————简单小游戏

时间:2023-12-22 14:00:53浏览次数:27  
标签:江鸟 head int void gotoxy next 小游戏 printf 中原

一、引言

  经过一段实践学习之后,我开始运用学习过的知识,自己实践创作了一个鸿蒙小型游戏。

二、游戏介绍

  我所创作的是一个贪吃蛇的小游戏,这个游戏主要思路是在一个1010方格上随机出现一个格子,当贪吃蛇的头出现在格子上时,则贪吃蛇整体长度加1。当贪吃蛇的头部在1010方格之外时,则失败,游戏结束。

三、代码实现

// 必要的头文件

#include <stdio.h>

#include <stdlib.h>

#include <conio.h>

#include <time.h>

#include <windows.h>

#include <string.h>

// 定义标记上下左右的明示常量

#define UP 1

#define DOWN 2

#define LEFT 3

#define RIGHT 4

#define ESC 5

#define FOOD 10

// 定义表示位置的结构体类型

typedef struct snake{

int x;

int y;

struct snake *next;

}snake;

// 定义全局变量

int score = 0; // 当前得分

int speed = 200; // 存储当前速度

int status;

snake *tail, *head; // 存储蛇头蛇尾

snake *food, *q;// q用于遍历链表

HANDLE hOUT;

void gotoxy(int x, int y); // 设置光标位置

int choice(void); // 载入游戏界面

int color(int c); // 设置字体颜色

void printGame(void); // 打印游戏界面

void printSnake(void); // 打印蛇身

void printFood(void); // 打印食物

void printTips(void); // 打印提示

void snakeMove(void); // 主操作函数

int biteSelf(void); // 判断是否咬到了自己

int encounterWall(void); // 判断是否撞墙

void keyboardControl(void); // 获取击键

void speedUp(void); // 加速

void speedDown(void); // 减速

int endGame(void); // 结束函数;

char *s_gets(char *st, int n); // 读取字符

void frees(snake *); // 释放内存

int main(int argc, char *argv[]){

while (1)

{

 if (choice() == 1)

  keyboardControl();

 else

 {

  gotoxy(5, 15);

  printf("按任意键返回");

  getchar(); // 去除前一个前导换行

  while (1)

  {

   if (getchar())

   {

    system("cls");

    break;

   }

  }

 }

}

frees(head);

return 0;

}

void gotoxy(int x, int y)

{

COORD c;

c.X = x;

c.Y = y;

SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), c);

}

int choice(void)

{

int yourchoice;

// 画出界面

gotoxy(35, 5);

color(11);

printf("\t贪吃蛇大作战\n");

printf("\n\n");

color(13);

printf("\t\t★★★★★★★★  Snake!");

printf("\t\t★★★★★★★★  Snake!");

gotoxy(25, 15);

color(12);

printf("1.进入游戏\t2.查看说明\t3.退出游戏\n");

color(11);

printf("请选择:");

scanf("%d", &yourchoice);

switch (yourchoice)

{

case 1:

 system("cls");

 // 初始化

 printGame();

 printSnake();

 printFood();

 break;

case 2:

 system("cls");

 printTips();

 break;

case 3:

 system("cls");

 gotoxy(30, 10);

 color(11);

 printf("Bye!");

 exit(0);

default:

 system("cls");

 printf("没有此序号,请输入1,2或3\n");

 Sleep(2000);

  system("cls");

 // 初始化

 printGame();

 printSnake();

 printFood();

 break;

case 2:

 system("cls");

 printTips();

 break;

case 3:

 system("cls");

 gotoxy(30, 10);

 color(11);

 printf("Bye!");

 exit(0);

default:

 system("cls");

 printf("没有此序号,请输入1,2或3\n");

 Sleep(2000);

 system("cls");

}

return yourchoice;

}

int color(int c)

{

SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), c);        //更改文字颜色

return 0;

}

void printGame()

{

int i, j;

gotoxy(5, 5);

printf("游戏载入中...请稍后");

Sleep(2000);

system("cls");

// 打印上下界面

for (i = 0; i <= 50; i += 2)

{

 gotoxy(i, 0);

 printf("□");

 gotoxy(i, 25);

 printf("□");

}

// 打印左右界面

for (i = 0; i <= 25; i += 1)

{

 gotoxy(0, i);

 printf("□");

 gotoxy(50, i);

 printf("□");

}

// 打印中间网格

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

{

 for (j = 2; j <= 48; j += 2)

 {

  gotoxy(j, i);

  color(11);

  printf("■");

 }

}

// 打印右侧的规则和计分栏

gotoxy(60, 13);

printf("当前分数:%d分,当前速度%d", score, speed);

gotoxy(60, 15);

printf("用↑ ↓ ← →分别控制蛇的移动\n");

gotoxy(60, 18);

printf("每次获取食物加10分  按下F1加速,F2减速,空格暂停\n");

gotoxy(60, 20);

printf("不能撞墙和咬到自己!");

gotoxy(60, 22);

printf("速度不低于100,不高于300");

}

void printSnake(void)

{

int i;

// 设定蛇尾(16,13),头插入,初始向右

tail = (snake*)malloc(sizeof(snake));

tail->x = 16;

tail->y = 13;

tail->next = NULL;

// 设定初始蛇长是4

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

{

 head = (snake*)malloc(sizeof(snake));

 head->next = tail;

 head->x = 16 + 2 * i;

 head->y = 13;

 tail = head; // 头成为尾

}

// 输出蛇身

while (tail->next)

{

 gotoxy(tail->x, tail->y);

 color(14);

 printf("★");

 tail = tail->next;

}

}

void printFood(void)

{

srand((unsigned)time(NULL)); // 利用时钟修改种子

food = (snake*)malloc(sizeof(snake));

food->x = 1; // 初始化x坐标

while (food->x % 2 && food->x)

{

 food->x = rand() % 46 + 2;// 2-48

}

food->y = rand() % 23 + 1; // 1-24

q = head; // 不改变头遍历链表

while (q->next)

{

 if (q->x == food->x && q->y == food->y)

 {

  free(food);

  printFood();

 }

 else

 {

  gotoxy(food->x, food->y);

  color(12);

  printf("●");

  break;

 }void printTips(void)

{

color(11);

printf("***********Tips************\n");

printf("1.采用合理的速度可以获得更高的分数哦!\n");

printf("2.一定不要撞到自己或者两边的墙!\n");

printf("3.游戏过程中按ESC退出游戏!\n");

}

void snakeMove(void)

{

snake *snakenext;

snakenext = (snake*)malloc(sizeof(snake));

if (biteSelf())

{

 gotoxy(60, 11);

 printf("咬到自己啦!");

 free(snakenext);

 Sleep(1500);

 system("cls");

 exit(0);

}

else if (encounterWall())

{

 gotoxy(60, 11);

 printf("撞到墙啦!");

 free(snakenext);

 Sleep(1500);

 system("cls");

 exit(0);

}

else

{

 // 前两个条件判断完成才开始移动

 Sleep(350 - speed);

 if (status == UP)

 {

  snakenext->x = head->x;

  snakenext->y = head->y - 1;

  snakenext->next = head;

  head = snakenext;

  q = head;

, 13);

   printf("当前分数:%d分,当前速度%d", score, speed);

   printFood();

  }

  else

  {

   while (q->next->next)

   {

    gotoxy(q->x, q->y);

    color(14);

    printf("★");

    q = q->next;

   }

   gotoxy(q->next->x, q->next->y);

   color(11);

   printf("■");

   free(q->next);

   q->next = NULL;

  }

 }

 else if (status == LEFT)

 {

  snakenext->x = head->x - 2;

  snakenext->y = head->y;

  snakenext->next = head;

  head = snakenext;

  q = head;

  if (snakenext->x == food->x && snakenext->y == food->y)

  {

   while (q)

   {

    gotoxy(q->x, q->y);

    color(14);

    printf("★");

    q = q->next;

   }

   score += FOOD;

   gotoxy(60, 13);

   printf("当前分数:%d分,当前速度%d", score, speed);

   printFood();

  }

  else

  {

   while (q->next->next)

   {

    gotoxy(q->x, q->y);

    color(14);

    printf("★");

    q = q->next;

   }

   gotoxy(q->next->x, q->next->y);

   color(11);

   printf("■");

   free(q->next);

   q->next = NULL;

  }

 }

 else if (status == RIGHT)

 {

  snakenext->x = head->x + 2;

  snakenext->y = head->y;

  snakenext->next = head;

  head = snakenext;

  q = head;

  if (snakenext->x == food->x && snakenext->y == food->y)

  {

   while (q)

   {

    gotoxy(q->x, q->y);

    color(14);

    printf("★");

    q = q->next;

   }

   score += FOOD;

   gotoxy(60, 13);

   printf("当前分数:%d分,当前速度%d", score, speed);

   printFood();

  }

四、总结

  经过一段时间的学习和这次代码经历,我发现了我还有很多的不足,但这次宝贵的经历也让我更加完善自己的思维和能力。

标签:江鸟,head,int,void,gotoxy,next,小游戏,printf,中原
From: https://blog.51cto.com/u_16464062/8935147

相关文章

  • 微信小游戏中的场景拖拽显示范围
    usingOrg.BouncyCastle.Crypto.Macs;usingSystem;usingSystem.Collections.Generic;usingUnityEngine;[RequireComponent(typeof(Camera))]publicclassCameraControl:MonoBehaviour{publicstaticCameraControlinstance;publicList<string>......
  • 微信小游戏中拖拽场景位置的限制代码
    usingSystem.Collections.Generic;usingUnityEngine;[RequireComponent(typeof(Camera))]publicclassCameraControl:MonoBehaviour{publicstaticCameraControlinstance;publicList<string>list_RayName=newList<string>();publ......
  • 做一个小游戏,跳跃的小球
    以下为代码:#-*-coding:utf-8-*-importsys#导入sys模块importpygame#导入pygame模块pygame.init()#初始化pygamesize=width,height=800,700#设置窗口screen=pygame.display.set_mode(size)#显示窗口color=(0,0,0)#设置颜......
  • 小游戏
    importpygameimportsysclassBird(object):"""定义一个鸟类"""def__init__(self):"""定义初始化方法"""self.birdRect=pygame.Rect(65,50,50,50)#鸟的矩形#定义鸟的3种状态列表......
  • 小游戏
    flybird游戏importpygameimportrandompygame.init()WIDTH=288HEIGHT=512screen=pygame.display.set_mode((WIDTH,HEIGHT))pygame.display.set_caption('FlappyBird')background_img=pygame.image.load('background.png').convert()bird_im......
  • 小游戏
    制作一个跳跃的小球游戏(Pygame基本使用)importsysimportpygamepygame.init()size=width,height=640,480screen=pygame.display.set_mode(size)color=(0,0,0)ball=pygame.image.load("ball123.png")ballrect=ball.get_rect()s......
  • C语言—猜数字小游戏
    #include<stdio.h>#include<time.h>#include<stdlib.h>voidmenu(){ printf("########################\n"); printf("#####1.play0.exit#####\n"); printf("########################\n");}voidgame(){......
  • 【开源】贪吃蛇小游戏
    #include<bits/stdc++.h>//清屏:system("cls"); 1:'◎'2:'⊙'3:'▲'4:'◆'5:'■'#include<windows.h>//停顿:Sleep();#include<conio.h>usingnamespacestd;inta[100][100],dir[100......
  • python——小游戏(ball,bird)
      ball #-*-coding:utf-8-*-"""CreatedonWedDec1309:19:382023@author:kabuqinuo"""importsys#导入sys模块importpygame#导入pygame模块pygame.init()#初始化pygamesize=width,height=640,480#设置窗......
  • 耐心极限大挑战,整蛊小游戏之「禁止向上走」【玩转Web小游戏】
    故事是这样开始的很久很久以前,我关注的一个游戏博主,发了一个游戏视频。然后我就见识到了什么叫,「游戏叫你一步噶,你绝对走不到第二步」。这个带那么点整蛊的性质的脑洞游戏,瞬间引起了我浓厚的兴趣。需要玩家克服大脑常规套路的惯性,那岂不是游戏处处是惊喜。不过,游戏的本质还是在于趣......