-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.py
245 lines (202 loc) · 8.44 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
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
237
238
239
240
241
242
243
244
245
import os
import pygame
from pygame.locals import *
from piece import Piece
from chess import Chess
from utils import Utils
class Game:
def __init__(self):
# screen dimensions
screen_width = 640
screen_height = 750
# flag to know if game menu has been showed
self.menu_showed = False
# flag to set game loop
self.running = True
# base folder for program resources
self.resources = "res"
# initialize game window
pygame.display.init()
# initialize font for text
pygame.font.init()
# create game window
self.screen = pygame.display.set_mode([screen_width, screen_height])
# title of window
window_title = "Chess"
# set window caption
pygame.display.set_caption(window_title)
# get location of game icon
icon_src = os.path.join(self.resources, "chess_icon.png")
# load game icon
icon = pygame.image.load(icon_src)
# set game icon
pygame.display.set_icon(icon)
# update display
pygame.display.flip()
# set game clock
self.clock = pygame.time.Clock()
def start_game(self):
"""Function containing main game loop"""
# chess board offset
self.board_offset_x = 0
self.board_offset_y = 50
self.board_dimensions = (self.board_offset_x, self.board_offset_y)
# get location of chess board image
board_src = os.path.join(self.resources, "board.png")
# load the chess board image
self.board_img = pygame.image.load(board_src).convert()
# get the width of a chess board square
square_length = self.board_img.get_rect().width // 8
# initialize list that stores all places to put chess pieces on the board
self.board_locations = []
# calculate coordinates of the each square on the board
for x in range(0, 8):
self.board_locations.append([])
for y in range(0, 8):
self.board_locations[x].append([self.board_offset_x+(x*square_length),
self.board_offset_y+(y*square_length)])
# get location of image containing the chess pieces
pieces_src = os.path.join(self.resources, "pieces.png")
# create class object that handles the gameplay logic
self.chess = Chess(self.screen, pieces_src, self.board_locations, square_length)
# game loop
while self.running:
self.clock.tick(5)
# poll events
for event in pygame.event.get():
# get keys pressed
key_pressed = pygame.key.get_pressed()
# check if the game has been closed by the user
if event.type == pygame.QUIT or key_pressed[K_ESCAPE]:
# set flag to break out of the game loop
self.running = False
elif key_pressed[K_SPACE]:
self.chess.reset()
winner = self.chess.winner
if self.menu_showed == False:
self.menu()
elif len(winner) > 0:
self.declare_winner(winner)
else:
self.game()
# for testing mechanics of the game
#self.game()
#self.declare_winner(winner)
# update display
pygame.display.flip()
# update events
pygame.event.pump()
# call method to stop pygame
pygame.quit()
def menu(self):
"""method to show game menu"""
# background color
bg_color = (255, 255, 255)
# set background color
self.screen.fill(bg_color)
# black color
black_color = (0, 0, 0)
# coordinates for "Play" button
start_btn = pygame.Rect(270, 300, 100, 50)
# show play button
pygame.draw.rect(self.screen, black_color, start_btn)
# white color
white_color = (255, 255, 255)
# create fonts for texts
big_font = pygame.font.SysFont("comicsansms", 50)
small_font = pygame.font.SysFont("comicsansms", 20)
# create text to be shown on the game menu
welcome_text = big_font.render("Chess", False, black_color)
created_by = small_font.render("Created by Shalini", True, black_color)
start_btn_label = small_font.render("Play", True, white_color)
# show welcome text
self.screen.blit(welcome_text,
((self.screen.get_width() - welcome_text.get_width()) // 2,
150))
# show credit text
self.screen.blit(created_by,
((self.screen.get_width() - created_by.get_width()) // 2,
self.screen.get_height() - created_by.get_height() - 100))
# show text on the Play button
self.screen.blit(start_btn_label,
((start_btn.x + (start_btn.width - start_btn_label.get_width()) // 2,
start_btn.y + (start_btn.height - start_btn_label.get_height()) // 2)))
# get pressed keys
key_pressed = pygame.key.get_pressed()
#
util = Utils()
# check if left mouse button was clicked
if util.left_click_event():
# call function to get mouse event
mouse_coords = util.get_mouse_event()
# check if "Play" button was clicked
if start_btn.collidepoint(mouse_coords[0], mouse_coords[1]):
# change button behavior as it is hovered
pygame.draw.rect(self.screen, white_color, start_btn, 3)
# change menu flag
self.menu_showed = True
# check if enter or return key was pressed
elif key_pressed[K_RETURN]:
self.menu_showed = True
def game(self):
# background color
color = (0,0,0)
# set backgound color
self.screen.fill(color)
# show the chess board
self.screen.blit(self.board_img, self.board_dimensions)
# call self.chess. something
self.chess.play_turn()
# draw pieces on the chess board
self.chess.draw_pieces()
def declare_winner(self, winner):
# background color
bg_color = (255, 255, 255)
# set background color
self.screen.fill(bg_color)
# black color
black_color = (0, 0, 0)
# coordinates for play again button
reset_btn = pygame.Rect(250, 300, 140, 50)
# show reset button
pygame.draw.rect(self.screen, black_color, reset_btn)
# white color
white_color = (255, 255, 255)
# create fonts for texts
big_font = pygame.font.SysFont("comicsansms", 50)
small_font = pygame.font.SysFont("comicsansms", 20)
# text to show winner
text = winner + " wins!"
winner_text = big_font.render(text, False, black_color)
# create text to be shown on the reset button
reset_label = "Play Again"
reset_btn_label = small_font.render(reset_label, True, white_color)
# show winner text
self.screen.blit(winner_text,
((self.screen.get_width() - winner_text.get_width()) // 2,
150))
# show text on the reset button
self.screen.blit(reset_btn_label,
((reset_btn.x + (reset_btn.width - reset_btn_label.get_width()) // 2,
reset_btn.y + (reset_btn.height - reset_btn_label.get_height()) // 2)))
# get pressed keys
key_pressed = pygame.key.get_pressed()
#
util = Utils()
# check if left mouse button was clicked
if util.left_click_event():
# call function to get mouse event
mouse_coords = util.get_mouse_event()
# check if reset button was clicked
if reset_btn.collidepoint(mouse_coords[0], mouse_coords[1]):
# change button behavior as it is hovered
pygame.draw.rect(self.screen, white_color, reset_btn, 3)
# change menu flag
self.menu_showed = False
# check if enter or return key was pressed
elif key_pressed[K_RETURN]:
self.menu_showed = False
# reset game
self.chess.reset()
# clear winner
self.chess.winner = ""