-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmySprites.py
191 lines (152 loc) · 6.86 KB
/
mySprites.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
'''Date:May 28, 2019
Author:Andrew Lin
Description: A Snake sprite group that gets called in the snake game.
'''
import pygame
import random
class Ball(pygame.sprite.Sprite):
'''A simple Sprite subclass to represent static ball sprites.'''
def __init__(self, screen):
'''This initializer takes a screen surface, and creates the ball sprites'''
# Call the parent __init__() method
pygame.sprite.Sprite.__init__(self)
# Set the image and rect attributes for the ball
self.image = pygame.image.load("Ball3.png").convert()
self.rect = self.image.get_rect()
self.screen=screen
self.image.set_colorkey((255,255,255))
def setPosition(self,x,y):
'''Sets the position of each ball'''
self.rect.centerx = x
self.rect.centery = y
class Body(pygame.sprite.Sprite):
'''A simple Sprite Subclass to represent a the snakes body Sprite'''
def __init__(self,screen):
'''This initializer takes a screen surface, and creates the body of the snake.'''
# Call the parent __init__() method
pygame.sprite.Sprite.__init__(self)
# Set the image and rect attributes for the bricks
self.image = pygame.Surface((20,20))
self.image.fill((162, 82, 3))
self.image=self.image.convert()
self.rect = self.image.get_rect()
def set_position(self,x,y):
'''This method is called and sets the position of the body'''
self.rect.centerx = x
self.rect.centery = y
def get_centerx(self):
'''The method returns a integer and the exact x value of the area of
the snake body'''
return self.rect.centerx
def get_centery(self):
'''The method returns a integer and the exact y value of the area of
the snake body'''
return self.rect.centery
class Head(pygame.sprite.Sprite):
'''A simple Sprite Subclass to represent a the snakes head '''
def __init__(self, screen):
'''This initializer takes a screen surface, and creates a head sprite'''
# Call the parent __init__() method
pygame.sprite.Sprite.__init__(self)
# Set the image and rect attributes for the bricks
self.image = pygame.Surface((20,20))
self.image.fill((0, 0, 255))
self.image=self.image.convert()
self.rect = self.image.get_rect()
self.rect.centerx = screen.get_width()/2
self.rect.centery = screen.get_height()/2
self.dx=0
self.dy=0
self.screen=screen
def go_left(self):
'''This method changes the dx to allow the snake to move left'''
self.dx = -20
self.dy = 0
def go_right(self):
'''This method changes the dyx to allow the snake to move right'''
self.dx = 20
self.dy = 0
def go_up(self):
'''This method changes the dy to allow the snake to move up'''
self.dx = 0
self.dy = -20
def go_down(self):
'''This method changes the dy to allow the snake to move down'''
self.dx = 0
self.dy = 20
def reset(self):
'''This method resets the snake position to the middle after it dies'''
self.rect.centerx = self.screen.get_width()/2
self.rect.centery = self.screen.get_height()/2
def get_x(self):
'''The method returns a integer and the exact x value of the area of
the snake head'''
return self.rect.centerx
def get_y(self):
'''The method returns a integer and the exact y value of the area of
the snake head'''
return self.rect.centery
def update(self):
'''This method will continusely update the snake to allow it to move'''
self.rect.centerx += self.dx
self.rect.centery += self.dy
class EndZone(pygame.sprite.Sprite):
'''This class defines the sprite for our end zones'''
def __init__(self, screen, number):
'''This initializer takes a screen surface, and a number to create the border of the game.'''
# Call the parent __init__() method
pygame.sprite.Sprite.__init__(self)
self.image = pygame.Surface((screen.get_width(),10))
self.image = self.image.convert()
self.image.fill((0, 0, 0))
if number==0:
# Set the rect attributes for the endzone
self.rect = self.image.get_rect()
self.rect.left = 0
self.rect.bottom = screen.get_height()
elif number==1:
# Set the rect attributes for the endzone
self.rect = self.image.get_rect()
self.rect.left = number
self.rect.bottom = 50
elif number==2:
self.image = pygame.Surface((10,screen.get_height()))
# Set the rect attributes for the endzone
self.rect = self.image.get_rect()
self.rect.right = 640
self.rect.bottom = screen.get_height()
elif number==3:
self.image = pygame.Surface((10,screen.get_height()))
# Set the rect attributes for the endzone
self.rect = self.image.get_rect()
self.rect.right = 10
self.rect.bottom = screen.get_height()
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.player1Score = 0
self.highScore=0
def reset(self):
'''This method will reset the score and change the high score'''
if self.player1Score > self.highScore:
self.highScore=self.player1Score
self.player1Score = 0
def get_score(self):
'''Returns the score value'''
return self.player1Score
def player(self):
'''This method adds one to the score for player 1'''
self.player1Score += 1
def update(self):
'''This method will be called automatically to display
the current score at the top of the game window.'''
message = "Score: " + str(self.player1Score)+" High Score: " + str(self.highScore)
self.image = self.font.render(message, 1, (0, 0, 0))
self.rect = self.image.get_rect()
self.rect.center = (300, 20)