-
Notifications
You must be signed in to change notification settings - Fork 0
/
BOsprites.py
236 lines (190 loc) · 8.85 KB
/
BOsprites.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
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
'''Date:Apr. 29, 2019
Author:Andrew Lin
Description: The classes that will be used in my BreakOut game.
'''
import pygame,random
class Ball(pygame.sprite.Sprite):
'''This class defines the sprite for our Ball.'''
def __init__(self, screen):
'''This initializer takes a screen surface as a parameter, initializes
the image and rect attributes, and x,y direction of the ball.'''
# Call the parent __init__() method
pygame.sprite.Sprite.__init__(self)
# Set the image and rect attributes for the Ball
self.image = pygame.Surface((16, 16))
self.image.fill((0, 0, 0))
self.image.set_colorkey((0,0,0))
pygame.draw.circle(self.image, (255, 0, 0), (8, 8), 8, 0)
self.rect = self.image.get_rect()
self.rect.center = (screen.get_width()/2,235)
# Instance variables to keep track of the screen surface
# and set the initial x and y vector for the ball.
self.window = screen
self.dx = 6
self.dy = -5
def changeDirection(self):
'''This method causes the x direction of the ball to reverse.'''
self.dy = -self.dy
def update(self):
'''This method will be called automatically to reposition the
ball sprite on the screen.'''
# Check if we have reached the left or right end of the screen.
# If not, then keep moving the ball in the same x direction.
if ((self.rect.left > 0) and (self.dx < 0)) or\
((self.rect.right < self.window.get_width()) and (self.dx > 0)):
self.rect.left += self.dx
# If yes, then reverse the x direction.
else:
self.dx = -self.dx
# Check if we have reached the top or bottom of the court.
# If not, then keep moving the ball in the same y direction.
if ((self.rect.top-40 > 0) and (self.dy > 0)) or\
((self.rect.bottom < self.window.get_height()) and (self.dy < 0)):
self.rect.top -= self.dy
# If yes, then reverse the y direction.
else:
self.dy = -self.dy
class Player(pygame.sprite.Sprite):
'''This class defines the sprite for Player 1 and the second paddle'''
def __init__(self, screen,x,y):
'''This initializer takes a screen surface, and player number as
parameters. Depending on the player number it loads the appropriate
image and positions it on the left or right end of the court'''
# Call the parent __init__() method
pygame.sprite.Sprite.__init__(self)
# Define the image attributes for a black rectangle.
self.image = pygame.Surface((100, 10))
self.image = self.image.convert()
self.image.fill((0, 0, 0))
self.rect = self.image.get_rect()
# If we are initializing a sprite for player 1,
# position it 10 pixels from screen left.
self.rect.top = y
# Center the player hoizontally in the window.
self.rect.centerx = x
self.window = screen
self.dx = 0
def setPosition(self,x,y):
'''This method recieves two int as a parameter to set the position
of the paddles.'''
self.rect.centerx = x
self.rect.top = y
return self.rect.centerx, self.rect.top
def changeDirection(self, xyChange):
'''This method takes a (x,y) tuple as a parameter, extracts the
x element from it, and uses this to set the players x direction.'''
self.dx = xyChange[0]
def update(self):
'''This method will be called automatically to reposition the
player sprite on the screen.'''
# Check if we have reached the right or left of the screen.
# If not, then keep moving the player in the same x direction.
if ((self.rect.left > 0) and (self.dx < 0)) or\
((self.rect.right < self.window.get_width()) and (self.dx > 0)):
self.rect.right += (self.dx*8)
class EndZone(pygame.sprite.Sprite):
'''This class defines the sprite for our bottom end zones'''
def __init__(self, screen, xPosition):
'''This initializer takes a screen surface, and x position as
parameters. For the left (player 1) endzone, x_position will = 0,
and for the right (player 2) endzone, x_position will = 639.'''
# Call the parent __init__() method
pygame.sprite.Sprite.__init__(self)
# Our endzone sprite will be a 1 pixel wide black line.
self.image = pygame.Surface((screen.get_width(),1))
self.image = self.image.convert()
self.image.fill((0, 0, 0))
# Set the rect attributes for the endzone
self.rect = self.image.get_rect()
self.rect.left = xPosition
self.rect.bottom = screen.get_height()
class Lives(pygame.sprite.Sprite):
'''This class defines a label sprite to display the score.'''
def __init__(self):
'''This initializer loads the system font "Arial", and
sets the starting score to 0:0'''
# Call the parent __init__() method
pygame.sprite.Sprite.__init__(self)
# Load our custom font, and initialize the starting score.
self.font=pygame.font.Font("walt.ttf", 30)
#self.font = pygame.font.SysFont("Arial", 30)
self.player1Lives = 3
def player1Lose(self):
'''This method minuses one to the lives for player 1'''
self.player1Lives -= 1
def loser(self):
'''Player 1 loses when 3 lives are lost.
This method returns 0 if there is no loser yet, 1 if player 1 has lost 3 lives
it returns 1.'''
return self.player1Lives == 0
def update(self):
'''This method will be called automatically to display
the current amount of lives at the top of the game window.'''
message = "Lives: " + str(self.player1Lives)
self.image = self.font.render(message, 1, (0, 0, 0))
self.rect = self.image.get_rect()
self.rect.center = (500, 15)
class Brick(pygame.sprite.Sprite):
'''A simple Sprite subclass to represent static Brick sprites.'''
def __init__(self, screen,row,location):
# Call the parent __init__() method
pygame.sprite.Sprite.__init__(self)
# Set the image and rect attributes for the bricks
self.image = pygame.Surface((35, 20))
self.image = self.image.convert()
self.rect = self.image.get_rect()
self.points=row
self.rect.center=location
if self.points==1:
self.image.fill((0,0,255))
elif self.points==2:
self.image.fill((0,255,0))
elif self.points==3:
self.image.fill((253,145,3))
elif self.points==4:
self.image.fill((253,253,3))
elif self.points==5:
self.image.fill((255,0,0))
elif self.points==6:
self.image.fill((128,3,253))
def getScore(self):
'''This method returns the value of the brick object .'''
return self.points
class ScoreKeeper(pygame.sprite.Sprite):
'''This class defines a label sprite to display the score.'''
def __init__(self):
'''This initializer loads the system font "Arial", and
sets the starting score to 0:0'''
# Call the parent __init__() method
pygame.sprite.Sprite.__init__(self)
# Load our custom font, and initialize the starting score.
self.font=pygame.font.Font("walt.ttf", 30)
#self.font = pygame.font.SysFont("Arial", 30)
self.player1Score = 0
def player1(self,points):
'''This method adds one to the score for player 1'''
self.score=points
if self.score==1:
self.player1Score += 1
if self.score==2:
self.player1Score += 2
if self.score==3:
self.player1Score += 3
if self.score==4:
self.player1Score += 4
if self.score==5:
self.player1Score += 5
if self.score==6:
self.player1Score += 6
def winner(self):
'''There is a winner when one player reaches 378 points.
This method returns 0 if there is no winner yet.'''
if self.player1Score == 378:
return 1
def update(self):
'''This method will be called automatically to display
the current score at the top of the game window.'''
message = "Player 1: " + str(self.player1Score)
self.image = self.font.render(message, 1, (0, 0, 0))
self.rect = self.image.get_rect()
self.rect.center = (200, 15)