Skip to content

Commit 9bbb648

Browse files
author
Kristian Rother
committed
ad prototype exercise
1 parent 6c47fc8 commit 9bbb648

4 files changed

Lines changed: 115 additions & 0 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ Below you find development tools and techniques that help you to write programs
3131

3232
## Coding Strategies
3333

34+
* [The Prototype](prototype.md)
3435
* [Coding Strategies](writing_code.md)
3536
* [Debugging](debugging.md)
3637

images/prototype.png

15.8 KB
Loading

prototype.md

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
2+
# The Prototype
3+
4+
Before attempting a more costly clean implementation, you may want to check whether the project is feasible at all.
5+
You can do such a check by implementing a quick and dirty proof of concept: a **prototype**.
6+
The goal of a prototype is usually to reduce risks in a project.
7+
8+
A prototype can answer questions like:
9+
10+
* is my programming environment set up properly?
11+
* can we solve a particular algorithmic problem?
12+
* does a library do what we need?
13+
* is the algorithm/library fast enough?
14+
* what safety/security risks are there?
15+
* did we understand the customer correctly?
16+
17+
## A Prototype for a Snake game
18+
19+
The following code is a prototype for a Snake game.
20+
The program is a simple game where you move a symbol around with the `WASD` keys.
21+
The goal of this prototype is to prove that we can process keyboard input and draw into a terminal.
22+
23+
:::include prototype.py
24+
25+
26+
## Exercise: Run the prototype
27+
28+
#### Step 1
29+
30+
Save the code in :::file prototype.py to a Python module.
31+
32+
#### Step 2
33+
34+
On Windows, you need to install `windows-curses`:
35+
36+
:::bash
37+
pip install windows-curses
38+
39+
#### Step 3
40+
41+
Change to the directory with the `.py` file and execute the code with:
42+
43+
:::bash
44+
python prototype.py
45+
46+
You should see a screen where you can control the `'O'` with the keys **W, A, S and D**:
47+
48+
![prototype output](images/prototype.png)
49+
50+
51+
## Reflection Questions
52+
53+
Discuss the following questions:
54+
55+
* Why do you think is the prototype focused on testing the input/output library?
56+
* Is it more work to write a cleaner version of the prototype later? Why or why not?
57+
* Is there any code that you don't understand?

prototype.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
"""
2+
Proof-of-concept: move around in a 2D frame
3+
"""
4+
import curses
5+
6+
# WASD keys
7+
KEY_COMMANDS = {97: "left", 100: "right", 119: "up", 115: "down"}
8+
9+
# prepare the screen
10+
screen = curses.initscr()
11+
curses.start_color()
12+
curses.init_pair(1, curses.COLOR_GREEN, curses.COLOR_BLACK)
13+
curses.curs_set(0)
14+
curses.noecho()
15+
curses.raw()
16+
screen.keypad(False)
17+
18+
win = curses.newwin(20, 20, 0, 0)
19+
win.nodelay(True)
20+
21+
22+
def game_loop(screen):
23+
"""called by curses"""
24+
x, y = 5, 5
25+
26+
# draw
27+
screen.clear()
28+
screen.addch(y, x, "O", curses.color_pair(1))
29+
win.refresh()
30+
screen.refresh()
31+
32+
while True:
33+
34+
# handle moves
35+
char = win.getch()
36+
direction = KEY_COMMANDS.get(char)
37+
if direction == "left":
38+
x -= 1
39+
elif direction == "right":
40+
x += 1
41+
elif direction == "up":
42+
y -= 1
43+
elif direction == "down":
44+
y += 1
45+
else:
46+
continue
47+
48+
# draw
49+
screen.clear()
50+
screen.addch(y, x, "O", curses.color_pair(1))
51+
win.refresh()
52+
screen.refresh()
53+
54+
55+
if __name__ == "__main__":
56+
curses.wrapper(game_loop)
57+
curses.endwin()

0 commit comments

Comments
 (0)