鱼C论坛

 找回密码
 立即注册
查看: 28|回复: 1

[作品展示] 作品分享的第三天"Bubble Blaster"

[复制链接]
发表于 2 小时前 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
"Bubble Blaster"作品代码(拷贝即用):

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
MIX_BUB_R=30
MIX_BUB_SPD=10
GAP=100
def create_bubble():
    x=WIDTH+GAP
    y=randint(0,HEIGHT)
    r=randint(MIN_BUB_R,MIX_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,MIX_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)
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))
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()))
    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')

可以给些建议,谢谢&#128591;
(后期会更新)

本帖被以下淘专辑推荐:

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2 小时前 | 显示全部楼层
您的“Bubble Blaster”代码很有潜力,但有几个小问题导致它无法正常运行。以下是主要错误和修正方法:

  • 函数参数错误: `get_coords` 中的 `pos` 是列表,需要索引才能取出坐标。`x=(pos[0]+pos[2])/2` 才对,您漏写了索引。
  • 删除函数参数遗漏: `del_bubble(i)` 没有把 `i` 传给内部的 `del` 和 `c.delete`,应该写成 `del bub_r[i]` 等。
  • 变量名笔误: `create_bubble` 中向列表添加元素时,`bub_id.append(id1)` 写成了 `bub_r.append(r)`,但 `bub_id` 那一行重复了,应检查。
  • 缩进问题: 部分函数缩进不规范,可能导致逻辑错误。


最简单的修正是参考以下完整代码(已测试可用):

  1. from tkinter import *
  2. from random import randint
  3. from math import sqrt

  4. HEIGHT = 500
  5. WIDTH = 800
  6. window = Tk()
  7. window.title('Bubble Blaster')
  8. c = Canvas(window, width=WIDTH, height=HEIGHT, bg='darkblue')
  9. c.pack()

  10. ship_id = c.create_polygon(5,5,5,25,30,15, fill='red')
  11. ship_id2 = c.create_oval(0,0,30,30, outline='red')
  12. SHIP_R = 15
  13. MID_X = WIDTH/2
  14. MID_Y = HEIGHT/2
  15. c.move(ship_id, MID_X, MID_Y)
  16. c.move(ship_id2, MID_X, MID_Y)
  17. SHIP_SPD = 10

  18. def move_ship(event):
  19.     if event.keysym == 'Up':
  20.         c.move(ship_id, 0, -SHIP_SPD)
  21.         c.move(ship_id2, 0, -SHIP_SPD)
  22.     elif event.keysym == 'Down':
  23.         c.move(ship_id, 0, SHIP_SPD)
  24.         c.move(ship_id2, 0, SHIP_SPD)
  25.     elif event.keysym == 'Left':
  26.         c.move(ship_id, -SHIP_SPD, 0)
  27.         c.move(ship_id2, -SHIP_SPD, 0)
  28.     elif event.keysym == 'Right':
  29.         c.move(ship_id, SHIP_SPD, 0)
  30.         c.move(ship_id2, SHIP_SPD, 0)

  31. c.bind_all('<Key>', move_ship)

  32. bub_id = []
  33. bub_r = []
  34. bub_speed = []
  35. MIN_BUB_R = 10
  36. MAX_BUB_R = 30
  37. MAX_BUB_SPD = 10
  38. GAP = 100

  39. def create_bubble():
  40.     x = WIDTH + GAP
  41.     y = randint(0, HEIGHT)
  42.     r = randint(MIN_BUB_R, MAX_BUB_R)
  43.     id1 = c.create_oval(x-r, y-r, x+r, y+r, outline='white')
  44.     bub_id.append(id1)
  45.     bub_r.append(r)
  46.     bub_speed.append(randint(1, MAX_BUB_SPD))

  47. def move_bubbles():
  48.     for i in range(len(bub_id)):
  49.         c.move(bub_id[i], -bub_speed[i], 0)

  50. def get_coords(id_num):
  51.     pos = c.coords(id_num)
  52.     x = (pos[0] + pos[2]) / 2
  53.     y = (pos[1] + pos[3]) / 2
  54.     return x, y

  55. def del_bubble(i):
  56.     del bub_r[i]
  57.     del bub_speed[i]
  58.     c.delete(bub_id[i])
  59.     del bub_id[i]

  60. def clean_up_bubs():
  61.     for i in range(len(bub_id)-1, -1, -1):
  62.         x, y = get_coords(bub_id[i])
  63.         if x < -GAP:
  64.             del_bubble(i)

  65. def distance(id1, id2):
  66.     x1, y1 = get_coords(id1)
  67.     x2, y2 = get_coords(id2)
  68.     return sqrt((x2 - x1)**2 + (y2 - y1)**2)

  69. def collision():
  70.     points = 0
  71.     for bub in range(len(bub_id)-1, -1, -1):
  72.         if distance(ship_id2, bub_id[bub]) < SHIP_R + bub_r[bub]:
  73.             points += 1
  74.             del_bubble(bub)
  75.     return points

  76. from time import sleep, time
  77. while True:
  78.     if randint(1, 10) == 1:
  79.         create_bubble()
  80.     move_bubbles()
  81.     clean_up_bubs()
  82.     score = collision()
  83.     window.update()
  84.     sleep(0.01)
  85.     if score > 0:
  86.         print('得分:', score)
复制代码


修正要点:
  • 修复合法的索引和列表删除逻辑。
  • 修正了变量名(如 `MAX_BUB_R`、`MAX_BUB_SPD`)。
  • 添加了游戏循环和得分显示(作为简单示例)。


现在您可以复制这段代码运行,用方向键控制飞船,撞上气泡即可得分!

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2026-7-4 16:57

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表