+ 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]

28th Nov 2020, 6:30 PM
Douglas
23 odpowiedzi
+ 1
You may use the append method
28th Nov 2020, 6:34 PM
Indranil
Indranil - avatar
+ 1
would you mind showing the change i should make?
28th Nov 2020, 6:36 PM
Douglas
+ 1
Can u send me the code snippet
28th Nov 2020, 6:37 PM
Indranil
Indranil - avatar
+ 1
Of u r question
28th Nov 2020, 6:37 PM
Indranil
Indranil - avatar
+ 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]
28th Nov 2020, 6:38 PM
Douglas
+ 1
self.turns.append(self.dirnx) self.turns.append(self.dirny)
28th Nov 2020, 6:41 PM
Indranil
Indranil - avatar
+ 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]
28th Nov 2020, 6:45 PM
Douglas
+ 1
append is a method so it will consist of the '()' And u current program also going wrong
28th Nov 2020, 6:47 PM
Indranil
Indranil - avatar
+ 1
Plz be checked with the append function
28th Nov 2020, 6:47 PM
Indranil
Indranil - avatar
+ 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
28th Nov 2020, 6:53 PM
Douglas
+ 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
28th Nov 2020, 6:54 PM
Douglas
+ 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
28th Nov 2020, 6:54 PM
Douglas
+ 1
I think ur quite function is not working
28th Nov 2020, 6:58 PM
Indranil
Indranil - avatar
+ 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
28th Nov 2020, 7:02 PM
Douglas
+ 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()
28th Nov 2020, 7:02 PM
Indranil
Indranil - avatar
+ 1
No please don't send me the whole code now because I'm so busy for my upcoming board exams😂😂
28th Nov 2020, 7:04 PM
Indranil
Indranil - avatar
+ 1
If u have other question for this question u can directly message to my sololearn account
28th Nov 2020, 7:06 PM
Indranil
Indranil - avatar
+ 1
thank you❤️ best of luck on your board exams‼️
28th Nov 2020, 7:07 PM
Douglas
+ 1
Is it works?
28th Nov 2020, 7:07 PM
Indranil
Indranil - avatar