Уведомления

Группа в Telegram: @pythonsu

#1 Июль 16, 2018 19:58:18

1Python1
Зарегистрирован: 2018-07-14
Сообщения: 4
Репутация: +  0  -
Профиль   Отправить e-mail  

проблемы с кодом

есть код
#Задаем параметры окна игры
from tkinter import*
HEIGHT=500
WIDTH=600
window=Tk()
window.title('Космический защитник')
c=Canvas(window,width=WIDTH,height=HEIGHT,bg='dimgray')
c.pack()
#рисуем космический корабль и устанавливаем его в начальную позицию
ship=c.create_polygon(30,0,0,40,60,40,fill='ivory',outline='cyan')
BOTTOM_YHEIGHT-50)
MID_X=WIDTH/2
c.move(ship,MID_X,BOTTOM_Y)
#Задаем движение космического корабля
SHIP_SPD=10
def ship_move(event):
key=event.keysym
if key=='Left':
c.move(ship,-SHIP_SPD,0)
if key=='Right':
c.move(ship,SHIP_SPD,0)
if key=='Up':
make_shoot()
c.bind_all('<Key>',ship_move)
#Рисуем вражеские космические корабли
from random import randint
enemy_id=list()
enemy_spd=list()
def create_enemy():
y=0
x=randint(40,WIDTH-40)
spd=randint(2,5)
id1=c.create_oval(x-20,y-20,x+20,y+20,fill='tomato',outline='linen')
enemy_id.append(id1)
enemy_spd.append(spd)
#задаем движение вражеского флота
def move_enemy():
for k in range(len(enemy_id)):
c.move(enemy_id,0,enemy_spd)
#задаем параметры снаряда и условия выстрела
shoot_id=list()
shoot_speed=30
def make_shoot():
if len(shoot_id)<3:
pos=c.coords(ship)
x=pos
y=pos
id2=c.create_oval(x-6,y-6,x+6,y+6,fill='yellow',outline='magenta')
shoot_id.append(id2)
#перемещение снаряда
def move_shoot():
for i in range(len(shoot_id)):
c.move(shoot_id,0,-shoot_speed)
#определение положения снаряда на игровом поле
def coords_shoot(id_num):
pos=c.coords(id_num)
xpos+pos)/2
ypos+pos)/2
return x,y
#удаление снаряда
def del_shoot(i):
c.delete(shoot_id)
del shoot_id
#стирание снаряда при вылете за экран
def clean_shoot():
for i in range(len(shoot_id)-1,-1,-1):
x,y=coords_shoot(shoot_id)
if y<0:
del_shoot(i)
#определение положения корабля противника на игровом поле
def coords_enemy(id_num):
pos=c.coords(id_num)
xpos+pos)/2
ypos+pos)/2
return x,y
#удаление корабля противника
def del_enemy(i):
c.delete(enemy_id)
del enemy_id
#стирание корабля противника при вылете за экран и вычитание очков
def clean_enemy():
points=0
for i in range(len(enemy_id)-1,-1,-1):
x,y=coords_enemy(enemy_id)
if y>HEIGHT:
points+=-50
del_enemy(i)
return points
#задаем функцию расчета расстояние между двумя объектами
from math import sqrt
def distance(id1,id2):
x1,y1=coords_enemy(id1)
x2,y2=coords_shoot(id2)
return sqrt((x2-x1)**2+(y2-y1)**2)
#стирание корабля противника при попадании снаряда и защитывание очков
def bum():
points=0
for n in range(len(shoot_id)-1,-1,-1):
for i in range(len(enemy_id)-1,-1,-1):
if distance(shoot_id,enemy_id,)<26:
points+=50
del_shoot(n)
del_enemy(i)
return points
#создание надписей
c.create_text(50,30,text='TIME',fill='white')
c.create_text(150,30,text='SCORE',fill='white')
time_text=c.create_text(50,50,fill='white')
score_text=c.create_text(150,50,fill='white')
def show_score(score):
c.itemconfig(score_text,text=str(score))
def show_time(time_left):
c.itemconfig(time_text,text=str(time_left))
from time import sleep,time
TIME_LIMIT=30
BONUS_SCORE=500
score=0
bonus=0
end=time()+TIME_LIMIT
from time import sleep,time

while time()<end:
if randint(1,13)==1:
create_enemy()
move_enemy()
move_shoot()
clean_shoot()
score+=clean_enemy()
score+=bum()
if (int(score/BONUS_SCORE))>bonus:
bonus+=1
end+TIME_LIMIT
show_score(score)
show_time(int(end-time()))
print(score)
window.update()
sleep(0.05)
c.create_text(MID_X,MID_Y,\
text='GAME OVER',fill='white',font'Helvetica',30))
c.create_text(MID_X,MID_Y+30,\
text='Score: ‘+str(score),fill=’white')
c.create_text(MID_X,MID_Y+45,\
text='Bonus time: ‘+str(bonus*TIME_LIMIT),fill=’white')

запускается нормально,но через время отключается по неизвестным причинам. выдавая это
Traceback (most recent call last):
File “C:\Users\User\space_invaders.py”, line 129, in <module>
score+=bum()
File “C:\Users\User\space_invaders.py”, line 100, in bum
if distance(shoot_id,enemy_id,)<26:
IndexError: list index out of range
есть варианты решения?

Офлайн

#2 Июль 17, 2018 11:34:53

PEHDOM
Зарегистрирован: 2016-11-28
Сообщения: 2196
Репутация: +  294  -
Профиль   Отправить e-mail  

проблемы с кодом

1Python1 ниасилил слишкаммногабукаф. отступы отсутвуют как и половина текста.
заключайте код в теги

[code python][/code]



==============================
Помещайте код в теги:
[code python][/code]
Бериегите свое и чужое время.

Офлайн

Board footer

Модераторировать

Powered by DjangoBB

Lo-Fi Version