问题描述
from tkinter import *
from time import sleep
import random
class Ball:
def __init__(self,canvas,color,size,x,y,xspeed,yspeed):
self.canvas = canvas
self.color = color
self.size = size
self.x = x
self.y = y
self.xspeed = xspeed
self.yspeed = yspeed
self.id=canvas.create_oval(x,x+size,y+size,fill=color)
def move(self):
self.canvas.move(self.id,self.xspeed,self.yspeed)
(x1,y1,x2,y2)=self.canvas.coords(self.id)
(self.x,self.y)=(x1,y1)
if x1<=0 or x2>=WIDTH:
self.xspeed=-self.xspeed
if y1<=0 or y2>=HEIGHT:
self.yspeed=-self.yspeed
WIDTH=800
HEIGHT=400
bullets=[]
def fire(event):
bullets.append(Ball(canvas,10,"red",100,200,0))
## global bullet_count
## bullet_count=0
window = Tk()
canvas = Canvas(window,width=WIDTH,height=HEIGHT)
canvas.pack()
canvas.bind("<Button-1>",fire)
spaceship = Ball(canvas,"green",0)
enemy = Ball(canvas,500,5)
while True:
for bullet in bullets:
bullet.move()
if (bullet.x+bullet.size) >= WIDTH:
canvas.delete(bullet.id)
bullets.remove(bullet)
if (bullet_count<9):
bullet_count+=1
else:
id=canvas.create_text(100,50,fill="red",font="Times 30 italic
bold",text="Hello World")
break
enemy.move()
window.update()
sleep(0.03)
我只想发射 10 发子弹,如果没有剩余子弹,我想创建一个像 id = canvas.create_text(100,font="Times 30 italic bold",text="Hello World")
这样的消息框。请帮我。我不擅长python
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)