Python Forum
Tetris - AttributeError: 'list' object has no attribute 'y'
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Tetris - AttributeError: 'list' object has no attribute 'y'
#1
Hi, im making a Tetris game



and I got stucked

the error showing on screen is the following:

Error:
Traceback (most recent call last): File "E:\Users\Usuario1\Documents\abscorp\tetris.py", line 314, in <module> main_menu(win) File "E:\Users\Usuario1\Documents\abscorp\tetris.py", line 309, in main_menu main(win) File "E:\Users\Usuario1\Documents\abscorp\tetris.py", line 253, in main current_piece.y += 1 AttributeError: 'list' object has no attribute 'y'
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
import pygame
import random
 
pygame.font.init()
 
#GLOBAL VARS
 
s_width = 800
s_height = 700
play_width = 300
play_height = 600
block_size = 30
 
top_left_x = (s_width - play_width)
top_left_y = s_height - play_height
 
#SHAPE FORMATS
 
S= [['.....',
    '.....',
    '..00.',
    '.00..',
    '.....',
    '.....'],
   ['.....',
    '..0..',
    '..00.',
    '...0.',
    '.....',
    '.....']]
 
Z= [['.....',
     '.....'
     '.00..'
     '..00.'
     '.....'
     '.....']]
 
I = [['..0..',
      '..0..',
      '..0..',
      '..0..',
      '..0..',],
     ['.....',
      '0000.',
      '.....',
      '.....',
      '.....',]]
 
O = [['.....',
      '.....',
      '.00..',
      '.00..',
      '.....']]
 
J = [['.....',
      '.0...',
      '.000.',
      '.....',
      '.....'],
      ['.....',
       '..00.',
       '..0..'
       '..0..',
       '.....'],
     ['.....',
      '.....',
      '.000.',
      '...0.',
      '.....'],
      ['.....',
       '..0..',
       '..0..'
       '.00..',
       '.....']]
L = [['.....',
      '...0.',
      '.000.',
      '.....',
      '.....'],
     ['.....',
      '..0..',
      '..0..',
      '..00.',
      '.....'],
     ['.....',
      '.000.',
      '.0...',
      '.....',
      '.....'],
     ['.....',
      '.00..',
      '..0..',
      '..0..',
      '.....']]
T = [['.....',
      '..0..',
      '.000.',
      '.....',
      '.....'],
     ['.....',
      '..0..',
      '..00.',
      '..0..',
      '.....'],
     ['.....',
      '.....',
      '.000.',
      '..0..',
      '.....'],
     ['.....',
      '..0..',
      '.00..',
      '..0..',
      '.....']]
 
shapes = [S, Z, I, 0, J, L, T]
shape_colors = [(0, 255, 0), (255, 0, 0), (0, 255, 255), (255, 255, 0), (255, 165, 0), (0, 0, 255), (120, 0, 120)]
#index 0- 6 represent shape
 
class piece (object): #*
    def __init__(self, x, y, shape):
        self.x = x
        self.y = y
        self.shape = shape
        self.color = shape_colors[shapes.index[shape]]
        self.rotation = 0
    pass
 
def create_grid(locked_pos = []): #*
    grid = [[(0, 0, 0) for _ in range(10)] for _ in range(20)]
 
    for i in range(len(grid)):
        for j in range(len(grid[i])):
            if (j, i) in locked_pos:
                c =  locked_pos[(j, i)]
                grid[i][j] = c
    return grid
 
def convert_shape_format(shape):
    positions = []
    format = shape.shape[shape.rotation % len(shape.shape)]
 
    for i, line in enumerate (format):
        row = list(line)
        for j, column in  enumerate(row):
            if column == '0':
                positions.append((shape.x  + j, shape.y + i))
 
    for i, pos in enumerate(positions):
        positions[i] = (pos[0] - 2, pos[1] - 4)
 
    return positions
         
def valid_space(shape, grid):
    accepted_pos = [[(j , i) for j in range(10) if grid[i][j] == (0, 0, 0)] for i in range(20)]
    accepted_pos = [j  for sub  in accepted_pos for j in sub]
 
    formatted = convert_shape_format(shape)
 
    for pos in formatted:
        if pos  not in  accepted_pos:
            if  pos [l] > - 1:
                return False
    return True
 
def check_lost():
    pass
 
def get_shape():
    return random.choice(shapes)
     
def draw_text_middle():
    pass
 
def draw_grid(surface, grid):
 
    sx =  top_left_x
    sy = top_left_y
 
    for i in range(len(grid)):
        pygame.draw.line(surface, (120, 120, 120), (sx, sy,  + i*block_size), (sx+play_width, sy+ i*block_size))
        for j in range(len(grid[i])):
            pygame.draw.line(surface, (120, 120, 120), (sx + j*block_size, sy), (sx+ j*block_size, sy + play_height))
                 
 
     
def clear_rows():
 
    inc = 0
    for i in range(len(grid)-1, -1, -1):
        row = grid[i]
        if (0,0,0) not in row:
            inc += 1
            ind = 1
            for j in range (len(row)):
                try:
                    del locked[(j, i)]
                except:
                    continue
     
 
def draw_next_shape(shape, surface):
    font = pygame.font.SysFont('comicsans', 30)
    label = font.render('Next Shape',1, (255, 255, 255))
 
    sx = top_left_x + play_width + 50
    sy = top_left_y + play_height/2 - 100
    format = shape.shape[shape.rotation% len(shape.shape)]
 
    for i, line in enumerate(format):
        row = list(line)
        for j, column, in enumerate(row):
            if column == '0':
                pygame.draw.rect(surface, shape.color, (sx + j*10, sy + i*block_size, block_size), 0)
                 
    surface.blit(label, (sx + 10, sy - 10))
                  
def draw_window(surface, grid): #*
    surface.fill((0, 0, 0))
 
    pygame.font.init()
    font = pygame.font.SysFont('comicsans', 60)
    label = font.render('Tetris', 1, (255, 255, 255))
 
    surface.blit(label, (top_left_x + play_width / 2 - (label.get.width) / 2), 30)
    draw_grid(surface, grid)
    pygame.display.update()
 
     
 
def main(win): #*
 
    locked_positions = []
    grid = create_grid(locked_positions)
 
    change_piece = False
    run = True
    current_piece = get_shape()
    next_piece = get_shape()
    clock = pygame.time.Clock()
    fall_time = 0
    fall_speed = 0.27
 
 
    while run:
        grid = create_grid(locked_positions)
        fall_time += clock.get_rawtime()
        clock.tick()
 
        if fall_time/1000 > fall_speed:
            fall_time = 0
            current_piece.y += 1
            if not(valid_space(current_piece. grid)) and current_piece.y > 0:
                current_piece.y -= 1
                change_piece = True
             
             
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
              run = False
              
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT:
                    current_piece.x -= 1
                    if not[valid_space(current_piece.grid)]:
                        current_piece += 1
                if event.key == pygame.K_RIGHT:
                    current_piece.x += 1
                    if not[valid_space(current_piece.grid)]:
                        current_piece -= 1
                if event.key == pygame.K_DOWN:
                    current_piece.y += 1
                    if not[valid_space(current_piece.grid)]:
                        current_piece.y -= 1
                if event.key == pygame.K_UP:
                    current_piece.rotation += 1
                    if not[valid_space(current_piece.grid)]:
                        current_piece -= 1
 
              
    shape_pos = convert_shape_format(current_piece)
 
    for i in range(len(shape_pos)):
        x, y = shape_pos =[i]
        if y > -1:
            grid[y][x] = current_piece.color
 
    if change_piece:
        for pos in shape_pos:
            p = (pos[0], pos[1])
            locked_positions[p] = current_piece.color
        current_piece = next_piece
        next_piece = get_shape()
        cange_piece = False
 
    draw_next_shape(next_piece, win)
    draw_window(win.grid)
 
    if check_lost(locked_positions):
        run = False
         
pygame.display.quit()   
             
     
     
                  
def  main_menu(win): #*
    main(win)
 
 
win =  pygame.display.set_mode((s_width, s_height))
pygame.display.set_caption('Tetris')
main_menu(win)
                  
                  
                  
 
 
 
 
 
 
 
 
                  
             
     
             
     
 
 
 
 
 
     
      
If anyone can help i would really appreciate it, thanks a lot

see u!
Reply
#2
shapes:
1
2
3
4
5
6
7
8
9
shapes = [S, Z, I, 0, J, L, T]
...
def get_shape():
    return random.choice(shapes)
...
current_piece = get_shape()
...
# line 253
current_piece.y += 1
current_piece is a simple list, you cannot add 1 to a list!

you can however add 1 to current_piece[3]
1
2
3
current_piece[3] += 1
# Or if y == index == 3
current_piece[y] += 1
Reply
#3
Also you want change that zero to an O.
1
shapes = [S, Z, I, 0, J, L, T]
to
1
shapes = [S, Z, I, O, J, L, T]
99 percent of computer problems exists between chair and keyboard.
Reply
#4
But ... if you change the zero to an O, you will no longer be able to increment it.
Reply
#5
It looks like he was building a shape list and hit zero by mistake.
He made a class that I don't see use on line 121. That what I think he trying to do.
99 percent of computer problems exists between chair and keyboard.
Reply
#6
Now i have the following error message with the same code:

Error:
Traceback (most recent call last): File "E:\Users\Usuario1\Documents\abscorp\tetris.py", line 314, in <module> main_menu(win) File "E:\Users\Usuario1\Documents\abscorp\tetris.py", line 309, in main_menu main(win) File "E:\Users\Usuario1\Documents\abscorp\tetris.py", line 239, in main current_piece = get_shape() File "E:\Users\Usuario1\Documents\abscorp\tetris.py", line 171, in get_shape return Piece(5, 0, random.choice(shapes)) File "E:\Users\Usuario1\Documents\abscorp\tetris.py", line 126, in __init__ self.color = shape_colors[shapes.index[shape]] TypeError: 'builtin_function_or_method' object is not subscriptable
Reply
#7
you don't understand basic indexing of dictionaries and lists.
Please read the following:
Dictionaries: https://en.wikibooks.org/wiki/How_to_Thi...ctionaries
Lists: https://en.wikibooks.org/wiki/How_to_Thi...ists#Lists
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question [PyGame] Tetris... xpos, ypos of next block is not initialized after hard drop dddd123p 0 1,094 Jun-06-2024, 06:25 AM
Last Post: dddd123p
  Tetris inteligence DanTDRG 5 2,953 Oct-05-2022, 08:30 AM
Last Post: DanTDRG
  [PyGame] object has no attribute 'add_internal' djwilson0495 7 10,516 Feb-26-2021, 05:06 PM
Last Post: nilamo
  Attribute Error: object has no attribute djwilson0495 3 6,105 Jan-14-2021, 08:29 PM
Last Post: Larz60+
  AttributeError : method has no attribute djwilson0495 2 4,738 Dec-07-2020, 01:38 PM
Last Post: djwilson0495
  Tetris 3 abscorpy 3 3,994 Dec-10-2019, 06:36 PM
Last Post: nilamo
  Tetris 2 abscorpy 1 2,487 Dec-05-2019, 12:03 AM
Last Post: Windspar
  beginner - object has no attribute gean 2 5,164 Nov-07-2019, 02:02 PM
Last Post: gean
  Tetris abscorpy 1 3,371 Nov-05-2019, 01:57 AM
Last Post: SheeppOSU
  Basically a Python Tetris game [pygame] rather keyboard arrows can be controlled with lsepolis123 9 6,802 Sep-10-2019, 08:12 PM
Last Post: metulburr

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020