-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.py
193 lines (153 loc) · 6.78 KB
/
game.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
import pygame as pg
from button import Button
from gameplay import Gameplay
pg.init()
pg.font.init()
class GameIntro():
def __init__(self):
'''
Init function for the GameIntro class that establishes many of the used variables in the program.
'''
# Clock object and FPS
self.clock = pg.time.Clock()
self.FPS = 15
# Colors
self.WHITE = (255,255,255)
self.BLACK = (0,0,0)
self.GREEN = (0, 255, 0)
self.RED = (255, 0, 0)
self.BLUE = (0, 0, 210)
# Display parameters, Border parameters
self.WIDTH = self.HEIGHT = 600
self.INTRO_BORDER_SIZE = 20
self.BORDER_SIZE = 40
# Button parameters
self.PLAY_COORDX = self.WIDTH // 5
self.QUIT_COORDX = (3 * self.WIDTH) // 5
self.PLAY_COORDY = self.QUIT_COORDY = (3 * self.HEIGHT) // 4
self.PLAY_WIDTH = self.QUIT_WIDTH = self.WIDTH // 5
self.PLAY_HEIGHT = self.QUIT_HEIGHT = self.WIDTH // 10
self.MESSAGES = {1: 'Play!', 2: 'Quit!'}
# Configure display
self.display = pg.display.set_mode([self.WIDTH, self.HEIGHT])
self.display.fill(self.WHITE)
# Font objects
self.smallerFont = pg.font.SysFont('freesansbold.ttf', 30)
self.largeFont = pg.font.SysFont('freesansbold.ttf', 45)
# Border coordinates for rectangle objects
self.BORDER_COORDINATES = {1: ((0,0), (self.WIDTH, self.BORDER_SIZE)),
2: ((0, self.BORDER_SIZE), (self.BORDER_SIZE,
self.HEIGHT)),
3: ((self.WIDTH - self.BORDER_SIZE, self.BORDER_SIZE),
(self.BORDER_SIZE, self.HEIGHT)),
4: ((0, self.WIDTH - self.BORDER_SIZE),
(self.WIDTH, self.BORDER_SIZE))}
def text(self, surface, sizeFont, color, message, centerX, centerY):
"""
Parameters
----------
surface : pygame.Surface
The Screen on which the game is played.
sizeFont : pygame.font
The font object that is displayed.
color : tuple
Global tuple object of R,G,B values.
message : String
Specific string to be displayed for the user.
centerX : Integer
x-coordinate of center of pygame.rect object.
centerY : Integer
y-coordinate of center of pygame.rect object.
Returns
-------
Nothing returned; text object appears on pygame.Surface object.
"""
text = sizeFont.render(message, True, color)
rect = text.get_rect()
rect.center = (centerX, centerY)
surface.blit(text, rect)
def button(self, surface, bgColor, textColor, bgnX, bgnY,
width, height, message, sizeFont):
'''
Parameters
----------
surface : pygame.Surface
The Screen on which the game is played.
bgColor : tuple
Color of the pygame.rect object.
textColor : tuple
Color of the pygame.font object.
bgnX : Integer
Top-left x-coordinate of the pygame.rect object.
bgnY : Integer
Top-left y-coordinate of the pygame.rect object.
width : Integer
Integer width (x-change in corners) of pygame.rect object.
height : Integer
Integer height (y-change in corners) of pygame.rect object.
message : String
Specific string to be displayed for the user.
sizeFont : pygame.font
The font object that is displayed.
Returns
-------
Nothing returned; however, rectangle and text object are drawn
on the pygame.Surface object.
'''
pg.draw.rect(surface, bgColor, ((bgnX, bgnY), (width, height)))
self.text(surface, sizeFont, textColor, message,
(bgnX + (width // 2)), (bgnY + (height // 2)))
def menu(self):
'''
Menu function for the intro screen. Contains button functionality from Button class.
'''
pg.draw.rect(self.display, self.BLUE,
((0,0), (self.WIDTH, self.INTRO_BORDER_SIZE)))
pg.draw.rect(self.display, self.BLUE,
((0,0), (self.INTRO_BORDER_SIZE, self.WIDTH)))
pg.draw.rect(self.display, self.BLUE,
((0, self.WIDTH - self.INTRO_BORDER_SIZE),
(self.WIDTH, self.INTRO_BORDER_SIZE)))
pg.draw.rect(self.display, self.BLUE,
((self.WIDTH - self.INTRO_BORDER_SIZE, 0),
(self.INTRO_BORDER_SIZE, self.WIDTH)))
self.text(self.display, self.largeFont, self.GREEN, 'Welcome to Snake!',
self.WIDTH // 2, self.HEIGHT // 3)
self.text(self.display, self.smallerFont, self.RED,
'Click the below buttons to Play or Quit the game!',
self.WIDTH // 2, self.HEIGHT // 2)
play = Button(self.display, self.GREEN, self.WHITE,
self.PLAY_COORDX, self.PLAY_COORDY, self.PLAY_WIDTH,
self.PLAY_HEIGHT, self.MESSAGES[1], self.largeFont)
end = Button(self.display, self.RED, self.WHITE,
self.QUIT_COORDX, self.QUIT_COORDY, self.QUIT_WIDTH,
self.QUIT_HEIGHT, self.MESSAGES[2], self.largeFont)
pg.display.flip()
return play, end
def main():
'''
Central function for the program.
'''
apple = pg.image.load('apple.png')
pg.display.set_icon(apple)
pg.display.set_caption('Snake')
intro = GameIntro()
game = Gameplay(intro.display, intro.WHITE, intro.GREEN, intro.WIDTH, intro.HEIGHT)
play, end = intro.menu()
played = False
while True:
event = pg.event.poll()
if play.checkUpdates(intro.MESSAGES):
game.setupDisplay(intro.display, intro.WHITE,
[game.sky, game.side, game.side2, game.ground],
intro.BORDER_COORDINATES)
played, length = game.gameLoop(intro.display, intro.WIDTH, intro.HEIGHT)
elif end.checkUpdates(intro.MESSAGES):
pg.quit()
if event.type == pg.QUIT:
pg.quit()
if played:
main()
intro.clock.tick(intro.FPS)
if __name__ == '__main__':
main()