2022年 11月 16日

python鼠标碰撞_获取鼠标指针测试item之间的碰撞

python海龟画图碰撞动画演示

“””

获取鼠标指针测试item之间的碰撞

这份源代码展示了如何实时获取鼠标指针坐标,

如何进行碰撞检测,这样用海龟画图模块制作游戏方便多了.

“””

import time

from turtle import Turtle,Screen

from random import random,randint

def randomcolor():

r = random()

g = random()

b = random()

return r,g,b

def mouse_pos():

“””获取相对于海龟屏幕的鼠标指针坐标,和屏幕的缩放参数scale无关。”””

pass

def isoverlap(item1,item2):

“””判断画布上两个项目是否重叠”””

pass

s = Screen() # 新建屏幕对象

s.delay(0)

canvas = s.cv

t = Turtle(shape=’square’)

t.shapesize(1.4)

t.speed(0)

t.penup()

t.bk(280)

haigui = t.turtle._item

# 下面是印刷此方块,

# 把它们放在squares列表中

squares = []

for _ in range(6):

t.fillcolor(randomcolor())

t.begin_fill()

for _ in range(4):

t.fd(50)

t.rt(90)

t.end_fill()

pass # 此处省略了代码

t.fd(100)

t.bk(600)

t.sety(t.ycor() – 100)

for _ in range(6):

t.fillcolor(randomcolor())

t.begin_fill()

for _ in range(4):

t.fd(50)

t.rt(90)

t.end_fill()

pass # 此处省略了代码

t.fd(100)

w = Turtle(shape=’blank’)

w.penup()

w.sety(200)

zi = (”,32,’underline’)

while True:

x,y = mouse_pos()

t.goto(x,y)

for sq in squares:

if isoverlap(haigui,sq):

w.clear()

w.write(‘碰到’ + str(sq) ,align=’center’,font=zi)

s.update()

需要完整源代码请扫码付款:

VIP免费查看

当前隐藏内容需要支付

39元

已有0人支付

立即购买