c 2b 2b how to falling numbers

Solutions on MaxInterview for c 2b 2b how to falling numbers by the best coders in the world

showing results for - "c 2b 2b how to falling numbers"
Briony
23 Mar 2020
1#include <iostream>
2#include <windows.h> 
3
4#include <stdio.h>
5#include <stdlib.h>
6#include <time.h>
7#include <conio.h>
8using namespace std;
9
10
11#define KB_UP 72
12#define KB_DOWN 80
13#define KB_LEFT 75
14#define KB_RIGHT 77
15#define KB_ESCAPE 27
16#define KB_F8 66
17
18
19/* Variables*/
20
21char screen_buffer[2000]={' '};
22int y_coord[2000]={0};
23int x=0, y=0,dy=0;
24int XMAX=77;
25int YMAX=23;
26int KB_code=0;
27bool QuitGame=false;
28int platformX=35, platformY=23;
29
30/* function prototypes*/
31
32void gotoxy(int x, int y);
33void clrscr(void);
34void setcolor(WORD color); 
35void simple_keyboard_input();  
36void draw_falling_numbers();
37void draw_platform();
38
39/*  main  */
40
41int main(void)
42{
43  /* generate random seed */
44  srand ( time(NULL) );
45
46  /* generate random number*/
47  for(int i=0;i<XMAX;i++) y_coord[i]=   rand() % YMAX;
48
49  while(!QuitGame)
50  {
51      /* simple keyboard input */
52      simple_keyboard_input();
53
54      /* draw falling numbers */
55      draw_falling_numbers();
56
57  }
58
59  /* restore text color */
60  setcolor(7);
61  clrscr( );
62  cout<<" \n";
63
64  cout<<" \nPress any key to continue\n";
65  cin.ignore();
66  cin.get();
67
68   return 0;
69}
70
71/* functions  */
72
73void draw_falling_numbers()
74{
75
76    for(x=0;x<=XMAX;x++)
77    {
78        /* generate random number */
79        int MatixNumber=rand() % 2 ;
80
81        /* update falling number */
82        y_coord[x]=y_coord[x]+1;
83
84        if (y_coord[x]>YMAX) y_coord[x]=0;
85
86        /* draw dark color */
87        setcolor(2);
88        gotoxy(x ,y_coord[x]-1); cout<<"  "<<MatixNumber<<"   ";
89
90        /* draw light color */
91        setcolor(10);
92        gotoxy(x ,y_coord[x]); cout<<"  "<<MatixNumber<<"   ";
93    }
94    /* wait some milliseconds */
95    Sleep(50);
96    //clrscr( );
97}
98
99
100void draw_platform()
101{
102  setcolor(7);
103 gotoxy(platformX ,platformY);cout<<"       ";
104
105 gotoxy(platformX ,platformY);cout<<"ÜÜÜÜÜÜ";
106 setcolor(7);
107 Sleep(5);
108}
109
110
111
112
113void simple_keyboard_input()
114{
115    if (kbhit())
116      {
117            KB_code = getch();
118            //cout<<"KB_code = "<<KB_code<<"\n";
119
120            switch (KB_code)
121            {
122
123                case KB_ESCAPE:
124
125                    QuitGame=true;
126
127                break;
128
129                case KB_LEFT:
130                           //Do something
131                    platformX=platformX-4;if(platformX<3) platformX=3;
132                break;
133
134                case KB_RIGHT:
135                           //Do something     
136                    platformX=platformX+4;if(platformX>74) platformX=74;
137                break;
138
139                case KB_UP:
140                           //Do something                     
141                break;
142
143                case KB_DOWN:
144                           //Do something                     
145                break;
146
147            }        
148
149      }
150
151}
152
153
154void setcolor(WORD color)
155{
156    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),color);
157    return;
158}
159
160
161void gotoxy(int x, int y)
162{
163  static HANDLE hStdout = NULL;
164  COORD coord;
165
166  coord.X = x;
167  coord.Y = y;
168
169  if(!hStdout)
170  {
171    hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
172  }
173
174  SetConsoleCursorPosition(hStdout,coord);
175}
176
177
178void clrscr(void)
179{
180  static HANDLE hStdout = NULL;      
181  static CONSOLE_SCREEN_BUFFER_INFO csbi;
182  const COORD startCoords = {0,0};   
183  DWORD dummy;
184
185  if(!hStdout)               
186  {
187    hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
188    GetConsoleScreenBufferInfo(hStdout,&csbi);
189  }
190
191  FillConsoleOutputCharacter(hStdout,
192                             ' ',
193                             csbi.dwSize.X * csbi.dwSize.Y,
194                             startCoords,
195                             &dummy);    
196  gotoxy(0,0);
197}