-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcatch.py
104 lines (76 loc) · 2.36 KB
/
catch.py
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
# To change this template, choose Tools | Templates
# and open the template in the editor.
__author__="karlo"
__date__ ="$Jun 5, 2011 3:58:52 PM$"
from gasp import *
COMPUTER_WINS = 1
PLAYER_WINS = 0
QUIT = -1
def distance(x1, y1, x2, y2):
return ((x2 - x1)**2 + (y2 - y1)**2)**0.5
def play_round():
ball_x = 10
ball_y = random_between(20, 280)
ball = Circle((ball_x, ball_y), 10, filled=True)
dx = 4
dy = random_between(-5, 5)
mitt_x = 780
mitt_y = random_between(20, 280)
mitt = Circle((mitt_x, mitt_y), 20)
while True:
if ball_y >= 590 or ball_y <= 10:
dy *= -1
ball_x += dx
ball_y += dy
if ball_x >= 810:
remove_from_screen(ball)
remove_from_screen(mitt)
return COMPUTER_WINS
move_to(ball, (ball_x, ball_y))
if key_pressed('k') and mitt_y <= 580:
mitt_y += 5
elif key_pressed('j') and mitt_y >= 20:
mitt_y -= 5
if key_pressed('escape'):
return QUIT
move_to(mitt, (mitt_x, mitt_y))
if distance(ball_x, ball_y, mitt_x, mitt_y) <= 30:
remove_from_screen(ball)
remove_from_screen(mitt)
return PLAYER_WINS
update_when('next_tick')
def play_game():
player_score = 0
comp_score = 0
while True:
pmsg = Text("Player: %d Points" % player_score, (10, 570), size=24)
cmsg = Text("Computer: %d Points" % comp_score, (640, 570), size=24)
sleep(3)
remove_from_screen(pmsg)
remove_from_screen(cmsg)
result = play_round()
if result == PLAYER_WINS:
player_score += 1
pmsg = Text("Player Wins!", (340, 290), size=32)
sleep(2)
remove_from_screen(pmsg)
elif result == COMPUTER_WINS:
comp_score += 1
else:
return QUIT
if player_score == 5:
return PLAYER_WINS
elif comp_score == 5:
return COMPUTER_WINS
def catch():
begin_graphics(800, 600, title="Catch", background=color.YELLOW)
set_speed(120)
result = play_game()
if result == PLAYER_WINS:
Text("Player Wins!", (340, 290), size=32)
elif result == COMPUTER_WINS:
Text("Computer Wins!", (340, 290), size=32)
sleep(4)
end_graphics()
if __name__ == "__main__":
catch()