"""
利用标签分组与collide_others方法
本程序运行后能用上下左右键操作虫子,碰到其它虫子时不会穿越过去。
"""
from sprites import *
screen = Screen()
screen.bgcolor('dodger blue')
screen.title('请用awsd键操作虫子')
sq = Sprite()
sq.color('red')
p1 = Sprite(5)
p1.goto(100,100)
p2 = Sprite(6)
p2.goto(-100,100)
p3 = Sprite(8)
p3.goto(-100,-100)
p4 = Sprite(9)
p4.goto(100,-100)
akey = Key('a') # 实例化a键
dkey = Key('d') # 实例化d键
wkey = Key('w') # 实例化w键
skey = Key('s') # 实例化s键
screen.listen()
clock = Clock()
while 1:
# 如果按了a键,则dx为-5,虫子将向左移动
if akey.down():dx = -5;dy=0
elif dkey.down():dx = 5;dy=0
elif wkey.down():dy = 5;dx=0
elif skey.down():dy = -5;dx=0
else:
dx = 0;dy = 0
sq.move(dx,dy) # 在水平和垂直方向上移动虫子
# 和tag标签为sprite的精灵进行碰撞检测
collide_list = sq.collide_others('sprite')
if collide_list:
if dx != 0: sq.addx(-dx) # 如果往左右移动
if dy != 0: sq.addy(-dy) # 如果往上下移动
clock.tick(60) # 设定fps为60
-
- 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 月

