反弹球2.0
来源:互联网 发布:疯狂淘宝李涛怎么样 编辑:程序博客网 时间:2024/05/17 17:44
用数组加入了多个球
#include <stdio.h>#include <Windows.h>#include <stdlib.h>#include <conio.h>#include <time.h>#define WIDTH 50#define HIGH 30#define BALL_NUMBERS 2#define BLOCK_NUMBERS 10int ball_x[BALL_NUMBERS], ball_y[BALL_NUMBERS], speed_x[BALL_NUMBERS], speed_y[BALL_NUMBERS];int radius, position_x, position_y;int block_x[BLOCK_NUMBERS], block_y[BLOCK_NUMBERS];int canvas[WIDTH][HIGH] = { 0 };void setup(void);void gotoxy(int x, int y);//VS中未包括gotoxy函数,必须自己实现;使光标定位到指定位置void HideCursor();//隐藏光标void show(void);void update_with_input(void);void update_without_input(void);main(){ srand(time(NULL)));//如果不用srand的话,每次rand函数出来的数都是一样的; //而且srand应该放在setup前,而且time(NULL)不是time setup(); while (1) { HideCursor(); //不能放在setup里 show(); update_without_input(); update_with_input(); } //getchar(); return 0;}void setup(void){ radius = 5, position_x = WIDTH/2, position_y = HIGH-1;//不能达到HIGH for (int i = 0; i < BLOCK_NUMBERS; i++) { block_x[i] = rand() % (WIDTH-1); block_y[i] = rand() % (HIGH/4); } for (int i = 0; i < BALL_NUMBERS; i++) { ball_x[i] = rand() % (WIDTH-1); ball_y[i] = rand() % (HIGH-(HIGH / 4)-5) + (HIGH / 4)-1; } for (int i = 0; i < BALL_NUMBERS; i++) { tag1: speed_x[i] = rand() % 3 - 1; speed_y[i] = rand() % 3 - 1; if (speed_x[i] == 0 || speed_y == 0) { goto tag1; } } for (int i = 0; i < BALL_NUMBERS; i++) { canvas[ball_x[i]][ball_y[i]] = 1; } for (int i = 0; i < BLOCK_NUMBERS; i++) { canvas[block_x[i]][block_y[i]] = 2; } for (int i = position_x - radius; i <= position_x + radius; i++) { canvas[i][position_y] = 3; } for (int i = 0; i < HIGH; i++) { canvas[WIDTH-1][i] = 4;//不能达到WIDTH }}void gotoxy(int x, int y){ HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE); COORD pos; pos.X = x; pos.Y = y; SetConsoleCursorPosition(handle, pos);}void HideCursor(void){ CONSOLE_CURSOR_INFO cursor_info = { 1,0 }; SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);}void show(void){ gotoxy(0, 0); for (int i = 0; i <HIGH; i++) { for (int j = 0; j <WIDTH; j++) { switch (canvas[j][i]) { case 1: printf("0"); break; case 2: printf("*"); break; case 3: printf("="); break; case 4: printf("|"); case 0: printf(" "); break; default: break; } } printf("\n"); } for (int i = 0; i < WIDTH; i++) { printf("-"); }}void update_without_input(void){ for (int i = 0; i < BALL_NUMBERS; i++) { if (ball_x[i] == 0 || ball_x[i] == WIDTH-2) speed_x[i] = -speed_x[i]; if (ball_y[i] == 0 ) speed_y[i] = -speed_y[i]; } static int delay = 0; if (delay < 4) delay++; else if (delay == 4) { for (int i = 0; i < BALL_NUMBERS; i++) { canvas[ball_x[i]][ball_y[i]] = 0; ball_x[i] += speed_x[i]; ball_y[i] += speed_y[i]; canvas[ball_x[i]][ball_y[i]] = 1; } delay = 0; } for (int j = 0; j < BLOCK_NUMBERS; j++) { for (int i = 0; i < BALL_NUMBERS; i++) { if (ball_x[i] == block_x[j] && ball_y[i] == block_y[j]) { block_x[j] = -1; speed_y[i] = -speed_y[i]; speed_x[i] = -speed_x[i]; } } } for (int i = 0; i < BALL_NUMBERS; i++) { if (ball_y[i] == position_y-1 && (ball_x[i] >= position_x - radius && ball_x[i] <= position_x + radius)) speed_y[i] = -speed_y[i]; if (ball_y[i] == position_y && (ball_x[i] < (position_x - radius) || ball_x[i] > (position_x + radius))) { printf("lose!"); Sleep(1000);//s要大写 exit(0); } }}void update_with_input(void){ char input; if (_kbhit())//有输入为1,没输入为0 { input = _getch(); if (position_x + radius < WIDTH && position_x - radius >= 0) { switch (input) { case 'a': //单引号而不是双引号 canvas[position_x + radius][position_y] = 0; position_x--; canvas[position_x - radius][position_y] = 3; if (position_x - radius < 0) position_x = radius; break; case'd': canvas[position_x - radius][position_y] = 0; position_x++; canvas[position_x + radius][position_y] = 3; if (position_x + radius >= WIDTH) position_x = WIDTH - radius - 1; break; } } }}
阅读全文
0 0
- 反弹球2.0
- 反弹球
- [VB.NET]反弹球
- OpenGL反弹球
- 45度反弹球
- 反弹球1.0
- D. 反弹球游戏 几何水题 ,画出图就可AC
- C语言小游戏————反弹球(简单的图形化界面)
- 弹球游戏
- 弹球游戏
- 弹球II
- 弹球小游戏
- 弹球游戏
- 弹球游戏
- 弹球游戏
- 弹球小游戏
- 弹球游戏
- 桌面弹球
- 抽屉布局+PullToRefreshListView展示
- Maven的安装以及配置(Eclipse)
- android中获取SHA1的代码
- windows下用navicat远程链接虚拟机Linux下MySQL数据库
- java--工厂模式
- 反弹球2.0
- CentOS7 常用命令集合
- <link>标签中的rel="sheetstyle"含义
- Codeforces 272E Dima and Horses【玄学暴力】
- js常用代码
- 调整虚拟机的尺寸
- 欢迎使用CSDN-markdown编辑器
- AngularJS行程
- 软件架构学习小结