Bubble Blaster Python代码有问题吗?
我正在做一个项目,并且已经完成了代码,但是仍然出现了一个错误,尽管我确信我的代码是正确的 下面是不断出现的错误:Bubble Blaster Python代码有问题吗?,python,tkinter,python-3.3,Python,Tkinter,Python 3.3,我正在做一个项目,并且已经完成了代码,但是仍然出现了一个错误,尽管我确信我的代码是正确的 下面是不断出现的错误: Traceback (most recent call last): File "C:\Python33\bubblee blaster.py", line 61, in <module> clean_up_bubs() NameError: name 'clean_up_bubs' is not defined 回溯(最近一次呼叫最后一次): 文件“C:\
Traceback (most recent call last):
File "C:\Python33\bubblee blaster.py", line 61, in <module>
clean_up_bubs()
NameError: name 'clean_up_bubs' is not defined
回溯(最近一次呼叫最后一次):
文件“C:\Python33\bubblee blaster.py”,第61行,在
清理
NameError:未定义名称“clean\u bubs”
以下是我的python代码:
from tkinter import *
HEIGHT = 500
WIDTH = 800
window = Tk()
window.title('bubble blaster')
c = Canvas(window, width=WIDTH, height=HEIGHT, bg='darkblue')
c.pack()
ship_id = c.create_polygon(5, 5, 5, 25, 30, 15, fill='red')
ship_id2 = c.create_oval(0, 0, 30, 30, outline='red')
SHIP_R = 15
MID_X = WIDTH / 2
MID_Y = HEIGHT / 2
c.move(ship_id, MID_X, MID_Y)
c.move(ship_id2, MID_X, MID_Y)
SHIP_SPD = 10
def move_ship(event):
if event.keysym == 'Up':
c.move(ship_id, 0, -SHIP_SPD)
c.move(ship_id2, 0, -SHIP_SPD)
elif event. keysym == 'Down':
c.move(ship_id, 0, SHIP_SPD)
c.move(ship_id2, 0, SHIP_SPD)
elif event.keysym == 'Left':
c.move(ship_id, -SHIP_SPD, 0)
c.move(ship_id2, -SHIP_SPD, 0)
elif event.keysym == 'Right':
c.move(ship_id, SHIP_SPD, 0)
c.move(ship_id2, SHIP_SPD, 0)
c.bind_all('<Key>' , move_ship)
from random import randint
bub_id = list()
bub_r = list()
bub_speed = list()
MIN_BUB_R = 10
MAX_BUB_R = 30
MAX_BUB_SPD = 110
GAP = 100
def create_bubble():
x = WIDTH + GAP
y = randint(0, HEIGHT)
r = randint(MIN_BUB_R, MAX_BUB_R)
id1 = c.create_oval(x - r, y - r, x + r, y + r, outline='white')
bub_id.append(id1)
bub_r.append(r)
bub_speed.append(randint(1, MAX_BUB_SPD))
def move_bubbles():
for i in range(len(bub_id)):
c.move(bub_id[i], -bub_speed[i], 0)
from time import sleep, time
BUB_CHANCE = 10
TIME_LIMIT = 30
BONUS_SCORE = 1000
score = 0
bonus = 0
end = time() + TIME_LIMIT
#MAIN GAME LOOP
while time() < end:
if randint(1, BUB_CHANCE) == 1:
create_bubble()
move_bubbles()
clean_up_bubs()
score += collision()
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.01)
def get_coords(id_num):
pos = c.coords(id_num)
x = (pos[0] + pos[2])/2
y = (pos[1] + pos[3])/2
return x, y
def del_bubble(i):
del bub_r[i]
del bub_speed[i]
c.delete(bub_id[i])
del bub_id[i]
def clean_up_bubs():
for i in range(len(bub_id)-1, -1, -1):
x, y = get_coords(bub_id[i])
if x < -GAP:
del_bubble(i)
from math import sqrt
def distance(id1, id2):
x1, y1 = get_coords(id1)
x2, y2 = get_coords(id2)
return sqrt((x2 - x1)**2 + (y2 - y1)**2)
def collision():
points = 0
for bub in range(len(bub_id)-1, -1, -1):
if distance(ship_id2, bub_id[bub]) < (SHIP_R + bub_r[bub]):
points += (bub_r[bub] + bub_speed[bub])
del_bubble(bub)
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))
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')
从tkinter导入*
高度=500
宽度=800
window=Tk()
窗口标题(“气泡爆炸机”)
c=画布(窗口,宽度=宽度,高度=高度,bg='darkblue')
c、 包()
ship_id=c.创建多边形(5,5,5,25,30,15,fill='red')
ship_id2=c.create_oval(0,0,30,30,outline='red')
船舶直径=15
中间X=宽度/2
中间Y=高度/2
c、 移动(船舶id、中X、中Y)
c、 移动(船号id2、船号X、船号Y)
船舶速度=10
def移动装运(事件):
如果event.keysym==“向上”:
c、 移动(船舶id,0,-船舶SPD)
c、 移动(船号id2,0,-船号SPD)
埃利夫事件。keysym==‘向下’:
c、 移动(船号,0,船号)
c、 移动(船号id2,0,船号SPD)
elif event.keysym==‘Left’:
c、 移动(船舶id,-船舶SPD,0)
c、 移动(船号id2,-船号SPD,0)
elif event.keysym=='Right':
c、 移动(船舶id,船舶SPD,0)
c、 移动(船号id2,船号SPD,0)
c、 绑定所有(“”,移动船舶)
从随机导入randint
bub_id=list()
bub_r=列表()
bub_速度=列表()
最小值为10
最大值为30
最大制动速度=110
间隙=100
def create_bubble():
x=宽度+间隙
y=randint(0,高度)
r=randint(最小值,最大值)
id1=c.create_oval(x-r,y-r,x+r,y+r,outline='white'))
bub_id.append(id1)
小部件附加(r)
bub_速度追加(随机数(1,最大bub_SPD))
def move_气泡():
对于范围内的i(len(bub_id)):
c、 移动(bub_id[i],-bub_速度[i],0)
从时间导入睡眠,时间
概率=10
时限=30
奖金=1000分
分数=0
奖金=0
结束=时间()+时间限制
#主游戏循环
当时间()结束时:
如果randint(1,BUB_机会)==1:
创建_bubble()
move_bubbles()
清理
分数+=碰撞()
如果(int(分数/奖金\分数))>奖金:
奖金+=1
结束+=时间限制
显示分数(分数)
显示时间(int(end-time()))
打印(分数)
window.update()
睡眠(0.01)
def get_coords(id_num):
pos=c.coords(id\u num)
x=(位置[0]+位置[2])/2
y=(位置[1]+位置[3])/2
返回x,y
def del_气泡(i):
德尔布布尔[i]
德尔布尤速度[i]
c、 删除(bub_id[i])
德尔布布迪德[i]
def清理工具():
对于范围内的i(len(bub_id)-1,-1,-1):
x、 y=获取坐标(基本id[i])
如果x<-间隙:
德鲁泡沫(一)
从数学导入sqrt
def距离(id1、id2):
x1,y1=获取坐标(id1)
x2,y2=获取坐标(id2)
返回sqrt((x2-x1)**2+(y2-y1)**2)
def collision():
分数=0
对于范围内的基本单位(长度(基本单位id)-1,-1,-1):
如果距离(船号id2,基本舱号[bub])<(船号+基本舱号[bub]):
点数+=(基本单位[基本单位]+基本单位速度[基本单位])
德鲁气泡(bub)
返回点
c、 创建文本(50,30,text='TIME',fill='white')
c、 创建文本(150、30、文本='SCORE',填充='white')
时间文本=c.创建文本(50,50,fill='white')
评分文本=c.创建文本(150,50,fill='white')
def显示_分数(分数):
c、 itemconfig(score_text,text=str(score))
def显示时间(剩余时间):
c、 itemconfig(time\u text,text=str(time\u left))
c、 创建文字(中间X、中间Y、\
text='GAME OVER',fill='white',font=('Helvetica',30))
c、 创建文字(中间X、中间Y+30、\
text='Score:'+str(Score),fill='white')
c、 创建文字(中间X、中间Y+45、\
text='Bonus time:'+str(奖金*时限),填充='white')
现在清理bubs
在第61行下面。在它被调用之前,你必须拥有它。您会注意到move_bubbles
和create_bubbles
在while循环之前,现在clean_bubbs
也必须如此clean_bubbs
在第61行下方。在它被调用之前,你必须拥有它。你会注意到move\u bubbles
和create\u bubbles
在while循环之前,对于clean\u bubbs
这段代码是一个不可读的烂摊子
您应该将所有导入内容放在脚本的顶部。接下来,定义全局常量(和全局变量,但最好是重新组织代码以尽量减少全局变量的使用)。
接下来定义您的功能。
然后将调用这些函数的代码放在末尾 这样做的原因是,您需要在使用它们之前定义它们。Python自上而下扫描脚本,通过执行定义创建各种对象(包括函数)。函数定义可以引用尚未定义的全局事物(包括其他函数)。但是,当您实际调用函数时,它引用的东西必须已经定义好 此外,在函数周围放置(至少)一个空行,以便更容易看到它们的开始和结束位置。并在其他地方使用空行,使程序的结构更加明显 我已经尝试实现这些更改,并运行了生成的代码,但我不确定它是否完全符合您的要求
from tkinter import *
from random import randint
from time import sleep, time
from math import sqrt
HEIGHT = 500
WIDTH = 800
SHIP_R = 15
MID_X = WIDTH / 2
MID_Y = HEIGHT / 2
SHIP_SPD = 10
bub_id = list()
bub_r = list()
bub_speed = list()
MIN_BUB_R = 10
MAX_BUB_R = 30
MAX_BUB_SPD = 110
GAP = 100
BUB_CHANCE = 10
TIME_LIMIT = 30
BONUS_SCORE = 1000
score = 0
bonus = 0
end = time() + TIME_LIMIT
def move_ship(event):
if event.keysym == 'Up':
c.move(ship_id, 0, -SHIP_SPD)
c.move(ship_id2, 0, -SHIP_SPD)
elif event.keysym == 'Down':
c.move(ship_id, 0, SHIP_SPD)
c.move(ship_id2, 0, SHIP_SPD)
elif event.keysym == 'Left':
c.move(ship_id, -SHIP_SPD, 0)
c.move(ship_id2, -SHIP_SPD, 0)
elif event.keysym == 'Right':
c.move(ship_id, SHIP_SPD, 0)
c.move(ship_id2, SHIP_SPD, 0)
def create_bubble():
x = WIDTH + GAP
y = randint(0, HEIGHT)
r = randint(MIN_BUB_R, MAX_BUB_R)
id1 = c.create_oval(x - r, y - r, x + r, y + r, outline='white')
bub_id.append(id1)
bub_r.append(r)
bub_speed.append(randint(1, MAX_BUB_SPD))
def move_bubbles():
for i in range(len(bub_id)):
c.move(bub_id[i], -bub_speed[i], 0)
def get_coords(id_num):
pos = c.coords(id_num)
x = (pos[0] + pos[2])/2
y = (pos[1] + pos[3])/2
return x, y
def del_bubble(i):
del bub_r[i]
del bub_speed[i]
c.delete(bub_id[i])
del bub_id[i]
def clean_up_bubs():
for i in range(len(bub_id)-1, -1, -1):
x, y = get_coords(bub_id[i])
if x < -GAP:
del_bubble(i)
def distance(id1, id2):
x1, y1 = get_coords(id1)
x2, y2 = get_coords(id2)
return sqrt((x2 - x1)**2 + (y2 - y1)**2)
def collision():
points = 0
for bub in range(len(bub_id)-1, -1, -1):
if distance(ship_id2, bub_id[bub]) < (SHIP_R + bub_r[bub]):
points += (bub_r[bub] + bub_speed[bub])
del_bubble(bub)
return points
def show_score(score):
c.itemconfig(score_text, text=str(score))
def show_time(time_left):
c.itemconfig(time_text, text=str(time_left))
window = Tk()
window.title('bubble blaster')
c = Canvas(window, width=WIDTH, height=HEIGHT, bg='darkblue')
c.pack()
ship_id = c.create_polygon(5, 5, 5, 25, 30, 15, fill='red')
ship_id2 = c.create_oval(0, 0, 30, 30, outline='red')
c.bind_all('<Key>' , move_ship)
c.move(ship_id, MID_X, MID_Y)
c.move(ship_id2, MID_X, MID_Y)
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' )
#MAIN GAME LOOP
while time() < end:
if randint(1, BUB_CHANCE) == 1:
create_bubble()
move_bubbles()
clean_up_bubs()
score += collision()
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.01)
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')
从tkinter导入*
从随机导入randint
从时间导入睡眠,时间
从数学导入sqrt
高度=500
宽度=800
船舶直径=15
中间X=宽度/2
中间Y=高度/2
船舶速度=10
bub_id=list()
bub_r=列表()
bub_速度=列表()
最小值为10
最大值为30
最大制动速度=110
间隙=100
概率=10
时限=30
奖金=1000分
分数=0
奖金=0
结束=时间()+时间限制
def移动装运(事件):
如果event.keysym==“向上”:
c、 移动(船舶id,0,-船舶SPD)
c、 移动(船号id2,0,-船号SPD)
elif event.keysym==“向下”:
c、 移动(船号,0,船号)
c、 移动(船号id2,0,船号SPD)