python鼠标碰撞_获取鼠标指针测试item之间的碰撞
python海龜畫圖碰撞動(dòng)畫演示
"""
獲取鼠標(biāo)指針測(cè)試item之間的碰撞
這份源代碼展示了如何實(shí)時(shí)獲取鼠標(biāo)指針坐標(biāo),
如何進(jìn)行碰撞檢測(cè),這樣用海龜畫圖模塊制作游戲方便多了.
"""
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():
"""獲取相對(duì)于海龜屏幕的鼠標(biāo)指針坐標(biāo),和屏幕的縮放參數(shù)scale無(wú)關(guān)。"""
pass
def isoverlap(item1,item2):
"""判斷畫布上兩個(gè)項(xiàng)目是否重疊"""
pass
s = Screen() # 新建屏幕對(duì)象
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()
需要完整源代碼請(qǐng)掃碼付款:
VIP免費(fèi)查看
當(dāng)前隱藏內(nèi)容需要支付
39元
已有0人支付
立即購(gòu)買
總結(jié)
以上是生活随笔為你收集整理的python鼠标碰撞_获取鼠标指针测试item之间的碰撞的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python中元组可以比较大小吗_pyt
- 下一篇: python2的xrange比range