|
|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
button1 、button2 和 connect 都是按钮
想实现的功能是:先点击 button1,再点击 button2,最后点击 connect,这时候线条会变成绿色,但是连续插入三个 if 不能并实现变色,求问大佬们最后的点击事件那里应该要怎么设置才能让线条变色?
(电脑不知为何加入不了图片,如有不便请原谅)
代码如下:- import pygame
- import sys
- from pygame.locals import *
- pygame.init()
- # 设置窗口
- win = pygame.display.set_mode((400, 400))
- GREEN = (0, 255, 0)
- RED = (255, 0, 0)
- # 创建线条类
- class Line:
- def __init__(self, color, x1, y1, x2, y2, width):
- self.color = color
- self.x1 = x1
- self.y1 = y1
- self.x2 = x2
- self.y2 = y2
- self.width = width
- def draw(self, win):
- pygame.draw.line(win, self.color, (self.x1, self.y1), (self.x2, self.y2), 3)
- # 创建按钮类
- class Button:
- def __init__(self, x, y, image):
- self.x = x
- self.y = y
- self.image = image
- # 加载按钮图片
- def blit(self, win):
- image = pygame.image.load(self.image)
- win.blit(image, (self.x, self.y))
- # 判断鼠标位置是否处于按钮范围内
- def isOver(self):
- pos = pygame.mouse.get_pos()
- if pos[0] > self.x and pos[0] < self.x + 37:
- if pos[1] > self.y and pos[1] < self.y + 22:
- return True
- return False
- button1 = Button(50, 150, '1.png')
- button2 = Button(250, 150, '2.png')
- button_connect = Button(120, 280, 'connect.png')
- line = Line(RED, 120, 180, 250, 180, 3)
- # 游戏主循环
- while True:
- button1.blit(win)
- button2.blit(win)
- button_connect.blit(win)
- line.draw(win)
- for event in pygame.event.get():
- if event.type == QUIT:
- pygame.quit()
- sys.exit()
- if event.type == pygame.MOUSEBUTTONDOWN:
- if button1.isOver():
- if button2.isOver():
- if button_connect.isOver():
- line.color = GREEN
- pygame.display.update()
复制代码
|
|