Dec-01-2017, 10:25 PM
# made by kikoko_2
from tkinter import *
GARUMS = 500
PLATUMS = 800
logs = Tk()
logs.title('Burbuļu spridzinātājs')
a = Canvas(logs, width=PLATUMS, height=GARUMS, bg='darkblue')
a.pack()
kuģa_id = a.create_polygon(5, 5, 5, 25, 30, 15, fill='red')
kuģa_id2 = a.create_oval(0, 0, 30, 30, outline='red')
KUĢA_R = 15
VID_X = PLATUMS / 2
VID_Y = GARUMS / 2
a.move(kuģa_id, VID_X, VID_Y)
a.move(kuģa_id2, VID_X, VID_Y)
KUĢA_ĀTR = 10
def pārvietot_kuģi(notikums):
if notikums.keysym == 'Up':
a.move(kuģa_id, 0, -KUĢA_ĀTR)
a.move(kuģa_id2, 0, -KUĢA_ĀTR)
elif notikums.keysym == 'Down':
a.move(kuģa_id, 0, KUĢA_ĀTR)
a.move(kuģa_id2, 0, KUĢA_ĀTR)
elif notikums.keysym == 'Left':
a.move(kuģa_id, -KUĢA_ĀTR, 0)
a.move(kuģa_id2, -KUĢA_ĀTR, 0)
elif notikums.keysym == 'Right':
a.move(kuģa_id, KUĢA_ĀTR, 0)
a.move(kuģa_id2, KUĢA_ĀTR, 0)
a.bind_all('<Key>', pārvietot_kuģi)
from random import randint
burb_id = list()
burb_r = list()
burb_ātrums = list()
MIN_BURB_R = 10
MAX_BURB_R = 30
MAX_BURB_ĀTR = 10
ATSTARPE = 100
def izveidot_burbuli():
x = PLATUMS + ATSTARPE
y = randint(0, GARUMS)
r = randint(MIN_BURB_R, MAX_BURB_R)
id1 = a.create_oval(x - r, y - r, x + r, y + r, outline='white')
burb_id.append(id1)
burb_r.append(r)
burb_ātrums.append(randint(1, MAX_BURB_ĀTR))
def pārvietot_burbuļus():
for i in range(len(burb_id)):
a.move(burb_id[i], -burb_ātrums[i], 0)
def iegūt_koord(id_skaitlis):
poz = a.coords(id_skaitlis)
x = (poz[0] + poz[2])/2
y = (poz[1] + poz[3])/2
return x, y
def dzēst_burbuli(i):
del burb_r[i]
del burb_ātrums[i]
a.delete(burb_id[i])
del burb_id[i]
def notīrīt_burbuļus():
for i in range(len(burb_id)-1, -1, -1):
x, y = iegūt_koord(burb_id[i])
if x < -ATSTARPE:
dzēst_burbuli(i)
from math import sqrt
def attālums(id1, id2):
x1, y1 = iegūt_koord(id1)
x2, y2 = iegūt_koord(id2)
return sqrt((x2 - x1)**2 + (y2 - y1)**2)
def sadursme():
punkti = 0
for burb in range(len(burb_id)-1, -1, -1):
if attālums(kuģa_id2, burb_id[burb]) < (KUĢA_R + burb_r[burb]):
punkti += (burb_r[burb] + burb_ātrums[burb])
dzēst_burbuli(burb)
return punkti
a.create_text(50, 30, text='TIME', fill='white')
a.create_text(150, 30, text='SCORE', fill='white')
laiks_teksts = a.create_text(50, 50, fill='white')
rezultāts_teksts = a.create_text(150, 50, fill='white')
def parādīt_rezultātu(rezultāts):
a.itemconfig(rezultāts_teksts, text=str(rezultāts))
def parādīt_laiku(laiks_palicis):
a.itemconfig(laiks_teksts, text=str(laiks_palicis))
from time import sleep, time
BURB_NEJAUŠI = 10
LAIKA_IEROBEŽOJUMS = 30
PAPILDLAIKA_REZ = 1000
rezultāts = 0
papildu = 0
beigas = time() + LAIKA_IEROBEŽOJUMS
rezultāts = 0
#SPĒLES GALVENAIS CIKLS
while time() < beigas:
if randint(1, BURB_NEJAUŠI) == 1:
izveidot_burbuli()
pārvietot_burbuļus()
notīrīt_burbuļus()
rezultāts += sadursme()
if (int(rezultāts / PAPILDLAIKA_REZ)) > papildu:
papildu += 1
beigas += LAIKA_IEROBEŽOJUMS
parādīt_rezultātu(rezultāts)
parādīt_laiku(int(beigas - time()))
logs.update()
sleep(0.01)
a.create_text(VID_X, VID_Y, \
text='GAME OVER', fill='white', font=('Helvetica',30))
a.create_text(VID_X, VID_Y + 30, \
text='Score: '+ str(rezultāts), fill='white')
a.create_text(VID_X, VID_Y + 45, \
text='Extra time: '+ str(papildu*LAIKA_IEROBEŽOJUMS), fill='white')Error:Traceback (most recent call last):
File "C:\Users\test\Desktop\python game\myfirstgame.py", line 109, in <module>
izveidot_burbuli()
File "C:\Users\test\Desktop\python game\myfirstgame.py", line 47, in izveidot_burbuli
id1 = a.create_oval(x - r, y - r, x + r, y + r, outline='white')
File "C:\Users\test\AppData\Local\Programs\Python\Python36-32\lib\tkinter\__init__.py", line 2489, in create_oval
return self._create('oval', args, kw)
File "C:\Users\test\AppData\Local\Programs\Python\Python36-32\lib\tkinter\__init__.py", line 2474, in _create
*(args + self._options(cnf, kw))))
_tkinter.TclError: invalid command name ".!canvas"Hello, I don't understand what I did wrong in the code. IDLE wasn't even responding for a while.
Hopefully someone finds the error.
Thanks!
