"""
tkinter飞机大战测试程序
本程序需要gameturtle模块0.2版支持。
利用标签对敌机和子弹进行分组,也用了颜色对敌机进行了分组。
"""
from gameturtle import *
from random import randint
bimage = Image.new("RGBA",(10,10),color=(255,255,255)) # 用来做子弹的图像
eimage = Image.new("RGBA",(20,40),color=(255,123,0)) # 用来做敌机的图像
root = Tk() # 建窗口
cv = Canvas(width=800,height=600,bg='blue') # 织画布
cv.pack() # 放画布
# 红色大方块代表玩家飞机
player = GameTurtle(cv,Image.new("RGBA",(100,50),color='red'))
bullets=[]
enemis=[]
counter = 0
while True:
mx,my = cv.mouse_pos() # 鼠标指针
if player.isalive():player.goto(mx,my) # 没死则移到鼠标指针
counter = counter + 1
if counter % 10 == 0 and player.isalive() : # 一定机率产生子弹
GameTurtle(cv,bimage,pos=(mx,my),heading=-90,tag='zidan')
x,y = randint(0,800),0
GameTurtle(cv,eimage,pos=(x,y),heading=90,tag='diren') # 产生敌人
bullets= group(cv,'zidan') # 所有的子弹
[b.fd(10) for b in bullets] # 所有子弹移动
[b.kill() for b in bullets if b.out_canvas()] # 超出画布自删
enemis= group(cv,'diren') # 所有的敌人
[e.fd(1) for e in enemis] # 所有敌人向下移动
# 超出画布范围或者碰到子弹则删除
[e.kill() for e in enemis if e.out_canvas() or e.collide_tag('zidan')]
if player.isalive():
if player.collide_color((255,123,0)):player.kill()
cv.update()
time.sleep(0.01)
-
- 2026 年 3 月
- 2026 年 2 月
- 2026 年 1 月
- 2025 年 12 月
- 2025 年 11 月
- 2025 年 10 月
- 2025 年 9 月
- 2025 年 6 月
- 2025 年 5 月
- 2025 年 3 月
- 2025 年 2 月
- 2025 年 1 月
- 2024 年 12 月
- 2024 年 8 月
- 2024 年 6 月
- 2024 年 5 月
- 2024 年 4 月
- 2024 年 3 月
- 2024 年 2 月
- 2023 年 11 月
- 2023 年 9 月
- 2023 年 6 月
- 2023 年 5 月
- 2023 年 4 月
- 2023 年 3 月
- 2023 年 2 月
- 2023 年 1 月
- 2022 年 12 月
- 2022 年 11 月
- 2022 年 10 月
- 2022 年 9 月
- 2022 年 8 月
- 2022 年 7 月
- 2022 年 6 月
- 2022 年 5 月
- 2022 年 4 月
- 2022 年 3 月
- 2022 年 2 月
- 2022 年 1 月
- 2021 年 12 月
- 2021 年 11 月
- 2021 年 10 月
- 2021 年 9 月
- 2021 年 8 月
- 2021 年 7 月
- 2021 年 6 月
- 2021 年 5 月
- 2021 年 4 月
- 2021 年 3 月
- 2021 年 2 月
- 2021 年 1 月
- 2020 年 12 月
- 2020 年 11 月
- 2020 年 10 月
- 2020 年 9 月
- 2020 年 8 月
- 2020 年 7 月
- 2020 年 6 月
- 2020 年 5 月
- 2020 年 4 月
- 2020 年 3 月
- 2020 年 2 月
- 2020 年 1 月
- 2019 年 12 月
- 2019 年 11 月
- 2019 年 10 月
- 2019 年 9 月
- 2019 年 8 月
- 2019 年 7 月
- 2019 年 6 月
- 2019 年 5 月
- 2019 年 4 月
- 2019 年 3 月
- 2019 年 2 月
- 2018 年 3 月
- 2018 年 1 月
- 2017 年 9 月
- 2017 年 5 月
- 2017 年 1 月

