python中碰撞的代码_Python…Tkinter碰撞

似乎检测工作正常。几乎无事可做。在

我只在镜头离开屏幕的时候删除它。在

在再次运行after()之前,我检查loaded_gun == 0:。

如果您继续运行after loop,下一个快照将有两个after loop,它将运行得更快2倍。第三杆跑得快3倍。在

我在我更改的地方添加了# furas。在from Tkinter import *

# creates window

window = Tk()

size = window.winfo_screenheight()

window.title("This is a window")

# set up geometry using string formatting operator %

window.geometry("%dx%d+%d+%d" % (1000, 1000, 10, 10))

window.update()

# creates canvas

canvas = Canvas(window, bg='green')

# pack is a layout manager

canvas.pack(fill=BOTH, expand=1)

canvas.update()

canvas.create_rectangle(0, 1000, 1000, 0, fill="orange", width=10, outline="white", tag="border")

def shooting():

global loaded_gun

c = canvas.coords("player")

canvas.create_line(c[0],c[1] + 20,c[2],c[3],width=5,fill="yellow",tag="shot")

loaded_gun = 0

move_shot("shot")

def move_shot(name):

global loaded_gun

canvas.move(name,0,-10)

canvas.update()

if loaded_gun == 0:

shot = canvas.coords(name) # furas

for x in enemies: # furas

en = canvas.coords(x) # furas

if en[0] <= shot[0] <= en[0] + 70: # furas

if shot[1] <= en[1] + 70: # furas

#print(canvas.coords(enemies[x]))

canvas.delete(x) # furas

#print(canvas.coords(name))

enemies.remove(x) # furas

canvas.delete(name)

loaded_gun = 1

break

if shot[1] < 0: # furas

canvas.delete(name) # furas

loaded_gun = 1 # furas

if loaded_gun == 0: # furas

window.after(50, move_shot, "shot")

def on_key_press(event):

global canvas,loaded_gun

c = canvas.coords("player")

if event.keysym == 'Left' and c[0] > 0:

canvas.move("player", -20,0)

elif event.keysym == 'Right' and c[2] < 1000:

canvas.move("player", 20, 0)

elif event.keysym == 'space':

if loaded_gun == 1:

shooting()

elif event.keysym == 'Shift_L':

loaded_gun = 1

canvas.delete("shot")

def draw_enemy():

global enemies

c = [100,100,170,170]

inc = 100

enemies = []

for x in range(8):

for y in range(4):

enemy = canvas.create_rectangle(c[0] + inc * x, c[1] + inc * y,c[2] + inc * x,c[3] + inc * y,fill="red",tag="enemy")

enemies.append(enemy)

def move_enemy():

canvas.move("enemy", 0, 20)

window.after(1000, move_enemy) # furas

#

canvas.create_line(500, 950,500,1000, width=15, fill="red",tag="player")

loaded_gun = 1

draw_enemy()

window.bind_all('', on_key_press) # furas

move_shot("shot") # furas

move_enemy() # furas

window.mainloop()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值