Repository navigation
Expand file tree
/
Copy pathbouncing-ball3.py
More file actions
executable file
·150 lines (124 loc) · 4.38 KB
/
Copy pathbouncing-ball3.py
File metadata and controls
executable file
·150 lines (124 loc) · 4.38 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#!/usr/bin/env python3
"""
Evolving the bouncing ball to include other things to hit
"""
import curses
from curses import KEY_RIGHT, KEY_LEFT
def main(screen):
"""
Bounce a ball now becomes a simple breakout
"""
curses.curs_set(0)
max_y, max_x = screen.getmaxyx()
x = 15
ball = "o"
board = "_"*x
plate = "_"
plate_y = 0, 1 # rows to have plates on
plates = []
for y in plate_y:
count = 2
while count < max_x-2:
plates.append([y, count])
count += len(plate)+1
board_x = max_x//4 - len(board)//2
ball_y, ball_x = 4, 2
y_dir = 1
x_dir = 1
delay = 30
delayed = 0
life = 3
score = 0
screen.timeout(0)
while True:
screen.clear()
text = "Score: " + str(score)
screen.addstr(max_y-1, 0, text)
screen.addstr(max_y-1, len(text)+5, "Lives left: " + str(life))
screen.addstr(ball_y, ball_x, ball)
for y, x in plates:
screen.addstr(y, x, plate)
screen.addstr(max_y-2, 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
elif q == ord(" "):
q = -1 # one (Pause/Resume)
while q != ord(" "):
q = screen.getch()
continue
# 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:
ball_x += x_dir
ball_y += y_dir
# check where ball is and reverse dir if needed
next_y, next_x = ball_y+y_dir, ball_x
if [next_y, next_x] in plates:
# hit a plate
plates.remove([next_y, next_x])
score += 1
if score > 1 and score % 2 == 0 and len(board) > 10:
board = board[1:]
elif score > 1 and score % 2 == 0 and delay > 10:
delay -= 1
y_dir = -y_dir # reverse
elif [next_y, next_x+x_dir] in plates:
plates.remove([next_y, next_x+x_dir])
score += 1
if score > 1 and score % 2 == 0 and len(board) > 10:
board = board[1:]
elif score > 1 and score % 2 == 0 and delay > 10:
delay -= 1
y_dir = -y_dir # reverse
elif ball_y == max_y-2 or ball_y == 0:
y_dir = -y_dir # reverse
# side-bunce
if ball_x == max_x-len(ball)-1 or ball_x == 0:
x_dir = -x_dir # reverse
# potential next board-bounce
if ball_y == max_y-3:
if ball_x+x_dir < board_x or ball_x+x_dir > board_x+len(board):
# ball will miss board
x_dir = -x_dir
life -= 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
y_dir = -y_dir
ball_x, ball_y = 4, 2
if ball_x+x_dir > board_x and ball_x+x_dir < board_x+len(board):
# Ball hit board
y_dir = -y_dir
if ball_x < 0:
ball_x = 0
elif ball_x >= max_x-len(ball)-1:
ball_x = max_x-len(ball)-1
if len(plates) == 0:
text = "GAME OVER!"
screen.addstr(max_y//2, max_x//2-len(text)//2, text)
screen.refresh()
curses.napms(2500)
delayed = 0
screen.refresh()
delayed += 1
curses.napms(3)
curses.endwin()
print("Final score:", score)
print(plates)
if __name__ == "__main__":
print(main.__doc__)
curses.wrapper(main)