TypeError: list indices must be integers or slices, not tuple | SoloLearn: Learn to code for FREE!

+1

TypeError: list indices must be integers or slices, not tuple

i followed a tutorial on youtube to create a snake game in python via Visual Studio code! this is the error message i keep getting when i press a key to play the game, wondering how to fix. Line of code causing error: self.turns[self.head.pos[:]] = [self.dirnx, self.dirny]

11/28/2020 6:30:28 PM

Douglas

23 Answers

New Answer

+1

You may use the append method

+1

would you mind showing the change i should make?

+1

Can u send me the code snippet

+1

Of u r question

+1

def move(self): for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() keys = pygame.key.get_pressed() for key in keys: if keys[pygame.KEY_LEFT]: self.dirnx = -1 self.dirny = 0 self.turns[self.head.pos[:]] = [self.dirnx, self.dirny] elif keys[pygame.KEY_RIGHT]: self.dirnx = 1 self.dirny = 0 self.turns[self.head.pos[:]] = [self.dirnx, self.dirny] elif keys[pygame.KEY_UP]: self.dirnx = 0 self.dirny = -1 self.turns[self.head.pos[:]] = [self.dirnx, self.dirny] elif keys[pygame.K_DOWN]: self.dirnx = 0 self.dirny = 1 self.turns[self.head.pos[:]] = [self.dirnx, self.dirny]

+1

self.turns.append(self.dirnx) self.turns.append(self.dirny)

+1

so the code should look like: for key in keys: if keys[pygame.KEY_LEFT]: self.dirnx = -1 self.dirny = 0 self.turns.append[self.head.pos[:]] = [self.dirnx, self.dirny]

+1

append is a method so it will consist of the '()' And u current program also going wrong

+1

Plz be checked with the append function

+1

https://www.sololearn.com/learn/Python/2433/

+1

I made the adjustments you suggested, my game window is not closing any more when I press the key to play. Now i assume i need to do something else within the Def Main() Call to accommodate for the changes i just made

+1

def move(self, dirnx, dirny): self.dirnx = dirnx self.dirny = dirny self.pos = (self.pos[0] + self.dirnx, self.pos[1] + self.dirny) this is the move code

+1

def main(): global width, rows, s, snack width = 500 rows = 20 win = pygame.display.set_mode((width, width)) s = snake((255,0,0), (10,10)) snack = cube(randomSnack(rows, s), color=(0,255,0)) flag = True clock = pygame.time.Clock() while flag: pygame.time.delay(50) clock.tick(10) s.move() redrawWindow(win) if s.body[0].pos == snack.pos: s.addCube() snack = cube(randomSnack(rows, s), color= (0,255,0)) for x in range(len(s.body)): if s.body[x].pos in list(map(lambda z:z.pos, s.body[x+1:])): print('Score: ', len(s.body)) message_box("You Lost", "Play Again") s.reset((10,10)) break this is the main() call

+1

I think ur quite function is not working

+1

explain please. do you want me to share the code with you? i can send it via email if you would like. either way i appreciate your help

+1

import sys def move(self): for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.display.quite() pygame.quit() #sys.exit() If pygame.display.quite() not work u can use sys.exit()

+1

No please don't send me the whole code now because I'm so busy for my upcoming board exams😂😂

+1

If u have other question for this question u can directly message to my sololearn account

+1

thank you❤️ best of luck on your board exams‼️

+1

Is it works?