• 自己做的一个小游戏(1)--吃金币(基础版)




    效果图

    代码

    #include<stdio.h>
    #include<conio.h>
    #include<Windows.h>
    #include<time.h>//计时器
    #include<math.h>
    #include<stdlib.h>
    
    //跳转函数
    void turnto(int x,int y)
    {
    	COORD loc;
    	loc.X = x;
    	loc.Y = y;
    	SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), loc);
    }
    
    //打印+清除
    void clean(int x,int y)
    {
    	printf("I");
    	turnto(x, y);
    	printf(" ");
    }
    
    //随机数
    int unknow(int x, int y)
    {
    	int i;
    	i = (rand() * (y - x) / RAND_MAX + x);
    	return i;
    }
    
    //o-(-.-)-I开始
    int main(void)
    {
    	clock_t start, end;
    	int t;
    	int x, y, i, a, b;
    	int score=0;
    	char name[10];
    	long coin[10];
    
    
    	//输入名字
    	printf("本游戏需要英文输入法");
    	Sleep(1500);
    	turnto(0, 0);
    	printf("请输入你的英文名字:_______");
    	scanf("%s", &name);
    	printf("w向上,s向下,a向左,d向右,q to quit");
    	
    	//初始化
    	
    	x = 20;
    	y = 13;
    	turnto(x, y);
    
    	printf("I");
    
    	//coin创建
    	srand((unsigned int)time(0));//不要把srand与rand放一个循环
    	for (i = 0; i <= 9; i++)
    	{
    		a = unknow(2, 117);
    		b = unknow(3, 27);
    		turnto(a, b);
    		printf("$");
    		coin[i] = a * 100 + b;
    	}
    	
    	//计时开始
    	start = clock();
    
    	//移动
    	while((i=_getch())!=0)//getch也行?
    	{
    		if (i == (int)'w')
    		{
    			turnto(x, y - 1);
    			clean(x, y);
    			turnto(x, y - 1);
    			y = y - 1;
    		}
    		else if(i==(int)'s')
    		{
    			turnto(x, y + 1);
    			clean(x, y);
    			turnto(x, y + 1);
    			y = y +1;
    		}
    		else if (i == (int)'a')
    		{
    			turnto(x-1, y );
    			clean(x, y);
    			turnto(x-1, y );
    			x = x - 1;
    		}
    		else if (i == (int)'d')
    		{
    			turnto(x + 1, y);
    			clean(x, y);
    			turnto(x + 1, y);
    			x = x + 1;
    		}
    		else if (i == (int)'q')
    		{
    			printf("你选择退出");
    			Sleep(1000);
    			return 0;
    		}
    		//遍历
    		for (i = 0; i <= sizeof(coin) / sizeof(long); i++)
    		{
    			if (x * 100 + y == coin[i])
    			{
    				coin[i] = 100000;
    				score++;
    				turnto(0, 1);
    				printf("得分:%d                    ",score);
    				turnto(x, y);
    			}
    		}
    		if (score == 10)
    		{
    			break;
    		}
    	}
    
    	//计时结束
    	end = clock();
    	t = (end - start) / CLOCKS_PER_SEC;
    	turnto(26, 1);
    	printf("  用时: %d 秒     
     ", t);
    	Sleep(1000);
    	printf("%s,你的综合得分为%d",name,110-t);
    	Sleep(1000);
    	return 0;
    }
    

    ps:这是我上大学前几天看了printf()与scanf()之后做出来的游戏;其中随机数与位置跳转函数都是上网查的。所以说编程很有意思,它让你创造而不是让你接受。

  • 相关阅读:
    从MVC和三层架构说到ssh整合开发-下
    Codeforces Round #226 (Div. 2)--A Bear and Raspberry
    ISA95的抽象惯例
    Difference between End-to-end testing and System testing
    javascript Date format(js日期格式化)
    给easyui datebox扩展一个清空按钮
    easyui combobox开启搜索自动完成功能
    给你的webstorm添加快速生成注释得快捷键
    location.origin兼容IE
    将json转换成struts参数
  • 原文地址:https://www.cnblogs.com/juicebox/p/9636442.html
Copyright © 2020-2023  润新知