Programming/C
HW 4
hannahchun
2022. 7. 3. 20:34
Problem 1
Bouncing Ball
1. Compile and run the following code.
* Put Console.c and Console.h into the current directory, and run the following commands:
gcc [file name].c Console.c -D_WINDOWS // on Windows
gcc [file name].c Console.c -D_MAC // on MacOS
2. Modify the code so that the ball bounces like the video below
skeleton code
#include <stdio.h>
#include "Console.h"
int main()
{
int screen_width = getWindowWidth(); // get width of current console window
int screen_height = getWindowHeight() - 3; // get width of current console window
int x = screen_width / 2, y = screen_height / 2; // set initial coordinate by center of the screen
int dx = 1, dy = 1;
int key = 0;
clrscr(); // clear screen (defined in Console.c)
gotoxy(1, screen_height + 1);
printf("screen size = %d x %d. Press ESC to fisnish.\n", screen_width, screen_height);
while(key != 27){ // the ASCII code of ESC is 27.
// save previous coordinate
int oldx = x;
int oldy = y;
// move the coordinate of the ball
y += dy;
if(y > screen_height) // if the ball reaches the bottom, restart from the top
y = 1;
// erase previous coordinate
gotoxy(oldx, oldy);
printf(" ");
// draw star at the new object
gotoxy(x, y);
printf("*");
// move the cursor to (1, 1) so that it does not blink
gotoxy(1, 1);
// flush screen (display all content in the output buffer and flush it)
// this is to display all messages immediately.
fflush(stdout);
// the following routine detects and reads a key press. this is to stop when ESC key is pressed.
if(kbhit())
key = getch();
// delay for 50 ms
MySleep(50); // defined in Console.c
}
clrscr();
gotoxy(1, 1);
printf("Bye!\n");
return 0;
}
revised code
#include <stdio.h>
#include "Console.h"
int main()
{
int screen_width = getWindowWidth(); // get width of current console window
int screen_height = getWindowHeight() - 3; // get height of current console window
int x = screen_width / 2, y = screen_height / 2; // set initial coordinate by center of the screen
int dx = 1, dy = 1;
int key = 0;
clrscr(); // clear screen (defined in Console.c)
gotoxy(1, screen_height + 1);
printf("screen size = %d x %d. Press ESC to finish.\n", screen_width, screen_height);
while(key != 27){ // the ASCII code of ESC is 27.
// save previous coordinate
int oldx = x;
int oldy = y;
// move the x and y coordinate
x += dx;
y += dy;
if(y<0 || y > screen_height) // if 'y' is out of the range of the minimum or maximum y coordinate of the screen,
dy*=-1; // multiply 'dy' by -1 to put the y coordinate within the range of the screen
if(x<0 || x > screen_width)
dx*=-1;
clrscr(); //clean screen
// draw star at the new object
gotoxy(x, y);
printf("*");
// move the cursor to (1, 1) so that it does not blink
gotoxy(1, 1);
// flush screen (display all content in the output buffer and flush it)
// this is to display all messages immediately.
fflush(stdout);
// the following routine detects and reads a key press. this is to stop when ESC key is pressed.
if(kbhit())
key = getch();
// delay for 50 ms
MySleep(50); // defined in Console.c
}
clrscr();
gotoxy(1, 1);
printf("Bye!\n");
return 0;
}
Problem 2
Center Editor
1. Compile and run the following code.
* Put Console.c and Console.h into the current directory, and run the following commands:
gcc [file name].c Console.c -D_WINDOWS // on Windows
gcc [file name].c Console.c -D_MAC // on MacOS
2. Complete the code so that it can run as the video below
skeleton code
#include <stdio.h>
#include <string.h>
#include "Console.h"
void PrintCenter(char text[], int cx, int cy);
// Displays a text at the center of the screen
void Erase(int len);
// Erases a text from the screen given its length
void EraseCenter(int len, int cx, int cy);
// Erases the previous message at the center of the screen given the length of the previous message
int main()
{
int screen_width = getWindowWidth(); // get width of current console window
int screen_height = getWindowHeight() - 3; // get width of current console window
int cx = screen_width / 2, cy = screen_height / 2; // center of the screen
char text[256] = "";
int len = 0; // the length of text
clrscr();
gotoxy(1, 1);
printf("Press ESC to quit.\n");
char key = 0;
while(key != 27){
int oldlen = len; // save the length of the current text
key = getch(); // reads a key from the user
switch(key){
case 27: // ESC key
// if key == ESC, do nothing
break;
case 8: // backspace key
// TO DO: erase the last character of text and decrease len.
// Then, reprint the text at the center of the screen
break;
case 13: // Enter key
// TO DO: print current text at (1, 2). (erase previous text at (1, 2))
// TO DO: clear text (fill text[0] by zero), and reset len to zero
// TO DO: Erase the center text
break;
default: // all other keys
// TO DO: append key at the end of text, and increase len
// TO DO: display new text at the center of the screen
break;
}
}
// print a goodbye message at (1, 3)
gotoxy(1, 3);
printf("Bye!\n");
return 0;
}
void PrintCenter(char text[], int cx, int cy)
// Displays a text at the center of the screen
{
int len = strlen(text); // returns the length of text. declared in string.h
gotoxy(cx - len / 2, cy);
printf("%s", text);
}
void Erase(int len)
// Erases a text from the screen given its length
{
// TO DO: print space character len times (use the for statement)
}
void EraseCenter(int len, int cx, int cy)
// Erases the previous message at the center of the screen given the length of the previous message
{
// TO DO: implement this function
}
revised code
#include <stdio.h>
#include <string.h>
#include "Console.h"
void PrintCenter(char text[], int cx, int cy);
// Displays a text at the center of the screen
void Erase(int len);
// Erases a text from the screen given its length
void EraseCenter(int len, int cx, int cy);
// Erases the previous message at the center of the screen given the length of the previous message
int main()
{
int screen_width = getWindowWidth(); // get width of current console window
int screen_height = getWindowHeight() - 3; // get width of current console window
int cx = screen_width / 2, cy = screen_height / 2; // center of the screen
char text[256] = "";
int len = 0; // the length of text
clrscr();
gotoxy(1, 1);
printf("Press ESC to quit.\n");
char key = 0;
while(key != 27){
int oldlen = len; // save the length of the current text
key = getch(); // reads a key from the user
switch(key){
case 27: // ESC key
// if key == ESC, do nothing
break;
case 8: // backspace key
// TO DO: erase the last character of text and decrease len.
// Then, reprint the text at the center of the screen
text[oldlen-1]=0;
oldlen-=1;
EraseCenter(oldlen,cx,cy);
PrintCenter(text, cx, cy);
break;
case 13: // Enter key
// TO DO: print current text at (1, 2). (erase previous text at (1, 2))
gotoxy(1,2);
Erase(screen_width);
gotoxy(1,2);
printf("%s",text);
// TO DO: clear text (fill text[0] by zero), and reset len to zero
text[0]=0;
len=0;
// TO DO: Erase the center text
EraseCenter(oldlen,cx,cy);
break;
default: // all other keys
// TO DO: append key at the end of text, and increase len
text[len]=(char)key;
len+=1;
text[len]=0;
// TO DO: display new text at the center of the screen
PrintCenter(text,cx,cy);
break;
}
}
// print a goodbye message at (1, 3)
gotoxy(1, 3);
printf("Bye!\n");
return 0;
}
void PrintCenter(char text[], int cx, int cy)
// Displays a text at the center of the screen
{
int len = strlen(text); // returns the length of text. declared in string.h
gotoxy(cx - len / 2, cy);
printf("%s", text);
}
void Erase(int len)
// Erases a text from the screen given its length
{
// TO DO: print space character len times (use the for statement)
for(int i=0 ; i<len ; i++)
printf(" ");
}
void EraseCenter(int len, int cx, int cy)
// Erases the previous message at the center of the screen given the length of the previous message
{
// TO DO: implement this function
char str[len];
for(int i=0 ; i<len ; i++)
str[i]=' ';
str[len]=0;
PrintCenter(str,cx,cy);
}