Repository navigation
Expand file tree
/
Copy pathbouncing-ball2.py
More file actions
executable file
·107 lines (88 loc) · 3.09 KB
/
Copy pathbouncing-ball2.py
File metadata and controls
executable file
·107 lines (88 loc) · 3.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#!/usr/bin/env python3
"""
Evolving the bouncing ball to include other items
"""
import curses
from curses import KEY_RIGHT, KEY_LEFT
def main(screen):
"""
Bounce a ball on a board and assign points for each bounce.
"""
curses.curs_set(0)
max_y, max_x = screen.getmaxyx()
ball = "o"
board = "_"*20
board_x = max_x//2 - len(board)//2
ball_y, ball_x = 3, 3
vert_dir = 1
hori_dir = 1
delay = 15
delayed = 0
life = 3
score = 0
screen.timeout(0)
while True:
screen.clear()
screen.addstr(0, 0, "Score: " + str(score))
screen.addstr(1, 0, "Lives left: " + str(life))
screen.addstr(ball_y, ball_x, ball)
screen.addstr(max_y-1, board_x, board)
q = screen.getch()
# if possible, move paddle
if q == KEY_LEFT and board_x > 0:
board_x -= 1
elif q == KEY_RIGHT and board_x < max_x-1-len(board):
board_x += 1
# quit
elif q == ord("q"):
#screen.clear()
text = "GAME OVER!"
screen.addstr(max_y//2, max_x//2-len(text)//2, text)
screen.refresh()
curses.napms(2500)
break
if delayed >= delay:
# check where ball is and reverse dir if needed
if ball_y == max_y-1 or ball_y == 0:
vert_dir = -vert_dir # reverse top
if ball_x == max_x-len(ball)-1 or ball_x == 0:
hori_dir = -hori_dir # reverse
if ball_y == max_y-2:
if ball_x > board_x and ball_x < board_x+len(board):
vert_dir = -vert_dir # reverse top
score += 1
if score > 1 and score % 5 == 0 and len(board) > 8:
board = board[1:]
elif score > 1 and score % 5 == 0 and delay > 8:
delay -= 1
screen.refresh()
# potential board bounce
if ball_y == max_y-1:
if ball_x < board_x or ball_x > board_x+len(board):
life -= 1
ball_y, ball_x = 3, 3
vert_dir, hori_dir = 1, 1
if life == 0:
#screen.clear()
text = "GAME OVER!"
screen.addstr(max_y//2, max_x//2-len(text)//2, text)
screen.refresh()
curses.napms(2500)
break
elif ball_x > board_x and ball_x < board_x+len(board):
score += 1
if score > 1 and score % 5 == 0 and len(board) > 8:
board = board[1:]
elif score > 1 and score % 5 == 0 and delay > 8:
delay -= 1
screen.refresh()
ball_x += hori_dir
ball_y += vert_dir
delayed = 0
delayed += 1
curses.napms(5)
curses.endwin()
print("Final score:", score)
if __name__ == "__main__":
print(main.__doc__)
curses.wrapper(main)