forked from feberhardt/Project-Digital
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.py
More file actions
205 lines (174 loc) · 6.13 KB
/
game.py
File metadata and controls
205 lines (174 loc) · 6.13 KB
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
# this is the file for our game
import pygame
import sys
import numpy
pygame.init()
background_color = (255, 255, 255)
WINDOW_DIMENSIONS = (640, 480)
width, height = 640, 480
# Bunch of variables
screen = pygame.display.set_mode((width, height))
screen.fill(background_color)
pygame.display.set_caption('Connect 4')
class Board:
def __init__(self, matrix, piecesize=100):
width, height = 5, 4
# remove dead code, or explain what it is and under what circumstances
# it might come back
# the things used for the working code
self.width = width
self.height = height
self.matrix = matrix
def draw(self, screen):
# Drawing the grid
for x in range(0, self.width * 100, 100):
for y in range(0, self.height * 100, 100):
rectangle = (x, y, 100, 100)
#print("Drawing x, y : ", x, y)
pygame.draw.rect(screen, (0, 0, 255), rectangle, 5)
# drawing the chips
for x in range(self.width):
for y in range(self.height):
pos = (x * 100 + 50, y * 100 + 50)
if self.matrix[y, x] == 1:
color = (255, 255, 0) # yellow
elif self.matrix[y, x] == 2:
color = (255, 0, 0) # red
else:
continue
# good use of color variable to avoid having
# two separate calls to pygame.draw.circle
pygame.draw.circle(screen, color, pos, 40)
def createboard(rows, columns):
# Identical to the method in functions.py.
# See the commentary there.
row_size = ''
for rows in range(rows):
if rows == 0:
row_size = row_size + '0'
else:
row_size = row_size + ',0'
fullmatrix = ''
for cols in range(columns):
if cols == 0:
fullmatrix = fullmatrix + row_size
else:
fullmatrix = fullmatrix + '; ' + row_size
return fullmatrix
def winning(matrix, connect):
# Also appears identical to the function in functions.py.
# See the comments there.
rows = matrix.shape[0]
columns = matrix.shape[1]
# horizontal
for x in range(rows):
for y in range(columns - connect + 1):
# add another == for Connect 4
if matrix[x, y] == matrix[x, y + 1] == matrix[x, y + 2] and matrix[x, y] != 0:
return True, matrix[x, y]
# vertical
for y in range(columns):
for x in range(rows - connect + 1):
# add another == for Connect 4
if matrix[x, y] == matrix[x + 1, y] == matrix[x + 2, y] and matrix[x, y] != 0:
return True, matrix[x, y]
# determines which side is longer for diagonal search
if rows > columns:
longer = rows
else:
longer = columns
# diagonal
arrayM = numpy.asarray(matrix)
for x in range(longer - 1):
a = numpy.diagonal(arrayM, x - (rows - connect + 1))
for i in range(len(a) - connect + 1):
if a[i] == a[i + +1] == a[i + 2] and a[i] != 0:
return True, a[i]
# reverse diagonal
arrayM = numpy.asarray(numpy.fliplr(matrix))
for x in range(longer - 1):
a = numpy.diagonal(arrayM, x - (rows - connect + 1))
for i in range(len(a) - connect + 1):
if a[i] == a[i + +1] == a[i + 2] and a[i] != 0:
return True, a[i]
if numpy.count_nonzero(arrayM) == rows * columns:
return True, 0
# if not won
return False, 0
pygame.init()
board = numpy.matrix('0,0,0,0,0; 0,0,0,0,0; 0,0,0,0,0; 0,0,0,0,0')
print(board)
# Initialise images (save images to folder)
#background = pygame.image.load('connect4Board.png').convert_alpha()
#redPiece = pygame.image.load('redPiece.png').convert_alpha()
#yellowPiece = pygame.image.load('yellowPiece.png').convert_alpha()
player = 0
Time = 0
endscreen = False
win = winning(board, 3)
def look_through_rows(board, column, player):
count = 3
count2 = 1
while count >= 0 and count2 == 1:
if board[count, column] == 0:
board[count, column] = player
count2 = count2 - 1
else:
count = count - 1
return board
# Keyboard input for player
running = True
while running:
screen.fill((255, 255, 255)) # set up background
boardC = Board(matrix=board)
boardC.draw(screen)
pygame.display.update()
if pygame.time.get_ticks() > (Time + 10):
Time = pygame.time.get_ticks()
win = winning(board, 3)
if win[0] == True:
running = False
endscreen = True
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if player == 0:
player = 1
elif player == 1:
player = 2
elif player == 2:
player = 1
if event.key == pygame.K_1:
look_through_rows(board, 0, player)
print(board)
if event.key == pygame.K_2:
look_through_rows(board, 1, player)
print(board)
if event.key == pygame.K_3:
look_through_rows(board, 2, player)
print(board)
if event.key == pygame.K_4:
look_through_rows(board, 3, player)
print(board)
if event.key == pygame.K_5:
look_through_rows(board, 4, player)
print(board)
boardC.draw(screen)
pygame.display.update()
while endscreen:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_q:
pygame.quit()
quit()
basicfont = pygame.font.SysFont(None, 20)
text = basicfont.render('Congrats! Player %.2d' % (
win[1]) + ' Has Won', True, (0, 0, 0), (255, 255, 255))
textrect = text.get_rect()
textrect.centerx = screen.get_rect().centerx
textrect.centery = screen.get_rect().centery
screen.blit(text, textrect)
pygame.display.update()
pygame.quit()