随心圆:以鼠标左键点击为圆心,画一个半径50 ,颜色随机的圆
需求分析:
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1:
pos = event.pos
r = randint(0,255)
g = randint(0,255)
b = randint(0,255)
c = r, g, b
4.画圆
pygame.draw.circle(s, c, pos, 50,width = 1)
方法一:
def homework1_1():
pygame.init()
s = pygame.display.set_mode((800, 600))
while 1:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1:
pos = event.pos
r = randint(0, 255)
g = randint(0, 255)
b = randint(0, 255)
c = r, g, b
s.fill((0, 0, 0))
pygame.draw.circle(s, c, pos, 50, width=1)
pygame.display.update()
# 处理组合关闭游戏
keys = pygame.key.get_pressed()
if keys[pygame.LALT] or keys[pygame.K_RALT] and keys[pygame.K_F4]:
sys.exit()
方法二:
def homework1_2():
pygame.init()
s = pygame.display.set_mode((800, 600))
while 1:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
# 处理组合关闭游戏
keys = pygame.key.get_pressed()
if keys[pygame.LALT] or keys[pygame.K_RALT] and keys[pygame.K_F4]:
sys.exit()
# 处理鼠标事件
mouse = pygame.mouse.get_pressed() # 获取鼠标点击状态元组 三个元素分别代表左键 ,中键 ,右键的点击状态.点了就是True否则为False
if mouse[0]: # 判断左键有没有点击
pos = pygame.mouse.get_pos() # 点击了左键就获取鼠标的位置作为圆的圆心
r = randint(0, 255)
g = randint(0, 255)
b = randint(0, 255)
c = r, g, b
s.fill((0, 0, 0))
pygame.draw.circle(s, c, pos, 50, width=1)
pygame.display.update()
与键盘的按键响应一样,鼠标的响应也可以用mouse模块来专门处理.这样会比用event事件直接处理来的简洁明了.
点不中的矩形心:在屏幕中初始化一个矩形,当鼠标点击这个矩形时,矩形移动到鼠标没有点到的地方.
备注:判断一下点有没有在矩形内,可以用矩形的collidepoint(x,y) 当点(x,y)在矩形内时返回True,否则返回False
pygame.draw.rect(s, c, rect, width = 1)
mouse = pygame.mouse.get_pressed()
if mouse[0]:
pos = pygame.mouse.get_pos()
while rect.collidepoint(pos[0], pos[1]):
rect.left = randint(0, 700)
rect.top = randint(0, 500)
def homework2():
pygame.init()
s = pygame.display.set_mode((800, 600))
rect = pygame.Rect(350, 250, 100, 100)
color = 0, 255, 0
pygame.draw.rect(s, color, rect, width=1)
pygame.display.update()
while 1:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
mouse = pygame.mouse.get_pressed() # 获取鼠标点击状态元组 三个元素分别代表左键 ,中键 ,右键的点击状态.点了就是True否则为False
if mouse[0]: # 判断左键有没有点击
pos = pygame.mouse.get_pos() # 点击了左键就获取鼠标的位置作为圆的圆心
while rect.collidepoint(pos[0], pos[1]):
rect.left = randint(0, 700)
rect.top = randint(0, 500)
s.fill((0, 0, 0))
pygame.draw.rect(s, color, rect, width=1)
pygame.display.update()
# 处理组合关闭游戏
keys = pygame.key.get_pressed()
if keys[pygame.LALT] or keys[pygame.K_RALT] and keys[pygame.K_F4]:
sys.exit()
复制与粘贴:屏幕上初始一个矩形, 当鼠标框选中矩形时,按CTRL + C 复制 这个矩形,然后在鼠标下一次点击时以鼠标点击处为矩形的中心粘贴这个矩形
rect = pygame.Rect(350, 250, 100, 100)
color = 0, 255, 0
pygame.draw.rect(s, color, rect, width = 1)
if event.type == pygame.MOUSEBUTTONDOWN and not have_rect:
if event.button == 1:
mouse_flag = True
start_pos = event.pos
if event.type == pygame.MOUSEMOTION and mouse_flag:
end_pos = event.pos
left = start_pos[0] if start_pos[0] < end_pos[0] else end_pos[0]
top = start_pos[1] if start_pos[1] < end_pos[1] else end_pos[1]
width = abs(start_pos[0] - end_pos[0])
height = abs(start_pos[1] - end_pos[1])
new_rect = pygame.Rect(left, top, width, height)
s.fill((0, 0, 0))
pygame.draw.rect(s, color, rect, width=1)
pygame.draw.rect(s, color, new_rect, width=1)
pygame.display.update()
if event.type == pygame.MOUSEBUTTONUP and mouse_flag:
if event.button == 1:
mouse_flag = False
if new_rect.contains(rect):
can_copy = True
s.fill((0, 0, 0))
pygame.draw.rect(s, (255, 0, 0), rect, width = 5) # 框选成功加粗显示
pygame.display.update()
keys = pygame.key.get_pressed()
if keys[pygame.K_LCTRL] or keys[pygame.K_RCTRL] and keys[pygame.K_v] and can_copy:
copy_rect = rect.move(0,0)
print(copy_rect)
can_copy = False
have_rect = True
5.粘贴
if event.type == pygame.MOUSEBUTTONDOWN and have_rect:
if event.button == 1:
have_rect = False
pos = event.pos
copy_rect.center = pos
s.fill((0, 0, 0))
pygame.draw.rect(s, color, rect, width=1)
pygame.draw.rect(s, color, copy_rect, width=1)
pygame.display.update()
def homework4():
pygame.init()
s = pygame.display.set_mode((800, 600))
rect = pygame.Rect(350, 250, 100, 100)
color = 0, 255, 0
pygame.draw.rect(s, color, rect, width=1)
pygame.display.update()
mouse_flag = False # 鼠标第一次点击的标识
have_rect = False # 有没有复制的标记
can_copy = False # 能不能复制的标记
while 1:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
if event.type == pygame.MOUSEBUTTONDOWN and not have_rect:
if event.button == 1:
mouse_flag = True
start_pos = event.pos
if event.type == pygame.MOUSEMOTION and mouse_flag:
end_pos = event.pos
left = start_pos[0] if start_pos[0] < end_pos[0] else end_pos[0]
top = start_pos[1] if start_pos[1] < end_pos[1] else end_pos[1]
width = abs(start_pos[0] - end_pos[0])
height = abs(start_pos[1] - end_pos[1])
new_rect = pygame.Rect(left, top, width, height)
s.fill((0, 0, 0))
pygame.draw.rect(s, color, rect, width=1)
pygame.draw.rect(s, color, new_rect, width=1)
pygame.display.update()
if event.type == pygame.MOUSEBUTTONUP and mouse_flag:
if event.button == 1:
mouse_flag = False
if new_rect.contains(rect):
can_copy = True
s.fill((0, 0, 0))
pygame.draw.rect(s, (255, 0, 0), rect, width = 5) # 框选成功加粗显示
pygame.display.update()
keys = pygame.key.get_pressed()
if keys[pygame.K_LCTRL] or keys[pygame.K_RCTRL] and keys[pygame.K_v] and can_copy:
copy_rect = rect.move(0,0)
print(copy_rect)
can_copy = False
have_rect = True
if event.type == pygame.MOUSEBUTTONDOWN and have_rect:
if event.button == 1:
have_rect = False
pos = event.pos
copy_rect.center = pos
s.fill((0, 0, 0))
pygame.draw.rect(s, color, rect, width=1)
pygame.draw.rect(s, color, copy_rect, width=1)
pygame.display.update()
# 处理组合关闭游戏
keys = pygame.key.get_pressed()
if keys[pygame.K_LALT] or keys[pygame.K_RALT] and keys[pygame.K_F4]:
sys.exit()