DOC PREVIEW
USF CS 112 - Introduction to Programming II

This preview shows page 1-2-19-20 out of 20 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 20 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 20 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 20 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 20 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 20 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

small lecturenumber - hepage : Cursessmall lecturenumber - hepage : Hello world in cursessmall lecturenumber - hepage : The piecessmall lecturenumber - hepage : Placing the cursorsmall lecturenumber - hepage : Outputsmall lecturenumber - hepage : Example - adding a marqueesmall lecturenumber - hepage : Example - adding a marqueesmall lecturenumber - hepage : Getting inputsmall lecturenumber - hepage : Animationsmall lecturenumber - hepage : Text displaysmall lecturenumber - hepage : Exercisesmall lecturenumber - hepage : Dealing with inputsmall lecturenumber - hepage : Clearing the screensmall lecturenumber - hepage : Using Curses in Project 4small lecturenumber - hepage : Advanced stuff: colorsmall lecturenumber - hepage : Advanced stuff: colorsmall lecturenumber - hepage : Advanced stuff: dealing with arrow keyssmall lecturenumber - hepage : Exercisesmall lecturenumber - hepage : More stuffIntroduction to Programming IICursesChris BrooksDepartment of Computer ScienceUniversity of San FranciscoDepartment of Computer Science — University of San Francisco – p. 1/??24-2: Curses•Curses is a C library for displaying ASCII characters at arbitrarypoints within a text window.•Nice for making better-looking text-based UIs.•C also has a number of third-party GUI libraries◦Nothing standard like Java hasDepartment of Computer Science — University of San Francisco – p. 2/??24-3: Hello world in curses#include <curses.h>int main(void) {int c;initscr();move(10,25);printw("Hello world!");refresh();c = getch();endwin();return (0);}Department of Computer Science — University of San Francisco – p. 3/??24-4: The pieces•#include <curses.h>•initscr() - this sets up the curses window. It should be the firstline in your program.•refresh() - redraws the screen.•endwin() - this cleans up curses. It should be the last thing inyour program.Department of Computer Science — University of San Francisco – p. 4/??24-5: Placing the cursor•curses uses (row, col) as its indexing method.•(0,0) is the upper-left corner.◦In other words, (y,x) is the representation•To place the cursor at a particular location, do move(y,x).Department of Computer Science — University of San Francisco – p. 5/??24-6: Output•printw() - a printf-style function that places a string at thecurrent cursor location.•putch() - write one character at the current location.•And, if you’re feeling lazy, you can do:◦mvaddch(row,col, char)◦mvprintw(row,col, string, arg1, arg2, ...)◦to combine moving and writing.Department of Computer Science — University of San Francisco – p. 6/??24-7: Example - adding a marquee•Let’s add a border to our “Hello World” app.Department of Computer Science — University of San Francisco – p. 7/??24-8: Example - adding a marquee•Let’s add a border to our “Hello World” app.•Actually, we could also use the box(stdscr, hchar, vchar)function.•Notice that box takes three arguments: window name,horizontal char, vertical char.•We can actually have multiple ’windows’.Department of Computer Science — University of San Francisco – p. 8/??24-9: Getting input•We can read input with:◦getch() = works just like getc()◦scanw() - works just like scanf.•In this case, we use getch() to leave the input on the screen.•We can also use usleep() to specify a number of microsecondsthe program should sleep.Department of Computer Science — University of San Francisco – p. 9/??24-10: Animation•To change the appearance on the screen, we just draw over theold content and draw our new content.•In curses, that means writing spaces over areas we want to beblank.•How can we make the print statement crawl across the screen?•Let’s modify it to print a command line argument.Department of Computer Science — University of San Francisco – p. 10/??24-11: Text display•We can also set the attributes of the text that’s printed, usingattron() and attroff()◦Interesting attributes are:•A_NORMAL•A_STANDOUT•A_UNDERLINE•A_REVERSE•A_BLINK•A_INVISDepartment of Computer Science — University of San Francisco – p. 11/??24-12: Exercise•: Make the ’hello’ string crawl left to right, with reverse videotext.Department of Computer Science — University of San Francisco – p. 12/??24-13: Dealing with input•As we mentioned before, you can use scanw() and getch() toread in input.•Let’s use this to create a simple quiz program.Department of Computer Science — University of San Francisco – p. 13/??24-14: Clearing the screen•We can use clear() to clear the whole screen.•Let’s add this to our quiz program. Remove the question beforeprinting ’correct’ or ’incorrect’.Department of Computer Science — University of San Francisco – p. 14/??24-15: Using Curses in Project 4•Where do you need to use this in project 4?•drawBoard should draw each component of the boardArray.•Draw a ’1’ if the square is alive.•Draw a ’ ’ if the square is dead.•Refresh once all cells are drawn.Department of Computer Science — University of San Francisco – p. 15/??24-16: Advanced stuff: color•To set the screen to draw in other colors, we do the following:◦Add start_color() to the beginning of your program.◦Create a set of color paris - these indicate the foregroundand background colors.◦init_pair(1, COLOR_RED, COLOR_BLACK);◦To start using a particular pair, set it as an attribute:◦attron(COLOR_PAIR(1));Department of Computer Science — University of San Francisco – p. 16/??24-17: Advanced stuff: color•Curses has 8 builtin colors:•COLOR_BLACK, COLOR_RED, COLOR_GREEN,COLOR_YELLOW, COLOR_BLUE, COLOR_MAGENTA,COLOR_CYAN, COLOR_WHITE•You can also change the builtin colors by setting their RGBvalues:◦init_color(COLOR_RED, 700, 700, 0);•Modify the marquee program to use at least three different colorpairs.Department of Computer Science — University of San Francisco – p. 17/??24-18: Advanced stuff: dealing with arrow keys•If you’re making a game, you typically want the user to provideinput via the arrow keys.•The numerical values returned by the arrow keys are:65,66,67,68.•You can do:int ch;ch = getc();if (ch == KEY_LEFT) {etc}Department of Computer Science — University of San Francisco – p. 18/??24-19: Exercise•Let’s use this to make a program that moves a characteraround the screen.•When the user pushes ’up’, move the character up one row.Etc.•Issues:◦Don’t forget to


View Full Document

USF CS 112 - Introduction to Programming II

Documents in this Course
Structs

Structs

4 pages

Trees

Trees

25 pages

Strings

Strings

27 pages

Queues

Queues

3 pages

Trees

Trees

24 pages

Arrays

Arrays

5 pages

ArrayList

ArrayList

24 pages

Stacks

Stacks

2 pages

Stacks

Stacks

8 pages

Trees

Trees

24 pages

Stacks

Stacks

8 pages

Queues

Queues

16 pages

Queues

Queues

17 pages

Queues

Queues

17 pages

Load more
Download Introduction to Programming II
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Introduction to Programming II and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Introduction to Programming II 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?