-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpvp7x8.py
277 lines (215 loc) · 9.47 KB
/
pvp7x8.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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
import random, copy, pygame
from pygame.locals import *
level = 2
human = 1
human2 = 2
element_size = 50 # size of each element of board
clock_speed = 30 # fps for board updation
screen_width = 640 # width of game window
screen_height = 480 # height of game window
board_width = 8 # number of columns
board_height = 7 # number of rows
x_margin = ((screen_width - board_width * element_size) // 2)
y_margin = ((screen_height - board_height * element_size) // 2)
white = (255, 255, 255)
bgcolor = (150,200,150)
pygame.init()
pygame.display.set_caption('Connect 4')
clock = pygame.time.Clock()
game_display = pygame.display.set_mode((screen_width, screen_height))
redtoken_img = pygame.image.load('4row_red.png')
redtoken_img = pygame.transform.scale(redtoken_img, (element_size , element_size ))
blacktoken_img = pygame.image.load('4row_black.png')
blacktoken_img = pygame.transform.scale(blacktoken_img, (element_size, element_size))
element_img = pygame.image.load('4row_board.png')
element_img = pygame.transform.scale(element_img, (element_size, element_size))
arrow_img = pygame.image.load('4row_arrow.png')
human_img = pygame.image.load('image 1.png')
human2_img = pygame.image.load('image 2.png')
tie_img = pygame.image.load('4row_tie.png')
redtoken_rect = pygame.Rect(element_size // 2, screen_height - (3 * element_size // 2), element_size, element_size)
blacktoken_rect = pygame.Rect(screen_width - int(3 * element_size / 2), screen_height - int(3 * element_size / 2), element_size, element_size)
win_rect = human_img.get_rect()
win_rect.center = ((screen_width // 2), (screen_height // 2))
help_rect = arrow_img.get_rect()
help_rect.left = redtoken_rect.right + 10
help_rect.centery = redtoken_rect.centery
def main():
while True:
game_loop()
def is_valid(board, col):# checks if move is valid
if col < 0 or col >= (board_width) or board[col][0] != None:
return False
return True
def is_full(board): # checks if board is full
for x in range(board_width):
if board[x][0] == None:
return False
return True
def make_board():
board = []
for x in range(board_width):
board.append([None] * board_height)
return board
def check_move(board, player, col):
lowest = lowest_space(board, col)
if lowest != -1:
board[col][lowest] = player
def draw_board(board, extra_token=None):
game_display.fill(bgcolor)
#
token_rect = pygame.Rect(0, 0, element_size, element_size)
for x in range(board_width):
for y in range(board_height):
token_rect.topleft = (x_margin + (x * element_size), y_margin + (y * element_size))
if board[x][y] == human:
game_display.blit(redtoken_img, token_rect)
elif board[x][y] == human2:
game_display.blit(blacktoken_img, token_rect)
# draw the token when in motion
if extra_token != None:
if extra_token['turn'] == human:
game_display.blit(redtoken_img, (extra_token['x'], extra_token['y'], element_size, element_size))
elif extra_token['turn'] == human2:
game_display.blit(blacktoken_img, (extra_token['x'], extra_token['y'], element_size, element_size))
# draw board
for x in range(board_width):
for y in range(board_height):
token_rect.topleft = (x_margin + (x * element_size), y_margin + (y * element_size))
game_display.blit(element_img, token_rect)
game_display.blit(redtoken_img, redtoken_rect) # draw red token on left
game_display.blit(blacktoken_img, blacktoken_rect) # draw black token on right
def human_move(board, is_first_move):
not_dragging = True
pos_x, pos_y = None, None
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
exit()
elif event.type == MOUSEBUTTONDOWN and not_dragging and redtoken_rect.collidepoint(event.pos):# start dragging
not_dragging = False
pos_x, pos_y = event.pos
elif event.type == MOUSEMOTION and not not_dragging:# update position while dragging
pos_x, pos_y = event.pos
elif event.type == MOUSEBUTTONUP and not not_dragging:# stop dragging
if pos_y < y_margin and pos_x > x_margin and pos_x < screen_width - x_margin:# drop the token
col = int((pos_x - x_margin) / element_size)
if is_valid(board, col):
move_token(board, col, human)
board[col][lowest_space(board, col)] = human
draw_board(board)
pygame.display.update()
return
pos_x, pos_y = None, None
not_dragging = True
if pos_x != None and pos_y != None:
draw_board(board, {'x':pos_x - int(element_size / 2), 'y':pos_y - (element_size // 2), 'turn':human})
else:
draw_board(board)
if is_first_move:
game_display.blit(arrow_img, help_rect)# show help for first move
pygame.display.update()
clock.tick()
def human2_move(board):
not_dragging = True
pos_x, pos_y = None, None
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
exit()
elif event.type == MOUSEBUTTONDOWN and not_dragging and blacktoken_rect.collidepoint(event.pos):# start dragging
not_dragging = False
pos_x, pos_y = event.pos
elif event.type == MOUSEMOTION and not not_dragging:# update position while dragging
pos_x, pos_y = event.pos
elif event.type == MOUSEBUTTONUP and not not_dragging:# stop dragging
if pos_y < y_margin and pos_x > x_margin and pos_x < screen_width - x_margin:# drop the token
col = int((pos_x - x_margin) / element_size)
if is_valid(board, col):
move_token(board, col, human2)
board[col][lowest_space(board, col)] = human2
draw_board(board)
pygame.display.update()
return
pos_x, pos_y = None, None
not_dragging = True
if pos_x != None and pos_y != None:
draw_board(board, {'x':pos_x - int(element_size / 2), 'y':pos_y - (element_size // 2), 'turn':human2})
else:
draw_board(board)
pygame.display.update()
clock.tick()
def move_token(board, col, player):
pos_x = x_margin + col * element_size
pos_y = y_margin - element_size
speed = 5
lowestNoneSpace = lowest_space(board, col)
while True:
pos_y += speed
if ((pos_y - y_margin) / element_size) >= lowestNoneSpace:
return
draw_board(board, {'x':pos_x, 'y':pos_y, 'turn':player})
pygame.display.update()
clock.tick()
def lowest_space(board, col):# gives the lowest empty row number of specified column
for y in range(board_height-1, -1, -1):
if board[col][y] == None:
return y
return -1
def game_loop():
is_help = True
turn = human
board = make_board()# make an empty board
while True:
if turn == human:
human_move(board, is_help)
if is_help:
# no help needed after first move has been played
is_help = False
if is_win(board, human):
win_img = human_img
break
turn = human2
else:
human2_move(board)
if is_win(board, human2):
win_img = human2_img
break
turn = human
if is_full(board):# tie situation
win_img = tie_img
break
while True:
draw_board(board)# display the board till user clicks on screen or exits
game_display.blit(win_img, win_rect)
pygame.display.update()
clock.tick()
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
exit()
elif event.type == MOUSEBUTTONUP:
return
is_help = False
def is_win(board, player):# check if any current board situation leads to winning for any player
for x in range(board_width - 3):
for y in range(board_height):
if board[x][y] == board[x+1][y] == board[x+2][y] == board[x+3][y] == player:
return True
for x in range(board_width):
for y in range(board_height - 3):
if board[x][y] == board[x][y+1] == board[x][y+2] == board[x][y+3] == player:
return True
for x in range(board_width - 3):
for y in range(3, board_height):
if board[x][y] == board[x+1][y-1] == board[x+2][y-2] == board[x+3][y-3] == player:
return True
for x in range(board_width - 3):
for y in range(board_height - 3):
if board[x][y] == board[x+1][y+1] == board[x+2][y+2] == board[x+3][y+3] == player:
return True
return False
if __name__ == '__main__':
main()