List Index out of Range

python
tkinter

#1

This is my code:

from tkinter import *
from random import *
def collision(item1, item2, distance):
    xdistance = abs(c.coords(item1)[0] - c.coords(item2)[0])
    ydistance = abs(c.coords(item1)[1] - c.coords(item2)[1])
    overlap = xdistance < distance and ydistance < distance
    return overlap
def checkLaser():
    for i in laserList:
        for rock in rockList:
            if collision(i, rock,20):
                updateScore()
                c.delete(rock)
                rockList.remove(rock)
                c.delete(i)
                laserList.remove(i)                
    window.after(100, checkLaser)
def updateScore():
    global score
    score = score + 1
    lscore.config(text = "Score: " + str(score))
def updateLives():
    global lives
    if lives == 1:
        endGame()
    else:
        lives = lives - 1
        llives.config(text = "Lives: " + str(lives))
def endGame():
    global score
    Gameover = c.create_text(375,150,text="Game Over",fill="white",font=('Times New Roman',30))
    Score = c.create_text(375, 300, text = "Final Score: " + str(score), fill = "white", font = ('Times New Roman', 30))
    window.after(5000, close)
def close():
    window.destroy()
def checkCollisions():
    score=0
    for i in rockList:
        if collision(i,spaceship,30):
            updateLives()
            c.delete(i)
            rockList.remove(i)                
    window.after(100, checkCollisions)
def moveMonster(event):
    key = event.keysym
    if key == "Up":
        c.move(spaceship, 0, -10)
    elif key == "Down":
        c.move(spaceship, 0, 10)
    elif key == "Right":
        c.move(spaceship, 10, 0)
    elif key == "Left":
        c.move(spaceship, -10, 0)
    elif key == "Space":
        c.move(spaceship, -10, 0)
def updateTimer():
    global gametime
    gametime = gametime + 1
    Stopwatch.config(text = "Time: " + str(gametime))
    window.after(1000, updateTimer)
def endTitle():
    c.delete(Text)
    c.delete(TText)
def makeSteroid():
    x = randint(0,750)
    rock = c.create_image(200, 200, image = mychar2)
    c.coords(rock, x, 0)
    rockList.append(rock)
    window.after(1000, makeSteroid)
def moveSteroid():
    for i in rockList:
        checkCollisions()
        rand = randint(1, 750)
        c.move(i, 0, 10)
        pos = c.coords(i)
        y = pos[1]
        if y >= 500:
            c.delete(i)
            rockList.remove(i)
    window.after(100, moveSteroid)
def shoot(event):
    pos=c.coords(spaceship)
    x= pos[0]
    y= pos[1]
    Laser = c.create_rectangle(x+16,y-40,x+18,y-80,fill="red")
    laserList.append(Laser)
    moveLaser()
def moveLaser():
    for laser in laserList:
        c.move(laser, 0, -10)
        checkLaser()
        pos = c.coords(laser)
        y = pos[1]
        if y <= 0:
            c.delete(laser)
            laserList.remove(laser)
    window.after(100, moveLaser)
window = Tk()
window.title("Rock Game")
gametime=0
score=0
lives=3
Stopwatch =Label(window,text="Time:"+str(gametime),font=("Times New Roman",15))
Stopwatch.pack()
lscore=Label(window,text="Score:"+str(score),font=("Times New Roman",15))
lscore.pack()
llives=Label(window,text="Lives:"+str(lives),font=("Times New Roman",15))
llives.pack()
c = Canvas(window, width = 750, height = 500, bg = "black")
c.pack()
laserList=[]
Text=c.create_text(325,250,text="Game",fill="white",font=('Times New Roman',30))
TText=c.create_text(325,275,text="Shoot as many astroids as you can in 30 seconds!",fill="white",font=('Times New Roman',10))
mychar = PhotoImage(file = "New Piskel (5).gif")
spaceship = c.create_image(325, 450, image = mychar)
mychar2 = PhotoImage(file = "New Piskel (1).gif")
c.bind_all('<Key>', moveMonster)
window.bind("<space>", shoot)
rockList = []
gametime = 0
window.after(3000,endTitle)
window.after(1000, updateTimer)
window.after(1000, makeSteroid)
window.after(100, moveSteroid)
window.mainloop()

When I run it, and my laser hits an asteroid, it gave this error:

Exception in Tkinter callback
Traceback (most recent call last):
File “C:\Users\Jayde Meng\AppData\Local\Programs\Python\Python36\lib\tkinter_init_.py”, line 1702, in call
return self.func(*args)
File “C:\Users\Jayde Meng\AppData\Local\Programs\Python\Python36\lib\tkinter_init_.py”, line 746, in callit
func(*args)
File “C:\Users\Jayde Meng\Documents\Pygame_Asteroids.py”, line 11, in checkLaser
if collision(i, rock,20):
File “C:\Users\Jayde Meng\Documents\Pygame_Asteroids.py”, line 4, in collision
xdistance = abs(c.coords(item1)[0] - c.coords(item2)[0])
IndexError: list index out of range

I want know to why it keeps giving the error. Help me please.