泡沫不出现是有原因的

2024-07-07 05:30:28 发布

您现在位置:Python中文网/ 问答频道 /正文

气泡(白色圆圈)不会出现,以下是我的代码:

from tkinter import *
from random import randint
from time import sleep, time
from math import sqrt
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='green')
ship_id2 = c.create_oval(0, 0, 30, 30, outline='green')
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)
    if event.keysym == 'Down':
        c.move(ship_id, 0, SHIP_SPD)
        c.move(ship_id2, 0, SHIP_SPD)
    if event.keysym == 'Left':
        c.move(ship_id, -SHIP_SPD, 0)
        c.move(ship_id2, -SHIP_SPD, 0)
    if event.keysym == 'Right':
        c.move(ship_id, SHIP_SPD, 0)
        c.move(ship_id2, SHIP_SPD, 0)
c.bind_all('<Key>', move_ship)
bub_id = list()
bub_r = list()
bub_speed = list()
MIN_BUB_R = 10
MAX_BUB_R = 30
MAX_BUB_SPD = 10
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
    bub_r.append(id1)
    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 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 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 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[1]
def distance(id1, id2):
    x1, y1 = get_coords(id1)
    x2, y2 = get_coords(id2)
    return sqrt((x2 - x1)**2 + (y2 - y1)**2)
c.create_text(50, 30, text='TIME', fill='blue' )
c.create_text(150, 30, text='SCORE', fill='yellow' )
time_text = c.create_text(50, 50, fill='blue' )
score_text = c.create_text(150, 50, fill='yellow' )
def show_score(score):
    c.itemconfig(score_text, text=str(score))
def show_time(time_left):
    c.itemconfig(time_text, text=str(time_left))
BUB_CHANCE = 10
TIME_LIMIT = 30
BONUS_SCORE = 1000
bonus = 0
thend = time() + TIME_LIMIT
score = 0
#MAIN GAME LOOP
while time() < thend:
    chance = randint(1, BUB_CHANCE)
    if chance == 1:
        create_bubble()
        move_bubbles()
        clean_up_bubs()
        score = collision()
    if score == BONUS_SCORE:
        bonus += 1
        end += TIME_LIMIT
    show_time(int(thend - time()))
    show_score(score)
    print(score)
    clean_up_bubs
    window.update()
    sleep(0.01)
c.create_text(MID_X, MID_Y, \
              text='GAME OVER', fill='red', font=('Helevica', 30))
c.create_text(MID_X, MID_Y + 30, \
              text='Score: '+ str(score), fill='yellow')
c.create_text(MID_X, MID_Y + 45, \
              text='Bonus time: '+ str(bonus*TIME_LIMIT), fill='green')

我需要帮助!如果你能帮我太好了! 如果你想知道气泡是用tkinter和python-3.5用idle编辑器画的白色圆圈,idle什么也没说,但是我知道有些地方不对劲,因为气泡(白色圆圈)没有出现。如果这个有什么问题,请告诉我。你知道吗


Tags: textidmoveiftimedefcreatefill
1条回答
网友
1楼 · 发布于 2024-07-07 05:30:28

要调用函数,必须使用括号。函数名计算为函数对象。在括号后面,通常带有参数,然后说'调用这个函数'。你知道吗

所以呢 小家伙_附加id 基本附加(id1)

应该是

bub_id.append(id1)
bub_r.append(r)

随着这种变化,气泡出现了。你知道吗

类似地,cleanup-bubs应该是cleanup-bubs()。此呼叫失败

Traceback (most recent call last):
  File "F:\Python\a\tem3.py", line 97, in <module>
    clean_up_bubs()
  File "F:\Python\a\tem3.py", line 53, in clean_up_bubs
    x, y = get_coords(bub_id[i])
  File "F:\Python\a\tem3.py", line 65, in get_coords
    x = (pos[0] + pos[2])/2
IndexError: list index out of range

我让你调查一下。你知道吗

我删除了python-3.5和python-idle标记,因为这些错误行为与idle或3.5没有特别的关系。你知道吗

用while、sleep和update编写自己的事件循环并不是使用Tkinter的最佳方法。在睡眠期间,应用程序不会对事件(例如按键)做出响应。对按键的反应可以和睡眠时间一样大。检查如何使用mainloop和after方法的示例。你知道吗

相关问题 更多 >