
本程序自己编写碰撞检测代码,方便学习矩形碰撞的基本原理。下面是部分代码预览:
"""
两个矩形碰撞原理演示程序_跟随鼠标移动的矩形.py
"""
import pygame
from pygame.locals import *
WIDTH,HEIGHT = 480,360
screen = pygame.display.set_mode((WIDTH,HEIGHT))
r1 = pygame.Rect(0,0,100,100)
r1.center = (WIDTH//2,HEIGHT//2) # r1在屏幕中央,宽高为100
r2 = pygame.Rect(0,0,50,50) # r2先在左上角的位置
running = True
while running :
event = pygame.event.poll() # 从事件队列中取一个事件
if event.type == QUIT:running = False
x,y = pygame.mouse.get_pos()
r2.center = x,y # 跟随鼠标移动的矩形
c1 = r2.right<r1.left
c2 = r2.bottom<r1.top
c3 = r2.left>r1.right
c4 = r2.top>r1.bottom
if c1 or c2 or c3 or c4:
pygame.display.set_caption("没有重叠")
else:
pygame.display.set_caption("重叠了")
screen.fill((0,0,0))
pygame.draw.rect(screen,(180,180,0),r1,2)
pygame.draw.rect(screen,(180,0,130),r2,2)
pygame.display.update()
pygame.quit()
如需要查看完整源代码,请
需要浏览更多吗?
成为会员后,登陆才能继续浏览!联系微信scratch8即可办理会员。
(会员专属:能浏览所有文章,下载所有带链接的Python资源。)
