鱼C论坛

 找回密码
 立即注册
查看: 2059|回复: 2

pygame鼠标事件求助,如何实现点击这3个按钮后让线条变色?

[复制链接]
发表于 2019-3-27 20:56:14 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
button1 、button2 和 connect 都是按钮
想实现的功能是:先点击 button1,再点击 button2,最后点击 connect,这时候线条会变成绿色,但是连续插入三个 if 不能并实现变色,求问大佬们最后的点击事件那里应该要怎么设置才能让线条变色?
(电脑不知为何加入不了图片,如有不便请原谅)
代码如下:
  1. import pygame
  2. import sys
  3. from pygame.locals import *

  4. pygame.init()
  5. # 设置窗口
  6. win = pygame.display.set_mode((400, 400))

  7. GREEN = (0, 255, 0)
  8. RED = (255, 0, 0)


  9. # 创建线条类
  10. class Line:
  11.     def __init__(self, color, x1, y1, x2, y2, width):
  12.         self.color = color
  13.         self.x1 = x1
  14.         self.y1 = y1
  15.         self.x2 = x2
  16.         self.y2 = y2
  17.         self.width = width

  18.     def draw(self, win):
  19.         pygame.draw.line(win, self.color, (self.x1, self.y1), (self.x2, self.y2), 3)


  20. # 创建按钮类
  21. class Button:
  22.     def __init__(self, x, y, image):
  23.         self.x = x
  24.         self.y = y
  25.         self.image = image

  26.     # 加载按钮图片
  27.     def blit(self, win):
  28.         image = pygame.image.load(self.image)
  29.         win.blit(image, (self.x, self.y))

  30.     # 判断鼠标位置是否处于按钮范围内
  31.     def isOver(self):
  32.         pos = pygame.mouse.get_pos()
  33.         if pos[0] > self.x and pos[0] < self.x + 37:
  34.             if pos[1] > self.y and pos[1] < self.y + 22:
  35.                 return True

  36.         return False


  37. button1 = Button(50, 150, '1.png')
  38. button2 = Button(250, 150, '2.png')
  39. button_connect = Button(120, 280, 'connect.png')
  40. line = Line(RED, 120, 180, 250, 180, 3)


  41. # 游戏主循环
  42. while True:

  43.     button1.blit(win)
  44.     button2.blit(win)
  45.     button_connect.blit(win)
  46.     line.draw(win)

  47.     for event in pygame.event.get():
  48.         if event.type == QUIT:
  49.             pygame.quit()
  50.             sys.exit()

  51.         if event.type == pygame.MOUSEBUTTONDOWN:
  52.             if button1.isOver():
  53.                 if button2.isOver():
  54.                     if button_connect.isOver():
  55.                         line.color = GREEN

  56.     pygame.display.update()
复制代码



小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2019-3-27 21:31:41 | 显示全部楼层
你的按钮没有设置状态标记,不然怎么实现1,2都按下去
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-3-27 22:44:07 | 显示全部楼层
塔利班 发表于 2019-3-27 21:31
你的按钮没有设置状态标记,不然怎么实现1,2都按下去

是说在 class 里面定义?请问大佬可以稍微示意一下怎么设置吗?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2026-1-14 21:31

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表