# I need help figuring out the problem for my code. Im confused on what to do

``````# Demo Game with tkinter for CS elective class

from tkinter import *
import random
import time
from math import *

def moveMonster(x,y):
canvas.move(body,x,y)
canvas.move(eye,x,y)
canvas.move(eyeball,x,y)

def moveMonsterPlayer(event):
key= event.keysym
if key == 'Up':
moveMonster(0,-30)
elif key == 'Down':
moveMonster(0,30)
elif key == 'Left':
moveMonster(-30,0)
elif key == 'Right':
moveMonster(30,0)

def deleteEnemy(enemy):
#find where this enemy is in the list  - item number
enemyIndex = enemyList.index(enemy)
del enemyList[enemyIndex]  #delete from list of enemies
canvas.delete(enemy)  #delete enemy from the screen as well

def distance(target1,target2):
target1coords = canvas.coords(target1)
target2coords = canvas.coords(target2)
x1 = (target1coords + target1coords ) / 2 #average x to find middle
y1 = (target1coords + target1coords ) / 2  #average y
x2 = (target2coords + target2coords)  / 2
y2 = (target2coords + target2coords) / 2\

d = sqrt( (x2-x1)** 2 + (y2-y1)** 2)  #pythagorean theorem to find distance
return d

def checkCollisions():
enemiesHit = 0  #count how many enemies are hit by scanning list
for e in enemyList:
if distance(body,e) < 20:
enemiesHit = enemiesHit + 1
deleteEnemy(e)  #delete enemy from list

return enemiesHit

def makeEnemies():
for e in range(enemyCount):
enemyx = random.randint(1,400)
enemyy = random.randint(1,400)
newEnemy = canvas.create_oval(enemyx,enemyy,enemyx+30,enemyy+30, fill='black')
enemyList.append(newEnemy)

def moveEnemies(deltaTime):
for e in enemyList:
canvas.move(e,deltaTime * enemySpeed,0)  #move amount calculated based on time and speed
enemyCoords = canvas.coords(e)
enemyCoordsX = enemyCoords
enemyCoordsY = enemyCoords
if enemyCoordsX > 400  or enemyCoordsY > 400:
#reset the coordinates
newX =  random.randint(1,400) #find the new x
newY = random.randint(1,400)  #find the new y
canvas.coords(e, newX, newY, newX+1, newY+1) #set coords to new ones

window = Tk()
window.title('Alien Game')

#set up canvas for game movement
canvas = Canvas(window, height=400, width=400, bg = 'white')
canvas.pack()

#set up timer
timeLeft = 30
timemessage = Label(window, text = "Time left : " + str(timeLeft))
timemessage.pack()

#setup score
score = 0
scoreMessage = Label(window,text = 'Score:' + str(score) )
scoreMessage.pack()
enemyCount = 20
enemySpeed = 10
#MAKE DIFFERENT LEVELS!!!!!!!!!!!!!!!!!!***************************************************************************************************************
print("There are three levels, 'Easy' , 'Medium' and 'Hard'")
level = input('What level do you want to do? ')
if level == 'Easy':
enemySpeed = 5
ememyCount = 10
else:
print('')
if level == 'Medium':
enemySpeed = 10
enemyCount = 15
else:
print('')
if level == 'Hard':
enemySpeed = 15
enemyCount = 25
else:
print('')

#make all the enemies

makeEnemies()   #this function updates the enemyList with enemyCount of enemies

#create monster player drawing
body = canvas.create_rectangle(200,200,240,240, fill = 'green')
eye = canvas.create_oval(210,180,230,200,fill = 'white')
eyeball = canvas.create_oval(215,185,225,195,fill = 'black')
#bind all keys for player
canvas.bind_all('<Key>',moveMonsterPlayer)

#set up game loop with time
lastTime = time.time()  #remember last time it was in the loop

while timeLeft > 0 and score < enemyCount:
currentTime = time.time()
deltaTime = currentTime - lastTime  #how long since this loop ran
lastTime = currentTime  #update the last time to this one

timeLeft = timeLeft - deltaTime  #overall time left in game decreases
timemessage.config(text = "Time left : " + str(round(timeLeft))) #update time

moveEnemies(deltaTime) #all enemies move based on time elapsed
hits = checkCollisions()  #return how many enemies were hit
if hits > 0:
#print('got non zero hits in this loop')
score = score + hits
scoreMessage.config(text = 'Score:' + str(score))
window.update()   #important to update all objects on the screen in loop

#LOOP IS OVER - print GAME OVER
canvas.create_text(200,200, text= 'You Won!', fill='green', font = ('Helvetica', 80))
print(score)
scoreMessage.config(text = 'Score:' + str(score))
``````

You didn’t define enemyList anywhere, at least, that is the error message i get, what is your question?

maybe define the list in makeEnemies function before appending to it?

1 Like

How do I define enemyList?

the way you would define any list:

``````list_name = []
``````
1 Like

I am here though what would I put in the brackets[]

this is the one thing I don’t know.

you can put things in your list when defining/declaring if that is what you wish, but the list can also be empty in the beginning, and `append` items to it later.