MCPcopy
hub / github.com/PySimpleGUI/PySimpleGUI / Ball

Class Ball

DemoPrograms/Demo_Pong_Multiple_Platforms.py:32–107  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

30
31
32class Ball:
33 def __init__(self, graph, bat_1, bat_2, colour):
34 self.graph = graph # type: sg.Graph
35 self.bat_1 = bat_1
36 self.bat_2 = bat_2
37 self.player_1_Score = player_1_Starting_Score
38 self.player_2_Score = player_2_Starting_Score
39 self.draw_P1 = None
40 self.draw_P2 = None
41 self.id = self.graph.draw_circle(
42 STARTING_BALL_POSITION, BALL_RADIUS, line_color=colour, fill_color=colour)
43 self.curx, self.cury = STARTING_BALL_POSITION
44 # self.graph.relocate_figure(self.id, STARTING_BALL_POSITION[0], STARTING_BALL_POSITION[1])
45 self.x = random.choice([-2.5, 2.5])
46 self.y = -2.5
47
48 def win_loss_check(self):
49 winner = None
50 if self.player_1_Score >= num_rounds:
51 winner = 'Player Right Wins'
52 if self.player_2_Score >= num_rounds:
53 winner = 'Player Left Wins'
54 return winner
55
56 def update_player1_score(self, val):
57 self.graph.delete_figure(self.draw_P1)
58 self.draw_P1 = self.graph.draw_text(
59 str(val), (170, 50), font=('Courier 60'), color='white')
60
61 def update_player2_score(self, val):
62 self.graph.delete_figure(self.draw_P2)
63 self.draw_P2 = self.graph.draw_text(
64 str(val), (550, 50), font=('courier 40'), color='white')
65
66 def hit_bat(self, pos):
67 bat_pos = (self.bat_1.curx, self.bat_1.cury)
68 if pos[0] >= bat_pos[0] and pos[0] <= bat_pos[0]+BAT_SIZE[0]:
69 if bat_pos[1] <= pos[1] <= bat_pos[1]+BAT_SIZE[1]:
70 return True
71 return False
72
73 def hit_bat2(self, pos):
74 bat_pos = (self.bat_2.curx, self.bat_2.cury)
75 if pos[0] >= bat_pos[0] and pos[0] <= bat_pos[0]+BAT_SIZE[0]:
76 if bat_pos[1] <= pos[1] <= bat_pos[1]+BAT_SIZE[1]:
77 return True
78 return False
79
80 def draw(self):
81 self.curx += self.x
82 self.cury += self.y
83 self.graph.relocate_figure(self.id, self.curx, self.cury)
84 if self.cury <= 0: # see if hit top or bottom of play area. If so, reverse y direction
85 self.y = 4
86 self.cury = 0
87 if self.cury >= GAMEPLAY_SIZE[1]-BALL_RADIUS/2:
88 self.y = -4
89 self.cury = GAMEPLAY_SIZE[1]-BALL_RADIUS/2

Callers 1

pongFunction · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected