In the Swap function, I am checking under some index a button class instance, which I checked using print statements, but for some reason it still gives me an error saying that it has no such attribute check click. Any tips on formatting are also welcome, I am just a beginner. I am using three different arrays to hold various instances, values and coordinates for each corresponding array position. I am trying to make a sort of match 3 game. Thanks for any help
JavaScript
x
157
157
1
from os import access
2
import pygame,sys
3
from random import randrange
4
import numpy
5
#Constants
6
Columns =5
7
Rows = 5
8
X,Y = 320,0
9
10
class Button:
11
def __init__(self,x,y,image,scale):
12
self.x = x
13
self.y=y
14
self.image = pygame.transform.scale(image,(scale,scale))
15
self.scale = scale
16
self.rect = self.image.get_rect(topleft=(x,y))
17
self.clicked = False
18
self.Action = False
19
20
21
def Draw(self):
22
Win.blit(self.image,(self.x,self.y))
23
24
def CheckClick(self):
25
isClicked = False
26
mousepos= pygame.mouse.get_pos()
27
if self.rect.collidepoint(mousepos):
28
if pygame.mouse.get_pressed()[0] ==1 and self.clicked == False:
29
self.clicked = True
30
self.Action = True
31
if pygame.mouse.get_pressed()[0] ==0:
32
self.clicked = False
33
return self.Action
34
35
36
37
#Win
38
WinWidth, WinHeight = 1280,800
39
Win = pygame.display.set_mode((WinWidth,WinHeight))
40
41
#IMAGES
42
test_img = pygame.image.load("test.png")
43
red_img = pygame.image.load("Red.png")
44
green_img = pygame.image.load("green.png")
45
blue_img = pygame.image.load("blue.png")
46
47
#Board
48
Board = [[ randrange(0,3) for column in range(Columns)] for row in range(Rows) ]
49
50
BoardObjs = []
51
52
BoardXYs = [[None for column in range(Columns)]for row in range(Rows)]
53
54
#Fill BoardXYS
55
for k in range(len(Board)):
56
for j in range(len(Board[1])):
57
BoardXYs[k][j] = [X,Y]
58
X += 160
59
X = 320
60
Y += 160
61
62
for r in range(len(Board)):
63
for t in range(len(Board[1])):
64
if Board[r][t] == 0:
65
BoardObjs.append(Button(BoardXYs[r][t][0],BoardXYs[r][t][1],red_img,100))
66
if Board[r][t] == 1:
67
BoardObjs.append(Button(BoardXYs[r][t][0],BoardXYs[r][t][1],green_img,100))
68
if Board[r][t] == 2:
69
BoardObjs.append(Button(BoardXYs[r][t][0],BoardXYs[r][t][1],blue_img,100))
70
71
72
73
BoardObjs = [[BoardObjs[0],BoardObjs[1],BoardObjs[2],BoardObjs[3],BoardObjs[4]],
74
[BoardObjs[5],BoardObjs[6],BoardObjs[7],BoardObjs[8],BoardObjs[9]],
75
[BoardObjs[10],BoardObjs[11],BoardObjs[12],BoardObjs[13],BoardObjs[14]],
76
[BoardObjs[15],BoardObjs[16],BoardObjs[17],BoardObjs[18],BoardObjs[19]],
77
[BoardObjs[20],BoardObjs[21],BoardObjs[22],BoardObjs[23],BoardObjs[24]]]
78
79
print(len(Board))
80
print(len(BoardObjs))
81
82
83
#Images
84
img_0 = pygame.Rect(0,0,64,64)
85
86
TouchingReds = 0
87
def CheckMatches(Board):
88
VerticalMatch = False
89
HorizontalMatch = False
90
for m in range(len(Board)):
91
for n in range(len(Board[1])-2):
92
if Board[m][n]==Board[m][n+1]==Board[m][n+2]:
93
HorizontalMatch = True
94
Board[m][n] = None
95
Board[m][n+1] = None
96
Board[m][n+2] = None
97
BoardObjs[m][n] = None
98
BoardObjs[m][n+1] = None
99
BoardObjs[m][n+2] = None
100
for o in range(len(Board[1])-2):
101
for u in range(len(Board)):
102
if Board[o][u]==Board[o+1][u]==Board[o+2][u]:
103
VerticalMatch = True
104
Board[o][u] = None
105
Board[o+1][u] = None
106
Board[o+2][u] = None
107
BoardObjs[o][u] = None
108
BoardObjs[o+1][u] = None
109
BoardObjs[o+2][u] = None
110
111
112
def Draw(Board,BoardXYs):
113
DoAppend = True
114
Win.fill((255,255,255))
115
for y in range(len(BoardObjs)):
116
for u in range(len(BoardObjs[1])):
117
if BoardObjs[y][u] != None:
118
BoardObjs[y][u].Draw()
119
pygame.display.update()
120
121
def Swap():
122
FirstClick = False
123
for i in range(len(BoardObjs)):
124
for t in range(len(BoardObjs[1])):
125
if BoardObjs[i][t] != None:
126
if BoardObjs[i][t].CheckClick and FirstClick == True:
127
BoardObjs[num][num2]=BoardObjs[i][t]
128
BoardObjs[i][t]=Buffer
129
FirstClick = False
130
print(BoardObjs[i][t])
131
if BoardObjs[i][t] !=None:
132
if BoardObjs[i][t].CheckClick:
133
Buffer = BoardObjs[i]
134
num =i
135
num2 = t
136
FirstClick = True
137
#ZEROS - RED
138
#ONES - GREEN
139
#TWOS - BLUE
140
141
clock = pygame.time.Clock()
142
def GameLoop():
143
run = True
144
while run:
145
clock.tick(60)
146
for event in pygame.event.get():
147
if event.type == pygame.QUIT:
148
run = False
149
CheckMatches(Board)
150
Draw(Board,BoardXYs)
151
Swap()
152
pygame.quit()
153
sys.exit()
154
155
if __name__ == "__main__":
156
GameLoop()
157
Advertisement
Answer
The mistake is in the Swap()
function. Buffer
is assigned to an item in the grid (BoardObjs[i][t]=Buffer
). So Buffer
needs to be a Button
object instead of a row (list of objects):
Buffer = BoardObjs[i]
JavaScript
1
2
1
Buffer = BoardObjs[i][t]
2
The initialization of the board can be simplified:
JavaScript
1
10
10
1
#Board
2
Board = [[randrange(0,3) for column in range(Columns)] for row in range(Rows) ]
3
BoardXYs = [[(j*160+320, k*160) for j in range(Columns)]for k in range(Rows)]
4
BoardObjs = []
5
for r in range(len(Board)):
6
BoardObjs.append([])
7
for t in range(len(Board[1])):
8
image = [red_img, green_img, blue_img][board[r][t]]
9
BoardObjs[-1].append(Button(*BoardXYs[r][t], image, 100))
10