1
+ import pygame
2
+
3
+ class Ball (pygame .sprite .Sprite ):
4
+ '''This class defines the sprite for our Ball.'''
5
+ def __init__ (self , screen ):
6
+ '''This initializer takes a screen surface as a parameter, initializes
7
+ the image and rect attributes, and x,y direction of the ball.'''
8
+ # Call the parent __init__() method
9
+ pygame .sprite .Sprite .__init__ (self )
10
+
11
+ # Set the image and rect attributes for the Ball
12
+ self .image = pygame .Surface ((20 , 20 ))
13
+ self .image .fill ((0 , 0 , 0 ))
14
+ self .image .set_colorkey ((0 ,0 ,0 ))
15
+ pygame .draw .circle (self .image , (255 , 0 , 0 ), (10 , 10 ), 10 , 0 )
16
+ #self.image = pygame.image.load("ball.jpg")
17
+ self .rect = self .image .get_rect ()
18
+ self .rect .center = (screen .get_width ()/ 2 ,screen .get_height ()/ 2 )
19
+
20
+ # Instance variables to keep track of the screen surface
21
+ # and set the initial x and y vector for the ball.
22
+ self .window = screen
23
+ self .dx = 5 #5
24
+ self .dy = - 3 #-3
25
+
26
+ def changeDirection (self ):
27
+ '''This method causes the x direction of the ball to reverse.'''
28
+ if self .dx < 0 :
29
+ self .dx -= 1
30
+ elif self .dx > 1 :
31
+ self .dx += 1
32
+ elif self .dy < 0 :
33
+ self .dx -= 1
34
+ elif self .dy > 1 :
35
+ self .dy += 1
36
+
37
+ self .dx = - self .dx
38
+
39
+
40
+ def update (self ):
41
+ '''This method will be called automatically to reposition the
42
+ ball sprite on the screen.'''
43
+ # Check if we have reached the left or right end of the screen.
44
+ # If not, then keep moving the ball in the same x direction.
45
+ if ((self .rect .left > 0 ) and (self .dx < 0 )) or \
46
+ ((self .rect .right < self .window .get_width ()) and (self .dx > 0 )):
47
+ self .rect .left += self .dx
48
+ # If yes, then reverse the x direction.
49
+ else :
50
+ self .dx = - self .dx
51
+
52
+ # Check if we have reached the top or bottom of the court.
53
+ # If not, then keep moving the ball in the same y direction.
54
+ if ((self .rect .top - 40 > 0 ) and (self .dy > 0 )) or \
55
+ ((self .rect .bottom + 40 < self .window .get_height ()) and (self .dy < 0 )):
56
+ self .rect .top -= self .dy
57
+ # If yes, then reverse the y direction.
58
+ else :
59
+ self .dy = - self .dy
60
+
61
+ class Player (pygame .sprite .Sprite ):
62
+ '''This class defines the sprite for Player 1 and Player 2'''
63
+ def __init__ (self , screen , playerNum ):
64
+ '''This initializer takes a screen surface, and player number as
65
+ parameters. Depending on the player number it loads the appropriate
66
+ image and positions it on the left or right end of the court'''
67
+ # Call the parent __init__() method
68
+ pygame .sprite .Sprite .__init__ (self )
69
+
70
+ # Define the image attributes for a black rectangle.
71
+ #self.image = pygame.Surface((10, 100))
72
+ #self.image = self.image.convert()
73
+ #self.image.fill((0, 0, 0))
74
+ self .image = pygame .image .load ("twig.jpg" )
75
+ self .rect = self .image .get_rect ()
76
+
77
+ # If we are initializing a sprite for player 1,
78
+ # position it 10 pixels from screen left.
79
+ if playerNum == 1 :
80
+ self .rect .left = 10
81
+ # Otherwise, position it 10 pixels from the right of the screen.
82
+ else :
83
+ self .rect .right = screen .get_width ()- 10
84
+
85
+ # Center the player vertically in the window.
86
+ self .rect .top = screen .get_height ()/ 2 + 50
87
+ self .window = screen
88
+ self .dy = 0
89
+
90
+ def changeDirection (self , xyChange ):
91
+ '''This method takes a (x,y) tuple as a parameter, extracts the
92
+ y element from it, and uses this to set the players y direction.'''
93
+ self .dy = xyChange [1 ]
94
+
95
+ def update (self ):
96
+ '''This method will be called automatically to reposition the
97
+ player sprite on the screen.'''
98
+ # Check if we have reached the top or bottom of the screen.
99
+ # If not, then keep moving the player in the same y direction.
100
+ if ((self .rect .top > 0 ) and (self .dy > 0 )) or \
101
+ ((self .rect .bottom < self .window .get_height ()) and (self .dy < 0 )):
102
+ self .rect .top -= (self .dy * 7 ) #5
103
+ # If yes, then we don't change the y position of the player at all.
104
+
105
+ class EndZone (pygame .sprite .Sprite ):
106
+ '''This class defines the sprite for our left and right end zones'''
107
+ def __init__ (self , screen , xPosition ):
108
+ '''This initializer takes a screen surface, and x position as
109
+ parameters. For the left (player 1) endzone, x_position will = 0,
110
+ and for the right (player 2) endzone, x_position will = 639.'''
111
+ # Call the parent __init__() method
112
+ pygame .sprite .Sprite .__init__ (self )
113
+
114
+ # Our endzone sprite will be a 1 pixel wide black line.
115
+ self .image = pygame .Surface ((1 , screen .get_height ()))
116
+ self .image = self .image .convert ()
117
+ self .image .fill ((0 , 0 , 0 ))
118
+
119
+ # Set the rect attributes for the endzone
120
+ self .rect = self .image .get_rect ()
121
+ self .rect .left = xPosition
122
+ self .rect .top = 0
123
+
124
+ class ScoreKeeper (pygame .sprite .Sprite ):
125
+ '''This class defines a label sprite to display the score.'''
126
+ def __init__ (self ):
127
+ '''This initializer loads the system font "Arial", and
128
+ sets the starting score to 0:0'''
129
+ # Call the parent __init__() method
130
+ pygame .sprite .Sprite .__init__ (self )
131
+
132
+ # Load our custom font, and initialize the starting score.
133
+ self .font = pygame .font .Font ("walt.ttf" , 30 )
134
+ #self.font = pygame.font.SysFont("Arial", 30)
135
+ self .player1Score = 0
136
+ self .player2Score = 0
137
+
138
+ def player1Scored (self ):
139
+ '''This method adds one to the score for player 1'''
140
+ self .player1Score += 1
141
+
142
+ def player2Scored (self ):
143
+ '''This method adds one to the score for player 1'''
144
+ self .player2Score += 1
145
+
146
+ def winner (self ):
147
+ '''There is a winner when one player reaches 3 points.
148
+ This method returns 0 if there is no winner yet, 1 if player 1 has
149
+ won, or 2 if player 2 has won.'''
150
+ if self .player1Score == 5 :
151
+ return 1
152
+ elif self .player2Score == 5 :
153
+ return 2
154
+ else :
155
+ return 0
156
+
157
+ def update (self ):
158
+ '''This method will be called automatically to display
159
+ the current score at the top of the game window.'''
160
+ message = "Player 1: " + str (self .player1Score ) + \
161
+ " vs. Player 2: " + str (self .player2Score )
162
+ self .image = self .font .render (message , 1 , (0 , 0 , 0 ))
163
+ self .rect = self .image .get_rect ()
164
+ self .rect .center = (320 , 15 )
0 commit comments